Skip to content

Commit

Permalink
refactor: update pact_ffi/pact_verifier_cli release workflows
Browse files Browse the repository at this point in the history
split to allow building individual targets, or multiple

- generate musl .so with `RUSTFLAGS="-C target-feature=-crt-static"`

linux/windows only
  • Loading branch information
YOU54F committed Jun 8, 2024

Verified

This commit was signed with the committer’s verified signature.
YOU54F Yousaf Nabi
1 parent db78345 commit c944365
Showing 4 changed files with 238 additions and 171 deletions.
182 changes: 86 additions & 96 deletions rust/pact_ffi/release-linux.sh
Original file line number Diff line number Diff line change
@@ -3,110 +3,82 @@
set -e
set -x

RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd )"
RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
APP_NAME=libpact_ffi

source "$RUST_DIR/scripts/gzip-and-sum.sh"
ARTIFACTS_DIR=${ARTIFACTS_DIR:-"$RUST_DIR/release_artifacts"}
mkdir -p "$ARTIFACTS_DIR"
export CARGO_TARGET_DIR=${CARO_TARGET_DIR:-"$RUST_DIR/target"}

# All flags passed to this script are passed to cargo.
cargo_flags=( "$@" )

# Build the x86_64 GNU linux release
build_x86_64_gnu() {
install_cross
cargo clean
cross build --target x86_64-unknown-linux-gnu "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-unknown-linux-gnu/release/libpact_ffi.a" \
"$ARTIFACTS_DIR/libpact_ffi-linux-x86_64.a.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-unknown-linux-gnu/release/libpact_ffi.so" \
"$ARTIFACTS_DIR/libpact_ffi-linux-x86_64.so.gz"
fi
install_cross() {
cargo install cross@0.2.5 --force
}

build_x86_64_musl() {
sudo apt-get install -y musl-tools
cargo clean
cargo build --target x86_64-unknown-linux-musl "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
BUILD_SCRIPT=$(cat <<EOM
apk add --no-cache musl-dev gcc && \
cd /scratch && \
ar -x libpact_ffi.a && \
gcc -shared *.o -o libpact_ffi.so && \
rm -f *.o
EOM
)

docker run \
--platform=linux/amd64 \
--rm \
-v "$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release:/scratch" \
alpine \
/bin/sh -c "$BUILD_SCRIPT"

gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/libpact_ffi.a" \
"$ARTIFACTS_DIR/libpact_ffi-linux-x86_64-musl.a.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/libpact_ffi.so" \
"$ARTIFACTS_DIR/libpact_ffi-linux-x86_64-musl.so.gz"
fi
install_cross_latest() {
cargo install cross --git https://github.com/cross-rs/cross --force
}

install_cross() {
cargo install cross@0.2.5
clean_cargo_release_build() {
rm -rf $CARGO_TARGET_DIR/release/build
}

build_aarch64_gnu() {
install_cross
cargo clean
cross build --target aarch64-unknown-linux-gnu "${cargo_flags[@]}"
export CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-"$RUST_DIR/target"}

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-unknown-linux-gnu/release/libpact_ffi.a" \
"$ARTIFACTS_DIR/libpact_ffi-linux-aarch64.a.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-unknown-linux-gnu/release/libpact_ffi.so" \
"$ARTIFACTS_DIR/libpact_ffi-linux-aarch64.so.gz"
fi
}

build_aarch64_musl() {
install_cross
cargo clean
cross build --target aarch64-unknown-linux-musl "${cargo_flags[@]}"
# All flags passed to this script are passed to cargo.
case $1 in
x86_64-unknown-linux-musl)
TARGET=$1
shift
;;
aarch64-unknown-linux-musl)
TARGET=$1
shift
;;
x86_64-unknown-linux-gnu)
TARGET=$1
shift
;;
aarch64-unknown-linux-gnu)
TARGET=$1
shift
;;
*) ;;
esac
cargo_flags=("$@")

build_target() {
TARGET=$1

case $TARGET in
x86_64-unknown-linux-musl)
FILE_SUFFIX=linux-x86_64-musl
RUSTFLAGS="-C target-feature=-crt-static"
;;
aarch64-unknown-linux-musl)
FILE_SUFFIX=linux-aarch64-musl
RUSTFLAGS="-C target-feature=-crt-static"
;;
x86_64-unknown-linux-gnu)
FILE_SUFFIX=linux-x86_64
;;
aarch64-unknown-linux-gnu)
FILE_SUFFIX=linux-aarch64
;;
*)
echo unknown target $TARGET
exit 1
;;
esac
RUSTFLAGS=${RUSTFLAGS:-""} cross build --target $TARGET "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
BUILD_SCRIPT=$(cat <<EOM
apk add --no-cache musl-dev gcc && \
cd /scratch && \
ar -x libpact_ffi.a && \
gcc -shared *.o -o libpact_ffi.so && \
rm -f *.o
EOM
)

docker run \
--platform=linux/arm64 \
--rm \
-v "$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release:/scratch" \
alpine \
/bin/sh -c "$BUILD_SCRIPT"

file "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.a"
file "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.so"
du -sh "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.a"
du -sh "$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.so"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/libpact_ffi.a" \
"$ARTIFACTS_DIR/libpact_ffi-linux-aarch64-musl.a.gz"
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.a" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.a.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/libpact_ffi.so" \
"$ARTIFACTS_DIR/libpact_ffi-linux-aarch64-musl.so.gz"
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.so" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.so.gz"
fi
}

@@ -122,8 +94,26 @@ build_header() {
--output "$ARTIFACTS_DIR/pact-cpp.h"
}

build_x86_64_gnu
build_x86_64_musl
build_aarch64_gnu
build_aarch64_musl
build_header
install_cross
if [ ! -z "$TARGET" ]; then
echo building for target $TARGET
build_target $TARGET

# If we are building indiv targets, ensure we build the headers
# for at least 1 nominated target
if [ "$TARGET" == "x86_64-unknown-linux-gnu" ]; then
build_header
fi
else
echo building for all targets
# clean release build to avoid conflicting symbols when building all targets
clean_cargo_release_build
build_target x86_64-unknown-linux-gnu
clean_cargo_release_build
build_target aarch64-unknown-linux-gnu
clean_cargo_release_build
build_target x86_64-unknown-linux-musl
clean_cargo_release_build
build_target aarch64-unknown-linux-musl
build_header
fi
76 changes: 44 additions & 32 deletions rust/pact_ffi/release-win.sh
Original file line number Diff line number Diff line change
@@ -3,50 +3,62 @@
set -e
set -x

RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd )"
RUST_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
APP_NAME=pact_ffi

source "$RUST_DIR/scripts/gzip-and-sum.sh"
ARTIFACTS_DIR=${ARTIFACTS_DIR:-"$RUST_DIR/release_artifacts"}
mkdir -p "$ARTIFACTS_DIR"
export CARGO_TARGET_DIR=${CARO_TARGET_DIR:-"$RUST_DIR/target"}
export CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-"$RUST_DIR/target"}

# All flags passed to this script are passed to cargo.
cargo_flags=( "$@" )
case $1 in
x86_64-pc-windows-msvc)
TARGET=$1
shift
;;
aarch64-pc-windows-msvc)
TARGET=$1
shift
;;
*) ;;
esac
cargo_flags=("$@")
build_target() {
TARGET=$1

# Build the x86_64 windows release
build_x86_64() {
cargo build --target x86_64-pc-windows-msvc "${cargo_flags[@]}"
case $TARGET in
x86_64-pc-windows-msvc)
FILE_SUFFIX=windows-x86_64
;;
aarch64-pc-windows-msvc)
FILE_SUFFIX=windows-aarch64
;;
*)
echo unknown target $TARGET
exit 1
;;
esac
cargo build --target $TARGET "${cargo_flags[@]}"

# If --release in cargo flags, then gzip and sum the release artifacts
if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/pact_ffi.dll" \
"$ARTIFACTS_DIR/pact_ffi-windows-x86_64.dll.gz"
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.dll" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.dll.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/pact_ffi.dll.lib" \
"$ARTIFACTS_DIR/pact_ffi-windows-x86_64.dll.lib.gz"
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.dll.lib" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.dll.lib.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-pc-windows-msvc/release/pact_ffi.lib" \
"$ARTIFACTS_DIR/pact_ffi-windows-x86_64.lib.gz"
"$CARGO_TARGET_DIR/$TARGET/release/$APP_NAME.lib" \
"$ARTIFACTS_DIR/$APP_NAME-$FILE_SUFFIX.lib.gz"
fi
}

# Build the aarch64 windows release
build_aarch64() {
cargo build --target aarch64-pc-windows-msvc "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/pact_ffi.dll" \
"$ARTIFACTS_DIR/pact_ffi-windows-aarch64.dll.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/pact_ffi.dll.lib" \
"$ARTIFACTS_DIR/pact_ffi-windows-aarch64.dll.lib.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/pact_ffi.lib" \
"$ARTIFACTS_DIR/pact_ffi-windows-aarch64.lib.gz"
fi
}

build_x86_64
build_aarch64
if [ ! -z "$TARGET" ]; then
echo building for target $TARGET
build_target $TARGET
else
echo building for all targets
build_target x86_64-pc-windows-msvc
build_target aarch64-pc-windows-msvc
fi
Loading

0 comments on commit c944365

Please sign in to comment.