Skip to content

Commit f280445

Browse files
authored
.github: cross-compile arm64 macOS asset (#796)
Continue the recent `zig cc` work [1], such that the next configlet release will have two new release assets: configlet_4.0.0-beta.14_macos_arm64.tar.gz configlet_4.0.0-beta.14_macos_arm64.tar.gz.minisig where the archive contains the executable: $ file ./configlet configlet: Mach-O 64-bit arm64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE|HAS_TLV_DESCRIPTORS> `configlet uuid` now uses [2] `std/sysrand`, which on macOS uses [3] the Security Framework. So to produce the macOS arm64 configlet, it's easiest to cross-compile from macOS x86_64, where the macOSX SDK is already available. Make the cross-compile and install-zig scripts support macOS, and add a build job for it. It looks like the macos-12 GitHub runner has SDK versions 12.3 and 13.1 installed. Use the latest one. The new executable is about 1.09 MiB, which is notably larger than the 655 KiB macOS x86_64 executable. Possible ways to reduce the size in the future: - Enable LTO when `zig ld` supports it [4]. - Compile it natively, when GitHub begins to provide a hosted arm64 macOS runner [5]. - Compile it natively, using a non-GitHub arm64 macOS runner. Refs: #24 Refs: #122 Refs: #764 [1] 0e8d665, ".github, config: use Zig to cross-compile arm64 Linux asset", 2023-08-13 [2] 53a75a2, "nimble, uuid: generate UUIDs via std/sysrand, not pragmagic/uuids", 2023-08-07 [3] https://github.com/nim-lang/Nim/blob/v2.0.0/lib/std/sysrand.nim#L235-L256 [4] https://www.github.com/ziglang/zig/issues/8680 [5] https://www.github.com/github/roadmap/issues/528
1 parent 7f2611a commit f280445

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

.github/bin/cross-compile

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ set -eo pipefail
33

44
archives_dir='archives'
55
build_tag="${GITHUB_REF_NAME}"
6+
# shellcheck disable=SC2153
7+
zig_target="${ZIG_TARGET}"
68

79
cross_compile() {
810
local target="$1"
@@ -15,7 +17,23 @@ cross_compile() {
1517
*) nim_os="${os}" ;;
1618
esac
1719

18-
nimble --verbose build --cpu:"${arch}" --os:"${nim_os}" -d:release -d:zig -d:target:"${target}"
20+
local build_options=(
21+
-d:release
22+
--cpu:"${arch}"
23+
--os:"${nim_os}"
24+
-d:zig
25+
-d:target:"${target}"
26+
)
27+
# On macOS, add to the compiler's and linker's framework search path.
28+
dir='/Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks'
29+
if [[ -d "${dir}" ]]; then
30+
build_options+=("--passC:-F${dir}")
31+
build_options+=("--passL:-F${dir}")
32+
# Strip
33+
build_options+=("--passL:-s")
34+
fi
35+
36+
nimble --verbose build "${build_options[@]}"
1937
local binary_name='configlet'
2038
if command -v llvm-strip &> /dev/null; then
2139
echo "stripping large comment section from executable..." >&2
@@ -28,17 +46,7 @@ cross_compile() {
2846

2947
main() {
3048
nimble --accept install --depsOnly
31-
32-
local targets=(
33-
aarch64-linux-musl
34-
# aarch64-macos-none
35-
# aarch64-windows-gnu
36-
)
37-
38-
for target in "${targets[@]}"; do
39-
cross_compile "${target}"
40-
done
41-
49+
cross_compile "${zig_target}"
4250
gh release upload "${build_tag}" "${archives_dir}"/*
4351
}
4452

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22
set -eo pipefail
33

44
version='0.11.0' # 2023-08-04
5-
release_name="zig-linux-x86_64-${version}"
6-
archive="${release_name}.tar.xz"
5+
6+
case "$(uname)" in
7+
Darwin*) os='macos' ;;
8+
Linux*) os='linux' ;;
9+
Windows*) os='windows' ;;
10+
MINGW*) os='windows' ;;
11+
MSYS_NT-*) os='windows' ;;
12+
*) os='linux' ;;
13+
esac
14+
15+
case "${os}" in
16+
windows*) ext='zip' ;;
17+
*) ext='tar.xz' ;;
18+
esac
19+
20+
release_name="zig-${os}-x86_64-${version}"
21+
archive="${release_name}.${ext}"
722
url="https://ziglang.org/download/${version}/${archive}"
823

924
curlopts=(
@@ -20,12 +35,27 @@ curl "${curlopts[@]}" --output "${archive}" "${url}"
2035

2136
# Check that the archive has the expected hash.
2237
echo "Verifying archive..." >&2
23-
archive_sha256='2d00e789fec4f71790a6e7bf83ff91d564943c5ee843c5fd966efc474b423047'
24-
echo "${archive_sha256} ${archive}" | sha256sum -c -
38+
case "${os}" in
39+
linux)
40+
archive_sha256='2d00e789fec4f71790a6e7bf83ff91d564943c5ee843c5fd966efc474b423047'
41+
echo "${archive_sha256} ${archive}" | sha256sum -c -
42+
;;
43+
macos)
44+
archive_sha256='1c1c6b9a906b42baae73656e24e108fd8444bb50b6e8fd03e9e7a3f8b5f05686'
45+
shasum -a 256 -c <<< "${archive_sha256} *${archive}"
46+
;;
47+
*)
48+
echo "${os} not yet supported" >&2
49+
exit 1
50+
;;
51+
esac
2552

2653
# Extract the archive, then remove it.
2754
echo "Extracting archive..." >&2
28-
tar xJf "${archive}"
55+
case "${ext}" in
56+
*zip) unzip "${archive}" ;;
57+
*) tar xJf "${archive}" ;;
58+
esac
2959
rm "${archive}"
3060

3161
# Add zig directory to `GITHUB_PATH`.

.github/workflows/build.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,18 @@ jobs:
7474

7575
cross-compile:
7676
needs: [create-empty-release]
77-
runs-on: ubuntu-22.04
78-
name: cross-compile
77+
strategy:
78+
fail-fast: false
79+
matrix:
80+
include:
81+
- runs-on: ubuntu-22.04
82+
zig_target: aarch64-linux-musl
83+
84+
- runs-on: macos-12
85+
zig_target: aarch64-macos-none
86+
87+
name: "${{ matrix.zig_target }}"
88+
runs-on: ${{ matrix.runs-on }}
7989
permissions:
8090
contents: write
8191
steps:
@@ -90,12 +100,13 @@ jobs:
90100
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
91101

92102
- name: Install Zig
93-
run: ./.github/bin/linux-install-zig
103+
run: ./.github/bin/install-zig
94104

95105
- name: Cross-compile
96106
run: ./.github/bin/cross-compile
97107
env:
98108
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
109+
ZIG_TARGET: ${{ matrix.zig_target }}
99110

100111
checksums:
101112
needs: [build, cross-compile]

0 commit comments

Comments
 (0)