How do you make add_custom_command configuration-specific on Windows?

It could be solved with the help of the next "generator expressions" (CMake 2.8.10):

  • $<0:...> = empty string (ignores "...")
  • $<1:...> = content of "..."
  • $<CONFIG:cfg> = '1' if config is "cfg", else '0'

You can combine them to reach behaviour that you need (pseudocode):

if debug then ${DEBUG_EXE_PATH} elseif release then ${RELEASE_EXE_PATH}

which translates to:

$<$<CONFIG:debug>:${DEBUG_EXE_PATH}>$<$<CONFIG:release>:${RELEASE_EXE_PATH}>

So your string will look like:

add_custom_command(TARGET unitTests POST_BUILD
                       COMMAND ${CMAKE_COMMAND} ARGS -E copy "$<$<CONFIG:debug>:${DEBUG_EXE_PATH}>$<$<CONFIG:release>:${RELEASE_EXE_PATH}>" "$<$<CONFIG:debug>:${DEBUG_NEW_EXE}>$<$<CONFIG:release>:${RELEASE_NEW_EXE}>")

Details: CMake:add_custom_command


This is a case for the generator expressions provided for use with add_custom_command.

In your case you want the full path to your compiled exe, and also its filename to append to your destination directory. These are $<TARGET_FILE:unitTests> and $<TARGET_FILE_NAME:unitTests> respectively.

Your full command would be:

add_custom_command(TARGET unitTests POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E
                       copy $<TARGET_FILE:unitTests>
                            ${RUN_UNITTESTS_DIR}/$<TARGET_FILE_NAME:unitTests>)

Tags:

Cmake