通过 Bazel 在 macOS 上构建并 运行 Qt 应用程序

Build and run a Qt application on macOS via Bazel

我尝试使用 Bazel 5.0.0 在 macOS (10.15.7) 上构建并 运行 Qt5 (5.15.2) 应用程序。 不幸的是,我运行陷入了一些问题。 构建部分似乎有效,但 运行 部分无效。

我使用 Homebrew 在我的机器上安装了 Qt5:

brew install qt@5
brew link qt@5

我改编了https://github.com/justbuchanan/bazel_rules_qt/ to my needs. See this PR。当我尝试 运行:

bazel run --cxxopt=-std=c++17 //tests/qt_resource:main

我收到 运行时间错误:

dyld: Symbol not found: __ZN10QByteArray6_emptyE

重现问题的步骤:

# brew install bazel # Install Bazel
# brew install qt@5  # Install Qt5
git clone https://github.com/Vertexwahn/bazel_rules_qt.git
cd bazel_rules_qt
git checkout add-macos-support
bazel build --cxxopt=-std=c++17 //... # should work
bazel run --cxxopt=-std=c++17 //tests/qt_resource:main # should give you the error message

尽管如此,使用 bazel build --cxxopt=-std=c++17 //... 构建一切似乎都可行。 我不是 100% 确定 link 选项 -F/usr/local/opt/qt5/Frameworks-framework QtCore 等是否正确。 也许有人可以证实这一点。

我是否使用了正确的 link 选项?

对我来说,有点不清楚 main 二进制文件需要什么依赖项。我尝试手动将 QtCore.framework 复制到 main 二进制文件的位置,但这不会更改错误消息。

main 二进制需要什么文件?

如果我在 main 二进制文件上尝试 运行 macdeployqt,我也会遇到一些错误。我在我的工作区根目录中执行 cd bazel-bin/tests/qt_resource 和 运行 然后 /usr/local/opt/qt5/bin/macdeployqt main:

ERROR: Could not find bundle binary for "main"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file:  (No such file or directory)\n"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file:  (No such file or directory)\n"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/otool-classic: can't open file:  (No such file or directory)\n"
WARNING:
WARNING: Could not find any external Qt frameworks to deploy in "main"
WARNING: Perhaps macdeployqt was already used on "main" ?
WARNING: If so, you will need to rebuild "main" before trying again.
ERROR: Could not find bundle binary for "main"
ERROR: "error: /Library/Developer/CommandLineTools/usr/bin/strip: can't open file:  (No such file or directory)\n"
ERROR: ""

我希望 macdeployqt 能为我收集所有需要的资源。知道为什么这不起作用吗?

如何使用 macdeployqt 收集 main 二进制文件所需的所有依赖项?

如果我通过 lipo -create -output universall_app main 将我的主程序转换为应用程序,然后执行 /usr/local/opt/qt5/bin/macdeployqt universall_app 我会收到相同的错误消息。

CMake 方法

为了确保我的系统设置没有一般性问题,我尝试使用 CMake 构建 Qt5 应用程序:

git clone https://github.com/euler0/mini-cmake-qt.git
cmake -DCMAKE_PREFIX_PATH=/usr/local/opt/qt5 .
make -j

这会产生一个 example.app。 双击此应用程序包, 可以启动应用程序。 这在我的系统上有效。

未来方向

看来 rules_apple 可以用来创建应用程序包。 我不确定是否需要将我的 Qt 应用程序二进制文件转换为应用程序包才能执行它。 可以使用 --sandbox_debug 来识别 Bazel 在做什么,并使用 dtruss 来比较 CMake 版本的差异。 我目前不确定下一步要做什么,希望有一个简单的解决方案。我也可以使用 Qt6 解决方案。

更新:备选答案

如果有人可以指出如何仅在 macOS 上使用 make 构建最小 Qt 应用程序并且安装了 brew Qt5 或者告诉我 linker 和编译器选项必须是什么样子,这也会很有帮助。

我按照你的步骤使用 Mac OSX 10.15.7、Qt(由自制软件安装)5.15.1 和 bazel 4.2.2-homebrew 和 5.0.0-homebrew,最初我无法从 git:

构建项目

* 3fe5f6c - (4 weeks ago) Add macOS support — Vertexwahn (HEAD -> add-macos-support, origin/add-macos-support)

这是我构建时得到的结果:

 % bazel build --cxxopt=-std=c++17  //...
 DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path:  /usr/local/opt/qt5
 INFO: Analyzed 14 targets (0 packages loaded, 0 targets configured).
 INFO: Found 14 targets...
 ERROR: /Users/home/Git/my_repo/bazel_rules_qt/tests/qt_qml/BUILD:4:10: Compiling tests/qt_qml/main.cc failed: (Aborted): wrapped_clang failed: error executing command external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG '-std=c++11' ... (remaining 38 argument(s) skipped)

 Use --sandbox_debug to see verbose messages from the sandbox
 tests/qt_qml/main.cc:1:10: fatal error: 'QtQml/QQmlApplicationEngine' file not found
 #include <QtQml/QQmlApplicationEngine>
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1 error generated.
 Error in child process '/usr/bin/xcrun'. 1
 INFO: Elapsed time: 0,594s, Critical Path: 0,32s
 INFO: 3 processes: 3 internal.
 FAILED: Build did NOT complete successfully

玩完 headers 并在 qt.BUILD 中包含路径:

 diff --git a/qt.BUILD b/qt.BUILD
 index 517c8db..8f110b5 100644
 --- a/qt.BUILD
 +++ b/qt.BUILD
 @@ -28,11 +28,12 @@ QT_LIBRARIES = [
      cc_library(
          name = "qt_%s_osx" % name,
          # When being on Windows this glob will be empty
 -        hdrs = glob(["%s/**" % include_folder], allow_empty = True),
 +        hdrs = glob(["include/%s/**" % include_folder], allow_empty = True),
          includes = ["."],
          linkopts = ["-F/usr/local/opt/qt5/lib"] + [
              "-framework %s" % library_name.replace("5", "") # macOS qt libs do not contain a 5 - e.g. instead of Qt5Core the lib is called QtCore
              ],
 +        strip_include_prefix= "include"
          # Available from Bazel 4.0.0
          # target_compatible_with = ["@platforms//os:osx"],
      )

我可以构建并 运行 项目:

 % bazel build --cxxopt=-std=c++17  //...
 DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path:  /usr/local/opt/qt5
 INFO: Analyzed 14 targets (1 packages loaded, 7422 targets configured).
 INFO: Found 14 targets...
 INFO: Elapsed time: 11,761s, Critical Path: 7,23s
 INFO: 3 processes: 1 internal, 2 darwin-sandbox.
 INFO: Build completed successfully, 3 total actions
 % bazel run --cxxopt=-std=c++17 //tests/qt_resource:main
 DEBUG: /private/var/tmp/_bazel_home/761aafaa2237a9607dd915f1f52bca3e/external/com_justbuchanan_rules_qt/qt_configure.bzl:43:14: Installation available on the default path:  /usr/local/opt/qt5
 INFO: Analyzed target //tests/qt_resource:main (0 packages loaded, 0 targets configured).
 INFO: Found 1 target...
 Target //tests/qt_resource:main up-to-date:
 bazel-bin/tests/qt_resource/main
 INFO: Elapsed time: 3,657s, Critical Path: 0,00s
 INFO: 1 process: 1 internal.
 INFO: Build completed successfully, 1 total action
 INFO: Build completed successfully, 1 total action
 opened resource file
 file1

与你的链接器选项是否正确的问题相关 -F/usr/local/opt/qt5/Frameworks -framework QtCore

是的,它们是正确的,您也可以使用 -F/usr/local/opt/qt5/lib(因为您已经在 qt.BUILD 中使用),因为 Frameworks 文件夹下的所有文件都是指向 lib 文件夹的链接。

使用 macdeployqtlipo 即使在测试 运行ning 成功后,我得到的结果与 OP 中的结果相同。