1- # Rust syntax target, either x86_64-unknown-linux-musl, aarch64-unknown-linux-musl, arm-unknown-linux-musleabi etc.
2- ARG RUST_TARGET="x86_64-unknown-linux-musl"
3- # Musl target, either x86_64-linux-musl, aarch64-linux-musl, arm-linux-musleabi, etc.
4- ARG MUSL_TARGET="x86_64-linux-musl"
1+ # The crate features to build this with
2+ ARG FEATURES=""
53
6- FROM --platform=$BUILDPLATFORM docker.io/alpine:latest as build
7- ARG RUST_TARGET
8- ARG MUSL_TARGET
4+ FROM docker.io/library/alpine:latest as build
5+ ARG FEATURES
96
107RUN apk upgrade && \
118 apk add curl gcc musl-dev && \
129 curl -sSf https://sh.rustup.rs | sh -s -- --profile minimal --default-toolchain nightly --component rust-src -y
1310
14- RUN source $HOME/.cargo/env && \
15- mkdir -p /app/.cargo && \
16- if [ "$RUST_TARGET" != $(rustup target list --installed) ]; then \
17- rustup target add $RUST_TARGET && \
18- curl -L "https://musl.cc/$MUSL_TARGET-cross.tgz" -o /toolchain.tgz && \
19- tar xf toolchain.tgz && \
20- ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-gcc" "/usr/bin/$MUSL_TARGET-gcc" && \
21- ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-ld" "/usr/bin/$MUSL_TARGET-ld" && \
22- ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-strip" "/usr/bin/actual-strip" && \
23- GCC_VERSION=$($MUSL_TARGET-gcc --version | grep gcc | awk '{print $3}' ) && \
24- echo -e "\
25- [build]\n \
26- rustflags = [\" -L\" , \" native=/$MUSL_TARGET-cross/$MUSL_TARGET/lib\" , \" -L\" , \" native=/$MUSL_TARGET-cross/lib/gcc/$MUSL_TARGET/$GCC_VERSION/\" , \" -l\" , \" static=gcc\" , \" -Clink-self-contained=y\" , \" -Clinker-flavor=gcc\" ]\n \
27- [target.$RUST_TARGET]\n \
28- linker = \" $MUSL_TARGET-gcc\"\n \
29- [unstable]\n \
30- build-std = [\" std\" , \" panic_abort\" ]\n \
31- " > /app/.cargo/config; \
32- else \
33- echo "skipping toolchain as we are native" && \
34- echo -e "\
35- [build]\n \
36- rustflags = [\" -L\" , \" native=/usr/lib\" ]\n \
37- [unstable]\n \
38- build-std = [\" std\" , \" panic_abort\" ]\n \
39- " > /app/.cargo/config && \
40- ln -s /usr/bin/strip /usr/bin/actual-strip; \
41- fi
42-
4311WORKDIR /app
4412
4513COPY ./Cargo.lock ./Cargo.lock
@@ -50,8 +18,11 @@ COPY ./Cargo.toml ./Cargo.toml
5018RUN mkdir src/
5119RUN echo 'fn main() {}' > ./src/main.rs
5220RUN source $HOME/.cargo/env && \
53- cargo build --release \
54- --target="$RUST_TARGET"
21+ cargo build \
22+ --release \
23+ -Zbuild-std=std,panic_abort \
24+ --target="$(uname -m)-unknown-linux-musl" \
25+ --features="$FEATURES"
5526
5627# Now, delete the fake source and copy in the actual source. This allows us to
5728# have a previous compilation step for compiling the dependencies, while being
@@ -63,19 +34,19 @@ RUN source $HOME/.cargo/env && \
6334# would have to be re-downloaded and re-compiled.
6435#
6536# Also, remove the artifacts of building the binaries.
66- RUN rm -f target/$RUST_TARGET /release/deps/twilight_gateway_queue*
37+ RUN rm -f target/$(uname -m)-unknown-linux-musl /release/deps/twilight_gateway_queue*
6738COPY ./src ./src
6839
6940RUN source $HOME/.cargo/env && \
70- cargo build --release \
71- --target="$RUST_TARGET" && \
72- cp target/$RUST_TARGET/release/twilight-gateway-queue /twilight-gateway-queue && \
73- actual-strip /twilight-gateway-queue
41+ cargo build \
42+ --release \
43+ -Zbuild-std=std,panic_abort \
44+ --target="$(uname -m)-unknown-linux-musl" --features="$FEATURES" && \
45+ cp target/$(uname -m)-unknown-linux-musl/release/twilight-gateway-queue /twilight-gateway-queue && \
46+ strip /twilight-gateway-queue
7447
7548FROM scratch
7649
77- # And now copy the binary over from the build container. The build container is
78- # based on a heavy image.
7950COPY --from=build /twilight-gateway-queue /twilight-gateway-queue
8051
8152CMD ["./twilight-gateway-queue" ]
0 commit comments