Skip to content

Commit

Permalink
clean up dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Erk- committed Jun 15, 2024
1 parent 03c987e commit 6fcff98
Showing 1 changed file with 64 additions and 44 deletions.
108 changes: 64 additions & 44 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,50 @@ ARG RUST_TARGET
ARG MUSL_TARGET
ARG FEATURES

RUN apk upgrade && \
apk add curl gcc musl-dev && \
RUN <<EOT bash
set -ex
apk upgrade
apk add curl gcc musl-dev
curl -sSf https://sh.rustup.rs | sh -s -- --profile minimal --default-toolchain nightly --component rust-src -y
EOT

RUN source $HOME/.cargo/env && \
mkdir -p /app/.cargo && \
if [ "$RUST_TARGET" != $(rustup target list --installed) ]; then \
rustup target add $RUST_TARGET && \
curl -L "https://musl.cc/$MUSL_TARGET-cross.tgz" -o /toolchain.tgz && \
tar xf toolchain.tgz && \
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-gcc" "/usr/bin/$MUSL_TARGET-gcc" && \
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-ld" "/usr/bin/$MUSL_TARGET-ld" && \
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-strip" "/usr/bin/actual-strip" && \
GCC_VERSION=$($MUSL_TARGET-gcc --version | grep gcc | awk '{print $3}') && \
echo -e "\
[build]\n\
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\
[target.$RUST_TARGET]\n\
linker = \"$MUSL_TARGET-gcc\"\n\
[unstable]\n\
build-std = [\"std\", \"panic_abort\"]\n\
" > /app/.cargo/config; \
else \
echo "skipping toolchain as we are native" && \
echo -e "\
[build]\n\
rustflags = [\"-L\", \"native=/usr/lib\"]\n\
[unstable]\n\
build-std = [\"std\", \"panic_abort\"]\n\
" > /app/.cargo/config && \
ln -s /usr/bin/strip /usr/bin/actual-strip; \
fi
RUN <<-EOT bash
set -ex

source $HOME/.cargo/env
mkdir -p /app/.cargo
if [ "$RUST_TARGET" != $(rustup target list --installed) ]; then
rustup target add $RUST_TARGET
curl -L "https://musl.cc/$MUSL_TARGET-cross.tgz" -o /toolchain.tgz
tar xf toolchain.tgz
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-gcc" "/usr/bin/$MUSL_TARGET-gcc"
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-ld" "/usr/bin/$MUSL_TARGET-ld"
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-strip" "/usr/bin/actual-strip"
GCC_VERSION=$($MUSL_TARGET-gcc --version | grep gcc | awk '{print $3}')
echo -e "\
[build]\n\
rustflags = [\"-L\", \"native=/$MUSL_TARGET-cross/$MUSL_TARGET/lib\", \
\"-L\", \"native=/$MUSL_TARGET-cross/lib/gcc/$MUSL_TARGET/$GCC_VERSION/\", \
\"-l\", \"static=gcc\", \
\"-C\", \"link-self-contained=y\", \
\"-C\", \"linker-flavor=gcc\"\
]\n\
[target.$RUST_TARGET]\n\
linker = \"$MUSL_TARGET-gcc\"\n\
[unstable]\n\
build-std = [\"std\", \"panic_abort\"]\n\
" > /app/.cargo/config;
else
echo "skipping toolchain as we are native"
echo -e "\
[build]\n\
rustflags = [\"-L\", \"native=/usr/lib\"]\n\
[unstable]\n\
build-std = [\"std\", \"panic_abort\"]\n\
" > /app/.cargo/config
ln -s /usr/bin/strip /usr/bin/actual-strip;
fi
EOT

WORKDIR /app

Expand All @@ -50,16 +62,20 @@ COPY ./Cargo.toml ./Cargo.toml

# We need a source directory so that it builds the dependencies and an empty
# binary.
RUN mkdir src/
RUN echo 'fn main() {}' > ./src/main.rs
RUN source $HOME/.cargo/env && \
if [ "$FEATURES" == "" ]; then \
RUN <<-EOT bash
set -ex

mkdir src/
echo 'fn main() {}' -> ./src/main.rs
source $HOME/.cargo/env
if [ "$FEATURES" == "" ]; then
cargo build --release \
--target="$RUST_TARGET"; \
else \
--target="$RUST_TARGET";
else
cargo build --release \
--target="$RUST_TARGET" --features="$FEATURES"; \
--target="$RUST_TARGET" --features="$FEATURES";
fi
EOT

# Now, delete the fake source and copy in the actual source. This allows us to
# have a previous compilation step for compiling the dependencies, while being
Expand All @@ -74,16 +90,20 @@ RUN source $HOME/.cargo/env && \
RUN rm -f target/$RUST_TARGET/release/deps/twilight_http_proxy*
COPY ./src ./src

RUN source $HOME/.cargo/env && \
if [ "$FEATURES" == "" ]; then \
RUN <<-EOT bash
set -ex

source $HOME/.cargo/env
if [ "$FEATURES" == "" ]; then
cargo build --release \
--target="$RUST_TARGET"; \
else \
--target="$RUST_TARGET"
else
cargo build --release \
--target="$RUST_TARGET" --features="$FEATURES"; \
fi && \
cp target/$RUST_TARGET/release/twilight-http-proxy /twilight-http-proxy && \
--target="$RUST_TARGET" --features="$FEATURES"
fi
cp target/$RUST_TARGET/release/twilight-http-proxy /twilight-http-proxy
actual-strip /twilight-http-proxy
EOT

FROM scratch

Expand Down

0 comments on commit 6fcff98

Please sign in to comment.