
編譯環(huán)境
- MacOS Catalina 10.15.6
- Xcode 12.2
- Python 2.x
- 安裝homebrew,已經(jīng)安裝可以忽略
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
- 安裝
cmake(3.19.1)
brew install cmake
- 安裝
ninja(1.10.2)
brew install ninja
- 安裝
VSCode(1.52.0) VSCode官網(wǎng)下載
安裝完成后 brew list 查看

image.png
編譯步驟
第一步:clone swift源碼(792.9M)

image.png
- 這里編譯的是
swift-5.3.1-Release??梢宰孕性诠倬W(wǎng)找到對(duì)應(yīng)的分支,需要注意的是:swift源碼版本需要與Xcode版本匹配(官方編譯文檔有說(shuō)明) - 在自己喜歡的位置創(chuàng)建
swift-source文件夾,在這個(gè)文件夾目錄執(zhí)行了下面的git clone命令。
git clone --branch swift-5.3.1-RELEASE https://github.com/apple/swift.git
第二步:update-checkout
確保你當(dāng)前的目錄是在上一步swift源碼路
這步主要是clone編譯swift相關(guān)的庫(kù),否則在編譯時(shí)一定會(huì)失敗,這步很關(guān)鍵?。?code>llvm-project比較大2.41G,其他庫(kù)很好不是很大,所以下載的時(shí)候需要耐心等待)
./swift/utils/update-checkout --tag swift-5.3.1-RELEASE --clone
這里編譯需要注意一點(diǎn):文件的路徑盡量避免出現(xiàn)中文,中文有很大的概率會(huì)報(bào)錯(cuò)
第三步:采用ninja編譯
- 編譯過(guò)程可以使用
ninja,也可以使用Xcode,但是Xcode編譯之后的支持性不是太好,所以這里采用ninja編譯(編譯過(guò)程會(huì)很長(zhǎng),建議休息時(shí)間編譯)
./swift/utils/build-script -r --debug-swift-stdlib --lldb
- 編譯錯(cuò)誤處理
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:325:9: error: no member named 'isless' in the global namespace
using ::isless;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:326:9: error: no member named 'islessequal' in the global namespace
using ::islessequal;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:327:9: error: no member named 'islessgreater' in the global namespace
using ::islessgreater;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:328:9: error: no member named 'isunordered' in the global namespace
using ::isunordered;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:329:9: error: no member named 'isunordered' in the global namespace
using ::isunordered;
~~^
13 errors generated.
[5/910][ 0%][9.914s] Building CXX obj...les/swiftImmediate.dir/Immediate.cpp.o
FAILED: lib/Immediate/CMakeFiles/swiftImmediate.dir/Immediate.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -DCMARK_STATIC_DEFINE -DGTEST_HAS_RTTI=0 -DHAVE_LIBEDIT -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Ilib/Immediate -I/Users/mwj/MWJ/Swift/swift-source/swift/lib/Immediate -Iinclude -I/Users/mwj/MWJ/Swift/swift-source/swift/include -I/Users/mwj/MWJ/Swift/swift-source/llvm-project/llvm/include -I/Users/mwj/MWJ/Swift/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/llvm-macosx-x86_64/include -I/Users/mwj/MWJ/Swift/swift-source/llvm-project/clang/include -I/Users/mwj/MWJ/Swift/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/llvm-macosx-x86_64/tools/clang/include -I/Users/mwj/MWJ/Swift/swift-source/cmark/src -I/Users/mwj/MWJ/Swift/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/cmark-macosx-x86_64/src -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -Wno-unknown-warning-option -Werror=unguarded-availability-new -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-class-memaccess -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -Werror=switch -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -DOBJC_OLD_DISPATCH_PROTOTYPES=0 -O2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -UNDEBUG -fno-exceptions -fno-rtti -Werror=gnu -target x86_64-apple-macosx10.9 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -arch x86_64 -F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/../../../Developer/Library/Frameworks -mmacosx-version-min=10.9 -O2 -g -UNDEBUG -DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS -std=c++14 -MD -MT lib/Immediate/CMakeFiles/swiftImmediate.dir/Immediate.cpp.o -MF lib/Immediate/CMakeFiles/swiftImmediate.dir/Immediate.cpp.o.d -o lib/Immediate/CMakeFiles/swiftImmediate.dir/Immediate.cpp.o -c /Users/mwj/MWJ/Swift/swift-source/swift/lib/Immediate/Immediate.cpp
- 解決方案:
看報(bào)錯(cuò)信息應(yīng)該是和CommandLine有關(guān),直接刪除Developer下的CommandLineTools,使用Xcode中的。
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select -s /Applications/Xcode.app
執(zhí)行完上述命令后重新編譯
-
編譯完成后
image.png
第四步:使用VSCode調(diào)試Swift
-
首先,在VSCode中安裝一個(gè)插件
CodeLLDB
image.png -
用
VSCode打開swift-source 文件夾,不能找不Debug。
image.png
- 添加配置文件
launch.json,并修改
image.png
需要注意的是program的路徑需要與你編譯的文件路徑一致
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/swift-macosx-x86_64/bin/swift",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
- 點(diǎn)擊
Debug運(yùn)行,斷住
image.png
Launching: /Users/mwj/MWJ/Swift/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/swift-macosx-x86_64/bin/swift
Launched process 10939
Stop reason: exec
-
然后過(guò)掉斷點(diǎn),可以看到
swift已成功運(yùn)行。
image.png -
這個(gè)時(shí)候我們可以在forEach中打個(gè)斷點(diǎn)
image.png 寫Swift代碼
[1,2,3,4].forEach { print($0) }

image.png
可以看到本地變量沒有任何顯示(原因是CodeLLDB這個(gè)插件的作者沒有支持swift語(yǔ)言),當(dāng)然并不妨礙調(diào)試源代碼。到這里就可以??調(diào)試源碼!
需要顯示本地變量的朋友可以繼續(xù)往下看。?
本地變量顯示支持Swift
分析:LLDBCode本質(zhì)上也是使用bin目錄下的LLDB來(lái)調(diào)試我們的源碼。
- 第一步:先找到
VSCode插件的安裝目錄,一般情況下在用戶目錄下:
/Users/***/.vscode/extensions/vadimcn.vscode-lldb-1.6.0/lldb/bin

image.png
- 第二步:找到
ninja編譯好的lldb-macosx-x86_64目錄下的bin文件夾中的文件全部復(fù)制到VSCode lldb/bin目錄下。(這里最好先將VSCode lldb/bin目錄下的內(nèi)容備份,防止出現(xiàn)錯(cuò)誤)這步的操作是要讓CodeLLDB使用我們編譯好的Swift中的LLDB來(lái)調(diào)試代碼。
/Users/***/***/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/lldb-macosx-x86_64/bin

image.png
- 第三步: 將
Swift編譯后lldb/bin下的lldb文件復(fù)制到VSCode lldb/lib目錄下,刪除本來(lái)就存在的liblldb.dylib文件,并將lldb文件改名為liblldb.dylib。(記得備份)。

image.png
第四步:重啟VSCode再次Debug下面看到變量了。

image.png
,
編譯總結(jié):
- 下載源碼編譯后文件共
49.82 GB,建議用移動(dòng)硬盤保存 - 編譯的第三步
ninja編譯,比較容易出錯(cuò),一般跟CommandLine有關(guān),直接刪除Developer下的CommandLineTools,使用Xcode中的。 -
CodeLLDB不支持本地變量顯示,需要手動(dòng)配置






