Skip to content

Commit

Permalink
fix: attempt at native cargo on native arch
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Nov 15, 2024
1 parent 93117cc commit 7211b15
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
21 changes: 9 additions & 12 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set shell := ["zsh", "-cu"]

# display a help message about available commands
default:
@just --list --unsorted
Expand Down Expand Up @@ -151,7 +153,7 @@ build-local-bolt-boost:
cd bolt-boost && docker build -t ghcr.io/chainbound/bolt-boost:0.1.0 . --load


# Cross platform builds
# Cross platform compilation
#
# We use cargo-zigbuild to build cross-platform binaries faster and to get around
# weird linking bugs that often happen with cross or similar tools.
Expand All @@ -165,19 +167,14 @@ build-local-bolt-boost:
#
# build the cross platform binaries for a package by name. available: "bolt-sidecar", "bolt-boost".
[private]
cross-build-binary package target_arch release_dir:
rustup target add {{target_arch}}


cd {{package}} && cargo zigbuild --target {{target_arch}} --profile release

mkdir -p dist/bin/{{release_dir}}
cp {{package}}/target/{{target_arch}}/release/{{package}} dist/bin/{{release_dir}}/{{package}}
cross-compile package target_arch release_dir:
chmod +x ./scripts/cross-compile.sh && \
./scripts/cross-compile.sh {{package}} {{target_arch}} {{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-build-binary {{package}} x86_64-unknown-linux-gnu amd64
@just cross-build-binary {{package}} aarch64-unknown-linux-gnu arm64
@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}} \
Expand All @@ -187,6 +184,6 @@ build-and-push-image package tag:
--push .

# build and push all the available packages to GHCR with the provided tag
build-and-push-all-images tag:
build-and-push-all-images tag='latest':
@just build-and-push-image bolt-sidecar {{tag}}
@just build-and-push-image bolt-boost {{tag}}
40 changes: 40 additions & 0 deletions scripts/cross-compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# Support script to build cross-platform binaries
#
# Usage: ./build-cross-binary.sh <package> <target_arch> <out_dir>
# Example: ./build-cross-binary.sh bolt-sidecar x86_64-unknown-linux-gnu /dist/bin

# Name of the package to build
PACKAGE=$1
# Target architecture to build for
TARGET_ARCH=$2
# Output directory for the binary
OUT_DIR=$3


# 1. install the toolchain if it's not already installed
if ! rustup target list | grep -q $TARGET_ARCH; then
rustup target add $TARGET_ARCH
fi

# 2. get the native architecture name (first part of the target triple)
NATIVE_ARCH=$(uname -m)
if [[ "$(uname)" == "Darwin" && "$NATIVE_ARCH" == "arm64" ]]; then
NATIVE_ARCH="aarch64"
fi

# 3. build the binary:
# - if the target is the same as the native architecture, build with cargo
# - otherwise, build with cargo-zigbuild
if [[ "$TARGE_ARCH" == "$NATIVE_ARCH-unknown-linux-gnu" ]]; then
cd $PACKAGE && cargo build --target $TARGE_ARCH --release
else
cd $PACKAGE && cargo zigbuild --target $TARGET_ARCH --profile release
fi

# 4. copy the binary to the output directory
mkdir -p dist/bin/$OUT_DIR
cp $PACKAGE/target/$TARGET_ARCH/release/$PACKAGE dist/bin/$OUT_DIR/$PACKAGE

echo "Built $PACKAGE for $TARGET_ARCH"

0 comments on commit 7211b15

Please sign in to comment.