So I have some cmake definition of ADD_EXTRA_STEP
that can be either true or false and is set depending on what the user wants. Then, at post build I have to execute a few commands. I currently have something like this:
add_custom_command(TARGET ${some_target}
POST_BUILD
COMMAND <command to generate FOO.out>
COMMAND <command that uses FOO.out and generates FOO2.out>
COMMENT <some comment>
VERBATIM
)
add_custom_command(TARGET ${some_target}
POST_BUILD
COMMAND <some other command>
COMMENT <some other comment>
VERBATIM
)
Now in between the two add_custom_command
I need to execute another command if and only if ADD_EXTRA_STEP
is set to true. The problem is that for this to work, I need to guarantee that FOO2.out
exists. My Idea so far is to do something like this:
add_custom_command(TARGET ${some_target}
POST_BUILD
COMMAND <command to generate FOO.out>
COMMAND <command that uses FOO.out and generates FOO2.out>
COMMENT <some comment>
VERBATIM
)
if(${ADD_EXTRA_STEP})
add_custom_command(TARGET ${some_target}
POST_BUILD
COMMAND <extra step command that uses FOO2.out>
COMMENT <some extra comment>
VERBATIM
)
endif()
add_custom_command(TARGET ${some_target}
POST_BUILD
COMMAND <some other command>
COMMENT <some other comment>
VERBATIM
)
However, I am in doubt whether this would work and whether it is a 'good' practice to implement it in this way. In other words, is it guaranteed that when the command in the add_custom_command
is executed the command in the previous add_custom_command
(i.e. the one that generates FOO2.out) will have already executed?
Thanks in advance!
You need to combine add_custom_command with add_custom_target
The main idea is to use the OUTPUT
form of the add_custom_command
on the first step with OUTPUT
being equal to FOO2.out
and then specify FOO2.out
as DEPENDS
in the conditional target/command. Whether to use add_custom_command
or add_custom_target
depends on the actual actions in the command and how the results of the command are used in later steps. Consider this simple example:
add_executable(some_executable some_executable_source.c)
add_custom_command(OUTPUT FOO2.out
DEPENDS some_executable
COMMAND ${CMAKE_COMMAND} -E touch FOO2.out
COMMAND ${CMAKE_COMMAND} -E echo "creating FOO2.out"
)
if (${ADD_EXTRA_STEP})
add_custom_target(run ALL
${CMAKE_COMMAND} -E echo "executing extra step"
DEPENDS FOO2.out
)
endif()
In this case extra step, if needed, will always be executed after FOO2.out is created
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