5

In CMake, using CPack I want CPACK_PACKAGE_FILE_NAME to include -debug suffix in case the package is produced from Debug configuration. For single-configuration CMake generators this is probably doable by checking CMAKE_BUILD_TYPE, but this does not work for multi-configuration generators like Visual Studio.

Paweł Bylica
  • 3,780
  • 1
  • 31
  • 44

2 Answers2

1

I have made it through the add_custom_target + expression generator + standalone cmake script for the cpack:

CMakeLists.txt

...

set(CPACK_PACKAGE_VERSION "1.0.0.0")
set(CPACK_MONOLITHIC_INSTALL 1)
set(CPACK_OUTPUT_CONFIG_FILE "${CMAKE_BINARY_DIR}/BundleConfig.cmake")

include(CPack)

set(CPACK_BUNDLE_TEMPLATE_CONFIG_FILE "${CMAKE_BINARY_DIR}/CPackConfig.cmake.in")
set(CPACK_BUNDLE_OUTPUT_CONFIG_FILE "${CMAKE_BINARY_DIR}/CPackProperties.cmake")

# make cpack configuration template for later replacements with the expression generator support
file(WRITE "${CPACK_BUNDLE_TEMPLATE_CONFIG_FILE}" "")
file(APPEND "${CPACK_BUNDLE_TEMPLATE_CONFIG_FILE}" "set(CPACK_PACKAGE_FILE_NAME \"\${CPACK_PACKAGE_FILE_NAME}\")\n")

add_custom_target(bundle
  COMMAND ${CMAKE_COMMAND}
    # this one must be written as is, DO NOT put the `$<CONFIGURATION>` inside a variable!
    -D "CPACK_PACKAGE_FILE_NAME=${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}-win32-$<CONFIGURATION>"
    -D "CPACK_BUNDLE_TEMPLATE_CONFIG_FILE=${CPACK_BUNDLE_TEMPLATE_CONFIG_FILE}"
    -D "CPACK_BUNDLE_OUTPUT_CONFIG_FILE=${CPACK_BUNDLE_OUTPUT_CONFIG_FILE}"
    # this one must be after all `-D`s
    -P "${CMAKE_CURRENT_LIST_DIR}/CPackMakeConfig.cmake"
  COMMAND "${CMAKE_CPACK_COMMAND}" 
    "-G" "NSIS"
    "-C" "$<CONFIGURATION>"
    "--config" "${CPACK_OUTPUT_CONFIG_FILE}")

CPackMakeConfig.cmake

if (NOT CPACK_BUNDLE_TEMPLATE_CONFIG_FILE)
  message(FATAL_ERROR "* CPACK_BUNDLE_TEMPLATE_CONFIG_FILE variable must be defined!")
endif()
if (NOT CPACK_BUNDLE_OUTPUT_CONFIG_FILE)
  message(FATAL_ERROR "* CPACK_BUNDLE_OUTPUT_CONFIG_FILE variable must be defined!")
endif()

CONFIGURE_FILE("${CPACK_BUNDLE_TEMPLATE_CONFIG_FILE}" "${CPACK_BUNDLE_OUTPUT_CONFIG_FILE}")

Because the cpack automatically includes the CPackProperties.cmake script at the end of the configuration script BundleConfig.cmake, then you only need is just to write the real values in it at the cmake build stage (BUNDLE target) before the cpack execution.

cmake --build . --config Debug --target BUNDLE
Andry
  • 2,273
  • 29
  • 28
1

I had the same issue which I fixed using file(GENERATE). It's a somewhat shorter solution than the previous one :

set(BUILD_TYPE <IF:$<OR:$<CONFIG:RelWithDebInfo>,$<CONFIG:Debug>>,debug,release>)
set(CPACK_PROPERTIES_FILE "${CMAKE_BINARY_DIR}/YOURPACKAGECPackProperties.cmake")
file(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/YOURPACKAGECPackProperties.cmake" #allows use of generator expression to retrieve CONFIG
        CONTENT "set(CPACK_PACKAGE_FILE_NAME \"yourpackagename_${BUILD_TYPE}\")\n")
BenjaminB
  • 1,809
  • 3
  • 18
  • 32