7

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

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

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

主文件

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

捆绑.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 仍然设置为我机器的路径。

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

4

3 回答 3

4

我在 CMakeLists.txt 的顶部添加了这一行

set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})

就是这样。

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

我不知道我是否没有阅读足够的文档,或者文档是否需要一些精度......

于 2013-08-01T08:36:25.210 回答
2

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

像这样:

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

(注意没有单独的脚本,而是嵌入的代码 - 不应该有所作为)。

于 2015-03-13T12:20:14.323 回答
1

为了回应 timlukins 的回答,如果您要在安装代码中调用任何 CMake 变量,那么您必须非常小心地避开它们以防止早期评估。

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

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

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

您需要保护CMAKE_INSTALL_PREFIX调用,使其完整地进入安装脚本,并使用 install-time 值。有两种方法可以做到这一点。

  1. 愤怒地逃跑在引号内...
    install(CODE "
      include(BundleUtilities)
      fixup_bundle(\"\$\{CMAKE_INSTALL_PREFIX\}/MyApp.app\" \"\" \"\")
    " COMPONENT Runtime)
    
  2. (容易得多:) 使用括号参数...
    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 的“使用qt.conf”:

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

于 2021-01-08T13:10:45.993 回答