美文网首页
Swift源码编译

Swift源码编译

作者: HotPotCat | 来源:发表于2020-12-02 01:43 被阅读0次

准备工作

  • 磁盘空间60G

编译环境

  • Xcode 12.2(12B45b)
  • Visual studio Code( 1.51.1)
  • Python 3.9.0
  • macOS Big Sur 11.0.1
  • brew install cmake //3.19.1
  • brew install ninja //1.10.2
# build tools
brew install cmake ninja

安装完成后 brew list 查看


image.png

Xcode,swift,macOS最好用对应的版本。

编译步骤

一:clone swift 源码

我这里是clone的最新的5.3.1版本源码地址

mkdir swift-source
cd swift-source
git clone --branch  swift-5.3.1-RELEASE https://github.com/apple/swift.git

如果clone缓慢的话换成:参考来源

git clone --branch  swift-5.3.1-RELEASE https://github.com.cnpmjs.org/apple/swift.git

5.3.1版本克隆下来大概700M的样子。

二:update-checkout

在swift-source目录下操作:

./swift/utils/update-checkout --tag swift-5.3.1-RELEASE --clone

这一步的目的是拉取我们在编译源码过程中所需要的其它分支文件,如果不拉取后续第三步编译swift源码的时候一定会失败。

错误处理

fatal: unable to access 'https://github.com/apple/swift-syntax.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

fatal: unable to access 'https://github.com/apple/swift-package-manager.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/jpsim/Yams.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/apple/swift-integration-tests.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

fatal: unable to access 'https://github.com/apple/swift-driver.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/apple/sourcekit-lsp.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

fatal: unable to access 'https://github.com/apple/indexstore-db.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/apple/llvm-project.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

fatal: unable to access 'https://github.com/apple/swift-xcode-playground-support.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

fatal: unable to access 'https://github.com/ninja-build/ninja.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/apple/swift-corelibs-xctest.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

fatal: unable to access 'https://github.com.cnpmjs.org/apple/swift.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/apple/swift-corelibs-foundation.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
fatal: unable to access 'https://github.com/apple/swift-llbuild.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 60
ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

ERROR: command terminated with a non-zero exit status 128, aborting

解决

➜  ~ git config --global http.postBuffer 524288000
➜  ~ git config --global http.sslVerify "false"

安装完了大概这样


image.png

文件大小大概3.4G左右。

三:编译

编译的过程中可以使用ninja,也可以使用Xcode。由于Xcode需要和特定版本绑定,并且Xcode编译之后的支持性不是特别好。推荐使用njnja来作为编译工具。
命令如下:

./swift/utils/build-script -r --debug-swift-stdlib --lldb

我编译的时候主要是遇到如下错误:

/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;
/Users/zaizai/improve/swift-source/llvm-project/llvm/include/llvm/Analysis/ScalarEvolution.h:1696:22: warning: '\c' command does not have a valid word argument [-Wdocumentation]
  /// by a call to \c @llvm.experimental.guard in \p BB.
                   ~~^
1 warning and 13 errors generated.
[42/1341][  3%][56.500s] Building CXX ...MakeFiles/swiftIRGen.dir/GenCast.cpp.o
ninja: build stopped: subcommand failed.
ERROR: command terminated with a non-zero exit status 1, aborting

解决方案:
看报错信息应该是和CommandLine有关,直接删除Developer下的CommandLineTools,使用Xcode中的。

  1. sudo rm -rf /Library/Developer/CommandLineTools
  2. sudo xcode-select -s /Applications/Xcode.app
    执行完上述命令后重新编译
    编译完成后


    image.png

    debug模式和release模式下大小差很多,debug模式下编译完文件大小大概50G左右。

四:配置VSCode调试Swift

安装CodeLLDB插件

image.png

配置json文件

选择的时候选择创建lldb json文件,目的是去到我们编译好的文件目录加载swift可执行文件。


image.png

文件路径为编译文件swift路径

/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/swift-macosx-x86_64/bin/swift

launch json文件如下:

{
  "version": "0.2.0",
  "configurations": [

    {
      "type": "lldb",
      "request": "launch",
      "name": "Debug",
      //此处路径为swift编译文件的路径
      "program": "${workspaceFolder}//build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/swift-macosx-x86_64/bin/swift",
      "args": [],
      "cwd": "${workspaceFolder}"
    }
  ]
}

接下来我们直接run起来后断点内容如下:

Launching: /Users/***/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/swift-macosx-x86_64/bin/swift
Launched process 89514
Stop reason: exec
image.png

过掉断点查看终端
这个时候就证明已经正常跑起来了。

***  You are running Swift's integrated REPL,  ***
***  intended for compiler and stdlib          ***
***  development and testing purposes only.    ***
***  The full REPL is built as part of LLDB.   ***
***  Type ':help' for assistance.              ***
(swift) 
image.png
这个时候我们可以写swift代码,在forEach中打个断点
image.png
可以看到本地变量没有任何显示(原因是CodeLLDB这个插件的作者没有支持swift语言),当然并不妨碍调试源代码。

本地变量显示支持swift

lldbcode本质上也是使用bin目录下的lldb来调试我们的源码。
1.复制编译的lldb目录下的bin文件到vscode目录
这一步的操作是要让codelldb使用我们编译好的swift中的lldb来调试代码。先找到VSCode插件的安装目录,一般情况下在用户目录下:

/Users/***/.vscode/extensions/vadimcn.vscode-lldb-1.6.0/lldb/lib
image.png

我们找到lldb目录下的bin文件夹,把编译过后的swift代码中lldb下的bin文件夹内容全部复制到vscode lldb/bin目录下。(这里最好先将vscode lldb/bin目录下的内容备份,防止出现错误)

/Users/***/***/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/lldb-macosx-x86_64/bin
image.png

2.复制编译好的lldb文件到vscode lib目录下
lldb会直接用到自身lib目录下liblldb.dylib动态库来调试,liblldb.dylib本身包含了LLDB.framework文件。这里替换改名的目的是为了让vscode去找bin中我们编译之后的LLDB.framework。(当然如果替换后也不显示,可以拷贝别人相同版本的LLDB.framework到对应目录使用)
swift编译后lldb/bin下的lldb文件复制到vscode lldb/lib目录下,删除本来就存在的liblldb.dylib文件,并将lldb文件改名为liblldb.dylib。(记得备份)。

image.png

重启vscode再次尝试应该就能在debug下面看到变量了。

image.png

终端输入swift代码调试

当然我们也可以直接在终端中输入swift代码

class HotpotCat {
    var name : String = "hotpot"
}
image.png
定义一个实例变量,var hotpot = HotpotCat()这里我们先不要回车,先找到私有方法_swift_allocObject_打个断点
image.png
接着回车,可以在debug下面看到变量的情况,这里我们可以看到p这个变量占了32个字节。
image.png
至此,我们就已经编译和配置好swift源码环境了。

编译总结:

一般到第二步都没问题,第三步遇见错误大概率是环境的问题,不行就

  • 1.删除/usr/local/include 文件夹
  • 2.重装Homebrew(慎重,之前通过brew装的都会没)
  • 3.重装cmake
  • 4.重装ninja
  • 5.配置CommandLine
    sudo rm -rf /Library/Developer/CommandLineTools
    sudo xcode-select -s /Applications/Xcode.app

然后重新编译。我大概是这么折腾好第三步的,最好别用公司电脑折腾,别导致工作环境出现问题。仅供参考。
当然我们也可以直接下载使用别人编译好的文件,只不过需要解压目录和别人编译的路径完全一样(编译源码的过程中文件路径已经写死)。

参考

Xcode releases:这个网站汇总了Xcode版本信息,比官网清晰。下载资源使用官方渠道。
Swift version:这个网站汇总了Swift版本和Xcode版本相关的信息。
https://stackoverflow.com/questions/59071881/problems-with-c-and-gems-on-osx-catalina

相关文章

  • Swift 进阶之路 文章汇总

    Swift-进阶 01:Swift源码编译[https://www.jianshu.com/p/284406133...

  • Swift 进阶汇总

    Swift-进阶 01:Swift源码编译[https://www.jianshu.com/writer#/not...

  • swift编译浅析

    1. 编译器介绍 swift的编译器是swiftc 1.1 编译历程 SwiftCode源码->通过词法分析、语法...

  • Swift源码编译

    准备工作 磁盘空间60G 编译环境 Xcode 12.2(12B45b) Visual studio Code( ...

  • Swift源码编译

    编译环境 xcode :Mac系统自带 Python :Mac系统自带 cmake:通过brew list命令查看...

  • Swift源码编译

    编译环境 macOS Big Sur 11.0.1 Xcode 12.2 Python 2.x brew inst...

  • Swift源码编译

    本文转载自逻辑iOS技术号[https://mp.weixin.qq.com/s/WjY4z4I_FFQlB1gE...

  • swift 源码编译

    编译环境 MacOS Big Sur 11.0.1Xcode 12.4Python 2.xbrew install...

  • Swift源码编译

    Swift源码编译,小坑比较多,历时2周(乐色)。 一、编译环境 官方文档:https://github.com/...

  • Swift-文章汇总

    Swift-01:Swift源码编译[https://www.jianshu.com/p/55ad02c72e5f...

网友评论

      本文标题:Swift源码编译

      本文链接:https://www.haomeiwen.com/subject/zlmewktx.html