@@ -5,15 +5,16 @@ RUN apt-get update \
55 && apt-get install -y ca-certificates wget \
66 && rm -rf /var/lib/apt/lists/*
77
8- FROM --platform=$BUILDPLATFORM rust:1.91 AS prepare
8+ FROM --platform=$BUILDPLATFORM rust:1.91-bookworm AS prepare
99
1010ARG RUNTIME=deno
1111
1212RUN apt-get update && apt-get install -y \
1313 gcc-aarch64-linux-gnu \
1414 gcc-x86-64-linux-gnu \
1515 libc6-dev-amd64-cross \
16- libc6-dev-arm64-cross
16+ libc6-dev-arm64-cross \
17+ qemu-user-static
1718
1819ENV CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
1920ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
@@ -32,11 +33,10 @@ WORKDIR /build
3233COPY . .
3334RUN cargo chef prepare --recipe-path recipe.json
3435
35- FROM prepare AS builder
36+ FROM prepare AS platform
3637
3738ARG RUNTIME=deno
38-
39- RUN mkdir -p /build
39+ ARG TARGETPLATFORM
4040
4141ENV RUST_BACKTRACE=1
4242ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
@@ -45,26 +45,9 @@ WORKDIR /build
4545
4646RUN touch $RUNTIME_SNAPSHOT_PATH
4747
48+ # Copy recipe from planner
4849COPY --from=planner /build/recipe.json recipe.json
4950
50- # Build dependencies - this is the caching Docker layer!
51- RUN --mount=type=cache,sharing=locked,target=$CARGO_HOME/git \
52- --mount=type=cache,sharing=locked,target=$CARGO_HOME/registry \
53- --mount=type=cache,sharing=locked,target=/build/target \
54- cargo chef cook --release --features=$RUNTIME --recipe-path recipe.json
55-
56- # Build application
57- COPY . /build
58-
59- # Note: We do NOT generate the snapshot here anymore because it would be
60- # generated for the build platform (e.g. x86_64), but we might need it for
61- # the target platform (e.g. aarch64). V8 snapshots are architecture-dependent.
62-
63- FROM builder AS platform
64-
65- ARG RUNTIME=deno
66- ARG TARGETPLATFORM
67-
6851RUN echo "Building for $TARGETPLATFORM with runtime $RUNTIME"
6952
7053# Build dependencies for target platform
@@ -77,9 +60,12 @@ RUN --mount=type=cache,id=cargo-$TARGETPLATFORM-$RUNTIME,sharing=locked,target=$
7760 *) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
7861 esac
7962
63+ # Copy source code AFTER cooking dependencies
64+ COPY . /build
65+
8066# Build application for target platform
8167# 1. Build the snapshot generator for the TARGET platform
82- # 2. Run it (via QEMU emulation if needed ) to generate the snapshot.bin
68+ # 2. Run it (via QEMU emulation explicitly ) to generate the snapshot.bin
8369# 3. Build the final runner which embeds the snapshot.bin
8470RUN --mount=type=cache,id=cargo-$TARGETPLATFORM-$RUNTIME,sharing=locked,target=$CARGO_HOME/git \
8571 --mount=type=cache,id=cargo-$TARGETPLATFORM-$RUNTIME,sharing=locked,target=$CARGO_HOME/registry \
@@ -88,16 +74,20 @@ RUN --mount=type=cache,id=cargo-$TARGETPLATFORM-$RUNTIME,sharing=locked,target=$
8874 "linux/arm64") \
8975 # Build snapshot generator for target \
9076 cargo build --release --features=$RUNTIME --bin snapshot --target aarch64-unknown-linux-gnu && \
91- # Generate snapshot (runs via QEMU ) \
92- /build/target/aarch64-unknown-linux-gnu/release/snapshot && \
77+ # Generate snapshot (explicitly using qemu-aarch64-static with cross libs ) \
78+ QEMU_LD_PREFIX=/usr/aarch64-linux-gnu qemu-aarch64-static /build/target/aarch64-unknown-linux-gnu/release/snapshot && \
9379 # Build runner \
9480 cargo build --release --features=$RUNTIME --target aarch64-unknown-linux-gnu && \
9581 mv /build/target/aarch64-unknown-linux-gnu/release/openworkers-runner /build/output ;; \
9682 "linux/amd64") \
9783 # Build snapshot generator for target \
9884 cargo build --release --features=$RUNTIME --bin snapshot --target x86_64-unknown-linux-gnu && \
99- # Generate snapshot \
100- /build/target/x86_64-unknown-linux-gnu/release/snapshot && \
85+ # Generate snapshot (direct execution on x86_64, qemu on other archs) \
86+ if [ "$(uname -m)" = "x86_64" ]; then \
87+ /build/target/x86_64-unknown-linux-gnu/release/snapshot; \
88+ else \
89+ QEMU_LD_PREFIX=/usr/x86_64-linux-gnu qemu-x86_64-static /build/target/x86_64-unknown-linux-gnu/release/snapshot; \
90+ fi && \
10191 # Build runner \
10292 cargo build --release --features=$RUNTIME --target x86_64-unknown-linux-gnu && \
10393 mv /build/target/x86_64-unknown-linux-gnu/release/openworkers-runner /build/output ;; \
0 commit comments