10

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!

2
  • 1
    OUTPUT outputfile.txt may be OUTPUT output.txt? Commented Sep 22, 2014 at 9:00
  • I don't get your question. dosomething depends on inputfile.txt and generates output.txt, thus output.txt is only re-generated if inputfile.txt has changed. Is this not the case? Or did I miss something. Commented Apr 4, 2015 at 9:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Removing add_custom_target will not work. Removing ALL will make it build only on demand (not included in make all)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.