Search code examples
c++visual-c++cmakepost-build-eventpost-build

CMake: How to copy different files to build directory according to configuration and architecture


I'm generating my vcxproj and sln files for MSVC with CMake. I want to copy some dlls in build directory as POST_BUILD event which are different according whether I'm building Debug or Release configuration and whether it is x86 or x64 architecture. I'm using add_custom_command the following way

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
    ${FILES_TO_COPY} ${CMAKE_CURRENT_BINARY_DIR})

I want to set FILES_TO_COPY to different values according to architecture and configuration or to use different add_custom_command according the same conditions. But CMake MSVC is multi config generator and CMAKE_BUILD_TYPE is empty under it and I cannot simply write something like:

if (CMAKE_SIZEOF_VOID_P EQUAL 4)
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")

    set (FILES_TO_COPY
      "${CMAKE_SOURCE_DIR}/Externals/DLLs/bdb/Debug/i386/libdb52d.dll")

  elseif (CMAKE_BUILD_TYPE STREQUAL "Release")

    set (FILES_TO_COPY
      "${CMAKE_SOURCE_DIR}/Externals/DLLs/bdb/Release/i386/libdb52.dll")

  else ()
    message (FATAL_ERROR "Invalid configuration name ${CMAKE_BUILD_TYPE}.")
  endif ()
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")

    set (FILES_TO_COPY
      "${CMAKE_SOURCE_DIR}/Externals/DLLs/bdb/Debug/x64/libdb52d.dll")

  elseif (CMAKE_BUILD_TYPE STREQUAL "Release")

    set (FILES_TO_COPY
      "${CMAKE_SOURCE_DIR}/Externals/DLLs/bdb/Release/x64/libdb52.dll")

  else ()
    message (FATAL_ERROR "Invalid configuration name ${CMAKE_BUILD_TYPE}.")
  endif ()
else ()
  message (FATAL_ERROR "Unsupported architecture with ${CMAKE_SIZEOF_VOID_P}
    bytes pointer size.")
endif ()

How to do this properly?


Solution

  • try this:

    set(arch "i386")
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(arch "x64")
    endif()
    
    set(FILES_TO_COPY "${CMAKE_SOURCE_DIR}/Externals/DLLs/bdb/$<CONFIG>/${arch}/libdb52$<$<CONFIG:Debug>:d>.dll")