Swift on Windows10
どうもこんにちは.id:naosuke2dxです.
今年の3月に行われたMicrosoft Build2016において,「WindowsでBashが動く!」という驚きの発表がありました*1.
その後,いろいろな解説記事が出て,どうやらWindowsの上にUbuntuが載って,その上でBashを走らせるというアーキテクチャになっていることがわかりました*2.
ここで,賢明な皆さんはお気づきかと思います.
「あれ,じゃあSwiftがWindowsでビルドできるのでは…?」と.
本記事では,WindowsでSwiftを動かす方法と,実際にビルドをして動くSwiftをお見せしたいと思います.
環境の確認
まずは環境の確認です.
今回は以下のバージョンで行います.
Windows10のBuild1511なので,bashが動くようになったバージョンだと思います.
C:\> bash root@OHANA1:~# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS" root@OHANA1:~# uname -a Linux OHANA1 3.4.0+ #1 PREEMPT Thu Aug 1 17:06:05 CST 2013 x86_64 x86_64 x86_64 GNU/Linux
Swiftの導入
まずは,公式ページからSwiftのバイナリをダウンロードしてきます.
この記事の執筆当時の最新バージョンは,2.1.1でした.
ダウンロード後は,展開して,swift-2.x.x-RELEASE-ubuntu14.04/usr/bin/
へ移動します.
root@OHANA1:~# wget https://swift.org/builds/swift-2.2.1-release/ubuntu1404/swift-2.2.1-RELEASE/swift-2.2.1-RELEASE- root@OHANA1:~# tar xf swift-2.2.1-RELEASE-ubuntu14.04.tar.gz root@OHANA1:~# cd swift-2.2.1-RELEASE-ubuntu14.04 root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04# ls usr root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04# cd usr/ root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04/usr# ls bin include lib share root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04/usr# cd bin/
ls
するとこんな感じです.
root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04/usr/bin# ls
lldb lldb-mi-3.8.0 swift swift-demangle
lldb-3.8.0 lldb-server swift-autolink-extract
lldb-argdumper lldb-server-3.8.0 swiftc
lldb-mi repl_swift swift-compress
では,早速動かしてみましょう.
root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04/usr/bin# ./swift error: failed to launch REPL process: process launch failed: 'A' packet returned an error: -1
はい,動きませんでした.
どうやら,必要なライブラリが足りてないみたいです.
必要なライブラリも追加してみましょう.
root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04/usr/bin# apt-get install clang libic-dev
root@OHANA1:~/swift-2.2.1-RELEASE-ubuntu14.04/usr/bin# ./swift -h OVERVIEW: Swift compiler USAGE: swift [options] <inputs> OPTIONS: -assert-config <value> Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement. -D <value> Specifies one or more build configuration options -framework <value> Specifies a framework which should be linked against -F <value> Add directory to framework search path -gline-tables-only Emit minimal debug info for backtraces only -gnone Don't emit debug info -g Emit debug info -help Display available options -I <value> Add directory to the import search path -j <n> Number of commands to execute in parallel -L <value> Add directory to library link search path -l<value> Specifies a library which should be linked against -module-cache-path <value> Specifies the Clang module cache path -module-link-name <value> Library to link against when using this module -module-name <value> Name of the module to build -nostdimport Don't search the standard library import path for modules -num-threads <n> Enable multi-threading and specify number of threads -Onone Compile without any optimization -Ounchecked Compile with optimizations and remove runtime safety checks -O Compile with optimizations -sdk <sdk> Compile against <sdk> -suppress-warnings Suppress all warnings -target-cpu <value> Generate code for a particular CPU variant -target <value> Generate code for the given target -version Print version information and exit -v Show commands to run and use verbose output -warnings-as-errors Treat warnings as errors -Xcc <arg> Pass <arg> to the C/C++/Objective-C compiler -Xlinker <value> Specifies an option which should be passed to the linker
はい,Swiftがうごきました!
実際にコードを実行してみる
さあ,実行してみましょう!
いつもの「Hello world」を出力してみます.
このように,Windowsのコマンドプロンプト上でSwiftが実行できました.
コンパイルしてみよう
swift
ができるならswiftc
もできるのでは? ということでやってみました.
ちょっと時間はかかりましたが,実際にコンパイルしてバイナリを作成することもできました.
まとめ
今回は,Windows(の上のUbuntu)でのSwiftの実行を紹介しました.
Bash on WindowsもSwift for Ubuntuも,どちらも開発中であり,実装途中のライブラリも多いなど,まだまだ実用には遠く感じました.
(自分の環境だと,対話環境も動かすことはできませんでした...)
今後の開発状況によっては,Windowsでも実用レベルのSwiftを書くことができるようになるかもしれません.
Swiftと,Windows/Ubuntuの今後の進化に期待しましょう.