Skip to content

Commit

Permalink
modify dockerfile & pipeline for cross compile
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-bcw committed Aug 19, 2024
1 parent 512527a commit 02cf69e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

Expand Down Expand Up @@ -57,6 +60,7 @@ jobs:
- name: Build and push pgo worker
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm64
context: .
file: pgo-worker.Dockerfile
push: true
Expand Down Expand Up @@ -174,6 +178,9 @@ jobs:
- name: Checkout sources
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

Expand Down Expand Up @@ -220,6 +227,7 @@ jobs:
- name: Build and push worker
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm64
context: .
# file: optimized-worker.Dockerfile
file: Dockerfile
Expand All @@ -232,6 +240,7 @@ jobs:
- name: Build and push leader
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm64
context: .
file: Dockerfile
push: true
Expand Down
22 changes: 19 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@
# - `/output` is where the binaries go.

ARG BUILD_BASE=rustlang/rust:nightly-bullseye-slim
FROM ${BUILD_BASE} AS build
FROM --platform=$BUILDPLATFORM ${BUILD_BASE} AS build
ARG TARGETPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM

# Install build dependencies.
RUN apt-get update && apt-get install -y \
RUN dpkg add --add-architecture arm64 && \
apt-get update && apt-get install -y \
# for jemalloc
libjemalloc-dev \
libjemalloc2 \
make \
# for openssl
libssl-dev \
pkg-config \
# for cross compilation
libssl-dev:arm64 \
gcc-aarch64-linux-gnu \
&& rustup target add aarch64-unknown-linux-gnu \
# clean the image
&& rm -rf /var/lib/apt/lists/*

Expand All @@ -48,9 +56,17 @@ set -eux
# .cargo/config.toml
cd /src

TARGET=""
case ${TARGETARCH} in \
arm64) TARGET="aarch64-unknown-linux-gnu" ;; \
amd64) TARGET="x86_64-unknown-linux-gnu" ;; \
*) exit 1 ;; \
esac

# use the cache mount
# (we will not be able to to write to e.g `/src/target` because it is bind-mounted)
CARGO_TARGET_DIR=/artifacts cargo build --locked "--profile=${PROFILE}" --all
CARGO_TARGET_DIR=/artifacts cargo build --locked "--profile=${PROFILE}" "--target=${TARGET}" --all

# narrow the find call to SUBDIR because if we just copy out all executables
# we will break the cache invariant
if [ "$PROFILE" = "dev" ]; then
Expand Down
23 changes: 19 additions & 4 deletions optimized-worker.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@
# - `/output` is where the binaries go.

ARG BUILD_BASE=rustlang/rust:nightly-bullseye-slim
FROM ${BUILD_BASE} AS build
FROM --platform=$BUILDPLATFORM ${BUILD_BASE} AS build
ARG TARGETPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM

# Install build dependencies.
RUN apt-get update && apt-get install -y \
RUN dpkg add --add-architecture arm64 && \
apt-get update && apt-get install -y \
# for jemalloc
libjemalloc-dev \
libjemalloc2 \
make \
# for openssl
libssl-dev \
pkg-config \
# for cross compilation
libssl-dev:arm64 \
gcc-aarch64-linux-gnu \
&& rustup target add aarch64-unknown-linux-gnu \
# clean the image
&& rm -rf /var/lib/apt/lists/*

Expand All @@ -51,12 +59,19 @@ set -eux
# .cargo/config.toml
cd /src

TARGET=""
case ${TARGETARCH} in \
arm64) TARGET="aarch64-unknown-linux-gnu" ;; \
amd64) TARGET="x86_64-unknown-linux-gnu" ;; \
*) exit 1 ;; \
esac

mkdir -p /artifacts/pgo-profiles
cp -r pgo-profiles/* /artifacts/pgo-profiles

# use the cache mount
# (we will not be able to to write to e.g `/src/target` because it is bind-mounted)
CARGO_TARGET_DIR=/artifacts cargo pgo optimize build -- --bin worker --locked "--profile=${PROFILE}" --target=x86_64-unknown-linux-gnu
CARGO_TARGET_DIR=/artifacts cargo pgo optimize build -- --bin worker --locked "--profile=${PROFILE}" "--target=${TARGET}"

# narrow the find call to SUBDIR because if we just copy out all executables
# we will break the cache invariant
Expand All @@ -72,7 +87,7 @@ find /artifacts/ -print -ls

mkdir /output

cp /artifacts/x86_64-unknown-linux-gnu/release/worker /output/worker
cp /artifacts/$TARGET/release/worker /output/worker

ls -lha /output
EOF
Expand Down
22 changes: 19 additions & 3 deletions pgo-worker.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@

ARG BUILD_BASE=rustlang/rust:nightly-bullseye-slim
FROM ${BUILD_BASE} AS build
ARG TARGETPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM

# Install build dependencies.
RUN apt-get update && apt-get install -y \
RUN dpkg add --add-architecture arm64 && \
apt-get update && apt-get install -y \
# for jemalloc
libjemalloc-dev \
libjemalloc2 \
make \
# for openssl
libssl-dev \
pkg-config \
# for cross compilation
libssl-dev:arm64 \
gcc-aarch64-linux-gnu \
&& rustup target add aarch64-unknown-linux-gnu \
# clean the image
python3 python3-pip \
python3:arm64 python3-pip:arm64 \
&& rm -rf /var/lib/apt/lists/*


Expand All @@ -42,14 +51,21 @@ RUN \
<<EOF
set -eux

cargo pgo build -- --locked --bin worker --target=x86_64-unknown-linux-gnu
TARGET=""
case ${TARGETARCH} in \
arm64) TARGET="aarch64-unknown-linux-gnu" ;; \
amd64) TARGET="x86_64-unknown-linux-gnu" ;; \
*) exit 1 ;; \
esac

cargo pgo build -- --locked --bin worker "--target=${TARGET}"

EOF

# NOTE: the bucket name should be set WITHOUT the `gs://` prefix
# BONUS NOTE: should we create a different bucket just for .profraw files?
ENV GCS_UPLOAD_BUCKET=zkevm-csv
ENV WORKER_PATH=./target/x86_64-unknown-linux-gnu/release/worker
ENV WORKER_PATH=./target/${TARGET}/release/worker
ENV PROFILE_DIRECTORY=./target/pgo-profiles/

# run the python wrapper, which will:
Expand Down

0 comments on commit 02cf69e

Please sign in to comment.