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
編譯通過。