Fix Dockerfile: skip cross-compilers when building natively

gcc-aarch64-linux-gnu is an x86→arm64 cross-compiler; it doesn't exist
on arm64 hosts (like the Pi). Only install cross-toolchains and set cargo
linker config when BUILDPLATFORM != TARGETPLATFORM.
This commit is contained in:
Claude 2026-03-20 10:40:12 +00:00
parent 2060606adc
commit 3096d251c6
No known key found for this signature in database

View file

@ -1,18 +1,23 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
# ── Build stage ─────────────────────────────────────────────────────────────── # ── Build stage ───────────────────────────────────────────────────────────────
# Always run the compiler on the *build* host for speed; cross-compile to target. # Run the compiler on the *build* host; cross-compile to target when needed.
FROM --platform=$BUILDPLATFORM rust:1.86-slim-bookworm AS builder FROM --platform=$BUILDPLATFORM rust:1.86-slim-bookworm AS builder
ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG TARGETARCH ARG TARGETARCH
ARG TARGETVARIANT ARG TARGETVARIANT
# Cross-compilation toolchains for every supported target. # Install cross-compilation toolchains only when actually cross-compiling.
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y pkg-config && \
pkg-config \ if [ "${BUILDPLATFORM}" != "${TARGETPLATFORM}" ]; then \
gcc-aarch64-linux-gnu \ case "${TARGETARCH}:${TARGETVARIANT}" in \
gcc-arm-linux-gnueabihf \ "arm64:") apt-get install -y gcc-aarch64-linux-gnu ;; \
gcc-arm-linux-gnueabi \ "arm:v7") apt-get install -y gcc-arm-linux-gnueabihf ;; \
&& rm -rf /var/lib/apt/lists/* "arm:v6") apt-get install -y gcc-arm-linux-gnueabi ;; \
esac; \
fi && \
rm -rf /var/lib/apt/lists/*
# Map TARGETARCH + TARGETVARIANT → Rust target triple, then install it. # Map TARGETARCH + TARGETVARIANT → Rust target triple, then install it.
RUN case "${TARGETARCH}:${TARGETVARIANT}" in \ RUN case "${TARGETARCH}:${TARGETVARIANT}" in \
@ -24,8 +29,9 @@ RUN case "${TARGETARCH}:${TARGETVARIANT}" in \
esac > /rust_target && \ esac > /rust_target && \
rustup target add "$(cat /rust_target)" rustup target add "$(cat /rust_target)"
# Tell Cargo which cross-linker to use for each foreign target. # Tell Cargo which cross-linker to use — only needed when cross-compiling.
RUN mkdir -p /root/.cargo && cat >> /root/.cargo/config.toml <<'EOF' RUN if [ "${BUILDPLATFORM}" != "${TARGETPLATFORM}" ]; then \
mkdir -p /root/.cargo && cat >> /root/.cargo/config.toml <<'EOF'
[target.aarch64-unknown-linux-gnu] [target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc" linker = "aarch64-linux-gnu-gcc"
@ -35,6 +41,7 @@ linker = "arm-linux-gnueabihf-gcc"
[target.arm-unknown-linux-gnueabi] [target.arm-unknown-linux-gnueabi]
linker = "arm-linux-gnueabi-gcc" linker = "arm-linux-gnueabi-gcc"
EOF EOF
fi
WORKDIR /build WORKDIR /build