Skip to content

Commit 3661646

Browse files
committed
Cross-compile binaries for aarch64-unknown-linux-gnu
GitHub does not provide a aarch64-unknown-linux-gnu runner that we would need to compile binaries for Raspberry Pi, but we can use `cross` to build those on the amd64 Ubuntu runner.
1 parent e459602 commit 3661646

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

.github/workflows/release.yml

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,20 @@ jobs:
4848
strategy:
4949
matrix:
5050
os:
51-
- ubuntu-latest
52-
- windows-latest
53-
- macos-latest
54-
55-
runs-on: ${{ matrix.os }}
51+
- name: ubuntu-latest
52+
cross_compile_os: linux
53+
cross_compile_target: aarch64-unknown-linux-gnu
54+
- name: ubuntu-latest
55+
cross_compile_os:
56+
cross_compile_target:
57+
- name: windows-latest
58+
cross_compile_os:
59+
cross_compile_target:
60+
- name: macos-latest
61+
cross_compile_os:
62+
cross_compile_target:
63+
64+
runs-on: ${{ matrix.os.name }}
5665

5766
steps:
5867
- uses: actions/checkout@v4
@@ -66,17 +75,34 @@ jobs:
6675
- name: Setup PostgreSQL
6776
uses: ikalnytskyi/action-setup-postgres@v6
6877

69-
- uses: actions-rs/cargo@v1
70-
with:
71-
command: build
72-
args: --release
78+
- run: |
79+
if [ -z "${CROSS_COMPILE_TARGET}" ]; then
80+
# normal build targetting the CPU architecture and the OS of the host
81+
cargo build --release
82+
else
83+
# cross build with the specified rustc target
84+
cargo install cross
85+
cross build --release --target ${CROSS_COMPILE_TARGET}
86+
fi
87+
env:
88+
CROSS_COMPILE_TARGET: ${{ matrix.os.cross_compile_target }}
7389
7490
- id: build
7591
run: |
76-
rustc --print cfg | grep = > rustc.vars
77-
source rustc.vars
92+
if [ -z "${CROSS_COMPILE_TARGET}" ]; then
93+
# normal build targetting the CPU architecture and the OS of the host
94+
rustc --print cfg | grep = > rustc.vars
95+
source rustc.vars
96+
97+
pushd target/release
98+
else
99+
# cross build with the specified rustc target
100+
export target_os=${CROSS_COMPILE_OS}
101+
export target_arch=${CROSS_COMPILE_TARGET}
102+
103+
pushd target/${CROSS_COMPILE_TARGET}/release
104+
fi
78105
79-
pushd target/release
80106
if [ "$RUNNER_OS" == "Windows" ]; then
81107
export ASSET_NAME="xsnippet-api-${target_arch}-${target_os}.exe.7z"
82108
7z a $ASSET_NAME xsnippet-api.exe
@@ -89,6 +115,8 @@ jobs:
89115
90116
echo "asset_path=target/release/$ASSET_NAME" >> $GITHUB_OUTPUT
91117
env:
118+
CROSS_COMPILE_OS: ${{ matrix.os.cross_compile_os }}
119+
CROSS_COMPILE_TARGET: ${{ matrix.os.cross_compile_target }}
92120
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
93121
GH_REPO: ${{ env.GITHUB_REPOSITORY }}
94122
RELEASE_TAG: ${{ needs.create_release.outputs.release_tag }}

Cross.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build]
2+
pre-build = [
3+
"dpkg --add-architecture $CROSS_DEB_ARCH",
4+
"apt-get update && apt-get install --assume-yes libssl-dev:$CROSS_DEB_ARCH libpq-dev:$CROSS_DEB_ARCH"
5+
]

0 commit comments

Comments
 (0)