CMake add_custom_command对构建/配置类型的输出支持

时间:2020-01-13 16:32:26

标签: cmake

我希望add_custom_command()生成不同的输出,具体取决于我是为调试还是为发布而构建(支持Visual Studio多配置构建)。

本质上,我想实现以下目标:

add_custom_command(
    OUTPUT $<IF:$<CONFIG:Debug>, file_to_generate_in_debug, file_to_generate_in_release>
    COMMAND something.exe
    ...
)

不幸的是,OUTPUT参数不支持生成器表达式。

我应该如何使用Modern CMake?


为了更具体一点,我尝试在windeployqt.exe上运行,根据实际的构建配置,我的输出应该是Qt5Core.dllQt5Cored.dll(可以知道在配置时(例如,对于Ninja)或在生成时(例如,对于Visual Studio)。

1 个答案:

答案 0 :(得分:1)

根据需要制作的{em> OUTPUT 中创建尽可能多的add_custom_command

# A command which can be applied for Debug configuration(s)
add_custom_command(
    OUTPUT file_to_generate_in_debug
    COMMAND something.exe
    ...
)
# A command which can be applied for Release configuration(s)
add_custom_command(
    OUTPUT file_to_generate_in_release
    COMMAND something.exe
    ...
)

仅该实例将在(其他)add_custom_command / add_custom_target中被 OUTPUT 用作 DEPENDS 的特定配置中处于活动状态。与 OUTPUT 不同, DEPENDS 子句支持生成器表达式。

# This will chose a command suitable for configuration
add_custom_target(MY_GEN_LIB
   DEPENDS $<IF:$<CONFIG:Debug>, file_to_generate_in_debug, file_to_generate_in_release>
)

如果其他一些目标需要依赖于配置的输出文件,则可以调整目标级别的依赖关系:

# Depends from the target which produces required file
add_dependencies(other_target MY_GEN_LIB)

即使另一个目标是导入目标,此方法也将起作用。