1、准备工作
XCode 15.3
Apple M1 Pro
macOS Sonoma 14.4
python3 3.9.6
ninja 1.11.1
cmake 3.29.0(较高版本保证编译成功)
sccache 0.7.7
磁盘空间60G
// 通过homebrew安装统一安装环境
brew install cmake ninja sccache
2、项目拉取
需要新建一个文件夹
mkdir ~/Documents/swift-project
cd ~/Documents/swift-project
然后找到你的 Xcode
所支持的 Swift 版别,由于自己的 Xcode 为 15.3 版别,所以直接下载 Swift 5.10 Release
。查找 Xcode 对应的 Swift 的版别有两种方式:
1、去官网,检查 Xcode 的 Release Notes,在 Overview 中会有介绍。例如:Xcode 15.3 Release Notes
2、终端运行指令检查 xcrun swift -version。
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
在新建的目录中履行如下指令拉取对应的 Swift 源码,并 cd
到源码目录:
git clone --branch swift-5.10-RELEASE git@github.com:apple/swift.git
cd swift
拉取源码后还须拉取其他依赖,在防止拉取依赖的过程中产生其他错误,还需要预先执行以下命令
git config --global http.postBuffer 524288000
git config --global http.sslVerify "false"
然后在swift目录下执行如下命令
cd swift
./utils/update-checkout --tag swift-5.10-RELEASE --clone --clone-with-ssh
后面的参数可以查看 update-checkout
目录下的 update_checkout.py
脚本文件,--clone-with-ssh
表示使用git@github.com:
流的形式拉取,而非https
的形式,以上操作需要开启代理
注意:在更新的过程中可能因为某个依赖过于大而更新失败。可以单独更新该依赖项。
例如:icu依赖项比较大,反复更新都不成功。
[icu] + git fetch --recurse-submodules=yes --tags
[icu] + git rebase FETCH_HEAD
[icu] b'git-lfs filter-process: git-lfs: command not found'
[icu] b'fatal: The remote end hung up unexpectedly'
[icu] b'Cannot rebase: You have unstaged changes.'
[icu] b'Please commit or stash them.'
Error on repo "/Users/shiji/work/github/swift-project/icu": Traceback (most recent call last):
File "/Users/shiji/work/github/swift-project/swift/utils/update_checkout/update_checkout/update_checkout.py", line 257, in update_single_repository
shell.run(["git", "rebase", "FETCH_HEAD"],
File "/Users/shiji/work/github/swift-project/swift/utils/swift_build_support/swift_build_support/shell.py", line 257, in run
raise eout
Exception: ['git', 'rebase', 'FETCH_HEAD']
通过查找update_checkout
文件夹下的update-checkout-config.json
文件,找到icu的更新地址单独更新,分支 release-65-1
cd swift-project
git clone git@github.com:unicode-org/icu.git
Cloning into 'icu'...
remote: Enumerating objects: 1301093, done.
remote: Counting objects: 100% (9326/9326), done.
remote: Compressing objects: 100% (3004/3004), done.
remote: Total 1301093 (delta 4674), reused 8591 (delta 3964), pack-reused 1291767
Receiving objects: 100% (1301093/1301093), 1.34 GiB | 3.52 MiB/s, done.
Resolving deltas: 100% (813983/813983), done.
git checkout -b origin/release-65-1
最终通过该方式,依次更新失败的依赖项。
如果上述依然更新不成功,文件删除,按照上述步骤重新执行一次
3、编译
1、前面准备工作做完了之后需求用到官方的脚本,在 utils 目录下的 build-script 脚本来编译我们的Swift项目,在编译前可以通过命令来查看编译选项的含义
cd swift
./utils/build-script -h
或者打开脚本文件 .swift/utils/build_swift/build_swift/driver_arguments.py
,我们使用第1519行给出的命令。
也可以查看命令参数
2、我这里使用如下命令编译
cd swift
./utils/build-script --swift-darwin-supported-archs="$(uname -m)" \
--release-debuginfo --debug-swift-stdlib \
--skip-ios --skip-watchos --skip-tvos \
--skip-early-swiftsyntax --skip-build-benchmarks \
--sccache --xcode
--swift-darwin-supported-archs
:设置构建平台,如果不设置,默认全平台构建
$(uname -m)
:获取当前mac的架构,我的mac为M2的arm64的架构
--release-debuginfo
:构建所有的内容RelWithDebInfo(包含debug和release)带有调试信息
--debug-swift-stdlib
: 编译带有调试信息的 Swift标准库stdlib --skip-ios --skip-watchos --skip-tvos
:跳过iOS、watchos、tvos相关内容
--skip-early-swiftsyntax
: 表示跳过earlyswiftsyntax
, 这个不加会编译出错。
--skip-build-benchmarks
:跳过构建swift基准测试套件
--sccache
:使用缓存工具,当删除构建目录重新构建的时候提高构建速度
--xcode
:使用Xcode方式构建
3、最终编译完成
4、Build
依次编译三个产物,顺序分别为
1、cmark-gfm.xcodeproj
2、LLVM.xcodeproj
3、Swift.xcodeproj
1、cmark-gfm.xcodeproj
Edit Scheme选择如下 RelWithDebInfo
编译通过
2、
LLVM.xcodeproj
,创建new Schema为ALL_BUILD
, Edit Scheme选择如下 RelWithDebInfo
。在编译 过程中会有i386架构的问题,不用理会,虽然工程最终build以fail结束,但是Swift编译所需的arm64已经准备好了。3、
Swift.xcodeproj
创建new Schema为ALL_BUILD
, Edit Scheme选择如下 RelWithDebInfo
编译通过。