原:嘗試在CentOS7.2上編譯安裝Swift4.0python
蘋果提供 Ubuntu上構建Swift 的教程,經過這個教程我嘗試使用CentOS7.2
上玩兒一把。目前已經成功在CentOS7.2上面成功安裝 swift 4.0
linux
swift --version Swift version 4.0-dev Target: x86_64-unknown-linux-gnu
yum install -y git clang gcc-c++ uuid-devel libicu-devel icu libedit-devel libxml2-devel sqlite-devel swig python-devel ncurses-libs ncurses-devel pkgconfig libuuid-devel epel-release libbsd-devel
更新CMakec++
對 CMake 3.4.3
版本有要求,必須比 CMake 3.4.3
高。CMake官網git
# 下載 wget https://cmake.org/files/v3.9/cmake-3.9.0.tar.gz # 解壓 tar -zxvf cmake-3.9.0.tar.gz cd cmake-3.9.0 ./bootstrap --prefix=/usr make sudo make install
打開 https://swift.org/source-code... 找到 Cloned Repositories
建立 mkdir -p /usr/local/swift && cd /usr/local/swift
目錄並進入swift
github
# 下載 swift-llvm wget https://github.com/apple/swift-llvm/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P llvm tar -zxvf swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -C src/llvm # 下載 swift-clang wget https://github.com/apple/swift-clang/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P clang # 下載 swift-lldb wget https://github.com/apple/swift-lldb/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P lldb # 下載 swift-cmark wget https://github.com/apple/swift-cmark/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P cmark # 下載 swift wget https://github.com/apple/swift/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P swift # 下載 ninja wget https://github.com/ninja-build/ninja/archive/v1.7.2.tar.gz -P ninja
上面採用 開發板編譯花了三個小時,後面用 released 版本編譯,警告少了許多,並且編譯快了至關多呢,因此你須要選擇一個穩定的 released 版本安裝。sql
wget https://github.com/ninja-build/ninja/archive/v1.7.2.tar.gz -P ninja wget https://github.com/apple/swift/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P swift wget https://github.com/apple/swift-cmark/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P cmark wget https://github.com/apple/swift-lldb/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P lldb wget https://github.com/apple/swift-clang/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P clang wget https://github.com/apple/swift-llvm/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-29-a.tar.gz -P llvm wget https://github.com/apple/swift-corelibs-libdispatch/archive/swift-DEVELOPMENT-SNAPSHOT-2017-07-30-a.tar.gz -P swift-corelibs-libdispatch
安裝以後的目錄結構bootstrap
swift/ └── src ├── clang ├── cmark ├── lldb ├── llvm ├── ninja ├── swift-corelibs-libdispatch └── swift
yum install re2c -y # re2c 是一個用於編寫快速靈活的詞法分析器的工具
編譯swift
./configure.py --bootstrap cp ninja /usr/local/bin/
在文件 vi /etc/profile
添加下面代碼,添加完成以後,source /etc/profile
更改當即生效。centos
export SWIFT_SOURCE_ROOT=/usr/local/swift/src
進入cd /usr/local/swift/src/swift/utils/
目錄,運行腳本bash
# ./build-script -R # 上面命令會報錯,建議使用下面命令 # https://github.com/apple/swift/pull/3594#issuecomment-234169759 ./build-script --libdispatch ./build-script --libdispatch --extra-cmake-options="-DSWIFT_BUILD_SOURCEKIT:BOOL=TRUE"
swap
區不夠大的緣由。致使clang++ kernel opps
clang: error: unable to execute command: Killed clang: error: linker command failed due to signal (use -v to see invocation)
請加大分區:
sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
開機自動掛載swap:
使用 vi 或 nano 在 /etc/fstab 文件底部添加以下內容:
/swapfile none swap sw 0 0
libatomic不存在
# 錯誤 /usr/bin/ld: cannot find /usr/lib64/libatomic.so.1.0.0
解決辦法,building swift on centos。
# 下載 # 這裏https://pkgs.org/download/libatomic找對應的rpm下載地址 wget --no-check-certificate -O /etc/yum.repos.d/libatomic-4.8.5-11.el7.x86_64.rpm ftp://ftp.pbone.net/mirror/ftp.centos.org/7.3.1611/os/x86_64/Packages/libatomic-4.8.5-11.el7.x86_64.rpm # 安裝 rpm -ivh libatomic-4.8.5-11.el7.x86_64.rpm
編譯完成,設置環境信息 vi ~/.bash_profile
PATH=$PATH:/usr/local/swift/src/build/Ninja-ReleaseAssert/swift-linux-x86_64/bin
當即生效source ~/.bash_profile
添加 touch ~/HelloWorld.swift
文件,添加下面代碼測試
print("Hello world!")
編譯 swift
文件
swiftc ~/HelloWorld.swift -o ~/Hello
運行 ~/Hello
輸出 Hello world!
大功告成!