无法使用fixup_bundle()来创建带有Qt的可移植包

时间:2013-07-31 15:22:32

标签: c++ qt cmake

我已经在其他帖子上搜索了这个问题,但到目前为止还没有。我在这里。

我想创建一个可移植的包。便携式,如“我可以在任何OS X机器上运行它,即使我没有安装我需要的库(Qt)”。不幸的是,我无法弄清楚如何使用fixup_bundle()(这似乎是它的正确工具)来实现这一目标。

这是我最小的CMake生成的C ++项目:

的main.cpp

#include <QString>
#include <iostream>

int main()
{    
    QString s("Hello, world!");
    std::cout << s.toStdString() << std::endl;
    return 0;
}

的CMakeLists.txt

cmake_minimum_required(VERSION 2.8.11)

project(test)

# That part because I use a custom build of Qt. That's not the
# relevant part (I guess?)
FILE(GLOB QTROOTS path_to_qt/Qt-4.8.1/osx/bin/qmake)
find_program(QT_QMAKE_EXECUTABLE NAMES qmake PATHS ${QTROOTS})
find_package(Qt4 COMPONENTS QtCore REQUIRED)
include(${QT_USE_FILE})

add_executable(test MACOSX_BUNDLE main.cpp)

target_link_libraries(test ${QT_LIBRARIES})

install(SCRIPT bundle.cmake)

bundle.cmake

INCLUDE(BundleUtilities)
fixup_bundle(test.app "" "")

这是生成的test.app结构

test.app
 - Contents
    - Info.plist
    - Frameworks
       - QtCore.framework
          - Versions
             - 4
                - QtCore
    - MacOS
       - test

所需的一切似乎都在捆绑中。一切顺利编译,运行良好,但是当我调用fixup_bundle时,这就是我得到的:

vincent@hpcd0016-lion:tmp/test_bundle/ (0) > make install

[100%] Built target test
Install the project...
-- Install configuration: ""
-- fixup_bundle
--   app='test.app'
--   libs=''
--   dirs=''
-- fixup_bundle: preparing...
-- fixup_bundle: copying...
-- 1/4: *NOT* copying '/Users/vincent/tmp/test_bundle/test.app/Contents/MacOS/test'
-- 2/4: copying 'path_to_qt/Qt-4.8.1/osx/lib//QtCore.framework/Versions/4/QtCore'
-- fixup_bundle: fixing...
-- 3/4: fixing up '/Users/vincent/tmp/test_bundle/test.app/Contents/MacOS/test'
  exe_dotapp_dir/='test.app/Contents/MacOS/'
  item_substring='/Users/vincent/t'
  resolved_embedded_item='/Users/vincent/tmp/test_bundle/test.app/Contents/MacOS/test'

Install or copy the item into the bundle before calling fixup_bundle.
Or maybe there's a typo or incorrect path in one of the args to fixup_bundle?

CMake Error at /Applications/CMake 2.8-11.app/Contents/share/cmake-2.8/Modules/BundleUtilities.cmake:568 (message):
  cannot fixup an item that is not in the bundle...
Call Stack (most recent call first):
  /Applications/CMake 2.8-11.app/Contents/share/cmake-2.8/Modules/BundleUtilities.cmake:656 (fixup_bundle_item)
  bundle.cmake:2 (fixup_bundle)
  cmake_install.cmake:31 (INCLUDE)


make: *** [install] Error 1

有依赖路径(由otool -L给出):

vincent@hpcd0016-lion:test.app/Contents/ (0) > otool -L test.app/Contents/MacOS/test     
    test.app/Contents/MacOS/test:
        path_to_qt/Qt-4.8.1/osx/lib//QtCore.framework/Versions/4/QtCore (compatibility version 4.8.0, current version 4.8.1)
        /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

vincent@hpcd0016-lion:tmp/test_bundle/ (0) > otool -L test.app/Contents/Frameworks/QtCore.framework/Versions/4/QtCore
    test.app/Contents/Frameworks/QtCore.framework/Versions/4/QtCore:
        path_to_qt/Qt-4.8.1/osx/lib//QtCore.framework/Versions/4/QtCore (compatibility version 4.8.0, current version 4.8.1)
        /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 41.0.0)
        /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 635.15.0)
        /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 55010.0.0)
        /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
        /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1094.0.0)
        /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 53.0.0)

显然,fixup_bundle没有为二进制文件修复捆绑包仍然将其ID设置为我的机器路径。

这个简单的例子我做错了什么?

3 个答案:

答案 0 :(得分:2)

我在CMakeLists.txt

的顶部添加了这一行
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})

就是这样。

默认情况下,显然,我的机器上的CMAKE_INSTALL_PREFIX设置为/ usr / local。如果将其更改为我当前的工作目录解决了问题,这意味着CMake正在尝试在/ usr / local上执行某些操作(不允许这样做)。那么为什么错误消息没有提到这样一个正确的访问错误?

我不知道我是否阅读了足够的文档,或者文档是否需要一些精确的内容......

答案 1 :(得分:1)

此外,我实际上必须更明确地了解安装路径(即在.app内)。

像这样:

set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
install(CODE "
    include(BundleUtilities)
    fixup_bundle(${CMAKE_INSTALL_PREFIX}/MyApp.app \"\" \"\")
" COMPONENT Runtime)

(N.B。没有单独的SCRIPT,而是嵌入式代码 - 不应该有所作为。)

答案 2 :(得分:0)

为了响应 timlukins 的回答,如果您要在 install 代码中调用任何 CMake 变量,那么您必须非常小心地避免早期评估。

您发布的代码将在将您的代码嵌入安装脚本之前立即解析 CMAKE_INSTALL_PREFIX。这意味着 cmake_install.cmake 文件将包含:

include(BundleUtilities)
fixup_bundle(/usr/local/MyApp.app "" "")

(或您在生成构建树时将 CMAKE_INSTALL_PREFIX 设置为的任何路径。)这不是您想要的,因为它使您的构建即使在安装之前也无法重定位。

您需要保护 CMAKE_INSTALL_PREFIX 调用,使其完好无损地进入安装脚本,并将使用安装时值。有两种方法可以做到这一点。

  1. 在引号中愤怒地逃逸...
    install(CODE "
      include(BundleUtilities)
      fixup_bundle(\"\$\{CMAKE_INSTALL_PREFIX\}/MyApp.app\" \"\" \"\")
    " COMPONENT Runtime)
    
  2. (更容易:)使用 bracket argument...
    install(CODE [[
      include(BundleUtilities)
      fixup_bundle("${CMAKE_INSTALL_PREFIX}/MyApp.app" "" "")
    ]] COMPONENT Runtime)
    

不会识别任何变量名称或替换括号参数内的引用,因此可以安全地使用未转义的变量名。 (生成器表达式仍会被处理,这有时会很方便。不是为了这个,确切地说,但我只是想了一种方法......)

对于 oarfish 的评论问题:

<块引用>

qt平台插件(libqcocoa.dylib)未捆绑的问题你是怎么解决的?

这就是我可以处理的方法。它经过了轻微的测试,似乎至少是在正确的轨道上。虽然我不禁觉得必须有一种更简单的方法来处理输出路径。

install(CODE [[
  include(BundleUtilities)

  # You could also do this part before the CODE using
  #   install(TARGETS MyApp BUNDLE DESTINATION foo)
  #
  file(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/foo"
    TYPE DIRECTORY FILES "$<TARGET_BUNDLE_DIR:MyApp>")

  # This string is crazy-long enough that it's worth folding into a var...
  set (_plugin_file "$<TARGET_FILE_NAME:Qt5::QCocoaIntegrationPlugin>")

  # Ditto the output paths for our installation
  set (_appdir "${CMAKE_INSTALL_PREFIX}/foo/MyApp.app")
  set (_outdir "${_appdir}/Contents/plugins/platforms")

  file(INSTALL DESTINATION "${_outdir}"
    TYPE FILE FILES "$<TARGET_FILE:Qt5::QCocoaIntegrationPlugin>")

  fixup_bundle("${_appdir}" "${_outdir}/${_plugin_file}" "")
]] COMPONENT Runtime)

生成构建系统后,您可以将输出到此 cmake_install.cmake 文件的二进制目录中的 CMakeLists.txt 文件读取,以查看 install(CODE...) 是否产生了您想要的.

目标路径

如果您想知道 ${_outdir} 路径从何而来,那就是上帝的话语。

来自 Qt 的“Using qt.conf”:

<块引用>

绝对路径按照 qt.conf 文件中的规定使用。所有路径都相对于 Prefix。 [...] 在 macOS 上,Prefix 相对于应用程序包中的 Contents。例如,application.app/Contents/plugins/ 是加载 Qt 插件的默认位置。请注意,插件需要放在plugins目录下的特定子目录中(详见How to Create Qt Plugins)。