Skip to content

Instantly share code, notes, and snippets.

@tuna2134
Last active February 3, 2024 18:12
Show Gist options
  • Select an option

  • Save tuna2134/b7ab4f3dd5cc29ffab47b55c72fae4ea to your computer and use it in GitHub Desktop.

Select an option

Save tuna2134/b7ab4f3dd5cc29ffab47b55c72fae4ea to your computer and use it in GitHub Desktop.
FROM rust:slim AS builder
WORKDIR /src/builder
RUN apt-get update && apt-get install -y musl-tools
ARG TARGETARCH
RUN if [ $TARGETARCH = "amd64" ]; then \
echo "x86_64" > /tmp/arch; \
elif [ $TARGETARCH = "arm64" ]; then \
echo "aarch64" > /tmp/arch; \
apt-get install -y clang llvm; \
else \
echo "Unsupported platform"; \
exit 1; \
fi
RUN rustup target add $(cat /tmp/arch)-unknown-linux-musl
COPY . .
RUN --mount=type=cache,target=/src/builder/target/ bash build.sh
FROM scratch
WORKDIR /src/app
COPY --from=builder /tmp/hello .
CMD ["./hello"]
#!/bin/bash
if [ $(cat /tmp/arch) = "aarch64" ]; then
export CC_aarch64_unknown_linux_musl=clang
export AR_aarch64_unknown_linux_musl=llvm-ar
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld"
else
echo "amd64 mode"
fi
cargo build --target=$(cat /tmp/arch)-unknown-linux-musl --release
cp target/$(cat /tmp/arch)-unknown-linux-musl/release/hello /tmp/hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment