1+ # This Dockerfile supports both native building and cross-compilation to x86-64, aarch64 and riscv64
12FROM --platform=$BUILDPLATFORM ubuntu:24.04
23
34ARG PROFILE=production
45ARG RUSTFLAGS
56# Incremental compilation here isn't helpful
67ENV CARGO_INCREMENTAL=0
78
9+ ENV PKG_CONFIG_ALLOW_CROSS=true
10+ ARG BUILDARCH
11+ ARG TARGETARCH
12+
813WORKDIR /code
914
1015RUN \
@@ -15,18 +20,68 @@ RUN \
1520 llvm \
1621 clang
1722
23+ RUN \
24+ if [ $BUILDARCH != "arm64" ] && [ $TARGETARCH = "arm64" ]; then \
25+ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
26+ g++-aarch64-linux-gnu \
27+ gcc-aarch64-linux-gnu \
28+ libc6-dev-arm64-cross \
29+ ; fi
30+
31+ RUN \
32+ if [ $BUILDARCH != "riscv64" ] && [ $TARGETARCH = "riscv64" ]; then \
33+ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
34+ g++-riscv64-linux-gnu \
35+ gcc-riscv64-linux-gnu \
36+ libc6-dev-riscv64-cross \
37+ ; fi
38+
39+ RUN \
40+ if [ $BUILDARCH != "amd64" ] && [ $TARGETARCH = "amd64" ]; then \
41+ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
42+ g++-x86-64-linux-gnu \
43+ gcc-x86-64-linux-gnu \
44+ libc6-dev-amd64-cross \
45+ ; fi
46+
1847RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none
1948
2049# Up until this line all Rust images in this repo should be the same to share the same layers
2150
2251COPY . /code
52+ ARG TARGETVARIANT
2353
24-
25- RUN /root/.cargo/bin/cargo build \
54+ RUN \
55+ if [ $BUILDARCH != "arm64" ] && [ $TARGETARCH = "arm64" ]; then \
56+ export RUSTFLAGS="$RUSTFLAGS -C linker=aarch64-linux-gnu-gcc" \
57+ ; fi && \
58+ if [ $BUILDARCH != "riscv64" ] && [ $TARGETARCH = "riscv64" ]; then \
59+ export RUSTFLAGS="$RUSTFLAGS -C linker=riscv64-linux-gnu-gcc" \
60+ ; fi && \
61+ if [ $TARGETARCH = "amd64" ] && [ "$RUSTFLAGS" = "" ]; then \
62+ case "$TARGETVARIANT" in \
63+ # x86-64-v2 with AES-NI
64+ "v2" ) export RUSTFLAGS="-C target-cpu=x86-64-v2" ;; \
65+ # x86-64-v3 with AES-NI
66+ "v3" ) export RUSTFLAGS="-C target-cpu=x86-64-v3 -C target-feature=+aes" ;; \
67+ # v4 is compiled for Zen 4+
68+ "v4" ) export RUSTFLAGS="-C target-cpu=znver4" ;; \
69+ # Default build is for Skylake
70+ *) export RUSTFLAGS="-C target-cpu=skylake" ;; \
71+ esac \
72+ ; fi && \
73+ if [ $BUILDARCH != "amd64" ] && [ $TARGETARCH = "amd64" ]; then \
74+ export RUSTFLAGS="$RUSTFLAGS -C linker=x86_64-linux-gnu-gcc" \
75+ ; fi && \
76+ sed -i 's/deny(unused_crate_dependencies)/allow(unused_crate_dependencies)/' alerter/src/main.rs && \
77+ RUSTC_TARGET_ARCH=$(echo $TARGETARCH | sed "s/amd64/x86_64/g" | sed "s/arm64/aarch64/g" | sed "s/riscv64/riscv64gc/g" ) && \
78+ /root/.cargo/bin/cargo -Zgitoxide -Zgit build \
2679 --locked \
80+ -Z build-std \
2781 --profile $PROFILE \
28- --bin alerter && \
29- mv target/*/alerter /alerter && \
82+ --bin alerter \
83+ --target $RUSTC_TARGET_ARCH-unknown-linux-gnu && \
84+ mv target/*/*/alerter /alerter && \
3085 rm -rf target
3186
3287FROM ubuntu:24.04
0 commit comments