Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to make add_custom_command execute only when input is changed?

Tags:

cmake

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

I currently have the following in my 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 because ALL is passed to add_custom_target(), the command gets executed every time I run make.

Is there any way I could change this so that the command is only run when the input file is changed? The command may take a while to complete, so ideally, it wouldn't be run unless it needed to.

Thanks in advance!

like image 642
Andrew Gunnerson Avatar asked Sep 14 '14 01:09

Andrew Gunnerson


1 Answers

Correct this:

add_custom_command(
    OUTPUT outputfile.txt

with this:

add_custom_command(
    OUTPUT output.txt

Then my guess is that you don't need an add_custom_target at all. If I am wrong, simply remove ALL from add_custom_target, and you should be ok.

like image 169
Antonio Avatar answered Oct 30 '22 12:10

Antonio