From de4d8d9e8e6182bfa71a60973357c3345e9117db Mon Sep 17 00:00:00 2001 From: messense Date: Sat, 24 Sep 2022 14:32:10 +0800 Subject: [PATCH 1/2] Cross compile maturin in Docker multi-platform build --- Dockerfile | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index fde1983c8..a9c397f3d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,28 @@ +# x86_64 base FROM quay.io/pypa/manylinux2014_x86_64 as base-amd64 +# x86_64 builder +FROM --platform=$BUILDPLATFORM quay.io/pypa/manylinux2014_x86_64 as builder-amd64 +ENV CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu +# aarch64 base FROM quay.io/pypa/manylinux2014_aarch64 as base-arm64 +# aarch64 cross compile builder +FROM --platform=$BUILDPLATFORM quay.io/pypa/manylinux2014_x86_64 as builder-arm64 +RUN yum makecache && yum install -y gcc-aarch64-linux-gnu gcc-c++-aarch64-linux-gnu glibc-headers +ENV CARGO_BUILD_TARGET=aarch64-unknown-linux-gnu \ + CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \ + CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \ + CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ ARG TARGETARCH -FROM base-$TARGETARCH as builder +FROM builder-$TARGETARCH as builder ENV PATH /root/.cargo/bin:$PATH # Use an explicit version to actually install the version we require instead of using the cache # It would be even cooler to invalidate the cache depending on when the official rust image changes, # but I don't know how to do that -RUN curl --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain 1.64.0 -y +RUN curl --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain 1.64.0 -y --target $CARGO_BUILD_TARGET # Compile dependencies only for build caching ADD Cargo.toml /maturin/Cargo.toml @@ -21,7 +33,7 @@ RUN --mount=type=cache,target=/root/.cargo/git \ mkdir /maturin/src && \ touch /maturin/src/lib.rs && \ echo 'fn main() { println!("Dummy") }' > /maturin/src/main.rs && \ - cargo rustc --bin maturin --manifest-path /maturin/Cargo.toml --release --features password-storage -- -C link-arg=-s + cargo rustc --target $CARGO_BUILD_TARGET --bin maturin --manifest-path /maturin/Cargo.toml --release --features password-storage -- -C link-arg=-s ADD . /maturin/ @@ -31,8 +43,8 @@ RUN touch /maturin/src/lib.rs /maturin/src/main.rs RUN --mount=type=cache,target=/root/.cargo/git \ --mount=type=cache,target=/root/.cargo/registry \ --mount=type=cache,target=/maturin/target,sharing=locked \ - cargo rustc --bin maturin --manifest-path /maturin/Cargo.toml --release --features password-storage -- -C link-arg=-s \ - && mv /maturin/target/release/maturin /usr/bin/maturin + cargo rustc --target $CARGO_BUILD_TARGET --bin maturin --manifest-path /maturin/Cargo.toml --release --features password-storage -- -C link-arg=-s \ + && mv /maturin/target/$CARGO_BUILD_TARGET/release/maturin /usr/bin/maturin FROM base-$TARGETARCH From 2baa2e52050f9ec48953ae62b9ade3fb2f5dd6f9 Mon Sep 17 00:00:00 2001 From: messense Date: Sat, 24 Sep 2022 15:16:31 +0800 Subject: [PATCH 2/2] Switch to static linked maturin in docker image --- Dockerfile | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index a9c397f3d..1bce514d2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,18 @@ # x86_64 base FROM quay.io/pypa/manylinux2014_x86_64 as base-amd64 # x86_64 builder -FROM --platform=$BUILDPLATFORM quay.io/pypa/manylinux2014_x86_64 as builder-amd64 -ENV CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu +FROM --platform=$BUILDPLATFORM messense/rust-musl-cross:x86_64-musl as builder-amd64 # aarch64 base FROM quay.io/pypa/manylinux2014_aarch64 as base-arm64 # aarch64 cross compile builder -FROM --platform=$BUILDPLATFORM quay.io/pypa/manylinux2014_x86_64 as builder-arm64 -RUN yum makecache && yum install -y gcc-aarch64-linux-gnu gcc-c++-aarch64-linux-gnu glibc-headers -ENV CARGO_BUILD_TARGET=aarch64-unknown-linux-gnu \ - CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \ - CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \ - CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ +FROM --platform=$BUILDPLATFORM messense/rust-musl-cross:aarch64-musl as builder-arm64 ARG TARGETARCH FROM builder-$TARGETARCH as builder ENV PATH /root/.cargo/bin:$PATH -# Use an explicit version to actually install the version we require instead of using the cache -# It would be even cooler to invalidate the cache depending on when the official rust image changes, -# but I don't know how to do that -RUN curl --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain 1.64.0 -y --target $CARGO_BUILD_TARGET - # Compile dependencies only for build caching ADD Cargo.toml /maturin/Cargo.toml ADD Cargo.lock /maturin/Cargo.lock