diff --git a/infra/Dockerfile.server b/infra/Dockerfile.server index 8020c0d..f6239b9 100644 --- a/infra/Dockerfile.server +++ b/infra/Dockerfile.server @@ -1,18 +1,23 @@ # syntax=docker/dockerfile:1 # ── 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 +ARG BUILDPLATFORM +ARG TARGETPLATFORM ARG TARGETARCH ARG TARGETVARIANT -# Cross-compilation toolchains for every supported target. -RUN apt-get update && apt-get install -y \ - pkg-config \ - gcc-aarch64-linux-gnu \ - gcc-arm-linux-gnueabihf \ - gcc-arm-linux-gnueabi \ - && rm -rf /var/lib/apt/lists/* +# Install cross-compilation toolchains only when actually cross-compiling. +RUN apt-get update && apt-get install -y pkg-config && \ + if [ "${BUILDPLATFORM}" != "${TARGETPLATFORM}" ]; then \ + case "${TARGETARCH}:${TARGETVARIANT}" in \ + "arm64:") apt-get install -y gcc-aarch64-linux-gnu ;; \ + "arm:v7") apt-get install -y gcc-arm-linux-gnueabihf ;; \ + "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. RUN case "${TARGETARCH}:${TARGETVARIANT}" in \ @@ -24,8 +29,9 @@ RUN case "${TARGETARCH}:${TARGETVARIANT}" in \ esac > /rust_target && \ rustup target add "$(cat /rust_target)" -# Tell Cargo which cross-linker to use for each foreign target. -RUN mkdir -p /root/.cargo && cat >> /root/.cargo/config.toml <<'EOF' +# Tell Cargo which cross-linker to use — only needed when cross-compiling. +RUN if [ "${BUILDPLATFORM}" != "${TARGETPLATFORM}" ]; then \ + mkdir -p /root/.cargo && cat >> /root/.cargo/config.toml <<'EOF' [target.aarch64-unknown-linux-gnu] linker = "aarch64-linux-gnu-gcc" @@ -35,6 +41,7 @@ linker = "arm-linux-gnueabihf-gcc" [target.arm-unknown-linux-gnueabi] linker = "arm-linux-gnueabi-gcc" EOF + fi WORKDIR /build