LoginSignup
64
61

More than 5 years have passed since last update.

goxでGoをクロスコンパイルする

Last updated at Posted at 2014-08-05

Goには、ひとつの端末上でMacやLinux、Windowsなど他のOS向けのバイナリをコンパイルする、クロスコンパイルの機能があります。ただしこのクロスコンパイル、Goをインストールしただけでは利用できず初期設定が必要、かつクロスコンパイルのインターフェイスも覚えにくいなど、やや使いにくいところがあります。

しかし本稿で紹介するgox(ミシェルハシモトさん作)を使えばセットアップもクロスコンパイルも簡単に実行でき、かつ複数のプラットフォーム向けのバイナリを同時・並列にコンパイルする事も出来ます。

goxをインストール+ツールチェインをビルド

$ go get github.com/mitchellh/gox
$ sudo gox -build-toolchain
Password:
The toolchain build can't be parallelized because compiling a single
Go source directory can only be done for one platform at a time. Therefore,
the toolchain for each platform will be built one at a time.

--> Toolchain: darwin/386
--> Toolchain: darwin/amd64
--> Toolchain: linux/386
--> Toolchain: linux/amd64
--> Toolchain: linux/arm
--> Toolchain: freebsd/386
--> Toolchain: freebsd/amd64
--> Toolchain: openbsd/386
--> Toolchain: openbsd/amd64
--> Toolchain: windows/386
--> Toolchain: windows/amd64
--> Toolchain: freebsd/arm
--> Toolchain: netbsd/386
--> Toolchain: netbsd/amd64
--> Toolchain: netbsd/arm
--> Toolchain: plan9/386

これでgoxが使えるようになりました。

クロスコンパイル(全てのプラットフォーム)

goxコマンドで、サポートされている全てのプラットフォームのコンパイルが実行される。

$cd $GOPATH/src/github.com/matope/helloworld
$gox
Number of parallel builds: 8

-->      darwin/386: github.com/matope/helloworld
-->    darwin/amd64: github.com/matope/helloworld
-->       linux/386: github.com/matope/helloworld
-->     linux/amd64: github.com/matope/helloworld
-->       linux/arm: github.com/matope/helloworld
-->     freebsd/386: github.com/matope/helloworld
-->   freebsd/amd64: github.com/matope/helloworld
-->     openbsd/386: github.com/matope/helloworld
-->   openbsd/amd64: github.com/matope/helloworld
-->     windows/386: github.com/matope/helloworld
-->   windows/amd64: github.com/matope/helloworld
-->     freebsd/arm: github.com/matope/helloworld
-->      netbsd/386: github.com/matope/helloworld
-->    netbsd/amd64: github.com/matope/helloworld
-->      netbsd/arm: github.com/matope/helloworld
-->       plan9/386: github.com/matope/helloworld
$ ls
helloworld_darwin_386        helloworld_freebsd_amd64     helloworld_linux_amd64       helloworld_netbsd_amd64      helloworld_openbsd_amd64     helloworld_windows_amd64.exe
helloworld_darwin_amd64      helloworld_freebsd_arm       helloworld_linux_arm         helloworld_netbsd_arm        helloworld_plan9_386         main.go
helloworld_freebsd_386       helloworld_linux_386         helloworld_netbsd_386        helloworld_openbsd_386       helloworld_windows_386.exe

特定プラットフォーム用にコンパイル

Mac用とLinux用のバイナリを同時にコンパイルします。

$ gox --osarch "linux/amd64 darwin/amd64"
Number of parallel builds: 8

-->     linux/amd64: github.com/matope/helloworld
-->    darwin/amd64: github.com/matope/helloworld
$ ls
helloworld_darwin_amd64 helloworld_linux_amd64  main.go

プラットフォーム

Go 1.3で利用可能なプラットフォーム表記一覧

  • darwin/386
  • darwin/amd64
  • linux/386
  • linux/amd64
  • linux/arm
  • freebsd/386
  • freebsd/amd64
  • openbsd/386
  • openbsd/amd64
  • windows/386
  • windows/amd64
  • freebsd/arm
  • netbsd/386
  • netbsd/amd64
  • netbsd/arm
  • plan9/386
64
61
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
64
61