【发布时间】:2021-11-26 10:20:10
【问题描述】:
我想在构建后运行 POST_BUILD 操作(但仅在调试配置中)。
阅读add_custom_command docs 和possible solution 后,我了解到我可以将我的命令“包装”到$<CONFIG:Debug> 生成器表达式中(以确保它在发布模式下为“空”)。
我尝试了以下方法:
cmake_minimum_required(VERSION 3.18)
project(post-build CXX)
file(WRITE main.cxx "int main() {}")
add_executable(foo main.cxx)
add_custom_command(
TARGET foo POST_BUILD
COMMAND $<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo "hi there from debug build">
)
但这给了我 CMake 配置时警告和 构建时的硬故障(使用 Ninja 生成器):
(...) && "$<1:C:\Program Files\CMake\bin\cmake.exe" -E echo "hi there from debug build" >""
[build] The system cannot find the path specified.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
我尝试了许多可能的引号组合(包括转义引号): COMMAND $<$<CONFIG:Debug>:"${CMAKE_COMMAND} -E echo \"hi there from debug build\"">
和 COMMAND "$<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo \"hi there from debug build\">"
等等
但即使它删除了配置时警告,它仍然会在构建时产生硬错误。
问题:实现我想要的正确方法是什么?有可能是这样还是这里有 CMake 限制?
(注意:如果可能的话,我希望将整个命令保留在一个地方执行。我也知道其他可能的解决方法)
【问题讨论】:
标签: cmake cmake-language