如何根据配置更改CPack包文件名?

时间:2017-05-24 09:01:58

标签: cmake cpack

在CMake中,使用CPack我希望CPACK_PACKAGE_FILE_NAME包含-debug后缀,以防从Debug配置生成包。对于单配置CMake生成器,这可能通过检查CMAKE_BUILD_TYPE来实现,但这对于像Visual Studio这样的多配置生成器不起作用。

1 个答案:

答案 0 :(得分:0)

我完成了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}")

因为cpack在配置脚本CPackProperties.cmake的末尾自动包含BundleConfig.cmake脚本,所以您只需要在cmake构建阶段在其中写入实际值BUNDLE执行前的cpack目标。

cmake --build . --config Debug --target BUNDLE