0

我正在尝试使用CMake 命令将生成的*.y输出(平面亮度值,无标题信息)复制到文本文件。add_custom_command如何copy在中添加命令add_custom_command

我想将生成的*.y输出重定向到文本文件。但是>重定向没有直接在 CMake 中运行add_custom_command,所以我尝试使用add_custom_command. 在这里我想使用copy而不是>,但在这两种情况下我都无法将*.y输出重定向或复制到*.txt文件。

macro(COMPARE file function type type_out)
  get_filename_component(main_base_name ${file} NAME_WE)
  set(main_base_name_mangled ${main_base_name}_${function}_${type_out})

  # file generated by simulation
  set(output_file ${function}_${type_out}_0_opt.y)
  # reference file generated by generator
  set(reference_file ${function}_${type_out}_0_ref.y)

  set(output_file_txt ${function}_${type_out}_0_opt.txt)
  set(reference_file_txt ${function}_${type_out}_0_ref.txt)

  add_custom_target(compare_${main_base_name_mangled}
    COMMAND cmp ${reference_file} ${output_file}
   COMMENT "Comparing ${reference_file} ${output_file}")

  add_dependencies(compare_${main_base_name_mangled}
    simulate_${main_base_name_mangled})

  add_test(NAME Compare_${main_base_name_mangled}
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}
                             --target compare_${main_base_name_mangled})

  # add dependency to compile first
  set_tests_properties(Compare_${main_base_name_mangled}
    PROPERTIES REQUIRED_FILES ${reference_file})
  set_tests_properties(Compare_${main_base_name_mangled}
    PROPERTIES REQUIRED_FILES  ${output_file})
  # test Compare_X depends on Simulate_X
  set_tests_properties(Compare_${main_base_name_mangled}
    PROPERTIES DEPENDS Simulate_${base_name_mangled})

  #Here I have added my code to redirect my *.y output to *.txt file  
  add_custom_command(
    OUTPUT ${output_file} 
    ${CMAKE_COMMAND} -E copy ${output_file} ${output_file_txt}
    MAIN_DEPENDENCY ${output_file} 
    COMMENT "Redirecting ${output_file} to ${output_file_txt}")

  add_custom_command(
    OUTPUT ${reference_file}
    ${CMAKE_COMMAND} -E copy ${reference_file} ${reference_file_txt}
    MAIN_DEPENDENCY ${reference_file}
    COMMENT "Redirecting ${reference_file} to ${reference_file_txt}")
endmacro()

运行此代码后,我想生成两个文本文件,但上述 CMake 更改仍然无法实现。

4

1 回答 1

0

OUTPUTadd_custom_command调用中的指令应指定自定义命令生成的文件(即输出文件),而不是输入文件。这是您的代码中的固定片段:

  # Here I have added my code to redirect my *.y output to *.txt file  
  add_custom_command(
    OUTPUT ${output_file_txt} 
    ${CMAKE_COMMAND} -E copy ${output_file} ${output_file_txt}
    MAIN_DEPENDENCY ${output_file} 
    COMMENT "Redirecting ${output_file} to ${output_file_txt}")

  add_custom_command(
    OUTPUT ${reference_file_txt}
    ${CMAKE_COMMAND} -E copy ${reference_file} ${reference_file_txt}
    MAIN_DEPENDENCY ${reference_file}
    COMMENT "Redirecting ${reference_file} to ${reference_file_txt}")
于 2019-07-31T12:02:59.573 回答