Compile environment
- MacOS Big Sur 11.0.1
- Xcode 12.2
- Python 2.x
- brew install cmake ninja
Compilation step
The preparatory work
-
Create a new folder and name it swift-source
-
Because in the process of pulling resources, we need to access the Internet, so we need a ladder (you can solve the problem by yourself)
Step 1: Clone Swift source code
Swift-5.3.1-release is compiled here. You can find the corresponding branch on the official website. It should be noted that the swift source version should match the Xcode version (the official compilation document has instructions).
Git clone - branch swift - 5.3.1 - RELEASE at https://github.com/apple/swift.gitCopy the code
Step 2: update-checkout
This step is mainly to clone the swift related library, otherwise the compile will fail, this step is very important! (The compilation process will take a long time, so it is recommended to compile at a break.)
./swift/utils/build-script -r --debug-swift-stdlib --lldb
Copy the code
Note here: the file path should avoid Chinese, Chinese has a high probability of error
Step 3: Ninja compile
You can either use Ninja or Xcode, but Xcode is not very supportive after compilation, so ninja compilation is used here
./swift/utils/build-script -r --debug-swift-stdlib --lldb
Copy the code
Step 4: debug Swift using VSCode
-
First, install the plug-in in VSCode
-
Add the configuration file launch.json and modify it
Note that the path of **program** needs to be the same as the path of the file you compiled
{" version ":" 0.2.0, "" configurations: [{" type" : "LLDB", "request" : "launch", "name" : "Debug", "the program" : "${workspaceFolder}/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/swift-macosx-x86_64/bin/swift", "args": [], "cwd": "${workspaceFolder}" } ] }Copy the code
- Click on the
**Debug**
Run, break
And then you go over the break point
-
The operation is successful if you see the following results
-
Then switch to terminal, we can start source debugging.
-
Enter the following code in the terminal (you can also copy it from the SWIFT file)
-
-
-
Search the source code for **swift_allocObject** (this will be explained later, only as a debugging example), and add a breakpoint
-
Continue on the terminal type **var t = CJLTeacher()**, press Enter
-
This way, we can have fun debugging Swift source code.
Data collected
Due to the limited space of this article, I can only briefly introduce some current work achievements and thinking, and there are still some new directions for each Swift to explore. If you are interested in the underlying principles of iOS, architecture design, system construction and how to interview, you can also follow me to get the latest information and interview related information in time. If you have any comments and suggestions welcome to give me a message!
Write bad place welcome everybody to point out, hope everybody leave a message to discuss, let us make progress together!
Those who like iOS can follow me and learn and communicate with me!!
included