Skip to content

Commit 92a0c01

Browse files
authored
Merge pull request #6 from rucoder/rucoder/rust-self-cross
Enable cross-compilation for cargo plugins
2 parents 877f7cc + f04fce7 commit 92a0c01

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

Dockerfile

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +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
7-
RUN apk add musl-dev linux-headers make
8-
32+
RUN apk add musl-dev linux-headers make clang mold
33+
34+
# copy the cargo plugins from the tools stage
35+
COPY --from=tools /cargo-cross /usr/local/cargo
36+
# we define target specific rustc flags for cross-compilation
37+
ADD config.toml /usr/local/cargo/

config.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[target.aarch64-unknown-linux-musl]
2+
linker = "/usr/bin/clang"
3+
rustflags = [
4+
"-C",
5+
"link-arg=--ld-path=/usr/bin/mold",
6+
"-C",
7+
"link-arg=--target=aarch64-unknown-linux-musl",
8+
]
9+
10+
[target.x86_64-unknown-linux-musl]
11+
linker = "/usr/bin/clang"
12+
rustflags = [
13+
"-C",
14+
"link-arg=--ld-path=/usr/bin/mold",
15+
"-C",
16+
"link-arg=--target=x86_64-unknown-linux-musl",
17+
]
18+
19+
# FIXME: riscv64 is not yet available in rust:1.80.1-alpine3.20
20+
# but both clang and mold support it. Alos musl-dev is available in Alpine 3.20
21+
# [target.riscv64gc-unknown-linux-gnu]
22+
# linker = "/usr/bin/clang"
23+
# rustflags = [
24+
# "-C",
25+
# "link-arg=--ld-path=/usr/bin/mold",
26+
# "-C",
27+
# "link-arg=--target=riscv64-unknown-linux-musl",
28+
# ]

0 commit comments

Comments
 (0)