【发布时间】:2012-11-14 15:25:02
【问题描述】:
如何配置/破解 cmake 以构建使用 add_executable() 添加但不安装的特定可执行文件?
可执行文件是一个单元测试,最终将使用 add_test 处理,但现在我只想尽可能少地从版本中剥离测试二进制文件。
谢谢
【问题讨论】:
标签: cmake
如何配置/破解 cmake 以构建使用 add_executable() 添加但不安装的特定可执行文件?
可执行文件是一个单元测试,最终将使用 add_test 处理,但现在我只想尽可能少地从版本中剥离测试二进制文件。
谢谢
【问题讨论】:
标签: cmake
由于 EXCLUDE_FROM_ALL 有undefined behavior if combined with INSTALL(cmake 试图警告你,设置 OPTIONAL 没关系),保证工作的解决方案更加复杂。
你应该:
Remove dependency of "install" target to "all" target(一次,在主 CMakeLists.txt 中):
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
将 OPTIONAL 添加到测试库中的 INSTALL 语句中。请注意我报告的 CMake 可能存在的错误 here
分别收集您想要包含在自定义“all_but_tests”中的所有目标 CMake add_custom_target depending on whole project being built(最难的一步)
创建custom all_but_tests target:
add_custom_target(all_but_tests DEPENDS <<list of targets>>)
将目标安装的依赖添加到 all_but_tests
add_dependency(install all_but_tests)
(对不起,这个我没试过,欢迎反馈)
add_custom_target(my_tests DEPENDS <<list of tests>>)
那么(假设您使用 make,但也适用于 ninja):
您可以调用make install,它会触发make all_but_tests,并在构建完成后安装。
您可以拨打make my_tests,然后拨打make install,在这种情况下,它将安装所有内容。您可以像这样连接命令
make my_tests && make install
或者,因为在这种情况下没有区别:
make [all] && make install
我被这个问题所吸引是因为我最近不得不面对一个类似的问题:Installing only one target and its dependencies
编辑:
add_dependency(install all_but_tests) 可能会not work。
所以,要么你use an adequate workaround,要么你打电话
make all_but_tests && make install
每次你想安装“all_but_tests”
【讨论】:
如果你对它应用install 函数,CMake 只会安装一个可执行目标,即:
install(TARGETS ExecutableTest RUNTIME DESTINATION "bin")
要防止为Release 构建安装ExecutableTest,请添加CONFIGURATIONS 限制:
install(TARGETS ExecutableTest RUNTIME DESTINATION "bin" CONFIGURATIONS Debug)
或者,您可以将ExecutableTest 设为可选目标,默认情况下不构建:
add_executable(ExecutableTest EXCLUDE_FROM_ALL ${ExecutableTestFiles})
然后可选地仅安装ExecutableTest(如果已明确构建):
install(TARGETS ExecutableTest RUNTIME DESTINATION "bin" OPTIONAL)
所有可选的测试目标都可以集中在一个超级目标中,以便一步构建它们:
add_custom_target(MyTests DEPENDS ExecutableTest ExecutableTest2 ExecutableTest3)
【讨论】:
add_custom_target(MyTests DEPENDS ExecutableTest ExecutableTest2 ExecutableTest3),所以加上DEPENDS关键字?