Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with cargo-zigbuild #135

Open
ghost opened this issue Jun 7, 2022 · 2 comments
Open

Compatibility with cargo-zigbuild #135

ghost opened this issue Jun 7, 2022 · 2 comments
Labels

Comments

@ghost
Copy link

ghost commented Jun 7, 2022

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?

@LukeMathWalker
Copy link
Owner

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

@ghost
Copy link
Author

ghost commented Jul 18, 2022

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"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant