Swift source code compilation environment construction and compilation process

Compiler Environment
  • Version preparation: macOS 10.15.3 Xcode 11.5
  • brew install cmake njnia
  • Python 2.X
  • Swift source address: Apple/Swift
Compilation process
① clone source code
  • clone command:
	git clone --branch swift-5.2.4-RELEASE https://github.com/apple/swift.git
  • Here I compile swift-5.2.4-RELEASE, because this is the version I use when compiling the source code.
  • If you need to compile the updated source code, you can find the corresponding version on the official website: Apple/Swift . At the same time, pay attention to the corresponding version of Xcode to match (there will be specific instructions when the official document is compiled).
  • clone is done as follows:
	Cloning into 'swift'...
	
	remote: Enumerating objects: 1, done.
	remote: Counting objects: 100% (1/1), done.
	remote: Total 1170695 (delta 0), reused 0 (delta 0), pack-reused 1170694
	Receiving objects: 100% (1170695/1170695), 614.74 MiB | 9.37 MiB/s, done.
	Resolving deltas: 100% (953771/953771), done.
	Note: switching to '0bab712aea8f0eb74f3acc303d96857f697a98d8'.
② update-checkout
  • Ensure that the current directory is under swift-source, and then execute the following command:
	./swift/utils/update-checkout --tag swift-5.2.4-RELEASE --clone
  • The results are as follows:
	Skipping cmake on Darwin
	Skipping icu on Darwin
	Skipping clone of 'sourcekit-lsp', directory already exists
	Skipping clone of 'swift-syntax', directory already exists
	Skipping clone of 'cmake', requested by user
	Skipping clone of 'swift-xcode-playground-support', directory already exists
	Skipping clone of 'swift-format', directory already exists
	Skipping clone of 'indexstore-db', directory already exists
	Skipping clone of 'swift-stress-tester', directory already exists
	Skipping clone of 'llvm-project', directory already exists
	Skipping clone of 'llbuild', directory already exists
	Skipping clone of 'cmark', directory already exists
	Skipping clone of 'swift-corelibs-foundation', directory already exists
	Skipping clone of 'swift-tools-support-core', directory already exists
	Skipping clone of 'swift-corelibs-xctest', directory already exists
	Skipping clone of 'ninja', directory already exists
	Skipping clone of 'swift-integration-tests', directory already exists
	Skipping clone of 'swiftpm', directory already exists
	Skipping clone of 'swift', directory already exists
	Skipping clone of 'swift-corelibs-libdispatch', directory already exists
	Skipping clone of 'icu', requested by user
  • This step is particularly important, because update-checkout will clone and compile swift-related libraries, otherwise the process of compiling swift will fail later.
③ Compile
  • During the compilation process, either njnia or Xcode can be used to compile. In the actual compilation and testing process, the support after Xcode compilation is not particularly good, it is recommended to use njnia to compile.
  • Compile using the script in the swift source code:
	./swift/utils/build-script -x -R --debug-swift
  • Or execute the following script to view the command:
	./swift/utils/build-script -r --debug-swift-stdlib --lldb
  • Compilation is as follows:

Insert picture description here

④ Debug Swift
  • To open a Swift project in Xcode, open /swift-source/build/Xcode-ReleaseAssert+swift-DebugAssert/swift-macosx-x86_64/Swift.xcodeproj.
  • It will automatically create many scenarios for all available targets. The common debugging process will involve:
    • Choose swift scheme.
    • Bring up the scheme editor (⌘⇧<).
    • Select the Arguments tab and click +.
    • Add a command line option, which is set according to your own needs. If there is no special need, compile normally.
    • Close the scheme editor.
    • Compile and run.
⑤ Use VSCode to debug Swift
  • Open VSCode to install the CodeLLDB plug-in, as shown below:

Insert picture description here

  • Configure the JSON file as follows:

Insert picture description here

  • The configuration content is as follows:
	"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}"
        }
    ]
  • Note: The above program file path needs to be the same as the file path you compiled.
  • After Run:

Insert picture description here

  • Pass the breakpoint:

Insert picture description here

  • Examples are as follows:

Insert picture description here

  • When debugging .swift files, the above 3 situation may not appear. The solution is as follows:
  • First find the installation path of LLDB, as follows:

Insert picture description here

  • Then find the file path of the compiled LLDB, and copy all the files inside to the above directory:

Insert picture description here

  • At the same time modify the dylib file under the lib file of CodeLLDB:

Insert picture description here

  • Switch to the terminal, and then you can debug the Swift source code!
  • Enter the following code in the terminal (you can also copy from the swift file):

Insert picture description here

  • Search for *_swift_allocObject in the source code and add a breakpoint:

Insert picture description here

  • Continue to enter var t = YDWTeacher() in the terminal, and then press Enter:

Insert picture description here

  • So we can debug the Swift source code!

Guess you like

Origin blog.csdn.net/Forever_wj/article/details/110559621