1

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?

bobeff
  • 3,543
  • 3
  • 34
  • 62
  • if you studied the output of the compiler when invoked with --help, or --version would that help? – Richard Hodges Jul 04 '17 at 15:37
  • @Richard Hodges I don't know how to study the output of the compiler from **CMake** and how to invoke it with `--help` or `--version` from **CMake**. – bobeff Jul 04 '17 at 15:54
  • 1
    execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version/help? OUTPUT_VARIABLE compiler_output) or similar – Richard Hodges Jul 04 '17 at 16:09
  • @Richard Hodges The output contains architecture type which I also can determine with `CMAKE_SIZEOF_VOID_P ` but it cannot contain `Debug` or `Release` which are real problem. – bobeff Jul 04 '17 at 16:15
  • Ah I see. The answer below using generator expressions solves it though, right? – Richard Hodges Jul 04 '17 at 16:39

1 Answers1

4

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")
onqtam
  • 4,356
  • 2
  • 28
  • 50
  • +1 10x This works for this case but I can't accept this answer because the path to files is not required to contain `Debug`, `Release`, `i386` or `x64`. For some of the other files which I have to add the pattern in the path can be different if any. – bobeff Jul 04 '17 at 15:43
  • 2
    @bobeff: For general cases you may use `$<$:path-debug>$<$:path-release>` pattern. See documentation for generator expressions: https://cmake.org/cmake/help/v3.7/manual/cmake-generator-expressions.7.html. – Tsyvarev Jul 04 '17 at 19:48
  • @Tsyvarev I think that this completes the answer and I'm accepting it. – bobeff Jul 05 '17 at 08:23