diff --git a/Dockerfile b/Dockerfile index 0fb05b2..0b45e55 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,33 +1,33 @@ -FROM rust:1-bullseye AS chef -RUN cargo install cargo-chef -WORKDIR /app +FROM --platform=${BUILDPLATFORM:-linux/amd64} tonistiigi/xx AS xx -FROM chef AS planner -COPY . . -RUN cargo chef prepare --recipe-path recipe.json +FROM --platform=${BUILDPLATFORM:-linux/amd64} rust:1-bullseye as builder -FROM chef AS builder +WORKDIR /app -RUN apt-get update && apt-get install -y build-essential cmake clang \ - && rm -rf /var/lib/apt/lists/* +COPY --from=xx / / -COPY --from=planner /app/recipe.json recipe.json +# install native dependencies first so we can cache them +RUN apt-get update && \ + apt-get install -y build-essential cmake clang llvm g++ -# Build dependencies - this is the caching Docker layer! -RUN cargo chef cook --release --recipe-path recipe.json +ARG TARGETPLATFORM +ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64} + +RUN xx-apt-get install -y xx-cxx-essentials g++ -# Build application COPY . . -RUN cargo build --release --bin aero2solver -FROM debian:bullseye-slim +RUN ./build.sh -RUN apt-get update && apt-get install -y libgomp1 \ - && rm -rf /var/lib/apt/lists/* +FROM debian:bullseye-slim AS runtime WORKDIR /app +RUN apt-get update && \ + apt-get install -y libgomp1 && \ + rm -rf /var/lib/apt/lists/* + COPY ./model/ ./model/ -COPY --from=builder /app/target/release/aero2solver ./aero2solver +COPY --from=builder /app/target/aero2solver ./aero2solver -ENTRYPOINT [ "/app/aero2solver" ] +ENTRYPOINT [ "/app/aero2solver" ] \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..50b1fb8 --- /dev/null +++ b/build.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +# set -x +set -e + +# shellcheck disable=SC1091 +. xx-cargo --setup-target-triple +RUST_TARGET_TRIPLE="$(xx-cargo --print-target-triple)" + +if ! xx-info is-cross; then + # remove these clang declarations to avoid linking errors + unset "CARGO_TARGET_$(echo "$RUST_TARGET_TRIPLE" | tr '[:lower:]' '[:upper:]' | tr - _)_LINKER"; + unset "CC_$(echo "$RUST_TARGET_TRIPLE" | tr - _)" +fi + +cargo build --release --bin aero2solver --target="$RUST_TARGET_TRIPLE" +xx-verify "./target/$RUST_TARGET_TRIPLE/release/aero2solver" +cp "./target/$RUST_TARGET_TRIPLE/release/aero2solver" "./target/aero2solver" \ No newline at end of file