Make the Docker image cross-compile without QEMU emulation (#129)

* Bump all dependencies & disable the pyo3 feature by default.
* Make the Docker image cross-compile without QEMU emulation
* Add a build cache on the Docker image
This commit is contained in:
Quentin Gliech
2023-09-28 20:42:26 +02:00
committed by GitHub
parent 71f24cf2b9
commit 4b9f2e2d64
8 changed files with 459 additions and 275 deletions

View File

@@ -16,19 +16,9 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v2
with:
platforms: arm64
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Inspect builder
run: docker buildx inspect
- name: Log in to DockerHub
uses: docker/login-action@v2
with:
@@ -64,9 +54,5 @@ jobs:
labels: "gitsha1=${{ github.sha }}"
tags: "${{ steps.set-tag.outputs.tags }}"
platforms: linux/amd64,linux/arm64
# arm64 builds OOM without the git fetch setting. c.f.
# https://github.com/rust-lang/cargo/issues/10583
build-args: |
CARGO_NET_GIT_FETCH_WITH_CLI=true
BUILD_PROFILE=release
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max

579
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -13,16 +13,16 @@ name = "synapse_compress_state"
required-features = ["clap"]
[dependencies]
indicatif = "0.17.0"
openssl = "0.10.55"
postgres = "0.19.0"
indicatif = "0.17.6"
openssl = "0.10.57"
postgres = "0.19.7"
postgres-openssl = "0.5.0"
rand = "0.8.0"
rayon = "1.3.0"
string_cache = "0.8.0"
rand = "0.8.5"
rayon = "1.7.0"
string_cache = "0.8.7"
env_logger = "0.10.0"
log = "0.4.14"
log-panics = "2.0.0"
log = "0.4.20"
log-panics = "2.1.0"
[dependencies.state-map]
git = "https://github.com/matrix-org/rust-matrix-state-map"
@@ -32,25 +32,25 @@ git = "https://github.com/matrix-org/rust-matrix-state-map"
crate-type = ["cdylib", "rlib"]
[dependencies.clap]
version = "4.0.15"
version = "4.4.2"
features = ["cargo"]
optional = true
[dependencies.pyo3]
version = "0.19.0"
version = "0.19.2"
features = ["extension-module"]
optional = true
[dependencies.pyo3-log]
version = "0.8.2"
version = "0.8.3"
optional = true
[dependencies.tikv-jemallocator]
version = "0.5.0"
version = "0.5.4"
optional = true
[features]
default = ["clap", "jemalloc", "pyo3"]
default = ["clap", "jemalloc"]
jemalloc = ["tikv-jemallocator"]
no-progress-bars = []
pyo3 = ["dep:pyo3", "dep:pyo3-log"]

View File

@@ -1,27 +1,54 @@
FROM docker.io/rust:alpine AS builder
# This uses the multi-stage build feature of Docker to build the binaries for multiple architectures without QEMU.
# The first stage is responsible for building binaries for all the supported architectures (amd64 and arm64), and the
# second stage only copies the binaries for the target architecture.
# We leverage Zig and cargo-zigbuild for providing a cross-compilation-capable C compiler and linker.
RUN apk add python3 musl-dev pkgconfig openssl-dev make git
ARG RUSTC_VERSION=1.72.0
ARG ZIG_VERSION=0.11.0
ARG CARGO_ZIGBUILD_VERSION=0.17.1
FROM --platform=${BUILDPLATFORM} docker.io/rust:${RUSTC_VERSION} AS builder
# Install cargo-zigbuild for cross-compilation
ARG CARGO_ZIGBUILD_VERSION
RUN cargo install --locked cargo-zigbuild@=${CARGO_ZIGBUILD_VERSION}
# Download zig compiler for cross-compilation
ARG ZIG_VERSION
RUN curl -L "https://ziglang.org/download/${ZIG_VERSION}/zig-linux-$(uname -m)-${ZIG_VERSION}.tar.xz" | tar -J -x -C /usr/local && \
ln -s "/usr/local/zig-linux-$(uname -m)-${ZIG_VERSION}/zig" /usr/local/bin/zig
# Install all cross-compilation targets
ARG RUSTC_VERSION
RUN rustup target add \
--toolchain "${RUSTC_VERSION}" \
x86_64-unknown-linux-musl \
aarch64-unknown-linux-musl
WORKDIR /opt/synapse-compressor/
COPY . .
ENV RUSTFLAGS="-C target-feature=-crt-static"
# Build for all targets
RUN cargo zigbuild \
--release \
--workspace \
--bins \
--features "openssl/vendored" \
--target aarch64-unknown-linux-musl \
--target x86_64-unknown-linux-musl
# arm64 builds consume a lot of memory if `CARGO_NET_GIT_FETCH_WITH_CLI` is not
# set to true, so we expose it as a build-arg.
ARG CARGO_NET_GIT_FETCH_WITH_CLI=false
ENV CARGO_NET_GIT_FETCH_WITH_CLI=$CARGO_NET_GIT_FETCH_WITH_CLI
ARG BUILD_PROFILE=dev
# Move the binaries in a separate folder per architecture, so we can copy them using the TARGETARCH build arg
RUN mkdir -p /opt/binaries/amd64 /opt/binaries/arm64
RUN mv target/x86_64-unknown-linux-musl/release/synapse_compress_state \
target/x86_64-unknown-linux-musl/release/synapse_auto_compressor \
/opt/binaries/amd64
RUN mv target/aarch64-unknown-linux-musl/release/synapse_compress_state \
target/aarch64-unknown-linux-musl/release/synapse_auto_compressor \
/opt/binaries/arm64
RUN cargo build --profile=$BUILD_PROFILE
FROM --platform=${TARGETPLATFORM} docker.io/alpine
WORKDIR /opt/synapse-compressor/synapse_auto_compressor/
ARG TARGETARCH
RUN cargo build
FROM docker.io/alpine
RUN apk add --no-cache libgcc
COPY --from=builder /opt/synapse-compressor/target/*/synapse_compress_state /usr/local/bin/synapse_compress_state
COPY --from=builder /opt/synapse-compressor/target/*/synapse_auto_compressor /usr/local/bin/synapse_auto_compressor
COPY --from=builder /opt/binaries/${TARGETARCH}/synapse_compress_state /usr/local/bin/synapse_compress_state
COPY --from=builder /opt/binaries/${TARGETARCH}/synapse_auto_compressor /usr/local/bin/synapse_auto_compressor

View File

@@ -6,16 +6,16 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
string_cache = "0.8.0"
string_cache = "0.8.7"
serial_test = "2.0.0"
openssl = "0.10.55"
postgres = "0.19.0"
openssl = "0.10.57"
postgres = "0.19.7"
postgres-openssl = "0.5.0"
rand = "0.8.0"
rand = "0.8.5"
synapse_compress_state = { path = "../", features = ["no-progress-bars"] }
synapse_auto_compressor = { path = "../synapse_auto_compressor/" }
env_logger = "0.10.0"
log = "0.4.14"
log = "0.4.20"
[dependencies.state-map]
git = "https://github.com/matrix-org/rust-matrix-state-map"

View File

@@ -1,4 +1,8 @@
[build-system]
requires = ["maturin>=0.11,<0.12"]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"
cargo-extra-args = "--no-default-features"
[tool.maturin]
profile = "release"
features = ["pyo3"]
no-default-features = true

View File

@@ -17,40 +17,40 @@ classifier = [
]
[dependencies]
openssl = "0.10.55"
postgres = "0.19.0"
openssl = { version = "0.10.57", features = ["vendored"] }
postgres = "0.19.7"
postgres-openssl = "0.5.0"
rand = "0.8.0"
rand = "0.8.5"
serial_test = "2.0.0"
synapse_compress_state = { path = "../", features = ["no-progress-bars"], default-features = false }
env_logger = "0.10.0"
log = "0.4.14"
log-panics = "2.0.0"
anyhow = "1.0.42"
log = "0.4.20"
log-panics = "2.1.0"
anyhow = "1.0.75"
# Needed for pyo3 support
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies.clap]
version = "4.0.15"
version = "4.4.2"
features = ["cargo"]
optional = true
[dependencies.pyo3]
version = "0.19.0"
version = "0.19.2"
features = ["extension-module"]
optional = true
[dependencies.pyo3-log]
version = "0.8.2"
version = "0.8.3"
optional = true
[dependencies.tikv-jemallocator]
version = "0.5.0"
version = "0.5.4"
optional = true
[features]
default = ["clap", "jemalloc", "pyo3"]
default = ["clap", "jemalloc"]
jemalloc = ["tikv-jemallocator", "synapse_compress_state/jemalloc"]
pyo3 = ["dep:pyo3", "dep:pyo3-log", "synapse_compress_state/pyo3"]

View File

@@ -0,0 +1,8 @@
[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"
[tool.maturin]
profile = "release"
features = ["pyo3"]
no-default-features = true