Add cross platform builds

This commit is contained in:
Andras Schmelczer 2025-08-28 21:52:43 +01:00
parent aada6a3b87
commit 88c9364448
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 102 additions and 13 deletions

View file

@ -1,18 +1,33 @@
FROM rust:1.89-alpine AS builder
FROM rust:1.89-slim-trixie AS builder
ARG TARGETPLATFORM
WORKDIR /usr/src/backend
# Install build dependencies for Alpine
RUN apk add --no-cache musl-dev pkgconf openssl-dev perl make
# Set RUST_TARGET based on platform and create sourceable script
RUN case "$TARGETPLATFORM" in \
"linux/amd64") echo 'export RUST_TARGET="x86_64-unknown-linux-musl"' > /tmp/rust_env ;; \
"linux/arm64") echo 'export RUST_TARGET="aarch64-unknown-linux-musl"' > /tmp/rust_env ;; \
"linux/arm/v7") echo 'export RUST_TARGET="armv7-unknown-linux-musleabihf"' > /tmp/rust_env ;; \
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
esac
# Install sqlx-cli
RUN cargo install sqlx-cli --features=openssl-vendored
# Install musl tools, cross-compilation toolchains, and OpenSSL dev packages
RUN . /tmp/rust_env && \
apt update && \
apt install -y musl-tools libssl-dev pkg-config \
gcc-aarch64-linux-gnu \
gcc-arm-linux-gnueabi &&\
rustup target add $RUST_TARGET && \
cargo install sqlx-cli
# Setup database and build
# Build application
COPY . .
RUN sqlx database create --database-url sqlite://db.sqlite3 && \
RUN . /tmp/rust_env && \
export CFLAGS="-Wno-stringop-overread" && \
sqlx database create --database-url sqlite://db.sqlite3 && \
sqlx migrate run --source src/app_state/database/migrations --database-url sqlite://db.sqlite3 && \
cargo build --release
cargo build --release --target $RUST_TARGET
# Runtime image
FROM alpine:3.22.0
@ -21,7 +36,7 @@ LABEL org.opencontainers.image.authors="andras@schmelczer.dev"
RUN apk add --no-cache curl ca-certificates
COPY --from=builder /usr/src/backend/target/release/sync_server /app/sync_server
COPY --from=builder /usr/src/backend/target/*/release/sync_server /app/sync_server
VOLUME /data
EXPOSE 3000/tcp
@ -30,4 +45,4 @@ WORKDIR /data
HEALTHCHECK --interval=30s --timeout=5s \
CMD curl -f http://localhost:3000/vaults/fake/ping || exit 1
ENTRYPOINT ["/app/sync_server"]
ENTRYPOINT ["/app/sync_server"]

View file

@ -1,4 +1,9 @@
[toolchain]
channel = "1.89.0"
targets = [ "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl" ]
targets = [
"x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-gnu",
"x86_64-pc-windows-gnu",
]
profile = "default"