kiv_apple
kiv_apple

Reputation: 671

CMake: dependecies of add_custom_command

Let's say I have a Python script which does something with just built executable. And I want CMake to rebuild that executable if the script was modified (actually it is enough to just re-run the script, but rebuild an executable is fine too).

add_executable(App src/main.cpp)

add_custom_command(
        TARGET App
        POST_BUILD
        COMMAND "${Python3_EXECUTABLE}" ARGS "scripts/do_stuff.py" "$<TARGET_FILE:App>"
        WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
)

How can I achieve that? add_custom_command with TARGET argument doesn't support DEPENDS argument. add_dependency(App "scripts/do_stuff.py") produces an error, because "scripts/do_stuff.py" is not a target, but just a file.

Running the script is very important for correct working of the executable so I don't want define completely separate target via add_custom_command allowing bypass script execution by building just App target.

Upvotes: 1

Views: 106

Answers (1)

KamilCuk
KamilCuk

Reputation: 141493

actually it is enough to just re-run the script

So the executable does not depend on the script. So re-run the script, not the executable.

add_executable(app src/main.cpp)

add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/did_do_stuff
        COMMAND "${Python3_EXECUTABLE}" "scripts/do_stuff.py" "$<TARGET_FILE:app>"
        COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/did_do_stuff
        DEPENDS "$<TARGET_FILE:app>"
                "${CMAKE_CURRENT_LIST_DIR}/scripts/do_stuff.py"
        WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
)
add_custom_target(do_stuff
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/did_do_stuff
)

And build target do_stuff (or all) to run it.

Upvotes: 1

Related Questions