Skip to content

Commit

Permalink
Merge pull request #389 from chainbound/nico/cross-builds
Browse files Browse the repository at this point in the history
feat: add fast multi-platform build
  • Loading branch information
thedevbirb authored Nov 19, 2024
2 parents 51f474c + c9fc73f commit e2d78bd
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 58 deletions.
14 changes: 12 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# log files
*_dump.log
logs/
target/

# environment secrets
.env

# IDEs
.vscode
.idea

# build artifacts
target/
dist/

# other
.DS_Store
.env
11 changes: 5 additions & 6 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Pay special attention to the `README` and `docker-compose` files.
For instance, for the Bolt sidecar this is in `bolt-sidecar/Cargo.toml`.
Similar changes should be made in the other packages getting updated.

We don't currently keep track of the version for flashbots forks.

Next, update the version of the Docker images used in any `docker-compose` files.
These currently only live inside the `testnets/` dir.

Expand All @@ -29,9 +27,10 @@ We use the built-in Github changelog feature to generate the changelog.

## 3. Build new Docker images

You can build new Docker images with the `just release <tag>` recipe.
You can build new Docker images with the `just build-and-push-all-images <tag>` recipe.

Example: `just release v0.2.0-alpha` will build and push the Docker images
Example: `just build-and-push-all-images v0.2.0-alpha` will build and push the Docker images
for all Bolt components with the tag `v0.2.0-alpha` for both `arm64` and `amd64`
architectures. This can take a long time... We recommend building from an ARM machine
because cross-compiling from x86 into ARM is slow as hell.
architectures. This can take a long time.

Since we use cross compilation, we recommend running this from an x86_64 linux box.
74 changes: 38 additions & 36 deletions bolt-boost/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions bolt-boost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "bolt-boost"
version = "0.3.0-alpha"
edition = "2021"

[profile.release]
lto = true
debug = true

[dependencies]
# core
tokio = { version = "1", features = ["full"] }
Expand All @@ -15,9 +19,7 @@ thiserror = "1.0.63"
reqwest = "0.12.7"

# crypto
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "ec3073e", features = [
"sha2-asm",
] }
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "ec3073e" }
tree_hash = "0.8"

# tracing & metrics
Expand Down
7 changes: 7 additions & 0 deletions bolt-boost/Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build]
pre-build = [
# We depend on openssl for the build, so we need to install its cross-compiled version
# more info at: https://github.com/cross-rs/cross/wiki/Configuration#build
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes --no-install-recommends install libssl-dev:$CROSS_DEB_ARCH"
]
4 changes: 4 additions & 0 deletions bolt-sidecar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version = "0.3.0-alpha"
edition = "2021"
default-run = "bolt-sidecar"

[profile.release]
lto = true
debug = true

[dependencies]
# core
clap = { version = "4.5.20", features = ["derive", "env"] }
Expand Down
11 changes: 11 additions & 0 deletions bolt-sidecar/Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[build]
pre-build = [
# We depend on openssl for the build, so we need to install its cross-compiled version
# more info at: https://github.com/cross-rs/cross/wiki/Configuration#build
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes --no-install-recommends install libssl-dev:$CROSS_DEB_ARCH",

# Update the default gcc and g++ to 10 to avoid a bug in gcc 9 that causes a build failure
# more info at: https://github.com/cross-rs/cross/issues/1565#issuecomment-2483968180
"apt-get --assume-yes --no-install-recommends install gcc-10 g++-10 && ln -sf /usr/bin/gcc-10 /usr/bin/gcc && ln -sf /usr/bin/g++-10 /usr/bin/g++"
]
50 changes: 39 additions & 11 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,51 @@ send-blob-preconf count='1':
--count {{count}}

# build all the docker images locally
build-images:
@just build-sidecar
@just build-bolt-boost
build-local-images:
@just build-local-sidecar
@just build-local-bolt-boost

# build the docker image for the bolt sidecar
[private]
build-sidecar:
build-local-sidecar:
cd bolt-sidecar && docker build -t ghcr.io/chainbound/bolt-sidecar:0.1.0 . --load

# build the docker image for bolt-boost
[private]
build-bolt-boost:
build-local-bolt-boost:
cd bolt-boost && docker build -t ghcr.io/chainbound/bolt-boost:0.1.0 . --load

# build and push multi-platform docker images to GHCR with the provided tag
[confirm("are you sure? this will build and push new images on ghcr.io")]
release tag:
chmod +x ./scripts/check_version_bumps.sh && ./scripts/check_version_bumps.sh {{tag}}
cd bolt-sidecar && docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/chainbound/bolt-sidecar:{{tag}} --push .
cd bolt-boost && docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/chainbound/bolt-boost:{{tag}} --push .

# Cross platform compilation with cargo cross.
# Install cross with: `cargo install cross --git https://github.com/cross-rs/cross`.
#
# Troubleshooting tips:
# * We have to clean the target directory before building for different targets because
# of a rustc incremental compilation bug. See: https://github.com/cross-rs/cross/issues/724#issuecomment-1484065725
# * If incurring into issues related to building `aws-lc-rs`, check this out:
# https://github.com/cross-rs/cross/issues/1565#issuecomment-2483968180
# * If incurring into issues related to building `sha2-asm`, make sure the "sha2-asm" feature
# is disabled in the `Cargo.toml` file you are trying to build.
#
# build the cross platform binaries for a package by name. available: "bolt-sidecar", "bolt-boost".
[private]
cross-compile package target_arch release_dir:
cd {{package}} && cargo clean && cross build --release --target {{target_arch}}
mkdir -p {{release_dir}} && cp {{package}}/target/{{target_arch}}/release/{{package}} {{release_dir}}

# build and push multi-platform docker images to GHCR for a package. available: "bolt-sidecar", "bolt-boost".
build-and-push-image package tag:
@just cross-compile {{package}} x86_64-unknown-linux-gnu amd64
@just cross-compile {{package}} aarch64-unknown-linux-gnu arm64

docker buildx build \
--build-arg BINARY={{package}} \
--file ./scripts/cross.Dockerfile \
--platform linux/amd64,linux/arm64 \
--tag ghcr.io/chainbound/{{package}}:{{tag}} \
--push .

# build and push all the available packages to GHCR with the provided tag
build-and-push-all-images tag='latest':
@just build-and-push-image bolt-sidecar {{tag}}
@just build-and-push-image bolt-boost {{tag}}
18 changes: 18 additions & 0 deletions scripts/cross.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This image is meant to enable cross-architecture builds.
# It assumes the binary has already been compiled for `$TARGETPLATFORM` and is
# locatable in `./dist/bin/$TARGETARCH/$BINARY`.

FROM --platform=$TARGETPLATFORM ubuntu:22.04

LABEL org.opencontainers.image.source=https://github.com/chainbound/bolt
LABEL org.opencontainers.image.licenses="MIT"

# Filled by docker buildx
ARG TARGETARCH

# Should be set by the caller when building the image
ARG BINARY

COPY ./dist/bin/$TARGETARCH/$BINARY /usr/local/bin/bolt

ENTRYPOINT ["/usr/local/bin/bolt"]

0 comments on commit e2d78bd

Please sign in to comment.