美文网首页
Swift源码编译环境配置

Swift源码编译环境配置

作者: 分贝丶 | 来源:发表于2020-12-13 21:53 被阅读0次

    编译环境

    • MacOS Catalina 10.15.6
    • Xcode 12.2
    • Python 2.x
    • 安装homebrew,已经安装可以忽略
    /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
    

    安装完成后 brew list 查看


    image.png

    编译步骤

    第一步:clone swift源码(792.9M)
    image.png
    • 这里编译的是swift-5.3.1-Release。可以自行在官网找到对应的分支,需要注意的是:swift源码版本需要与Xcode版本匹配(官方编译文档有说明
    • 在自己喜欢的位置创建swift-source文件夹,在这个文件夹目录执行了下面的git clone命令
    git clone --branch swift-5.3.1-RELEASE https://github.com/apple/swift.git
    
    第二步:update-checkout

    确保你当前的目录是在上一步swift源码路
    这步主要是clone编译swift相关的库,否则在编译时一定会失败,这步很关键!(llvm-project比较大2.41G,其他库很好不是很大,所以下载的时候需要耐心等待)

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

    这里编译需要注意一点:文件的路径尽量避免出现中文,中文有很大的概率会报错

    第三步:采用ninja编译
    • 编译过程可以使用ninja,也可以使用Xcode,但是Xcode编译之后的支持性不是太好,所以这里采用ninja编译 (编译过程会很长,建议休息时间编译)
    ./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;
          ~~^
    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
    
    • 解决方案:
      看报错信息应该是和CommandLine有关,直接删除Developer下的CommandLineTools,使用Xcode中的。
    sudo rm -rf /Library/Developer/CommandLineTools
    sudo xcode-select -s /Applications/Xcode.app
    

    执行完上述命令后重新编译

    • 编译完成后


      image.png
    第四步:使用VSCode调试Swift
    • 首先,在VSCode中安装一个插件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}"
            }
        ]
    }
    
    • 点击Debug运行,断住
      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
    
    
    • 然后过掉断点,可以看到swift成功运行

      image.png
    • 这个时候我们可以在forEach中打个断点


      image.png
    • 写Swift代码

    [1,2,3,4].forEach { print($0) }
    
    image.png

    可以看到本地变量没有任何显示(原因是CodeLLDB这个插件的作者没有支持swift语言),当然并不妨碍调试源代码。到这里就可以😸调试源码!
    需要显示本地变量的朋友可以继续往下看。☞

    本地变量显示支持Swift

    分析:LLDBCode本质上也是使用bin目录下的LLDB来调试我们的源码。

    • 第一步:先找到VSCode插件的安装目录,一般情况下在用户目录下:
    /Users/***/.vscode/extensions/vadimcn.vscode-lldb-1.6.0/lldb/bin
    
    image.png
    • 第二步:找到ninja编译好lldb-macosx-x86_64目录下的bin文件夹中的文件全部复制到VSCode lldb/bin目录下。(这里最好先将VSCode lldb/bin目录下的内容备份,防止出现错误)这步的操作是要让CodeLLDB使用我们编译好的Swift中的LLDB来调试代码。
    /Users/***/***/swift-source/build/Ninja-RelWithDebInfoAssert+stdlib-DebugAssert/lldb-macosx-x86_64/bin
    
    image.png
    • 第三步: 将Swift编译后lldb/bin下的lldb文件复制到VSCode lldb/lib目录下,删除本来就存在的liblldb.dylib文件,并将lldb文件改名为liblldb.dylib。(记得备份)。
    image.png

    第四步:重启VSCode再次Debug下面看到变量了。

    image.png

    编译总结:

    • 下载源码编译后文件共49.82 GB,建议用移动硬盘保存
    • 编译的第三步ninja编译,比较容易出错,一般跟CommandLine有关,直接删除Developer下的CommandLineTools,使用Xcode中的。
    • CodeLLDB不支持本地变量显示,需要手动配置

    相关文章

      网友评论

          本文标题:Swift源码编译环境配置

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