Skip to content

Commit 5a7cd97

Browse files
authored
build: Faster and streamlined cross builds (#70)
Switches to cross-building in a container matching the native host architecture. For now, this assumes arm64 -> amd64, but will be easy to extend also for amd64 -> arm64 cross builds.
1 parent 0428d98 commit 5a7cd97

File tree

6 files changed

+86
-40
lines changed

6 files changed

+86
-40
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848

4949
- name: Build Binary
5050
run: |
51-
cargo build --release --target=${{ matrix.target }} --bin objectstore
51+
cargo build --release --locked --target=${{ matrix.target }} --bin objectstore
5252
cp target/${{ matrix.target }}/release/objectstore ./objectstore
5353
5454
- name: Set up Docker Buildx

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
FROM gcr.io/distroless/cc-debian12:nonroot
22

3-
ENV FSS_PATH="/data"
4-
5-
# Copy the pre-built binary
6-
COPY objectstore /bin/objectstore
3+
ARG BINARY=objectstore
4+
COPY ${BINARY} /bin/application
75

86
VOLUME ["/data"]
9-
ENTRYPOINT ["/bin/objectstore"]
7+
ENV FSS_PATH="/data"
8+
9+
ENTRYPOINT ["/bin/application"]
1010
EXPOSE 8888

Dockerfile.cross

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dockerfile for cross-compiling from aarch64 (host) to amd64 (target)
2+
FROM rust:slim-bookworm
3+
4+
RUN dpkg --add-architecture amd64 \
5+
&& apt-get update -qq \
6+
&& apt-get upgrade -y \
7+
&& apt-get install -y --no-install-recommends protobuf-compiler libprotobuf-dev pkg-config libssl-dev:amd64 gcc-x86-64-linux-gnu g++-x86-64-linux-gnu \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
RUN rustup target add x86_64-unknown-linux-gnu
11+
12+
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
13+
ENV CC=x86_64-linux-gnu-gcc
14+
ENV PKG_CONFIG_ALLOW_CROSS=1
15+
ENV OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu
16+
ENV OPENSSL_INCLUDE_DIR=/usr/include
17+
18+
WORKDIR /workspace
19+
ENTRYPOINT ["cargo", "build", "--release", "--target=x86_64-unknown-linux-gnu"]
20+
CMD ["--bin", "objectstore"]

Dockerfile.stresstest

Lines changed: 0 additions & 33 deletions
This file was deleted.

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,39 @@ Ensure `protoc` and the latest stable Rust toolchain are installed on your
3030
machine. A release build can be created with:
3131

3232
```sh
33-
cargo build --release
33+
cargo build --release --locked
3434
```
3535

3636
The server binary will be located at `target/release/objectstore`.
3737

38+
### Docker Container
39+
40+
To build the Docker container, first build the release binary, then build the
41+
container:
42+
43+
```sh
44+
docker build -f Dockerfile target/release/
45+
```
46+
47+
The last argument must be the path to the directory that contains the
48+
`objectstore` binary.
49+
50+
### Cross-building for AMD64 from ARM64
51+
52+
We have utilities to cross-build an AMD64 container from an ARM64 host, such as
53+
a macOS machine running on Apple Silicon. To do this, run:
54+
55+
```sh
56+
scripts/build-cross.sh
57+
```
58+
59+
This script will take care of the entire cross-compilation build process inside
60+
an ARM-based Docker container and use the project's `target/` directory for
61+
caching and build output. The build results in two artifacts:
62+
63+
- The binary at `target/x86_64-unknown-linux-gnu/release/objectstore`
64+
- The docker container, tagged with `objectstore:latest`
65+
3866
## Development
3967

4068
### Environment Setup

scripts/build-cross.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
cd "$(dirname "$0")/.."
5+
6+
BINARY=${1:-objectstore}
7+
case "$BINARY" in
8+
objectstore) PACKAGE="objectstore-server" ;;
9+
*) PACKAGE="$BINARY" ;;
10+
esac
11+
12+
docker build \
13+
--platform linux/arm64 \
14+
-f Dockerfile.cross \
15+
-t objectstore-build \
16+
.
17+
18+
docker run --rm \
19+
--platform linux/arm64 \
20+
-v "$(pwd)":/workspace \
21+
-v "$HOME/.cargo/registry":/usr/local/cargo/registry \
22+
-v "$HOME/.cargo/git":/usr/local/cargo/git \
23+
objectstore-build \
24+
-p "$PACKAGE"
25+
26+
docker build \
27+
--platform linux/amd64 \
28+
-f Dockerfile \
29+
--build-arg BINARY="$BINARY" \
30+
-t "$BINARY:latest" \
31+
"target/x86_64-unknown-linux-gnu/release/"

0 commit comments

Comments
 (0)