初探Swift源代码之旅
2023-12-19 17:43:12
我们都听说过一句话:
学习框架最好的方式是学习它的源码。
对于 Objective-C Runtime 源码,虽然代码不少但起码可以找到下手的地方。可面对 Swift 源码的时候,第一感觉就是这工程也忒大了,一时间竟无从下手。
按照 startup 文档的指示,拉下来单单是相关工程就有十几个,看着就挺吓人的。
我们可以先试试看,看看能不能找到一些有规律的。先不用下载 framework 源码,看看编译器源码情况如何。
cd /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/
我们进入编译器工具链的 Swift 源码目录。
tree .
我们可以通过 tree 命令递归地输出当前目录的目录结构。
我们可以看到:
├── ancillary
│ ├── TypeMetadata.swift
│ └── __TypeMetadata.swift
├── libdispatch
│ ├── Semaphore.h
│ ├── atomic.h
│ └── semaphore.h
├── libexecinfo
│ ├── backtrace.h
│ ├── backtrace_symbols.h
│ ├── dwarf.h
│ ├── mach-o.h
│ └── unistd.h
├── libicu
├── libicucore
│ └── bidi.h
├── libidn2
│ ├── idn2.h
│ └── punycode.h
├── libicuuc
│ └── ubidi.h
├── libiconv
│ ├── iconv.h
│ ├── libiconv.dylib
│ └── libiconv.so
├── libswiftCore.dylib
├── libswiftDispatch.dylib
├── libswiftFoundation.dylib
├── libswiftGlibc.dylib
├── libswiftIntel.dylib
├── libswiftDarwin.dylib
├── libswiftObjectiveC.dylib
├── libswiftMetal.dylib
├── libswiftOpenSSL.dylib
├── libswiftRemoteMirror.dylib
├── libswiftShims.dylib
├── libswiftSockets.dylib
├── libswiftXPC.dylib
├── swift
│ ├── linux
│ ├── tools
│ │ ├── build
│ │ ├── install
│ │ ├── run
│ │ └── version
│ ├── tools-symlinks
│ │ ├── fatal-error-handler-setup.sh
│ │ ├── frontend-main.sh
│ │ ├── frontend-server
│ │ ├── include-dirs.sh
│ │ ├── libedit.sh
│ │ ├── module-map.sh
│ │ ├── rustup.sh
│ │ ├── runtime-elf-symbols.sh
│ │ ├── swift-gen
│ │ ├── swift-gen-top
│ │ ├── swift-indent
│ │ ├── swift-parse-as-json
│ │ ├── swift-resolve
│ │ └── swiftc
│ ├── xcodebuild
│ │ ├── host
│ │ └── platform
│ └── xcodebuild-resources
├── XCTest
│ ├── Package.swift
│ ├── Sources
│ ├── Templates
│ ├── Tests
│ └── XcodeTest.xcodeproj
└── xpc
└── xpc.h
这些目录中,只有 libdispatch、libexecinfo、libiconv、swift 目录下有 .swift
文件。
ls -l swift
我们看看 swift 目录下的内容:
total 704
drwxr-xr-x 5 root wheel 170 Oct 24 03:43 linux
drwxr-xr-x 5 root wheel 170 Oct 24 03:43 tools
drwxr-xr-x 3 root wheel 90 Oct 24 03:43 tools-symlinks
-rw-r--r-- 1 root wheel 2764 Oct 24 03:43 xcodebuild
-rw-r--r-- 1 root wheel 933 Oct 24 03:43 xcodebuild-resources
可见除了 linux、tools、tools-symlinks、xcodebuild、xcodebuild-resources 这几个目录外,swift 目录下并没有其他的东西了。
ls -l tools
我们接着看看 tools 目录下的内容:
total 38
drwxr-xr-x 3 root wheel 90 Oct 24 03:43 build
-rwxr-xr-x 1 root wheel 166 Oct 24 03:43 install
-rwxr-xr-x 1 root wheel 30 Oct 24 03:43 run
-rwxr-xr-x 1 root wheel 24 Oct 24 03:43 version
明显只有一个版本号文件。
ls -l tools-symlinks
再看看 tools-symlinks 目录下的内容:
total 28
lrwxr-xr-x 1 root wheel 22 Oct 24 03:43 fatal-error-handler-setup.sh -> ../libdispatch/fatal-error-handler-setup.sh
lrwxr-xr-x 1 root wheel 16 Oct 24 03:43 frontend-main.sh -> ../libdispatch/frontend-main.sh
lrwxr-xr-x 1 root wheel 16 Oct 24 03:43 frontend-server -> ../libdispatch/frontend-server
lrwxr-xr-x 1 root wheel 13 Oct 24 03:43 include-dirs.sh -> ../libdispatch/include-dirs.sh
lrwxr-xr-x 1 root wheel 10 Oct 24 03:43 libedit.sh -> ../libdispatch/libedit.sh
lrwxr-xr-x 1 root wheel 16 Oct 24 03:43 module-map.sh -> ../libdispatch/module-map.sh
lrwxr-xr-x 1 root wheel 10 Oct 24 03:43 rustup.sh -> ../libdispatch/rustup.sh
lrwxr-xr-x 1 root wheel 27 Oct 24 03:43 runtime-elf-symbols.sh -> ../libdispatch/runtime-elf-symbols.sh
lrwxr-xr-x 1 root wheel 10 Oct 24 03:43 swift-gen -> ../libdispatch/swift-gen
lrwxr-xr-x 1 root wheel 10 Oct 24 03:43 swift-gen-top -> ../libdispatch/swift-gen-top
lrwxr-xr-x 1 root wheel 13 Oct 24 03:43 swift-indent -> ../libdispatch/swift-indent
lrwxr-xr-x 1 root wheel 20 Oct 24 03:43 swift-parse-as-json -> ../libdispatch/swift-parse-as-json
lrwxr-xr-x 1 root wheel 15 Oct 24 03:43 swift-resolve -> ../libdispatch/swift-resolve
lrwxr-xr-x 1 root wheel 7 Oct 24 03:43 swiftc -> ../libdispatch/swiftc
这里是一些符号链接文件。
ls -l xcodebuild
接着看看 xcodebuild 目录下的内容:
total 8
drwxr-xr-x 3 root wheel 90 Oct 24 03:43 host
drwxr-xr-x 3 root wheel 90 Oct 24 03:43 platform
这里有两个子目录。
ls -l xcodebuild-resources
最后,看看 xcodebuild-resources 目录下的内容:
total 0
这里什么也没有。
可见,这些目录结构中,并没有可以作为阅读源代码的起始位置。