|
| 1 | +# This Dockerfile supports both native building and cross-compilation to x86-64, aarch64 and riscv64 |
| 2 | +FROM --platform=$BUILDPLATFORM ubuntu:22.04 |
| 3 | + |
| 4 | +ARG RUSTC_VERSION=nightly-2024-10-22 |
| 5 | +ARG PROFILE=production |
| 6 | +ARG RUSTFLAGS |
| 7 | +# Incremental compilation here isn't helpful |
| 8 | +ENV CARGO_INCREMENTAL=0 |
| 9 | +ENV PKG_CONFIG_ALLOW_CROSS=true |
| 10 | + |
| 11 | +ARG BUILDARCH |
| 12 | +ARG TARGETARCH |
| 13 | + |
| 14 | +WORKDIR /code |
| 15 | + |
| 16 | +RUN \ |
| 17 | + apt-get update && \ |
| 18 | + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ |
| 19 | + ca-certificates \ |
| 20 | + protobuf-compiler \ |
| 21 | + curl \ |
| 22 | + git \ |
| 23 | + llvm \ |
| 24 | + clang \ |
| 25 | + automake \ |
| 26 | + libtool \ |
| 27 | + pkg-config \ |
| 28 | + make |
| 29 | + |
| 30 | +RUN \ |
| 31 | + if [ $BUILDARCH != "arm64" ] && [ $TARGETARCH = "arm64" ]; then \ |
| 32 | + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ |
| 33 | + g++-aarch64-linux-gnu \ |
| 34 | + gcc-aarch64-linux-gnu \ |
| 35 | + libc6-dev-arm64-cross \ |
| 36 | + ; fi |
| 37 | + |
| 38 | +RUN \ |
| 39 | + if [ $BUILDARCH != "riscv64" ] && [ $TARGETARCH = "riscv64" ]; then \ |
| 40 | + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ |
| 41 | + g++-riscv64-linux-gnu \ |
| 42 | + gcc-riscv64-linux-gnu \ |
| 43 | + libc6-dev-riscv64-cross \ |
| 44 | + ; fi |
| 45 | + |
| 46 | +RUN \ |
| 47 | + if [ $BUILDARCH != "amd64" ] && [ $TARGETARCH = "amd64" ]; then \ |
| 48 | + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ |
| 49 | + g++-x86-64-linux-gnu \ |
| 50 | + gcc-x86-64-linux-gnu \ |
| 51 | + libc6-dev-amd64-cross \ |
| 52 | + ; fi |
| 53 | + |
| 54 | +RUN \ |
| 55 | + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain $RUSTC_VERSION && \ |
| 56 | + /root/.cargo/bin/rustup target add wasm32-unknown-unknown |
| 57 | + |
| 58 | +COPY Cargo.lock /code/Cargo.lock |
| 59 | +COPY Cargo.toml /code/Cargo.toml |
| 60 | +COPY rust-toolchain.toml /code/rust-toolchain.toml |
| 61 | + |
| 62 | +COPY crates /code/crates |
| 63 | +COPY domains /code/domains |
| 64 | +COPY shared /code/shared |
| 65 | +COPY test /code/test |
| 66 | + |
| 67 | +# Up until this line all Rust images in this repo should be the same to share the same layers |
| 68 | + |
| 69 | +ARG TARGETVARIANT |
| 70 | + |
| 71 | +RUN \ |
| 72 | + if [ $BUILDARCH != "arm64" ] && [ $TARGETARCH = "arm64" ]; then \ |
| 73 | + export RUSTFLAGS="$RUSTFLAGS -C linker=aarch64-linux-gnu-gcc" \ |
| 74 | + ; fi && \ |
| 75 | + if [ $BUILDARCH != "riscv64" ] && [ $TARGETARCH = "riscv64" ]; then \ |
| 76 | + export RUSTFLAGS="$RUSTFLAGS -C linker=riscv64-linux-gnu-gcc" \ |
| 77 | + ; fi && \ |
| 78 | + if [ $TARGETARCH = "amd64" ] && [ "$RUSTFLAGS" = "" ]; then \ |
| 79 | + case "$TARGETVARIANT" in \ |
| 80 | + # x86-64-v2 with AES-NI |
| 81 | + "v2") export RUSTFLAGS="-C target-cpu=x86-64-v2" ;; \ |
| 82 | + # x86-64-v3 with AES-NI |
| 83 | + "v3") export RUSTFLAGS="-C target-cpu=x86-64-v3 -C target-feature=+aes" ;; \ |
| 84 | + # v4 is compiled for Zen 4+ |
| 85 | + "v4") export RUSTFLAGS="-C target-cpu=znver4" ;; \ |
| 86 | + # Default build is for Skylake |
| 87 | + *) export RUSTFLAGS="-C target-cpu=skylake" ;; \ |
| 88 | + esac \ |
| 89 | + ; fi && \ |
| 90 | + if [ $BUILDARCH != "amd64" ] && [ $TARGETARCH = "amd64" ]; then \ |
| 91 | + export RUSTFLAGS="$RUSTFLAGS -C linker=x86_64-linux-gnu-gcc" \ |
| 92 | + ; fi && \ |
| 93 | + RUSTC_TARGET_ARCH=$(echo $TARGETARCH | sed "s/amd64/x86_64/g" | sed "s/arm64/aarch64/g" | sed "s/riscv64/riscv64gc/g") && \ |
| 94 | + /root/.cargo/bin/cargo -Zgitoxide -Zgit build \ |
| 95 | + --locked \ |
| 96 | + -Z build-std \ |
| 97 | + --profile $PROFILE \ |
| 98 | + --bin subspace-gateway \ |
| 99 | + --target $RUSTC_TARGET_ARCH-unknown-linux-gnu && \ |
| 100 | + mv target/*/*/subspace-gateway subspace-gateway && \ |
| 101 | + rm -rf target |
| 102 | + |
| 103 | +FROM ubuntu:22.04 |
| 104 | + |
| 105 | +COPY --from=0 /code/subspace-gateway /subspace-gateway |
| 106 | + |
| 107 | +USER nobody:nogroup |
| 108 | + |
| 109 | +ENTRYPOINT ["/subspace-gateway"] |
0 commit comments