Skip to content

Instantly share code, notes, and snippets.

@PaulVanSchayck
Created January 23, 2022 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulVanSchayck/86f9966ffe7adc597327d728143b5d0e to your computer and use it in GitHub Desktop.
Save PaulVanSchayck/86f9966ffe7adc597327d728143b5d0e to your computer and use it in GitHub Desktop.
Cross compile Rust for Raspberry Pi using Docker container

This is a simple Docker environment for cross compiling Rust for the Raspberry Pi.

Usage

Build image:

docker build -t raspios-bullseye-rust-cross-compile .

Go to the root of your source code on your host system and execute:

docker run -v "$PWD:/home/local/src" -it raspios-bullseye-rust-cross-compile /bin/bash

This will mount the source directory in the ~/src directory in the container.

In the container execute:

cd src
cargo build --release --target armv7-unknown-linux-gnueabihf
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
build-essential \
gcc-arm-linux-gnueabihf \
curl
# Switch user
RUN useradd -ms /bin/bash local
USER local
WORKDIR /home/local
# Download rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
# Add cargo and rust to path
RUN echo 'source $HOME/.cargo/env' >> $HOME/.bashrc
ENV PATH="/home/local/.cargo/bin:${PATH}"
# Setup cross compiling
RUN rustup target add armv7-unknown-linux-gnueabihf
COPY config $HOME/.cargo/config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment