doridoridoriand’s diary

主に技術的なことを書いていく予定(たぶん)

Ubuntu上に最新のSwiftをソースからコンパイルして使えるようにする

Ubuntu上で行うことを想定。 ひとまず公式サイト https://swift.org/ に何か載っていないか見てみる。

公式サイトで案内されているgithubリポジトリ参照してみると普通にやり方書いてあった。 https://github.com/apple/swift

とりあえず公式が案内している方法に従ってみる。

$ sudo apt-get install git cmake ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev autoconf libtool systemtap-sdt-dev tzdata

$ mkdir swift-source
$ cd swift-source
$ git clone git@github.com:apple/swift.git
$ ./swift/utils/update-checkout --clone-with-ssh
$ sudo apt-get install cmake
$ git clone git@github.com:ninja-build/ninja.git && cd ninja
$ git checkout release
$ cat README
$ cd
$ ./swift-source/swift/utils/build-script -r -t

エラーが出た。

CMake Error at CMakeLists.txt:613 (message):
  Python 2.7 or newer is required

Pythonが古いとのこと。しかし、現在インストールしているPythonは3.6.0。おかしい。 もしかして2系じゃないといけないのかと思い、pyenvで2.7.13をインストールしてみる。

どうやらビンゴ。コンパイルが進み始めた。結構掛かりそう。

。。エラーがまた出た。

[6/29] Building HTML documentation
FAILED: cd /home/dorian/swift-source/swift/docs && /home/dorian/.pyenv/shims/sphinx-build -W -D latex_elements.papersize=letter -d /home/dorian/swift-source/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/doctrees -b html . /home/dorian/swift-source/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/docs/html
pyenv: sphinx-build: command not found

The `sphinx-build' command exists in these Python versions:
  anaconda-2.4.0

[6/29] Compiling /home/dorian/swift-source/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/stdlib/private/SwiftPrivateLibcExtras/linux/x86_64/SwiftPrivateLibcExtras.o
ninja: build stopped: subcommand failed.
./swift-source/swift/utils/build-script: fatal error: command terminated with a non-zero exit status 1, aborting

どうやらpython周りが結構面倒くさそう。私の環境がデフォルトと違うっていうのが大きそうだけれども。 エラー文言を読んでみると sphinx-buildanaconda-2.4.0 にしかないとのこと。 対処療法的だけれども、anaconda-2.4.0に切り替えて、再度ビルドを実行。 進み始めた。

Testing Time: 396.22s
  Expected Passes    : 2891
  Expected Failures  : 88
  Unsupported Tests  : 837
-- check-swift-linux-x86_64 finished --
--- Finished tests for swift ---

インストールが終わった模様。 最後にパスを通す必要がある。

export PATH=/home/自分のユーザー名/swift-source/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin:"${PATH}"

REPLを実行してみる。

$ swift
***  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)
(swift) let greeting = "Hello!"
// greeting : String = "Hello!"
(swift) print(greeting)
Hello!
(swift)

問題なく動いていそう。

インストール時にpythonとの依存関係がちょっと面倒だったけれども、その後は割とすんなりいった印象。 最新を使いたいとかでなけれは実はDockerHubにDockerfileがあったりする。

https://hub.docker.com/_/swift/

いい時代だ。 次回はまたPythonに戻る予定。