Consider the following custom command (latest CMake + ninja):
add_custom_command(
OUTPUT
${OUTPUT}
COMMAND
${Python3_EXECUTABLE} script.py ${INPUT} > ${OUTPUT}
DEPENDS
${INPUT}
VERBATIM
COMMAND_EXPAND_LISTS
)
When script.py
runs without errors, it works fine.
However, when script.py
fails with an error, ${OUTPUT}
is still created.
So current build fails as expected but next build sees ${OUTPUT}
newer than ${INPUT}
and does not try to run the custom command again as it should.
I would expect the build system to automatically delete ${OUTPUT}
when the command fails, to prevent such case, but apparently this does not happen.
- Is there a way to perform an action "on failure" of a custom command?
If there was, I could delete${OUTPUT}
there. - Alternatively, what is the simplest way to prevent output creation unless command succeeds?
I've tried naively to do something like:
${Python3_EXECUTABLE} script.py ${INPUT} > ${OUTPUT} || rm -f ${OUTPUT}
But that doesn't work because the command result code is in fact the rm
result code instead of Python's result code, therefore the custom command doesn't fail as it should on a subsequent build.