这曾经是 CMake 中的一个错误。
它在以下提交中得到修复:
commit 4adf1dad2a6e462364bae81030c928599d11c24f
Author: Brad King <brad.king@kitware.com>
Date: Mon Mar 30 16:32:26 2015 -0400
Makefile: Tell GNU make to delete rule outputs on error (#15474)
Add .DELETE_ON_ERROR to the "build.make" files that contain the actual
build rules that generate files. This tells GNU make to delete the
output of a rule if the recipe modifies the output but returns failure.
This is particularly useful for custom commands that use shell
redirection to produce a file.
Do not add .DELETE_ON_ERROR for Borland or Watcom make tools because
they may not tolerate it and would not honor it anyway. Other make
tools that do not understand .DELETE_ON_ERROR will not be hurt.
Suggested-by: Andrey Vihrov <andrey.vihrov@gmail.com>
该修复的最早版本是 CMake 3.3.0。
解决方法
- 创建一个附加的输出目标,该目标取决于成功运行的自定义命令。(下面这个叫
main.done
)
- 将自定义命令从 a
POST_BUILD
on aTARGET
切换为独立命令。因此使其成为OUTPUT
一个文件(main.intermediate_step
如下)。
- 使用该选项运行一秒钟
COMMAND
以使用touch
.
代码:
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."
)
add_custom_command(OUTPUT main.intermediate_step
COMMAND exit 1 # In the real example, I am changing capabilities of the resulting binary with /sbin/setcap, which might fail.
COMMAND touch main_setcap.passed
DEPENDS main
COMMENT "Doing stuff."
)
add_custom_target(main.done ALL DEPENDS main.intermediate_step)
注意:make main
不会运行自定义命令。为此使用make main.done
。make
并将make all
按预期工作。