Skip to content

Instantly share code, notes, and snippets.

@denisoster
Forked from jmpinit/Dockerfile
Created February 11, 2020 09:11
Show Gist options
  • Save denisoster/4c484df6c1ed3567731ae763a5928b29 to your computer and use it in GitHub Desktop.
Save denisoster/4c484df6c1ed3567731ae763a5928b29 to your computer and use it in GitHub Desktop.
Example of cross-compiling Rust for ARMv7.

Example of Cross-compiling Rust

Run docker build -t armv7-rust ., then docker run -it --rm armv7-rust. The compiled binary will be found at /root/hello/target/armv7-unknown-linux-gnueabihf/debug/hello in the container. Copy it to an ARMv7 machine and run it. It should say "Hello, world!".

This is based on the instructions found in the rust-cross project.

# configures cargo for cross compilation
if [ ! -z "$(uname -a | grep -o Darwin)" ]; then
echo "You seem to be running this from OS X."
echo "This is meant to be run in an Ubuntu container. Exiting."
exit 1
fi
mkdir -p /root/.cargo
cat >>/root/.cargo/config <<EOF
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
EOF
FROM ubuntu
MAINTAINER Owen Trueblood
# general dependencies
RUN apt-get install -y curl
# install target toolchain
RUN apt-get update --fix-missing
RUN apt-get install -y build-essential gcc-arm-linux-gnueabihf
# install Rust
WORKDIR /root
RUN curl https://sh.rustup.rs -sSf > rustup.sh
RUN chmod +x rustup.sh
RUN /bin/sh -c './rustup.sh -y'
RUN echo 'export PATH="$HOME/.cargo/bin:$PATH"' > /root/.bashrc
# install cross-compiled standard crates
RUN /root/.cargo/bin/rustup target add armv7-unknown-linux-gnueabihf
# configure cargo for cross compilation
COPY ./configure.sh /root/configure.sh
RUN /root/configure.sh
# generate hello world
WORKDIR /root
ENV USER root
ENV PATH $PATH:/root/.cargo/bin
RUN cargo init --bin hello
# build
WORKDIR /root/hello
RUN cargo build --target=armv7-unknown-linux-gnueabihf
ENTRYPOINT /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment