CMake: How to make the add_custom_command command run only when input changes?

For my project, I would like to run a command that generates the installed file (in other words, the generated file is just a data file, not the source code).

I currently have CMakeLists.txt

add_custom_command( OUTPUT outputfile.txt COMMAND dosomething ${CMAKE_CURRENT_SOURCE_DIR}/inputfile.txt ${CMAKE_CURRENT_BINARY_DIR}/output.txt DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/inputfile.txt ) add_custom_target( run_gen_command ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/output.txt ) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/output.txt DESTINATION ${CMAKE_INSTALL_DATADIR}/somewhere ) 

This works fine, but since ALL is passed to add_custom_target() , the command is run every time make run.

Is there a way to change this so that the command runs only when the input file changes? A team may take some time, so ideally it will not be launched if it is not needed.

Thanks in advance!

+5
source share
1 answer

To fix this:

 add_custom_command( OUTPUT outputfile.txt 

with this:

 add_custom_command( OUTPUT output.txt 

Then I guess you don't need add_custom_target at all. If I'm wrong, just remove ALL from add_custom_target and you should be fine.

0
source

Source: https://habr.com/ru/post/1202516/


All Articles