LoginSignup
1
0

More than 1 year has passed since last update.

Windows上でRustのクロス環境の作成(ARMv7 Linux)

Last updated at Posted at 2022-08-30

概要

  • Windows環境でRustのクロス開発環境を用意する

  • ターゲットはarmv7-unknown-linux-gnueabihf(Linux用バイナリ)とする

  • WSL2、docker、msys2等は使用しない

  • プロジェクト作成後はVSCodeで編集、ターミナルでビルドする想定です。

  • この環境で作成できるバイナリはインストールしたARM用のToolchainのバージョンに依存します。(サポートしているglibc等のバージョンに注意が必要です)

    Rustでサポートされているtarget、arm Developerで配布されているWinodws用のPrebuildのtoolchainの組み合わせであれば、簡単に環境構築が可能です。(GCC + msys2のクロス環境よりもかなり楽です。)

環境構築

1. Rustのインストール

最新版を確認

rustup update

2. rustupでARMv7用のターゲットのインストール

rustup target add armv7-unknown-linux-gnueabihf

3. ARMv7のToolchain(Windows用)のインストール

gcc-arm-11.2-2022.02-mingw-w64-i686-arm-none-linux-gnueabihf.exe

インストーラ形式のファイルをダウンロードします。(ファイルサイズが小さい上に環境変数の登録まで行ってくれるため)

インストール終了時にPATHを追加するオプションを選択してください。

ユーザー環境変数のPATHに以下のディレクトリが追加されていることを確認してください。

"C:\Program Files (x86)\Arm GNU Toolchain arm-none-linux-gnueabihf\11.2 2022.02\bin"

4. /Users/ユーザー名/.cargo/configの設定

エクスプローラで/Users/ユーザー名/.cargoのフォルダを開きます。

explorer %HOMEPATH%\.cargo

configを新規作成します。

.cargo/config

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-none-linux-gnueabihf-gcc"

プロジェクトの作成

cargo new コマンドでプロジェクトを作成します

cargo new test(プロジェクト名)

cd test

以下のファイルが作成されます。

// main.rs
fn main() {
    println!("Hello, world!");
}

ターゲットを指定してビルドを行います

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

ファイルをstripします

arm-none-linux-gnueabihf-strip.exe .\target\armv7-unknown-linux-gnueabihf\release\test

これでファイルサイズがかなり小さくなります。

一連の処理をバッチファイルにまとめたものが以下になります。

build.bat

set target_arch=armv7-unknown-linux-gnueabihf
cargo build --target %target_arch% --release
set THIS_PATH=%~dp0
for %%1 in ("%THIS_PATH:~0,-1%") do set FOLDER_NAME=%%~nx1
arm-none-linux-gnueabihf-strip.exe .\target\%target_arch%\release\%FOLDER_NAME%
move .\target\%target_arch%\release\%FOLDER_NAME% ./

カレントのディレクトリ名を取得する処理は

を利用させていただきました。

1
0
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
1
0