CMake 3.20 enables using generator expressions in the add_custom_command(OUTPUT)
argument.
I am now trying to create a custom command that only runs a command for a subset of the available CMAKE_CONFIGURATION_TYPES
. After a lot of trial and error I came up with an example that works for a single configuration:
# Works with warning when compiling in non-Debug
add_custom_command(
COMMAND $<$<CONFIG:Debug>:${CMAKE_COMMAND}> $<$<CONFIG:Debug>:-E> $<$<CONFIG:Debug>:touch> $<$<CONFIG:Debug>:${CMAKE_BINARY_DIR}/A_Stamp_$<CONFIG>.stamp>
OUTPUT $<$<CONFIG:Debug>:${CMAKE_BINARY_DIR}/A_Stamp_$<CONFIG>.stamp>
)
add_custom_target(
testConfigCommands
DEPENDS $<$<CONFIG:Debug>:${CMAKE_BINARY_DIR}/A_Stamp_$<CONFIG>.stamp>
)
I failed to create an example that works for multiple configurations. I tried something like:
add_custom_command(
COMMAND $<$<IN_LIST:$<CONFIG>,Debug;Release>:${CMAKE_COMMAND}> $<$<IN_LIST:$<CONFIG>,Debug;Release>:-E> $<$<IN_LIST:$<CONFIG>,Debug;Release>:touch> $<$<IN_LIST:$<CONFIG>,Debug;Release>:${CMAKE_BINARY_DIR}/A_Stamp_$<CONFIG>.stamp>
OUTPUT $<$<IN_LIST:$<CONFIG>,Debug;Release>:${CMAKE_BINARY_DIR}/A_Stamp_$<CONFIG>.stamp>
)
add_custom_target(
testConfigCommands
DEPENDS $<$<IN_LIST:$<CONFIG>,Debug;Release>:${CMAKE_BINARY_DIR}/A_Stamp_$<CONFIG>.stamp>
)
but this will fail with the error:
OUTPUT containing a “<” is not allowed.
I tried escaping the semicolon and other stuff but I keep hitting various errors that I have trouble deciphering. So in my desparation I came here and ask if anybody can post a similar example that creates the stamp-file only when selecting the Release or Debug configuration when using the Visual Studio generator. If that would work without a waring when building the target for a configruation that is not Release or Debug it would be perfect.
It would be even better if the target was only available for a subset of configurations.