To run our project on the Roboter, we need to cross compile our code to the ISA (instruction set architecture) of it's target processor.
Cross-compiling rust projects requires the rust toolchain and a suitable cross compiler for our target.
To run our project on the Brick, we need to cross-compile our code to the ISA (instruction set architecture) of the target processor. Cross-compiling rust projects requires the rust toolchain and a suitable cross-compiler for our target.
The Modbrick is powerered by a Raspberry Pi Zero 2W, its BCM2710 SoC includes four Cortex-A53 ARMv8-CPUs, which support the aarch64 ISA.
Raspberry Pi OS is based on Linux and we try to circumvent libc-compatibility-errors by using musl instead of glibc.
All this Information combined, the cross compilation target for the Roblab becomes aarch64-unknown-linux-musl.
This guide (mis-)uses Ziglang as a C/C++ cross-compiler/linker and cargo-zigbuild for convenient cross-compilation of rust projects from any host system.
For background information about using zigcc as a cross compiler for rust, see this blog post and this other blog post.
Please test your installation after going through the installation steps!
Install Zig with your package manager:
winget install Zig.Zig # Windows (can take a while...)
brew install zig # macOS
pacman -S zig # Arch
dnf install zig # Fedora
# Debian, Ubuntu, Linux Mint
wget https://debian.griffo.io/apt/pool/main/z/zig-0/zig-0_0.15.2-3+trixie_amd64.deb
sudo dpkg -i zig-0_0.15.2-3+trixie_amd64.deb
# Others
pipx install ziglang
ln -s ~/.local/bin/python-zig ~/.local/bin/zig
Alternatively, download the latest stable release from ziglang.org, extract it to a suitable location (e.g. ~/.zig) and add it to your PATH.
For macOS and Arch Linux, you can install cargo-zigbuild with your package manager:
brew install cargo-zigbuild # macOS
pacman -S cargo-zigbuild # Arch
For all other Operating Systems, either manually download the latest release of cargo-zigbuild for your System and unpack it to ~/.cargo/bin, or use the powershell/shell command presented at the top of the release page.
Alternatively, if you want to build from scratch, use the following cargo command:
cargo install cargo-zigbuild
Verify your Zig and cargo-zigbuild installation:
zig version
> 0.15.2 # (must be at least 0.11)
cargo-zigbuild --version
> cargo-zigbuild 0.22.1 # (must be at least 0.22.0)
If your version is wrong, uninstall and reinstall with cargo.
Validate that cross-compilation works:
rustup target add aarch64-unknown-linux-musl
cargo new hello
cd hello
cargo-zigbuild build --target aarch64-unknown-linux-musl