LoginSignup
15

More than 5 years have passed since last update.

RustのLinux/Windows/OS X向けバイナリをCIで生成する(更新版)

Posted at

はじめに

以前「RustのLinux/Windows/OS X向けバイナリをCIで生成する」という記事を書いたのですが半年たって情報が古くなってしまったので更新します。
また、前回記事を書いた後に気づいたのですが、rust-everywhereというのもあるようです。こちらもCIでRustのバイナリ生成するテンプレートになっていて、ARMとかもサポートしているようです。

変更点

前回からの変更点は以下の2点です。

  • rustup.rsの使用
  • muslターゲットに変更

rustup.rsは現在のRustのデフォルトのセットアップツール兼ツールチェーン管理ツールです。以前のrustup.shmultirustは非推奨になったようなので皆さん乗り換えましょう。
rustup.rsではクロスコンパイル用のターゲット追加が簡単にできるようになっているのでCI用のスクリプトがだいぶ簡素化されました。

muslはi686/x86_64のLinux環境で選択可能なターゲットで、libcとしてglibcを使うのではなく、musl libcというライブラリを使い、staticリンクしたバイナリを生成できます。CIで生成したバイナリをそのまま配布する場合、glibcのバージョンが古い環境などで動かないことがあったのでこちらに変更しました。ただ、後述しますが、現状では問題もあるようです。

Travis-CIによるLinux/OS X向けビルド

travis.yml
language: rust

os:
  - linux
  - osx

env:
  - ARCH=i686
  - ARCH=x86_64

before_script:
  - curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain beta
  - export PATH=~/.cargo/bin:$PATH
  - if [[ $TRAVIS_OS_NAME == "linux" && $ARCH == "x86_64" ]]; then rustup target add x86_64-unknown-linux-musl; fi
  - if [[ $TRAVIS_OS_NAME == "linux" && $ARCH == "i686"   ]]; then rustup target add i686-unknown-linux-musl;   fi
  - if [[ $TRAVIS_OS_NAME == "osx"   && $ARCH == "i686"   ]]; then rustup target add i686-apple-darwin;         fi

script:
  - if [[ $TRAVIS_OS_NAME == "osx"   && $ARCH == "x86_64" ]]; then cargo build --release --target=x86_64-apple-darwin;       fi
  - if [[ $TRAVIS_OS_NAME == "osx"   && $ARCH == "i686"   ]]; then cargo build --release --target=i686-apple-darwin;         fi
  - if [[ $TRAVIS_OS_NAME == "linux" && $ARCH == "x86_64" ]]; then cargo build --release --target=x86_64-unknown-linux-musl; fi
  - if [[ $TRAVIS_OS_NAME == "linux" && $ARCH == "i686"   ]]; then cargo build --release --target=i686-unknown-linux-musl;   fi

rustupのおかげでrustup target addするだけで新規ターゲット追加ができます。Travis-CIのRustインストーラがrustup.rsになってくれればもっと楽なのですが。
また、現状(Rust1.11)ではTravis-CI環境でmuslターゲットのビルドは失敗するようです。Rust1.12では直るようなのでとりあえずbetaチャンネルを使っています(--default-toolchain betaの部分です)が、来月にはstableでビルドできるようになるかと思います。

AppVeyorによるWindows向けビルド

appveyor.yml
platform:
  - x64

environment:
  matrix:
    - TARGET: i686-pc-windows-msvc
    - TARGET: x86_64-pc-windows-msvc

install:
  - curl -sSf -o rustup-init.exe https://win.rustup.rs
  - rustup-init.exe -y
  - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
  - if NOT "%TARGET%" == "x86_64-pc-windows-msvc" rustup target add %TARGET%

build_script:
  - cmd: cargo build --release --target=%TARGET%

rustup.rsはちゃんとWindowsにも対応しているのでTravis-CI側と同じようなコマンドでOKです。

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
15