swift: Package Managerを試してみました

概要

LinuxSwiftを動かす方法は2つあります。1つは、Dockerイメージを使う方法です( Dockerイメージでswiftを動かしてみました)(もう一つは、Vagrantを使う方法です。Introduction to Open Source Swift on Linux)。今回は、Vagrantを使う方法を試してみました。同時に、Package Managerを使って見ました。

MacOSX El Capitanで行いましたが、Windowsでも同じようにできると思います。

Linuxswift環境を構築します

Introduction to Open Source Swift on Linuxの通りに行います。

1. 環境を準備します

VirtualBoxvとVagrantをインストールします。

2. Vagrantファイルを編集します

Vagrant.configure(2) do |config|
  ## 1
  config.vm.box = "https://cloud-images.ubuntu.com/vagrant/trusty/20151201/trusty-server-cloudimg-amd64-vagrant-disk1.box"
 
config.vm.provision "shell", inline: <<-SHELL
    ## 2
    sudo apt-get --assume-yes install clang
    ## 3
    curl -O https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz
    ## 4
    tar zxf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz
    ## 5
    echo "export PATH=/home/vagrant/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin:\"${PATH}\"" >> .profile
    echo "Swift has successfully installed on Linux"
  SHELL
end

3. 起動します

時間がかかるのでしばらく待ちます。

$ vagrant up

4. ログインします

$ vagrant ssh

swiftが動いているか確認します。

$ swift --version
Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Target: x86_64-unknown-linux-gnu

HelloWorldを実行します。

$ cat helloworld.swift
print("Hello, world")

$ swift helloworld.swift
print("Hello, world")

コンパイルします。

$ swiftc helloworld.swift

実行ファイルが作成されます。

$ ls -l
total 87576
-rwxrwxr-x 1 vagrant vagrant    13684 Dec 10 22:01 helloworld
-rw-rw-r-- 1 vagrant vagrant       22 Dec 10 21:52 helloworld.swift

作成された実行ファイルを実行してみます。

$ ./helloworld
print("Hello, world")

実行ファイルの先頭をダンプしてみます。

$ xxd helloworld | less
0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
0000010: 0200 3e00 0100 0000 700a 4000 0000 0000  ..>.....p.@.....

Package Managerを使ってみます

Package Managerは、swiftのモジュールを管理するツールです。Swiftビルドシステムを統合しているおり、依存モジュールのダウンロード、コンパイル、リンクを自動的に行ってくれます。

1. Package Managerを使ってモジュールを作成します(パッケージ名未指定)

適当なディレクトリを作成し、以下のように、main.swiftをSourcesディレクトリにPackage.swift(空ファイルでOK)を作成します。main.swiftに処理を記述します。

mkdir helloworld-project && cd helloworld-project
~/helloworld-project$ tree
.
|-- Package.swift
`-- Sources
    `-- main.swift

$ cat Sources/main.swift
print("Hello, world")

ビルドします。

$ swift build
Compiling Swift Module 'helloworldproject' (1 sources)
Linking Executable:  .build/debug/helloworld-project

隠しディレクトリ(.build)以下に実行ファイルが作成されます。 ここで、カレントディレクトリ(helloworld-project)がパッケージ名になっています。 Package.swiftにパッケージ名を指定しないとカレントディレクトリがパッケージ名になるようです。

実行します。

$ .build/debug/helloworld-project
Hello, world

ファイルの先頭をダンプします。

$ .build/debug/helloworld-project | less
0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
0000010: 0200 3e00 0100 0000 700a 4000 0000 0000  ..>.....p.@.....

生成されたファイル一覧です。

~/helloworld-project$ tree -a
.
|-- .build
|   `-- debug
|       |-- helloworld-project
|       |-- helloworld-project.o
|       |   |-- build.db
|       |   |-- helloworld-project
|       |   |   |-- helloworld.d
|       |   |   |-- helloworld.swiftdeps
|       |   |   |-- main.d
|       |   |   |-- main~partial.swiftdoc
|       |   |   |-- main~partial.swiftmodule
|       |   |   |-- main.swiftdeps
|       |   |   |-- master.swiftdeps
|       |   |   `-- output-file-map.json
|       |   |-- home
|       |   |   `-- vagrant
|       |   |       `-- helloworld-project
|       |   |           `-- .build
|       |   |               `-- debug
|       |   |-- llbuild.yaml
|       |   `-- Sources
|       |       `-- main.swift.o
|       |-- helloworldproject.swiftdoc
|       `-- helloworldproject.swiftmodule
|-- Package.swift
`-- Sources
    `-- main.swift

2. Package Managerを使ってモジュールを作成します(パッケージ指定)

パッケージ名(SwiftTest)を指定してビルドします。

$ cd helloworld-project

$ cat Package.swift
let package = Package(
    name: "SwiftTest"
)

$ swift build
Compiling Swift Module 'SwiftTest' (1 sources)
Linking Executable:  .build/debug/SwiftTest

生成されたファイル一覧です。指定したパッケージ(ディレクトリ)にファイルが作成されていることがわかります。

vagrant@vagrant-ubuntu-trusty-64:~/helloworld-project$ tree -a
.
|-- .build
|   `-- debug
|       |-- SwiftTest
|       |-- SwiftTest.o
|       |   |-- build.db
|       |   |-- home
|       |   |   `-- vagrant
|       |   |       `-- helloworld-project
|       |   |           `-- .build
|       |   |               `-- debug
|       |   |-- llbuild.yaml
|       |   |-- Sources
|       |   |   `-- main.swift.o
|       |   `-- SwiftTest
|       |       |-- main.d
|       |       |-- main~partial.swiftdoc
|       |       |-- main~partial.swiftmodule
|       |       |-- main.swiftdeps
|       |       |-- master.swiftdeps
|       |       `-- output-file-map.json
|       |-- SwiftTest.swiftdoc
|       `-- SwiftTest.swiftmodule
|-- Package.swift
`-- Sources
    `-- main.swift

実行します。

$ .build/debug/SwiftTest
Hello, world

関連資料