In CMake, I would like to use add_custom_command(... POST_BUILD ...)
with a COMMAND
that might fail.
Observation
- Running
make
will fail the first time, because the exit code ofadd_custom_command( ... COMMAND exit 1)
is not0
. --> This is what I would expect. - Running
make
a second time will pass, because the command specified in theadd_custom_command
will not be run again. --> This is not what I want. I would wantmake
to fail until the custom command works.
Minimium Failing Example
project(Foo)
cmake_minimum_required(VERSION 3.2)
# Create main.cc
##include <iostream>
#
#int main() {
# std::cout << "Hello, World!" << std::endl;
#}
add_executable(main main.cc)
add_custom_command(TARGET main POST_BUILD
COMMAND exit 1 # In the real example, I am changing capabilities of the resulting binary with /sbin/setcap, which might fail.
COMMENT "Doing stuff."
)
Question
- How can I resolve this issue?
- Is this intended behavior of CMake?
One solution
I am aware that I can create a custom command that is not a POST_BUILD
but instead outputs a file TARGET.passed
on success. However, I would like to avoid this. Because POST_BUILD seems the most appropriate usage here. (I am changing capabilities of the resulting file.)