cargo-chef icon indicating copy to clipboard operation
cargo-chef copied to clipboard

Compatibility with cargo-zigbuild

Open ghost opened this issue 3 years ago • 2 comments

Firstly thank you for this amazing project. I have used it for a long time and it's always worked well. It actually makes rust development in containers enjoyable. Recently I have had to build multiarch containers. I really like cargo-zigbuild

Is it possible to get this to work with cargo-chef?

ghost avatar Jun 07 '22 15:06 ghost

Hey! I have never used cargo-zigbuild. What would it entail to make it compatible?

LukeMathWalker avatar Jul 04 '22 15:07 LukeMathWalker

I'm not completely sure what is happening during the cargo chef cook phase. Is this interacting with cargo build or is more complicated than that? If it is it would be a case of using cargo zigbuild instead of cargo build if a certain argument was added similar to how it currently works with musl.

I have had quite a few issues with musl in the past and zigbuild seems to run better. Here is a dockerfile I have using cargo zigbuild. This is using docker cache mounts and works great locally but doesn't work nearly as well as cargo chef on CI.

I'm not really sure how to help with this but would be happy to if I can.

# Builder
FROM --platform=$BUILDPLATFORM rust:1.62.0 AS builder
ARG BUILDPLATFORM
ARG ZIGVERSION=0.9.1
ARG ZIGBUILDVERSION=0.10.2
RUN apt-get update && apt-get install -y \
    ca-certificates \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Zig
RUN if [ "${BUILDPLATFORM#*linux/}" = "amd64" ]; then \
    wget https://ziglang.org/download/${ZIGVERSION}/zig-linux-x86_64-${ZIGVERSION}.tar.xz && \
    tar -xf zig-linux-x86_64-${ZIGVERSION}.tar.xz && \
    rm -rf zig-linux-x86_64-${ZIGVERSION}.tar.xz && \
    ln -s /zig-linux-x86_64-${ZIGVERSION}/zig /usr/bin/; \
    else \
    wget https://ziglang.org/download/${ZIGVERSION}/zig-linux-aarch64-${ZIGVERSION}.tar.xz && \
    tar -xf zig-linux-aarch64-${ZIGVERSION}.tar.xz && \
    rm -rf zig-linux-aarch64-${ZIGVERSION}.tar.xz && \
    ln -s /zig-linux-aarch64-${ZIGVERSION}/zig /usr/bin/; \
    fi

# Cargo zigbuild
RUN cargo install cargo-zigbuild --version ${ZIGBUILDVERSION}
RUN rustup target add aarch64-unknown-linux-gnu
RUN rustup target add x86_64-unknown-linux-gnu

# Build
WORKDIR /app
COPY . .
RUN --mount=type=cache,target=/root/.cargo \
    --mount=type=cache,target=/app/target \
    cargo zigbuild --release --target aarch64-unknown-linux-gnu && \
    cargo zigbuild --release --target x86_64-unknown-linux-gnu && \
    mkdir /app/linux && \
    cp target/aarch64-unknown-linux-gnu/release/app /app/linux/arm64 && \
    cp target/x86_64-unknown-linux-gnu/release/app /app/linux/amd64

# Final
FROM debian:11-slim AS runtime
ARG TARGETPLATFORM

COPY --from=builder /app/${TARGETPLATFORM} /app/app

ENTRYPOINT ["/app/app"]

crc-sstubbs avatar Jul 18 '22 12:07 crc-sstubbs