Skip to content

Commit

Permalink
Merge pull request #6 from rucoder/rucoder/rust-self-cross
Browse files Browse the repository at this point in the history
Enable cross-compilation for cargo plugins
  • Loading branch information
deitch authored Aug 18, 2024
2 parents 877f7cc + f04fce7 commit 92a0c01
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
ARG RUST_VERSION=1.80.1
FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine3.20 AS tools-host
ARG BUILDPLATFORM
ARG TARGETARCH

ENV TARGETS="x86_64-unknown-linux-musl aarch64-unknown-linux-musl x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu riscv64gc-unknown-linux-gnu"
RUN rustup target add ${TARGETS}
RUN apk add musl-dev linux-headers make clang mold

FROM tools-host AS target-amd64
ENV CARGO_BUILD_TARGET="x86_64-unknown-linux-musl"

FROM tools-host AS target-arm64
ENV CARGO_BUILD_TARGET="aarch64-unknown-linux-musl"

FROM tools-host AS target-riscv64
ENV CARGO_BUILD_TARGET="riscv64gc-unknown-linux-gnu"

FROM target-$TARGETARCH AS tools
RUN echo "Cargo target: $CARGO_BUILD_TARGET"

ADD config.toml /usr/local/cargo/
# CARGO_BUILD_TARGET is respected by cargo install and other cargo commands
RUN cargo install --root /cargo-cross [email protected] [email protected]


FROM rust:${RUST_VERSION}-alpine3.20
ENV TARGETS="x86_64-unknown-linux-musl aarch64-unknown-linux-musl x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu riscv64gc-unknown-linux-gnu"
RUN rustup target add ${TARGETS}

# needed for cargo-chef and cargo-sbom, as well as many other compilations
RUN apk add musl-dev linux-headers make
RUN cargo install [email protected] [email protected]
RUN apk add musl-dev linux-headers make clang mold

# copy the cargo plugins from the tools stage
COPY --from=tools /cargo-cross /usr/local/cargo
# we define target specific rustc flags for cross-compilation
ADD config.toml /usr/local/cargo/
28 changes: 28 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[target.aarch64-unknown-linux-musl]
linker = "/usr/bin/clang"
rustflags = [
"-C",
"link-arg=--ld-path=/usr/bin/mold",
"-C",
"link-arg=--target=aarch64-unknown-linux-musl",
]

[target.x86_64-unknown-linux-musl]
linker = "/usr/bin/clang"
rustflags = [
"-C",
"link-arg=--ld-path=/usr/bin/mold",
"-C",
"link-arg=--target=x86_64-unknown-linux-musl",
]

# FIXME: riscv64 is not yet available in rust:1.80.1-alpine3.20
# but both clang and mold support it. Alos musl-dev is available in Alpine 3.20
# [target.riscv64gc-unknown-linux-gnu]
# linker = "/usr/bin/clang"
# rustflags = [
# "-C",
# "link-arg=--ld-path=/usr/bin/mold",
# "-C",
# "link-arg=--target=riscv64-unknown-linux-musl",
# ]

0 comments on commit 92a0c01

Please sign in to comment.