1.写在前面
2.编译环境
- macOS Big Sur 11.6.5
- Xcode 13.2.1 (13C100)
- swift 5.5.2
- brew install cmake ninja sccache
3.编译准备工作
- 新建一个文件夹,命名为
swift_source, cd 到swift_source文件夹下 - 由于拉取资源过程中,需要访问外网 自备梯子
4.clone swift 源码
- 查看swift version
swiftc -version
swift源码版本需要与Xcode版本匹配
git clone --branch swift-5.5.2-RELEASE https://github.com/apple/swift.git
5.update-checkout
这步主要是clone编译swift相关的库 ,swift_source 目录下
./swift/utils/update-checkout --tag swift-5.5.2-RELEASE --clone
6. buid Swift (等待 时间很长)
swift_source/swift 目录下执行命令
- 通过 Ninja
utils/build-script --skip-build-benchmarks \
--skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs "$(uname -m)" \
--sccache --release-debuginfo --swift-disable-dead-stripping --test
build 文件夹下 生成了 Ninja-RelWithDebInfoAssert
- 通过xcode
- 终于成功了, 高兴ing
7 .调试swift 源码
要在 Xcode 中打开 Swift 项目,请打开/swift_source/build/Xcode-ReleaseAssert+swift-DebugAssert/swift-macosx-x86_64/Swift.xcodeproj
点赞本文,开始你的swift 底层探索之旅了。
8.1 遇到问题 报错
"error: using unsupported Xcode version:"
"Install one of:"
"Or set 'SKIP_XCODE_VERSION_CHECK=1' in the environment
解决方案
删除验证代码(自己删的,在网上没找到解决方案)swift\utils\build-script
8.2 遇到问题
** BUILD FAILED **
ERROR: command terminated with a non-zero exit status 65, aborting
- 解决方法
换中编译指令
utils/build-script --skip-build-benchmarks \
--skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs "$(uname -m)" \
--sccache --release-debuginfo --swift-disable-dead-stripping --test
- 错误
utils/build-script --skip-build-benchmarks \
--skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs "$(uname -m)" \
--sccache --debug --swift-disable-dead-stripping --test \
--xcode --skip-test-cmark --libswift=off
其它记录
使用生成SIL语言的命令并打开sil文件
- 使用Xcode创建 macOS --Command Line Tool - main.swift、书写
swiftc -emit-sil main.swift >> main.sil
- 查看汇编代码:
swiftc -emit-assembly main.swift
参考
blog.csdn.net/SharkToping… www.jianshu.com/p/59e9d754d… bbs.huaweicloud.com/blogs/33143… juejin.cn/post/684790… swift.gg/2016/12/30/… github.com/apple/swift… juejin.cn/post/706407… blog.csdn.net/henry_lei/a…