sthlm58
sthlm58

Reputation: 1234

CMake add_custom_command OUTPUT support for build/configuration type

I want add_custom_command() to produce a different output depending on whether I'm building for Debug or Release (with a support for Visual Studio multi-configuration builds).

Essentially, I want to achieve something like:

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

Unfortunately, the generator expressions are not supported for the OUTPUT argument.

How should I do it using Modern CMake?


To make it more concrete, I'm trying to run windeployqt.exe to and my output should be either Qt5Core.dll or Qt5Cored.dll depending on the actual build configuration (which could either be known at Configure-time (e.g. for Ninja) or at Generate-time (e.g. for Visual Studio)).

Upvotes: 2

Views: 1708

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

Create as many add_custom_command as different OUTPUT you need to produce.

# 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
    ...
)

Only that instance will be active in a specific configuration which OUTPUT is used as DEPENDS in (other) add_custom_command/add_custom_target. Unlike to OUTPUT, DEPENDS clause supports generator expressions.

# 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>
)

If some other target needs your configuration-dependent output file, then you may adjust target-level dependencies:

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

This will work even if the other target is an IMPORTED one.

Upvotes: 2

Related Questions