大いに参考にさせていただいた記事: ラズパイに向けてRustをクロスコンパイル!
以下はUbuntu 20.04 LTS on WSL2で確認済み
- 
Rustをインストール 
- 
arm用のgcc一式を入れる 
sudo apt update && sudo apt upgrade -y
sudo apt install gcc-arm-linux-gnueabihf
- ARM v7のツールチェーンを入れる
rustup target add armv7-unknown-linux-gnueabihf
- .cargo/configを書く
~/.cargo/config に以下を追記する。
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
- お試しコンパイル
適当にクレートを作って以下が通ればOK
cargo build --target=armv7-unknown-linux-gnueabihf
dieselクレートが必要とするlibsqlite3-devなどのARM版ライブラリを入れておくことでdieselも容易にクロスコンパイルできる。
- dpkgでarmhfバイナリも落とせるようにする
sudo dpkg --add-architecture armhf
- /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
- リポジトリを更新する
sudo apt update
これでエラーが出なければよい。
armhf用のlibsqlite3-devを入れる
sudo apt install libsqlite3-dev:armhf
あとはdieselのsqlite3サンプルプログラムを以下のコマンドでビルドできればOK
cargo build --target=armv7-unknown-linux-gnueabihf
Raspbian 11(Bullseye)から64bit版OSがリリースされるようになったので、ARM64のクロスビルド方法も書いておく。 ARM64はaarch64アーキテクチャを使用する。
- 
Rustをインストール 
- 
ARM64用gcc一式を入れる 
sudo apt update && sudo apt upgrade -y
sudo apt install g++-aarch64-linux-gnu
- aarch64のツールチェーンを入れる
rustup install stable-aarch64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu
- .cargo/configを書く
~/.cargo/config に以下を追記する。
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
- dpkgでarm64を扱えるようにする
sudo dpkg --add-architecture arm64
- /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
- libsqlite3-dev:arm64をインストール
sudo apt install libsqlite3-dev:arm64
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などのコンパイラに入っている機能で、ヘッダーとライブラリのあるルートディレクトリのことらしい。
つまり、 bin と include と lib があるディレクトリを指定する感じである。
PKG_CONFIG_SYSROOT_DIR=/ の場合がデフォルトの設定(多分、要検証)。
PKG_CONFIG_SYSROOT_DIR=/usr/aarch64-linux-gnu とすると、aarch64のライブラリを使用するようになる。