Skip to content

Commit f04fce7

Browse files
committed
Enable cross compilation for cargo plugins
Signed-off-by: Mikhail Malyshev <[email protected]>
1 parent 785f70b commit f04fce7

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Dockerfile

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
ARG RUST_VERSION=1.80.1
2+
FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine3.20 AS tools-host
3+
ARG BUILDPLATFORM
4+
ARG TARGETARCH
5+
6+
ENV TARGETS="x86_64-unknown-linux-musl aarch64-unknown-linux-musl x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu riscv64gc-unknown-linux-gnu"
7+
RUN rustup target add ${TARGETS}
8+
RUN apk add musl-dev linux-headers make clang mold
9+
10+
FROM tools-host AS target-amd64
11+
ENV CARGO_BUILD_TARGET="x86_64-unknown-linux-musl"
12+
13+
FROM tools-host AS target-arm64
14+
ENV CARGO_BUILD_TARGET="aarch64-unknown-linux-musl"
15+
16+
FROM tools-host AS target-riscv64
17+
ENV CARGO_BUILD_TARGET="riscv64gc-unknown-linux-gnu"
18+
19+
FROM target-$TARGETARCH AS tools
20+
RUN echo "Cargo target: $CARGO_BUILD_TARGET"
21+
22+
ADD config.toml /usr/local/cargo/
23+
# CARGO_BUILD_TARGET is respected by cargo install and other cargo commands
24+
RUN cargo install --root /cargo-cross [email protected] [email protected]
25+
26+
227
FROM rust:${RUST_VERSION}-alpine3.20
328
ENV TARGETS="x86_64-unknown-linux-musl aarch64-unknown-linux-musl x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu riscv64gc-unknown-linux-gnu"
429
RUN rustup target add ${TARGETS}
530

631
# needed for cargo-chef and cargo-sbom, as well as many other compilations
732
RUN apk add musl-dev linux-headers make clang mold
8-
RUN cargo install cargo-chef cargo-sbom
33+
34+
# copy the cargo plugins from the tools stage
35+
COPY --from=tools /cargo-cross /usr/local/cargo
936
# we define target specific rustc flags for cross-compilation
1037
ADD config.toml /usr/local/cargo/

0 commit comments

Comments
 (0)