“This is the third day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Compile using Xcode

Installing the Compilation tool

First, install Cmake and NIJIA using Homebrew

brew install cmake ninja
Copy the code

My PC local compilation tool environment for

Xcode: 12.3 Swift Version: Apple Swift Version 5.3.2 (Swiftlang-1200.0.45 clang-1200.0.32.28) Target: X86_64-apple-darwin19.6.0 cmake: cmake version 3.18.0 python: Python 2.7.16Copy the code

Pull the source code

Create swift-source in your home directory and pull the source code from Github to match your local Swift version.

Git clone - branch swift - 5.3.2 - RELEASE [email protected]: apple/swift. GitCopy the code

Pull the dependencies needed to compile the source code

. / swift/utils/update - checkout - tag swift - 5.3.2 - RELEASE - cloneCopy the code

This step involves scaling the wall and pulling all compiled dependencies.

compile

./swift/utils/build-script --release-debuginfo --debug-swift-stdlib -x --skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs="x86_64"
Copy the code

Compile using build-script, which is an official compilation script provided by Apple.

  • --release-debuginfo: compiles withRelWithDebInfoEngineering of environment variables, similar toDebugandReleaseMode,RelWithDebInfoPart of it is optimized, but debugging information is retained.
  • --debug-swift-stdlibCompile with debugging informationSwift, the standard libraryIf you want to debugSwift, the compiler, you can use--debug-swift.
  • -x: equivalent to the--xcode, will generate oneSwift.xcodeprojEngineering.
  • --skip-ios: skipios, so you can compile faster, just compilemacosThe program.
  • --swift-darwin-supported-archs="x86_64": only compilex86_64Architecture to speed up compilation.

You can use./swift/utils/build-script –help to see more compilation options.

After compiling, the Xcode-RelWithdebInfoAssert + stdlib-Debugassert folder is generated, as shown in the figure

Source debug Settings

1, in Swift. Xcodeproj project, create Target LYCSwift and rely on ALL_BUILD Target in RelWithDebInfo environment variable

2. Set Enable Handened Runtime in LYCSwifttarget to NO

The source code to debug

In main.swift, add the following code

Set breakpoints in heapObject.cpp

In RelWithDebInfo mode, run LYCSwift so we can debug the source code in Xcode.

The problem

1, but none of these is a common dependency of the other(s). This is not allowed by the Xcode “new build system”.

This is because MY original cmake version was 3.19.2. After 3.19.0, cmake will default to “new Build System “, so I have reverted cmake to 3.18.0. At compile time, using the previous build system, you can download the corresponding version of DMG from the CMake website and install it. Then use sudo “/ Applications/CMake. App/Contents/bin/CMake – GUI” – the install link to/usr/local/bin directory.

2. After modifying the problems of the build system, the following problems occur:

I don’t know why this is happening, but at this point,Swift.xcodeprojProject has been generated, we open the project, manually selectALL_BUILDThe scheme,

Select ReWithDebInfo to continue compiling.

3. When compilingALL_BUILDDuring the project, it reappearedclang: error: no such file or directory: '/Users/mac/swift-source/build/Xcode-ReleaseAssert+stdlib-DebugAssert/cmark-macosx-x86_64/src/RelWithDebInfo/libcmark.a'The problem.

Open cmark-Macosx-x86_64 /cmark. xcodeProj, select CmarkScheme, compile Swift. Xcodeproj.

Clang: Error: No such file or directory: ‘/Users/mac/swift-source/build/Xcode-ReleaseAssert+stdlib-DebugAssert/swift-macosx-x86_64/stdlib/public/runtime/Swift.bu Ild RelWithDebInfo/swiftRuntime – macosx – x86_64. Build/Objects – normal/arm64 AnyHashableSupport. O ‘problem, in this, only x86_64 architectures compile files, Since my computer is Intel chip, I don’t need ARM64. In order to solve the compilation error, JUST copy the x86_64 folder and rename it as arm64.

If you feel that there is a harvest please follow the way to a love three even: 👍 : a praise to encourage. 🌟 : Collect articles, easy to look back! . 💬 : Comment exchange, mutual progress! .

reference

Swift ReadMe

Swift source code debugging operation guide

Debugging Swift Compiler

How to read Swift source code

# Xcode new build system with cmake 3.19 compatibility solution