Reproducible cross-compilation for Rust (with Docker)

Error: Toolchain / Library XX not found. Aborting compilation.

How many times did you get this kind of message when trying to follow the build instructions of a project, or cross compile it?

This problem can be solved with immutable toolchains and reproductible build environements. All of that powered by Docker.

Cross-compile a Rust application from Linux to Windows

FROM rust:latest

RUN apt update && apt upgrade -y
RUN apt install -y g++-mingw-w64-x86-64

RUN rustup target add x86_64-pc-windows-gnu
RUN rustup toolchain install stable-x86_64-pc-windows-gnu


CMD ["cargo", "build", "--target", "x86_64-pc-windows-gnu"]
$ docker build . -t rust_cross_compile/windows -f
# in your Rust project
$ docker run --rm -ti -v `pwd`:/app rust_cross_compile/windows

Cross-compile a Rust application to aarch64 (armv8)


FROM rust:latest

RUN apt update && apt upgrade -y
RUN apt install -y g++-aarch64-linux-gnu libc6-dev-arm64-cross

RUN rustup target add aarch64-unknown-linux-gnu
RUN rustup toolchain install stable-aarch64-unknown-linux-gnu


    CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \

CMD ["cargo", "build", "--target", "aarch64-unknown-linux-gnu"]
$ docker build . -t rust_cross_compile/aarch64 -f Dockerfile.aarch64
# in your Rust project
$ docker run --rm -ti -v `pwd`:/app rust_cross_compile/aarch64

Cross-compile a Rust application to armv7


FROM rust:latest

RUN apt update && apt upgrade -y
RUN apt install -y g++-arm-linux-gnueabihf libc6-dev-armhf-cross

RUN rustup target add armv7-unknown-linux-gnueabihf
RUN rustup toolchain install stable-armv7-unknown-linux-gnueabihf


    CC_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-gcc \

CMD ["cargo", "build", "--target", "armv7-unknown-linux-gnueabihf"]
$ docker build . -t rust_cross_compile/armv7 -f Dockerfile.armv7
# in your Rust project
$ docker run --rm -ti -v `pwd`:/app rust_cross_compile/armv7


Now, enough for the theory. In practice, you should use cross, a project developed and maintained by the Tools team which allows you to easily cross-compile Rust projects without messing with custom Dockerfiles.

$ cargo install -f cross
$ cross build --target aarch64-unknown-linux-gnu
$ cross build --target x86_64-pc-windows-gnu
$ cross build --target armv7-unknown-linux-gnueabihf
# ...

You can learn more about it on GitHub.

The code is on GitHub

As usual, you can find the code on GitHub: (please don't forget to star the repo 🙏).

1 email / week to learn how to (ab)use technology for fun & profit: Programming, Hacking & Entrepreneurship.
I hate spam even more than you do. I'll never share your email, and you can unsubscribe at any time.

Tags: hacking, programming, rust, tutorial, devops

Want to learn Rust, Cryptography and Security? Get my book Black Hat Rust!