Skip to content

Commit 020617b

Browse files
awbxrvagg
authored andcommitted
arm64-musl: set up rust cross-compilation for aarch64-musl
Following sxa's feedback that just adding `rust` and `cargo` apk packages is insufficient for the cross-compiled arm64-musl recipe. Alpine's `rust` package only ships the host (x86_64-musl) standard library, so building `cargo --target=aarch64-unknown-linux-musl` without a matching std artifact fails. Pull the prebuilt `aarch64-unknown-linux-musl` std archive from the upstream Rust dist server (version pinned to match the host toolchain, SHA256 verified against the publisher's signed manifest) and drop it into `/usr/lib/rustlib/`. Add a cargo config that points the cross-compile linker at the musl-cross-make-built `aarch64-linux-musl-gcc`. Approach based on nikeee's exploratory commit at nodejs@74ae38b Refs: nodejs#227 Refs: nodejs/build#4316
1 parent 7623492 commit 020617b

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

‎recipes/arm64-musl/Dockerfile‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ RUN apk add --no-cache \
2323
xz \
2424
patch \
2525
# rustc/cargo are needed for the Temporal API support in Node 26+
26-
# (build silently skips Temporal when they are not in PATH)
27-
rust \
28-
cargo \
26+
# (the build silently skips Temporal when they are not in PATH).
27+
# Pinned so the rust-std target download below matches.
28+
rust=1.87.0-r1 \
29+
cargo=1.87.0-r1 \
2930
# extra packages required for creating the musl based cross-compiler
3031
rsync make gcc g++ bzip2 git
3132

@@ -50,6 +51,32 @@ RUN RELEASE="3635262e4524c991552789af6f36211a335a77b3" && \
5051
cd / && \
5152
rm -rf /musl-cross-make-${RELEASE}/ ${RELEASE}.tar.gz
5253

54+
# Install the aarch64-unknown-linux-musl Rust std library for cross-compiling.
55+
# Alpine's `rust` package only ships the host (x86_64-musl) standard library;
56+
# building `cargo --target=aarch64-unknown-linux-musl` requires a matching
57+
# std artifact under `/usr/lib/rustlib/`. Pull the prebuilt std from the
58+
# upstream Rust dist server, matching the version of the host toolchain.
59+
# Hash verified against the publisher's signed sha256 manifest at
60+
# https://static.rust-lang.org/dist/rust-std-1.87.0-aarch64-unknown-linux-musl.tar.xz.sha256
61+
# Approach based on nikeee's commit in
62+
# https://github.com/nodejs/unofficial-builds/commit/74ae38b3117b8dc29f6f2f00e37205cbe447e167
63+
RUN RUST_STD_VERSION="1.87.0" && \
64+
RUST_STD_SHA256="15fd0652e2f3a8b678be8a83e396a1103fb1bb941afabe3fdaec706f3bdc85d0" && \
65+
wget -O /tmp/rust-std.tar.xz \
66+
"https://static.rust-lang.org/dist/rust-std-${RUST_STD_VERSION}-aarch64-unknown-linux-musl.tar.xz" && \
67+
echo "${RUST_STD_SHA256} /tmp/rust-std.tar.xz" | sha256sum -c - && \
68+
tar -xJf /tmp/rust-std.tar.xz -C /tmp && \
69+
cp -r /tmp/rust-std-${RUST_STD_VERSION}-aarch64-unknown-linux-musl/rust-std-aarch64-unknown-linux-musl/lib/rustlib/aarch64-unknown-linux-musl \
70+
/usr/lib/rustlib/ && \
71+
rm -rf /tmp/rust-std*
72+
73+
# Tell Cargo to use the musl-cross-make linker when targeting aarch64-musl.
74+
COPY <<-EOF /usr/local/cargo/config.toml
75+
[target.aarch64-unknown-linux-musl]
76+
linker = "/opt/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc"
77+
EOF
78+
ENV CARGO_HOME=/usr/local/cargo
79+
5380
COPY --chown=node:node run.sh /home/node/run.sh
5481

5582
VOLUME /home/node/.ccache

0 commit comments

Comments
 (0)