From 7211b1575094119a105fdb38b9664b0e71e9035e Mon Sep 17 00:00:00 2001 From: nicolas <48695862+merklefruit@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:25:25 +0100 Subject: [PATCH] fix: attempt at native cargo on native arch --- justfile | 21 +++++++++------------ scripts/cross-compile.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 12 deletions(-) create mode 100755 scripts/cross-compile.sh diff --git a/justfile b/justfile index ad3188c0..196f6181 100644 --- a/justfile +++ b/justfile @@ -1,3 +1,5 @@ +set shell := ["zsh", "-cu"] + # display a help message about available commands default: @just --list --unsorted @@ -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. @@ -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}} \ @@ -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}} diff --git a/scripts/cross-compile.sh b/scripts/cross-compile.sh new file mode 100755 index 00000000..aadea035 --- /dev/null +++ b/scripts/cross-compile.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Support script to build cross-platform binaries +# +# Usage: ./build-cross-binary.sh +# 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"