Skip to content

Commit

Permalink
ci: cross-compilation for aarch64 on macOS and Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc authored Aug 30, 2023
1 parent f15110e commit a943a21
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 143 deletions.
87 changes: 50 additions & 37 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,60 +39,73 @@ jobs:
build:
name: Build and test
needs: lint
defaults:
run:
shell: bash
runs-on: ${{ matrix.os }}
env:
CARGO_BUILD_TARGET: ${{ matrix.triple }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
triple: x86_64-unknown-linux-gnu
# - os: ubuntu-latest
# triple: aarch64-unknown-linux-gnu
- os: macos-latest
triple: x86_64-apple-darwin
# - os: macos-latest
# triple: aarch64-apple-darwin
# - os: windows-latest
# triple: x86_64-pc-windows-msvc
# - os: windows-latest
# triple: aarch64-pc-windows-msvc
os:
- ubuntu-latest
- macos-latest
# - windows-latest
arch:
- x86_64
- aarch64
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Rust
shell: bash
- name: Install Cross
if: matrix.os == 'ubuntu-latest' && matrix.arch == 'aarch64'
uses: taiki-e/install-action@v2
with:
tool: cross
- name: Setup environment
run: |
rustup target add $CARGO_BUILD_TARGET
if [ "${{ matrix.os }}" = "macos-latest" ]; then
CARGO_CMD=cargo
CARGO_BUILD_TARGET="${{ matrix.arch }}-apple-darwin"
rustup target add $CARGO_BUILD_TARGET
else
if [ "${{ matrix.arch }}" = "aarch64" ]; then
CARGO_CMD=cross
else
CARGO_CMD=cargo
fi
CARGO_BUILD_TARGET="${{ matrix.arch }}-unknown-linux-gnu"
fi
echo "CARGO_CMD=$CARGO_CMD" >> $GITHUB_ENV
echo "CARGO_BUILD_TARGET=$CARGO_BUILD_TARGET" >> $GITHUB_ENV
- name: Build (maa-cli)
shell: bash
run: |
cargo build --bin maa --locked
$CARGO_CMD build --package maa-cli --locked
- name: Test (maa-cli)
if: matrix.arch == 'x86_64' || matrix.os == 'ubuntu-latest'
run: |
$CARGO_CMD test --package maa-cli --locked
- name: Install MaaCore
shell: bash
if: matrix.arch == 'x86_64'
env:
MAA_API_URL: https://github.com/MaaAssistantArknights/MaaRelease/raw/main/MaaAssistantArknights/api/version
run: |
cargo run install beta
cargo run -- install beta
- name: Show installation
shell: bash
run: |
ls -l "$(cargo run dir library)"
ls -l "$(cargo run dir resource)"
- name: Test (maa-cli)
shell: bash
if: matrix.arch == 'x86_64'
run: |
export DYLD_FALLBACK_LIBRARY_PATH="$(cargo run dir library)"
cargo test --bin maa --locked
- name: Try run
shell: bash
MAA_CORE_DIR="$(cargo run -- dir lib)"
RESOURCE_DIR="$(cargo run -- dir resource)"
cd "$MAA_CORE_DIR" || exit 1
ls -l "$MAA_CORE_DIR"
cd "$RESOURCE_DIR" || exit 1
ls -l "$RESOURCE_DIR"
echo "MAA_CORE_DIR=$MAA_CORE_DIR" >> $GITHUB_ENV
- name: Try run with MaaCore
if: matrix.arch == 'x86_64'
run: |
export DYLD_FALLBACK_LIBRARY_PATH="$(cargo run dir library)"
export DYLD_FALLBACK_LIBRARY_PATH="$MAA_CORE_DIR"
cargo run -- version
- name: Test (maa-sys, static)
shell: bash
if: matrix.arch == 'x86_64'
run: |
export MAA_CORE_DIR="$(cargo run dir library)"
cd maa-sys || exit 1
cargo clean && cargo test
cargo test --package maa-sys --locked
181 changes: 75 additions & 106 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,59 @@ on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
# Just used for testing
pull_request:
branches:
- main
paths:
- ".github/workflows/release.yml"
workflow_dispatch:

jobs:
linux-gnu:
runs-on: ubuntu-latest
build:
runs-on: ${{ matrix.os }}
permissions:
contents: write
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
targets:
- x86_64-unknown-linux-gnu
- aarch64-unknown-linux-gnu
env:
CARGO_BUILD_TARGET: ${{ matrix.targets }}
os:
- ubuntu-latest
- macos-latest
# - windows-latest
arch:
- x86_64
- aarch64
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Rust
run: |
rustup target add $CARGO_BUILD_TARGET
- name: Setup cross-compilation for aarch64
if: matrix.targets == 'aarch64-unknown-linux-gnu'
- name: Install Cross
if: matrix.arch == 'aarch64' && matrix.os == 'ubuntu-latest'
uses: taiki-e/install-action@v2
with:
tool: cross
- name: Setup environment
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
if [ "${{ matrix.os }}" = "macos-latest" ]; then
CARGO_CMD=cargo
CARGO_BUILD_TARGET="${{ matrix.arch }}-apple-darwin"
rustup target add $CARGO_BUILD_TARGET
else
if [ "${{ matrix.arch }}" = "aarch64" ]; then
CARGO_CMD=cross
else
CARGO_CMD=cargo
fi
CARGO_BUILD_TARGET="${{ matrix.arch }}-unknown-linux-gnu"
fi
echo "CARGO_CMD=$CARGO_CMD" >> $GITHUB_ENV
echo "CARGO_BUILD_TARGET=$CARGO_BUILD_TARGET" >> $GITHUB_ENV
- name: Build
run: |
cargo build --release --locked --bin maa
$CARGO_CMD build --release --locked --package maa-cli
- name: Checkout version branch
uses: actions/checkout@v3
with:
Expand All @@ -42,54 +66,62 @@ jobs:
run: |
version_file="$PWD/version/version.json"
target="$CARGO_BUILD_TARGET"
maa_cli_version=$(yq -oy ".package.version" maa-cli/Cargo.toml)
cd target/x86_64-unknown-linux-gnu/release || exit 1
version=$maa_cli_version
version=$(yq -oy ".package.version" maa-cli/Cargo.toml)
cd "target/$target/release" || exit 1
if [ "${{ matrix.os }}" == "ubuntu-latest" ]; then
archive_name="maa_cli-v$version-$target.tar.gz"
tar -czvf $archive_name maa &&
checksum=$(shasum -a 256 $archive_name) &&
echo "$checksum" > $archive_name.sha256sum &&
yq -i -oj ".maa-cli.$target.version = \"$version\"" $version_file &&
yq -i -oj ".maa-cli.$target.tag = \"v$version\"" $version_file &&
yq -i -oj ".maa-cli.$target.name = \"$archive_name\"" $version_file &&
yq -i -oj ".maa-cli.$target.size = $(stat -c %s $archive_name)" $version_file &&
yq -i -oj ".maa-cli.$target.sha256sum = \"$(echo $checksum | cut -d ' ' -f 1)\"" $version_file &&
echo "name=v$version" >> $GITHUB_OUTPUT &&
exit 0
tar -czvf $archive_name maa || exit 1
yq -i -oj ".maa-cli.$target.size = $(stat -c %s $archive_name)" $version_file
else
archive_name="maa_cli-v$version-$target.zip"
zip $archive_name maa || exit 1
yq -i -oj ".maa-cli.$target.size = $(stat -f %z $archive_name)" $version_file
fi
checksum="$(shasum -a 256 $archive_name)"
echo "$checksum" > $archive_name.sha256sum
yq -i -oj ".maa-cli.$target.version = \"$version\"" $version_file
yq -i -oj ".maa-cli.$target.tag = \"v$version\"" $version_file
yq -i -oj ".maa-cli.$target.name = \"$archive_name\"" $version_file
yq -i -oj ".maa-cli.$target.sha256sum = \"$(echo $checksum | cut -d ' ' -f 1)\"" $version_file
echo "name=v$version" >> $GITHUB_OUTPUT
- name: Upload to GitHub Releases
if: github.event_name == 'push' && github.ref == 'refs/tags/v*'
uses: svenstaro/upload-release-action@v2
with:
release_name: ${{ steps.archive.outputs.name }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
file_glob: true
file: target/x86_64-unknown-linux-gnu/release/maa*.tar.gz*
file: target/*/release/maa.*z*
overwrite: true
- name: Commit and push version.json
if: github.event_name == 'push' && github.ref == 'refs/tags/v*'
run: |
cd version || exit 1
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
if [ -z "$(git diff version.json)" ]; then
echo "No changes to commit"
exit 0
exit 1
fi
git commit version.json -m "Update version.json" &&
git push
universal-apple-darwin:
runs-on: macos-latest
needs: build
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Rust
- name: Get tag
run: |
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
- name: Build
run: |
cargo build --release --locked --bin maa --target x86_64-apple-darwin --target aarch64-apple-darwin
echo "MAA_CLI_VERSION=$(yq -oy ".package.version" maa-cli/Cargo.toml)" >> $GITHUB_ENV
- name: Download artifacts from GitHub Release
uses: robinraju/[email protected]
with:
tag: "v${{ env.MAA_CLI_VERSION }}"
filename: maa_cli-v${{ env.MAA_CLI_VERSION }}-*-apple-darwin.zip
- name: Checkout version branch
uses: actions/checkout@v3
with:
Expand All @@ -99,10 +131,12 @@ jobs:
id: archive
run: |
version_file="$PWD/version/version.json"
maa_cli_version=$(yq -oy ".package.version" maa-cli/Cargo.toml)
version=$MAA_CLI_VERSION
for arch in x86_64 aarch64; do
mkdir -p "arch-apple-darwin"
unzip maa_cli-v$version-$arch-apple-darwin.zip -d arch-apple-darwin
done
target="universal-apple-darwin"
cd target || exit 1
version=$maa_cli_version
archive_name="maa_cli-v$version-$target.zip"
lipo -create -output maa x86_64-apple-darwin/release/maa aarch64-apple-darwin/release/maa &&
zip $archive_name maa &&
Expand All @@ -117,12 +151,14 @@ jobs:
exit 0
- name: Upload to GitHub Releases
uses: svenstaro/upload-release-action@v2
if: github.event_name == 'push' && github.ref == 'refs/tags/v*'
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file_glob: true
file: target/maa*.zip*
overwrite: true
- name: Commit and push version.json
if: github.event_name == 'push' && github.ref == 'refs/tags/v*'
run: |
cd version || exit 1
git config --local user.name "github-actions[bot]"
Expand All @@ -132,70 +168,3 @@ jobs:
exit 0
fi
git commit version.json -m "Update version.json" && git push
# windows-msvc:
# runs-on: windows-latest
# permissions:
# contents: write
# strategy:
# fail-fast: false
# matrix:
# targets:
# - x86_64-pc-windows-msvc
# - aarch64-pc-windows-msvc
# env:
# CARGO_BUILD_TARGET: ${{ matrix.targets }}
# steps:
# - name: Checkout
# uses: actions/checkout@v3
# - name: Setup Rust
# shell: bash
# run: |
# rustup target add $CARGO_BUILD_TARGET
# - name: Build
# shell: bash
# run: |
# cargo build --release --locked --bin maa
# - name: Checkout version branch
# uses: actions/checkout@v3
# with:
# ref: version
# path: version
# - name: Archive binaries, generate checksums and update version.json
# id: archive
# shell: bash
# run: |
# version_file="$PWD/version/version.json"
# target="$CARGO_BUILD_TARGET"
# maa_cli_version=$(yq -oy ".package.version" maa-cli/Cargo.toml)
# cd target/x86_64-pc-windows-msvc/release || exit 1
# version=$maa_cli_version
# archive_name="maa_cli-v$version-$target.zip"
# zip $archive_name maa.exe &&
# checksum=$(shasum -a 256 $archive_name) &&
# echo "$checksum" > $archive_name.sha256sum &&
# yq -i -oj ".maa-cli.$target.version = \"$version\"" $version_file &&
# yq -i -oj ".maa-cli.$target.tag = \"v$version\"" $version_file &&
# yq -i -oj ".maa-cli.$target.name = \"$archive_name\"" $version_file &&
# yq -i -oj ".maa-cli.$target.size = $(stat -c %s $archive_name)" $version_file &&
# yq -i -oj ".maa-cli.$target.sha256sum = \"$(echo $checksum | cut -d ' ' -f 1)\"" $version_file &&
# echo "name=v$version" >> $GITHUB_OUTPUT &&
# exit 0
# - name: Upload to GitHub Releases
# uses: svenstaro/upload-release-action@v2
# with:
# repo_token: ${{ secrets.GITHUB_TOKEN }}
# file_glob: true
# file: target/x86_64-pc-windows-msvc/release/maa*.zip*
# overwrite: true
# - name: Commit and push version.json
# shell: bash
# run: |
# cd version || exit 1
# git config --local user.name "github-actions[bot]"
# git config --local user.email "github-actions[bot]@users.noreply.github.com"
# if [ -z "$(git diff version.json)" ]; then
# echo "No changes to commit"
# exit 0
# fi
# git commit version.json -m "Update version.json" && git push

0 comments on commit a943a21

Please sign in to comment.