我想运行一个解析整个源树的 cmake 命令,所以我无法在 cmake 的 add_custom_command/add_custom_target 命令中列出所有可能的依赖项。
是否可以告诉 cmake 只在没有任何条件的情况下运行命令?我尝试了网上找到的所有解决方案(包括 SO),但他们都假设该命令依赖于少数已知文件是最新的。
我找到了一个解决方案,但它不能可靠地工作:
cmake_minimum_required(VERSION 2.6)
project(main)
add_custom_command(
OUTPUT file1
COMMAND echo touching file1
COMMAND touch file1
DEPENDS file2)
add_custom_target(dep ALL DEPENDS file1 file2)
# this command re-touches file2 after dep target is "built"
# and thus forces its rebuild
ADD_CUSTOM_COMMAND(TARGET dep
POST_BUILD
COMMAND echo touching file2
COMMAND touch file2
)
这是输出:
queen3@queen3-home:~/testlib$ make
[100%] Generating file1
touching file1
touching file2
[100%] Built target dep
queen3@queen3-home:~/testlib$ make
[100%] Generating file1
touching file1
touching file2
[100%] Built target dep
queen3@queen3-home:~/testlib$ make
touching file2
[100%] Built target dep
queen3@queen3-home:~/testlib$
如您所见,在第三次运行时,它没有生成 file1,即使之前已触及 file2。有时每 2 次运行一次,有时每 3 次运行一次,有时每 4 次运行一次。这是一个错误吗?有没有另一种方法可以在 cmake 中没有任何依赖的情况下运行命令?
奇怪,但如果我添加两个命令来重新触摸 file2,即只是复制粘贴 post-build 命令,它可以可靠地工作。或者它可能每 1000 次运行就会失败,我还不确定;-)
最佳答案
虽然我对这个解决方案一点也不满意,但自从我偶然发现这个页面并且没有看到它被提及后就发帖了。
您可以添加一个引用丢失文件的自定义目标,
例如:
add_custom_target(
my_custom_target_that_always_runs ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/__header.h
)
add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/__header.h # fake! ensure we run!
${CMAKE_CURRENT_BINARY_DIR}/header.h # real header, we write.
# this command must generate: ${CMAKE_CURRENT_BINARY_DIR}/header.h
COMMAND some_command
)
这将继续运行自定义命令,因为 __header.h
没有找到。见 working example在哪里使用。
关于cmake - 无论任何依赖项如何在构建时始终运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920072/