Skip to content

Commit

Permalink
Merge pull request #1 from YOU54F/feat/slim_bins
Browse files Browse the repository at this point in the history
Feat/slim bins
  • Loading branch information
YOU54F authored Apr 30, 2024
2 parents 40abddf + eed40d6 commit 94204d2
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 59 deletions.
55 changes: 45 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ on:
release:
types: [published]

pull_request:
branches:
- main
push:
branches:
- feat/musl_linux_win_aarch64
- feat/slim_bins

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: false
Expand All @@ -13,18 +21,43 @@ jobs:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-20.04, windows-2019, macos-12]
env:
pact_do_not_track: true
include:
- operating-system: ubuntu-20.04
targets: x86_64-unknown-linux-gnu,x86_64-unknown-linux-musl,aarch64-unknown-linux-gnu,aarch64-unknown-linux-musl
- operating-system: windows-2019
targets: aarch64-pc-windows-msvc,x86_64-pc-windows-msvc
- operating-system: macos-12
targets: aarch64-apple-darwin,x86_64-apple-darwin
fail-fast: false
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install stable Rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
override: true
target: aarch64-apple-darwin
targets: ${{ matrix.targets }}

- name: Rust caching
uses: Swatinem/rust-cache@v2
with:
workspaces: rust

- name: Set up QEMU
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
if: runner.os == 'Linux'
uses: docker/setup-buildx-action@v3
- name: Cargo flags
id: cargo-flags
shell: bash
run: |
if [[ "${{ github.event_name }}" = "release" ]]; then
echo "flags=--release" >> "$GITHUB_OUTPUT"
else
echo "flags=" >> "$GITHUB_OUTPUT"
fi
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
Expand All @@ -35,10 +68,12 @@ jobs:
- run: ./release.sh ${{ runner.os }} ${{ github.ref }}
shell: bash
- name: Upload Release Assets
if: |
startsWith(github.ref, 'refs/tags/v')
id: upload-release-asset
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/artifacts/*
file: release_artifacts/*
file_glob: true
tag: ${{ github.ref }}
tag: ${{ github.ref }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# will have compiled files and executables
target/

# Generated Release Artifacts
release_artifacts
# These are backup files generated by rustfmt
**/*.rs.bk

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ trim-margin = "0.1.0"
built = { version = "0.7.2", features = [ "git2" ] }
os_info = "3.8.2"

[profile.release]
strip = true
opt-level = "z"
codegen-units = 1
lto = true
10 changes: 8 additions & 2 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[target.aarch64-unknown-linux-gnu]
[target.x86_64-unknown-linux-musl]
pre-build=[
"dpkg --add-architecture arm64 && apt-get update && apt-get install --assume-yes wget unzip",
"apt-get update && apt-get install --assume-yes wget unzip",
"wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip",
"unzip protoc-21.5-linux-x86_64.zip -d /usr/local/"
]
[target.aarch64-unknown-linux-musl]
pre-build=[
"dpkg --add-architecture arm64 && apt-get update && apt-get install --assume-yes wget unzip",
"wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip",
"unzip protoc-21.5-linux-x86_64.zip -d /usr/local/"
]
168 changes: 122 additions & 46 deletions release.sh
Original file line number Diff line number Diff line change
@@ -1,61 +1,137 @@
#!/bin/bash

if [ $# -lt 1 ]
set -e
set -x

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

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"}

if [ $# -lt 2 ]
then
echo "Usage : $0 <Linux|Windows|macOS> <version tag>"
echo "Usage : $0 <Linux|Windows|macOS> <release version> <cargo flags>"
exit
fi

set -e
APP=pact-protobuf-plugin
OS=$1
shift;
VERSION=$1
shift;
echo Building Release for "$OS"
# All flags passed to this script are passed to cargo.
cargo_flags=( "$@" )
build_manifest() {
cp pact-plugin.json release_artifacts
NEXT=$(echo "$VERSION" | sed 's/^refs\/tags\/v-//')
# get latest release tag, if NEXT still contains refs
if [[ "${NEXT}" =~ "refs"* ]]; then
CRATE_VERSION=$(cat Cargo.toml| grep 'version = ".*"' -m1 | cut -d '"' -f 2)
echo "defaulting NEXT=$VERSION to version from Cargo.toml $CRATE_VERSION"
NEXT=$CRATE_VERSION
# LATEST_RELEASE=$(echo $(curl -s https://api.github.com/repos/pact-foundation/pact-stub-server/releases/latest | jq -r '.name') | sed 's/v//')
# echo "defaulting NEXT=$VERSION to latest release $LATEST_RELEASE"
# NEXT=$LATEST_RELEASE
fi
sed -e 's/VERSION=\"0.1.5\"/VERSION=\"'${NEXT}'\"/' "$RUST_DIR/scripts/install-plugin.sh" > "$ARTIFACTS_DIR/install-plugin.sh"
openssl dgst -sha256 -r $ARTIFACTS_DIR/install-plugin.sh > "$ARTIFACTS_DIR/install-plugin.sh.sha256"
}
install_cross() {
cargo install cross@0.2.5
}

build_linux_x86_64() {
install_cross
cargo clean
cross build --target=x86_64-unknown-linux-musl "${cargo_flags[@]}"
if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-unknown-linux-musl/release/${APP}" \
"$ARTIFACTS_DIR/${APP}-linux-x86_64.gz"
# cargo clean
fi
}

build_linux_aarch64() {
install_cross
cargo clean
cross build --target=aarch64-unknown-linux-musl "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-unknown-linux-musl/release/${APP}" \
"$ARTIFACTS_DIR/${APP}-linux-aarch64.gz"
fi
}
# Build the x86_64 darwin release
build_macos_x86_64() {
cargo build --target x86_64-apple-darwin "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-apple-darwin/release/${APP}" \
"$ARTIFACTS_DIR/${APP}-osx-x86_64.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/x86_64-apple-darwin/release/${APP}" \
"$ARTIFACTS_DIR/${APP}-macos-x86_64.gz"
fi
}

# Build the aarch64 darwin release
build_macos_aarch64() {
cargo build --target aarch64-apple-darwin "${cargo_flags[@]}"

if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-apple-darwin/release/${APP}" \
"$ARTIFACTS_DIR/${APP}-osx-aarch64.gz"
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-apple-darwin/release/${APP}" \
"$ARTIFACTS_DIR/${APP}-macos-aarch64.gz"
fi
}

# Build the x86_64 windows release
build_windows_x86_64() {
cargo build --target x86_64-pc-windows-msvc "${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/${APP}.exe" \
"$ARTIFACTS_DIR/${APP}-windows-x86_64.exe.gz"
fi
}

echo Building Release for "$1" - "$2"
# Build the aarch64 windows release
build_windows_aarch64() {
cargo build --target aarch64-pc-windows-msvc "${cargo_flags[@]}"

cargo clean
mkdir -p target/artifacts
if [[ "${cargo_flags[*]}" =~ "--release" ]]; then
gzip_and_sum \
"$CARGO_TARGET_DIR/aarch64-pc-windows-msvc/release/${APP}.exe" \
"$ARTIFACTS_DIR/${APP}-windows-aarch64.exe.gz"
fi
}

case "$1" in
case "$OS" in
Linux) echo "Building for Linux"
docker run --rm --user "$(id -u)":"$(id -g)" -v "$(pwd):/workspace" -w /workspace -t \
pactfoundation/rust-musl-build -c 'LIBZ_SYS_STATIC=1 cargo build --release'
gzip -c target/release/pact-protobuf-plugin > target/artifacts/pact-protobuf-plugin-linux-x86_64.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-linux-x86_64.gz > target/artifacts/pact-protobuf-plugin-linux-x86_64.gz.sha256
cp pact-plugin.json target/artifacts
NEXT=$(echo "$2" | cut -d\- -f2)
sed -e 's/VERSION=\"0.1.5\"/VERSION=\"'${NEXT}'\"/' scripts/install-plugin.sh > target/artifacts/install-plugin.sh
openssl dgst -sha256 -r target/artifacts/install-plugin.sh > target/artifacts/install-plugin.sh.sha256

# Build aarch64
cargo install cross --git https://github.com/cross-rs/cross
cross build --target aarch64-unknown-linux-gnu --release
gzip -c target/aarch64-unknown-linux-gnu/release/pact-protobuf-plugin > target/artifacts/pact-protobuf-plugin-linux-aarch64.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-linux-aarch64.gz > target/artifacts/pact-protobuf-plugin-linux-aarch64.gz.sha256
build_linux_x86_64
build_linux_aarch64
build_manifest
;;
Windows) echo "Building for Windows"
cargo build --release
gzip -c target/release/pact-protobuf-plugin.exe > target/artifacts/pact-protobuf-plugin-windows-x86_64.exe.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-windows-x86_64.exe.gz > target/artifacts/pact-protobuf-plugin-windows-x86_64.exe.gz.sha256
Windows) echo "Building for windows"
build_windows_x86_64
build_windows_aarch64
;;
macOS) echo "Building for OSX"
cargo build --release
gzip -c target/release/pact-protobuf-plugin > target/artifacts/pact-protobuf-plugin-osx-x86_64.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-osx-x86_64.gz > target/artifacts/pact-protobuf-plugin-osx-x86_64.gz.sha256
# macos
gzip -c target/release/pact-protobuf-plugin > target/artifacts/pact-protobuf-plugin-macos-x86_64.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-macos-x86_64.gz > target/artifacts/pact-protobuf-plugin-macos-x86_64.gz.sha256

# M1
# export SDKROOT=$(xcrun -sdk macosx11.1 --show-sdk-path)
# export MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx11.1 --show-sdk-platform-version)
export MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET:-12}
cargo build --target aarch64-apple-darwin --release

gzip -c target/aarch64-apple-darwin/release/pact-protobuf-plugin > target/artifacts/pact-protobuf-plugin-osx-aarch64.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-osx-aarch64.gz > target/artifacts/pact-protobuf-plugin-osx-aarch64.gz.sha256
# macos
gzip -c target/aarch64-apple-darwin/release/pact-protobuf-plugin > target/artifacts/pact-protobuf-plugin-macos-aarch64.gz
openssl dgst -sha256 -r target/artifacts/pact-protobuf-plugin-macos-aarch64.gz > target/artifacts/pact-protobuf-plugin-macos-aarch64.gz.sha256
macOS) echo "Building for macos"
build_macos_x86_64
build_macos_aarch64
;;
*) echo "$1 is not a recognised OS"
*) echo "$OS is not a recognised OS"
exit 1
;;
esac
esac
18 changes: 18 additions & 0 deletions scripts/gzip-and-sum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Gzip and sum a file.
#
# Usage: gzip_and_sum <orig_file> <target_file> [<digest_file>]
#
# - orig_file: the file to gzip and sum
# - target_file: the file to write the gzipped file to
# - digest_file: the file to write the digest to. If not provided, defaults to
# <target_file>.sha256
gzip_and_sum() {
orig_file=$1
target_file=$2
digest_file=${3:-$target_file.sha256}

gzip --stdout --best "$orig_file" > "$target_file"
openssl dgst -sha256 -r "$target_file" > "$digest_file"
}

0 comments on commit 94204d2

Please sign in to comment.