I want to add a cmake custom command that is only executed when the custom target is build in the Debug configuration while using the Visual Studio multi-config generator. Is there a clean way to do this?
In order to implement this, I first tried wrapping the whole command list in a generator expression like this.
add_custom_command(
...
COMMAND $<$<CONFIG:Debug>:cmake;-E;echo;foo>
)
But this gives me a syntax error when the command is executed. After some trial-and-error I got the following hacky solution to work. This wraps each word of the command list in a generator expression like this.
add_custom_command(
...
COMMAND $<IF:$<CONFIG:Debug>,cmake,echo>;$<IF:$<CONFIG:Debug>,-E, >;$<IF:$<CONFIG:Debug>,echo, >;$<IF:$<CONFIG:Debug>,foo, >
)
This executes the cmake -E echo foo
command when compiling the Debug configuration and the dummy command echo " " " " " "
for all other configurations.
This is quite ugly and the dummy command must be changed depending on the host system. On Linux it could be ":" ":" ":" ":"
. So Is there a cleaner way to do this?
Thank you for your time!
if (CMAKE_BUILD_TYPE STREQUAL "Debug") add_custom_command(...) endif()
? – ArchleCMAKE_BUILD_TYPE STREQUAL "Debug"
is not valid for Visual Studio projects. The use of CONFIG is required in this case, but it is a build-type property since it is not know until then. You probably want to execute a cmake script in your add_custom_command and pass it the value of CONFIG with something like-DBUILD_CONFIG=$<CONFIG>
. Then, in the script you can determine if BUILD_CONFIG equals "DEBUG", etc and act accordingly. That is slightly different than your request though. – Communize