CMake:如何根据配置和体系结构将不同的文件复制到构建目录

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

我正在为 MSVCCMake 生成我的 vcxprojsln 文件。我想在构建目录中复制一些 dlls 作为 POST_BUILD 事件,这根据我构建的是 Debug 还是 Release 配置以及它是否是 x86 而不同或 x64 架构。我正在按以下方式使用 add_custom_command

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

我想根据体系结构和配置将 FILES_TO_COPY 设置为不同的值,或者根据相同的条件使用不同的 add_custom_command。但是 CMake MSVC 是多配置生成器,CMAKE_BUILD_TYPE 下面是空的,我不能简单地写这样的东西:

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 ()

如何正确执行此操作?

试试这个:

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")