Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docker file that cross-compiles for Raspberry Pi Zero #33

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

# Cross-compiling for RPi Zero in docker

- Note: targets other than RPi Zero aren't (yet) supported

- Build the docker container
`docker build docker/ -f docker/pizero.dockerfile -t pizero:local`

- Run cargo build inside the docker container (this will mount the current
directory inside the container).
```
docker run --rm --mount "type=bind,source=$(pwd),target=/haxo" \
--mount "type=bind,source=$HOME/.cargo,target=/cargo" pizero:local \
cargo build --target arm-unknown-linux-gnueabihf --release --features midi
```

## References
- https://capnfabs.net/posts/cross-compiling-rust-apps-linker-shenanigans-multistrap-chroot/
- https://earthly.dev/blog/cross-compiling-raspberry-pi/
18 changes: 18 additions & 0 deletions docker/fix-sysroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Fix broken symlinks in sysroot.
#
# Some libraries are installed with absolute paths, but they are now
# nested within the /sysroot/ folder.
#
# Inspired by https://capnfabs.net/posts/cross-compiling-rust-apps-linker-shenanigans-multistrap-chroot/

find /sysroot/ -xtype l | while read link; do
fixed=/sysroot$(readlink $link)
if [[ -e $fixed ]]; then
ln -sf $fixed $link
else
echo "failed to fix symlink $link" >&2
exit 1
fi
done
19 changes: 19 additions & 0 deletions docker/multistrap-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[General]
# Target architecture
arch=armhf
# Sysroot location
directory=/sysroot/
# Unpack the packages
unpack=true
# Tidy up afterwards (makes the container smaller)
cleanup=true
# Both of these refer to the 'Raspbian' stanza, below
bootstrap=Raspbian
aptsources=Raspbian

[Raspbian]
# Required packages for our build
packages=libasound2-dev libdbus-1-dev libssl-dev libc6-dev libfluidsynth-dev
source=http://raspbian.raspberrypi.org/raspbian/
# Distribution version name
suite=bullseye
41 changes: 41 additions & 0 deletions docker/pizero.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
FROM --platform=linux/amd64 rust:1.75-bullseye

WORKDIR /setup

RUN apt-get update && apt-get install multistrap gpg --assume-yes

# This is where we'll create the sysroot for compilation.
RUN mkdir /sysroot/

# Install raspberry pi keyring.
# We install it directly into the sysroot so that multistrap can use it.
RUN mkdir -p /sysroot/etc/apt/trusted.gpg.d
RUN curl -sL http://archive.raspbian.org/raspbian.public.key | gpg --import - \
&& gpg --export 9165938D90FDDD2E \
> /sysroot/etc/apt/trusted.gpg.d/raspbian-archive-keyring.gpg

# Setup the sysroot.
COPY multistrap-config .
RUN multistrap -f ./multistrap-config
# Fix broken symlinks in the sysroot.
COPY fix-sysroot.sh .
RUN ./fix-sysroot.sh

# Download the cross compiler.
RUN mkdir -p /opt/
RUN wget -q -O- https://github.com/tttapa/docker-arm-cross-toolchain/releases/latest/download/x-tools-armv6-rpi-linux-gnueabihf.tar.xz \
| tar xJ -C /opt

# Add the rust target.
RUN rustup target add arm-unknown-linux-gnueabihf

# Setup compilation environment variables.
ENV PKG_CONFIG_LIBDIR_arm_unknown_linux_gnueabihf=/sysroot/usr/lib/arm-linux-gnueabihf/pkgconfig
ENV PKG_CONFIG_SYSROOT_DIR_arm_unknown_linux_gnueabihf=/sysroot/
ENV CARGO_HOME=/cargo
ENV RUSTFLAGS="-C link-arg=--sysroot=/sysroot/ -C linker=/opt/x-tools/armv6-rpi-linux-gnueabihf/bin/armv6-rpi-linux-gnueabihf-gcc"

# Change workdir (this is where the haxo source should get mounted).
WORKDIR /haxo
# Prevent git complaining about "detected dubious ownership in repository"
RUN git config --global --add safe.directory /haxo