Skip to content

Instantly share code, notes, and snippets.

@takubokudori
Last active July 6, 2024 15:54
Show Gist options
  • Save takubokudori/e790f7623825a9fc63a22ad0fdba77c6 to your computer and use it in GitHub Desktop.
Save takubokudori/e790f7623825a9fc63a22ad0fdba77c6 to your computer and use it in GitHub Desktop.
WSL上のRustでラズパイ用バイナリをビルドする方法

WSL上のRustでラズパイ用バイナリをビルドする方法

大いに参考にさせていただいた記事: ラズパイに向けてRustをクロスコンパイル!

以下はUbuntu 20.04 LTS on WSL2で確認済み

  1. Rustをインストール

  2. arm用のgcc一式を入れる

sudo apt update && sudo apt upgrade -y
sudo apt install gcc-arm-linux-gnueabihf
  1. ARM v7のツールチェーンを入れる
rustup target add armv7-unknown-linux-gnueabihf
  1. .cargo/config を書く

~/.cargo/config に以下を追記する。

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
  1. お試しコンパイル

適当にクレートを作って以下が通ればOK

cargo build --target=armv7-unknown-linux-gnueabihf

依存ARMライブラリをaptで入れられるようにしておく

dieselクレートが必要とするlibsqlite3-devなどのARM版ライブラリを入れておくことでdieselも容易にクロスコンパイルできる。

  1. dpkgでarmhfバイナリも落とせるようにする
sudo dpkg --add-architecture armhf
  1. /etc/apt/sources.list を書き換える

既に deb http://archive.ubuntu.com/ubuntu のような有効行が書き連ねてあると思うが、それらの行すべてのdebの後に [arch=amd64,i386] を書き加える。 deb-srcなどがコメントアウトされていないのであればそれにも付け足すこと。

deb [arch=amd64,i386] http:// # 省略

その後、 /etc/apt/sources.list に下記を追記する。 このうち focal の部分は、すでに記述してあった http://archive... のところに書いてあるものに変更する。 例えば Ubuntu 20.04 LTSの場合はfocalで、Ubuntu 18.04 LTSの場合はbionicである。 その他のバージョンは Ubuntuのバージョン履歴 で確認する。

deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports focal main restricted universe multiverse
deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports focal-updates main restricted universe multiverse
deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports focal-security main restricted universe multiverse
  1. リポジトリを更新する
sudo apt update

これでエラーが出なければよい。

dieselを試しにクロスコンパイルする

armhf用のlibsqlite3-devを入れる

sudo apt install libsqlite3-dev:armhf

あとはdieselのsqlite3サンプルプログラムを以下のコマンドでビルドできればOK

cargo build --target=armv7-unknown-linux-gnueabihf

ARM64版ビルド

Raspbian 11(Bullseye)から64bit版OSがリリースされるようになったので、ARM64のクロスビルド方法も書いておく。 ARM64はaarch64アーキテクチャを使用する。

  1. Rustをインストール

  2. ARM64用gcc一式を入れる

sudo apt update && sudo apt upgrade -y
sudo apt install g++-aarch64-linux-gnu
  1. aarch64のツールチェーンを入れる
rustup install stable-aarch64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu
  1. .cargo/config を書く

~/.cargo/config に以下を追記する。

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

libsqlite3-devのインストール

  1. dpkgでarm64を扱えるようにする
sudo dpkg --add-architecture arm64
  1. /etc/apt/sources.list に追記
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports focal main restricted universe multiverse
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports focal-updates main restricted universe multiverse
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports focal-security main restricted universe multiverse
  1. libsqlite3-dev:arm64をインストール
sudo apt install libsqlite3-dev:arm64

libdbus-sysのビルド

WSL上で普通にビルドすると、こんな感じにエラーが出てくる。

  --- stderr
  pkg_config failed: pkg-config has not been configured to support cross-compilation.

  Install a sysroot for the target platform and configure it via
  PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
  cross-compiling wrapper for pkg-config and set it via
  PKG_CONFIG environment variable.
  One possible solution is to check whether packages
  'libdbus-1-dev' and 'pkg-config' are installed:
  On Ubuntu:
  sudo apt install libdbus-1-dev pkg-config
  On Fedora:
  sudo dnf install dbus-devel pkgconf-pkg-config

これを回避するためには、 PKG_CONFIG_SYSROOT_DIR 環境変数を設定する。 sysroot とは、gccなどのコンパイラに入っている機能で、ヘッダーとライブラリのあるルートディレクトリのことらしい。 つまり、 binincludelib があるディレクトリを指定する感じである。 PKG_CONFIG_SYSROOT_DIR=/ の場合がデフォルトの設定(多分、要検証)。 PKG_CONFIG_SYSROOT_DIR=/usr/aarch64-linux-gnu とすると、aarch64のライブラリを使用するようになる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment