Cosi10a1
Cosi10a1

Reputation: 41

How to determine current build type of visual studio with cmake

I don't know how to configure multiple build types in Visual Studio with Cmake. For example, when debug is selected in Visual Studio, I need to copy *d.dll to ${CMAKE_BINARY_DIR}/rundir/debug/bin and when release is selected in Visual Studio, I need to copy *.dll to ${CMAKE_BINARY_DIR}/rundir/release/bin.

Can someone tell me how to do that?

Upvotes: 1

Views: 6185

Answers (2)

Moreno Trlin
Moreno Trlin

Reputation: 41

The previous answer was very usefull for me, but it didn't work for me so I maid these modifications to make it work:

  • I replaced if $<CONFIG:Debug> by if $<CONFIG:Debug> neq 0, the same for if $<CONFIG:Release>
  • ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG} and ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE} where not defined in my case, so I replaced them by $<TARGET_FILE_DIR:myTarget>

Upvotes: 2

Torbj&#246;rn
Torbj&#246;rn

Reputation: 5800

As you want a post-build action, there is a CMake command and also the appropriate variables available.

The platform independent command to copy around files with CMake is using CMake itself on the command line:

${CMAKE_COMMAND} -E copy_if_different "${src}" "${dest}"

The "current" configuration can be extracted with generator expressions:

$<CONFIG>

and even directly tested for trueness

$<CONFIG:Debug>

The output directory for a target's binaries (i.e. executables and shared libraries/DLLs) is given with the target property RUNTIME_OUTPUT_DIRECTORY (and RUNTIME_OUTPUT_DIRECTORY_<CONFIG>) which are pre-populated with the global variable CMAKE_RUNTIME_OUTPUT_DIRECTORY (and CMAKE_RUNTIME_OUTPUT_DIRECTORY_<CONFIG>).

Finally, we can compose the post-build command

add_custom_command(TARGET myTarget POST_BUILD
                   COMMAND if $<CONFIG:Debug> ("${CMAKE_COMMAND}" -E copy_if_different "${path_to_dependent_dll}/dependent.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}")
                   COMMAND if $<CONFIG:Release> ("${CMAKE_COMMAND}" -E copy_if_different "${path_to_dependent_dll}/dependent.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}")
                   COMMENT "Copying dependent DLL"
)

Upvotes: 3

Related Questions