我真的很难让CPack发挥我想象中的方式。我在下面有一个小的示例设置,在将其应用到更大的项目之前,我一直在尝试进行工作。本质上,我试图使用fixup_bundle收集所有依赖项并将它们复制到打包目录中,以合并到安装程序(和zip)中。

main.cpp:

int main(unsigned int argc, const char* argv[])
{
    return 0;
}

CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)

find_package(Qt5 REQUIRED COMPONENTS Core Gui Multimedia Widgets AxContainer OpenGL)

add_executable(TEST main.cpp)

target_link_libraries(TEST
    Qt5::AxContainer
    Qt5::Core
    Qt5::Multimedia
    Qt5::Gui
    Qt5::Widgets
    Qt5::OpenGL
    )

install(
    TARGETS TEST
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    COMPONENT Runtime
    )

install(CODE "MESSAGE(\"CPACK_PACKAGING_PREFIX=${CPACK_PACKAGING_PREFIX}\")")
install(CODE "MESSAGE(\"CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}\")")
install(CODE "MESSAGE(\"CPACK_INSTALL_PREFIX=${CPACK_INSTALL_PREFIX}\")")
install(CODE "MESSAGE(\"CPACK_INSTALL_DIRECTORY=${CPACK_INSTALL_DIRECTORY}\")")
install(CODE "MESSAGE(\"CPACK_PACKAGE_INSTALL_DIRECTORY=${CPACK_PACKAGE_INSTALL_DIRECTORY}\")")
install(CODE "MESSAGE(\"CPACK_PACKAGING_INSTALL_PREFIX=${CPACK_PACKAGING_INSTALL_PREFIX}\")")
install(CODE "MESSAGE(\"CPACK_PACKAGE_DIRECTORY=${CPACK_PACKAGE_DIRECTORY}\")")
install(CODE "MESSAGE(\"CPACK_TOPLEVEL_DIRECTORY=${CPACK_TOPLEVEL_DIRECTORY}\")")
install(CODE "MESSAGE(\"CPACK_TEMPORARY_DIRECTORY=${CPACK_TEMPORARY_DIRECTORY}\")")
install(CODE "MESSAGE(\"CPACK_OUTPUT_FILE_NAME=${CPACK_OUTPUT_FILE_NAME}\")")
install(CODE "MESSAGE(\"CPACK_OUTPUT_FILE_PATH=${CPACK_OUTPUT_FILE_PATH}\")")
install(CODE "MESSAGE(\"CPACK_TEMPORARY_PACKAGE_FILE_NAME=${CPACK_TEMPORARY_PACKAGE_FILE_NAME}\")")
install(CODE "MESSAGE(\"CPACK_NATIVE_INSTALL_DIRECTORY=${CPACK_NATIVE_INSTALL_DIRECTORY}\")")
install(CODE "MESSAGE(\"CPACK_TEMPORARY_INSTALL_DIRECTORY=${CPACK_TEMPORARY_INSTALL_DIRECTORY}\")")


install(CODE "
   include(BundleUtilities)
   fixup_bundle(\"${CPACK_PACKAGE_DIRECTORY}/TEST${CMAKE_EXECUTABLE_SUFFIX}\"   \"\"   \"${DIRS}\")
   " COMPONENT Runtime)

set(CPACK_GENERATOR "ZIP" "NSIS")

include(CPack)

输出总是一堆空变量:
CPACK_PACKAGING_PREFIX=
CMAKE_INSTALL_PREFIX=C:/Program Files (x86)/Project
CPACK_INSTALL_PREFIX=
CPACK_INSTALL_DIRECTORY=
CPACK_PACKAGE_INSTALL_DIRECTORY=
CPACK_PACKAGING_INSTALL_PREFIX=
CPACK_PACKAGE_DIRECTORY=
CPACK_TOPLEVEL_DIRECTORY=
CPACK_TEMPORARY_DIRECTORY=
CPACK_OUTPUT_FILE_NAME=
CPACK_OUTPUT_FILE_PATH=
CPACK_TEMPORARY_PACKAGE_FILE_NAME=
CPACK_NATIVE_INSTALL_DIRECTORY=
CPACK_TEMPORARY_INSTALL_DIRECTORY=
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.4/Modules/BundleUtilities.cmake:861 (message):
  error: fixup_bundle: not a valid bundle, app: /TEST.exe

显然,它将找不到正确的可执行文件。我期望CPACK_PACKAGE_DIRECTORY是这样的:

$ {CMAKE_BINARY_DIR} / _ CPack_Packages / $ {CPACK_SYSTEM_NAME} / $ {CPACK_GENERATOR} / $ {CPACK_PACKAGE_FILE_NAME}

更新2016-06-23

感谢@Tsyvarev帮助确定问题和解决方案。问题是安装命令中的变量过早扩展。 CMake将对文本行中的任何$ {}变量进行一级扩展。将变量转义为\ $ {}可以避免这种扩展,以后将由CPack正确地扩展。

即使使用上述方法,除一个变量外,所有变量仍为空。幸运的是,它是正确的:
install(CODE "MESSAGE(\"CMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX}\")")

结果是:
CMAKE_INSTALL_PREFIX=C:/Users/User/Documents/dev/build-test_deploy-Desktop_Qt_5_5_1_MSVC2012_32bit-Default/_CPack_Packages/win32/ZIP/Project-0.1.1-win32

现在 bundle 将这样工作:
install(CODE "
   include(BundleUtilities)
   fixup_bundle(\"\${CMAKE_INSTALL_PREFIX}/bin/TEST${CMAKE_EXECUTABLE_SUFFIX}\"   \"\"   \"\")
   " COMPONENT Runtime)

也许还有一个更重要的说明,因为fixup_bundle使用了一组固定的字符串,因此您将必须具有漂亮的ridge安装结构。基本上,以上内容仅适用于整体安装:
set(CPACK_MONOLITHIC_INSTALL on)

我希望有一种比上述更好的解决方案,但是我可以解决这个问题。

最佳答案

正确使用CPack变量的方法,该变量是通过install(CODE)命令在安装时间定义的:

install(CODE "
    include(CPack)
    MESSAGE(\"CPACK_PACKAGE_DIRECTORY=\${CPACK_PACKAGE_DIRECTORY}\")
")

这里要点:
  • 应该对变量的扩展名进行转义:\${...}。否则,如果尚 undefined variable ,则会在配置阶段立即对其进行扩展。
  • 安装代码仍然需要include(CPack)才能使CPack变量可用。
  • 关于c++ - CPack空CPACK_PACKAGE_DIRECTORY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37981374/

    10-10 09:52