在macOS M1 上编译 arm64 qt5.15

许多库,共享出来的代码在往往存在一定的编译问题,Qt 源代码的问题也很多。不同版本存在不同的问题,建议参考 vcpkg 中编译的流程和需要的补丁。

环境

system: macOS 12.4 (21F79)
cpu: Apple M1
Xcode 13.2.1
macOS SDK 12.1

使用vcpkg编译安装arm64版

指令:vcpkg install qt5:arm64-osx, 本次安装的版本为 5.15.4

  • icu
    目前macOS使用vcpkg安装icu有些问题,没有自动依赖或指出需要的环境,需要手动安装autoconf,automake,autoconf-archive
  • XXX-NOTFOUND
    编译时qmake command还出现了XXX-NOTFOUND的字段,实质是依赖某个库但是没有正确找到。本次编译过程中出现了 ZSTD-XXX-NOTFOUNDLZMA_DEBUG-NOTFOUND, vcpkg 中重新安装zstdliblzma后解决了问题,推测这两个包应该是被文件加密系统影响没有正确的安装。
  • OSX_DEPLOYMENT_TARGET
    使用默认arm64 triplet(arm64-osx)时,系统最小版本会设置为当前macOS SDK的版本(本次编译为12.1),而qt5.15.4 的最高设置的版本为 10.15, vcpkg 会自动设置为10.15(实际编译提示的好像是11.0), 而 M1 芯片支持的最低操作系统也是11.0 big sur。
    若直接指定为10.13,则有一个依赖库pcre2会报错 "Must target Big Sur or newer"
  • Cleaning before build xxx
    这一步非常慢,但是也不占用cpu、内存、硬盘,需要耐性等待,每次至少30分钟,耐心等待。

自行编译

源代码为Qt在线安装器中的5.15.2
尝试使用以下指令

1
2
3
./configure -prefix /Users/Shared/Qt/5.15.2/macos -release -opensource -nomake tests -confirm-license QMAKE_APPLE_DEVICE_ARCHS=arm64 QMAKE_MACOSX_DEPLOYMENT_TARGET=10.13
make -j16
make install

其中QMAKE_APPLE_DEVICE_ARCHS=arm64可以指定编译的架构,而QMAKE_MACOSX_DEPLOYMENT_TARGET=10.13则好像无效,观测到vcpkg中是使用设定 macOS SDK 的版本来指定的
特别注明 configure 重新配置环境时,需要删除全部缓存才能成功,最保险的办法是配置前保存一份源码的压缩包,每次重新配置都删除旧的文件夹再解压
编译失败,代码错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -O2 -std=c++1z  -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -mmacosx-version-min=10.13 -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -Wall -Wextra -Winconsistent-missing-override -Wobjc-interface-ivars -Wobjc-method-access -Wobjc-multiple-method-names -Werror=unguarded-availability -Werror=unguarded-availability-new -Werror=unsupported-availability-guard -fPIC -DQT_NO_LINKED_LIST -DQT_NO_JAVA_STYLE_ITERATORS -DQT_NO_FOREACH -DQT_DEPRECATED_WARNINGS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DGL_SILENCE_DEPRECATION -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_ACCESSIBILITY_SUPPORT_LIB -DQT_THEME_SUPPORT_LIB -DQT_FONTDATABASE_SUPPORT_LIB -DQT_GRAPHICS_SUPPORT_LIB -DQT_CLIPBOARD_SUPPORT_LIB -DQT_PRINTSUPPORT_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I../../../../include -I../../../../include/QtAccessibilitySupport -I../../../../include/QtAccessibilitySupport/5.15.2 -I../../../../include/QtAccessibilitySupport/5.15.2/QtAccessibilitySupport -I../../../../include/QtThemeSupport -I../../../../include/QtThemeSupport/5.15.2 -I../../../../include/QtThemeSupport/5.15.2/QtThemeSupport -I../../../../include/QtFontDatabaseSupport -I../../../../include/QtFontDatabaseSupport/5.15.2 -I../../../../include/QtFontDatabaseSupport/5.15.2/QtFontDatabaseSupport -I../../../../include/QtGraphicsSupport -I../../../../include/QtGraphicsSupport/5.15.2 -I../../../../include/QtGraphicsSupport/5.15.2/QtGraphicsSupport -I../../../../include/QtPrintSupport/5.15.2 -I../../../../include/QtPrintSupport/5.15.2/QtPrintSupport -I../../../../include/QtWidgets/5.15.2 -I../../../../include/QtWidgets/5.15.2/QtWidgets -I../../../../include/QtGui/5.15.2 -I../../../../include/QtGui/5.15.2/QtGui -I../../../../include/QtClipboardSupport -I../../../../include/QtClipboardSupport/5.15.2 -I../../../../include/QtClipboardSupport/5.15.2/QtClipboardSupport -I../../../../include/QtPrintSupport -I../../../../include/QtWidgets -I../../../../include/QtGui -I../../../../include/QtCore/5.15.2 -I../../../../include/QtCore/5.15.2/QtCore -I../../../../include/QtCore -I.moc -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/AGL.framework/Headers -I../../../../mkspecs/macx-clang -F/Users/Mink/Qt/5.15.2/Src/qtbase/lib -o .obj/qprintengine_mac.o qprintengine_mac.mm
In file included from qiosurfacegraphicsbuffer.mm:40:
./qiosurfacegraphicsbuffer.h:54:32: error: unknown type name 'CGColorSpaceRef'; did you mean 'QColorSpace'?
void setColorSpace(QCFType<CGColorSpaceRef> colorSpace);
^~~~~~~~~~~~~~~
QColorSpace
../../../../include/QtCore/../../src/corelib/kernel/qmetatype.h:2090:1: note: 'QColorSpace' declared here
QT_FOR_EACH_STATIC_GUI_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
^
../../../../include/QtCore/../../src/corelib/kernel/qmetatype.h:178:24: note: expanded from macro 'QT_FOR_EACH_STATIC_GUI_CLASS'
F(QColorSpace, 87, QColorSpace) \
^
qiosurfacegraphicsbuffer.mm:90:32: error: out-of-line definition of 'setColorSpace' does not match any declaration in 'QIOSurfaceGraphicsBuffer'
void QIOSurfaceGraphicsBuffer::setColorSpace(QCFType<CGColorSpaceRef> colorSpace)
^~~~~~~~~~~~~
./qiosurfacegraphicsbuffer.h:54:24: note: type of 1st parameter of member declaration does not match definition ('QCFType<QColorSpace>' vs 'QCFType<CGColorSpaceRef>')
void setColorSpace(QCFType<CGColorSpaceRef> colorSpace);

更具 vcpkg 中的补丁,该错误需要增加补丁

cocoa.patch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
diff --git a/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h b/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
index e070ba97..07c75b04 100644
--- a/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
+++ b/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
@@ -40,6 +40,7 @@
diff --git a/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h b/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
index e070ba97..07c75b04 100644
--- a/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
+++ b/src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h
@@ -40,6 +40,7 @@
#ifndef QIOSURFACEGRAPHICSBUFFER_H
#define QIOSURFACEGRAPHICSBUFFER_H

+#include <CoreGraphics/CGColorSpace.h>
#include <qpa/qplatformgraphicsbuffer.h>
#include <private/qcore_mac_p.h>


但是还有不少地方需要打补丁,建议使用或参照 vcpkg 中的流程