我想在构建后运行 POST_BUILD 操作(但仅在调试配置中)。
看完之后add_custom_command 文档 https://cmake.org/cmake/help/latest/command/add_custom_command.html and a 可能的解决方案 https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912我明白我可以将我的命令“包装”成$<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\"">
and
COMMAND "$<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo \"hi there from debug build\">"
etc.
但即使它删除了配置时警告,它仍然会在构建时产生硬错误。
Question: 实现我想要的目标的正确方法是什么?是否有可能像这样或者这里有 CMake 限制?
(注意:如果可能的话,我希望将整个命令保留在一个地方执行。我还知道其他可能的解决方法)