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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With