Repository navigation
Expand file tree
/
Copy pathbuild-xcframework.sh
More file actions
executable file
·270 lines (247 loc) · 10.9 KB
/
Copy pathbuild-xcframework.sh
File metadata and controls
executable file
·270 lines (247 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bash
# Builds girocrab-core for Apple platforms, generates the UniFFI Swift
# bindings and bundles everything as a local Swift package:
#
# generated/GiroCrabCorePackage/
# Package.swift
# girocrab_coreFFI.xcframework (static libs + C header + modulemap)
# Sources/GiroCrabCore/girocrab_core.swift
#
# Usage:
# ./build-xcframework.sh # macOS (arm64) + iOS device + iOS simulator
# ./build-xcframework.sh --macos-only # quick local build (used by `cargo build`)
# ./build-xcframework.sh --ios # force iOS device + simulator slices (also with --macos-only)
# ./build-xcframework.sh --no-sim # macOS + iOS device only (skips aarch64-apple-ios-sim;
# # enough for archive/TestFlight, not for the iOS simulator)
# ./build-xcframework.sh --debug # unoptimised libraries
# ./build-xcframework.sh --check # only compare generated/.stamp with the
# # current core sources; exit 1 if stale
# --check --need-ios # also require an iOS device slice
# --check --strict-env # also require profile=release and the same
# # GIROCRAB_FINTS_PRODUCT_ID as at build time
# # (used by notarize.sh to skip redundant rebuilds)
#
# Every build writes generated/.stamp with a fingerprint of everything that
# goes into the library and the bindings (girocrab-core sources, uniffi-bindgen,
# Cargo.lock, this script). The git hooks in .githooks run --check after a
# checkout, merge or rebase, so stale bindings show up before `swift build`
# fails with misleading errors.
#
# Requirements: Xcode 15+, rustup with the targets
# aarch64-apple-darwin aarch64-apple-ios aarch64-apple-ios-sim
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"
export PATH="$HOME/.cargo/bin:$PATH"
CARGO="${CARGO:-cargo}"
MACOS_ONLY=0
FORCE_IOS=0
CHECK_ONLY=0
NO_SIM=0
NEED_IOS=0
STRICT_ENV=0
PROFILE=release
for arg in "$@"; do
case "$arg" in
--macos-only) MACOS_ONLY=1 ;;
--ios) FORCE_IOS=1 ;;
--debug) PROFILE=debug ;;
--check) CHECK_ONLY=1 ;;
--no-sim) NO_SIM=1 ;;
--need-ios) NEED_IOS=1 ;;
--strict-env) STRICT_ENV=1 ;;
-h|--help) sed -n '2,31p' "$0"; exit 0 ;;
*) echo "unknown option: $arg" >&2; exit 2 ;;
esac
done
STAMP="$ROOT/generated/.stamp"
# Inputs of the static library and the generated bindings (Cargo.lock is
# hashed separately: cargo may rewrite it during the build).
FINGERPRINT_INPUTS=(girocrab-core/src girocrab-core/Cargo.toml girocrab-core/build.rs
uniffi-bindgen Cargo.toml build-xcframework.sh)
# sha256 over "content-hash path" of every input file in the working tree, so
# uncommitted edits count too. Untracked files only if they are not ignored and
# are .rs/.toml (a stray vault.rs.orig must not mark the bindings stale).
sources_fingerprint() {
local f files=()
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
while IFS= read -r -d '' f; do
if [[ -f "$ROOT/$f" ]]; then files+=("$f"); fi
done < <(git -C "$ROOT" ls-files -z -c -- "${FINGERPRINT_INPUTS[@]}")
while IFS= read -r -d '' f; do
if [[ ( "$f" == *.rs || "$f" == *.toml ) && -f "$ROOT/$f" ]]; then files+=("$f"); fi
done < <(git -C "$ROOT" ls-files -z -o --exclude-standard -- "${FINGERPRINT_INPUTS[@]}")
else
while IFS= read -r -d '' f; do files+=("$f"); done \
< <(cd "$ROOT" && find "${FINGERPRINT_INPUTS[@]}" -type f -print0 2>/dev/null)
fi
(( ${#files[@]} )) || { echo none; return; }
(cd "$ROOT" && printf '%s\0' "${files[@]}" | LC_ALL=C sort -zu | xargs -0 shasum -a 256) \
| shasum -a 256 | cut -d' ' -f1
}
lock_fingerprint() { shasum -a 256 < "$ROOT/Cargo.lock" 2>/dev/null | cut -d' ' -f1; }
combine_fingerprint() { printf '%s\n%s\n' "$1" "$2" | shasum -a 256 | cut -d' ' -f1; }
# Hash (never the value) of the FinTS product ID compiled in by build.rs, so a
# library built without it is not mistaken for a release build.
env_fingerprint() {
if [[ -n "${GIROCRAB_FINTS_PRODUCT_ID:-}" ]]; then
printf '%s' "$GIROCRAB_FINTS_PRODUCT_ID" | shasum -a 256 | cut -d' ' -f1
else
echo none
fi
}
stamp_value() { sed -n "s/^$1=//p" "$STAMP" 2>/dev/null | head -1; }
if [[ $CHECK_ONLY -eq 1 ]]; then
if [[ ! -d "$ROOT/generated/GiroCrabCorePackage" ]]; then
echo "warning: generated/GiroCrabCorePackage is missing – run ./build-xcframework.sh --macos-only" >&2
exit 1
fi
hint="./build-xcframework.sh --macos-only"
[[ "$(stamp_value slices)" == *ios* ]] && hint="./build-xcframework.sh"
if [[ ! -f "$STAMP" ]]; then
echo "warning: generated/.stamp is missing (last build failed, or predates stamps) – run $hint" >&2
exit 1
fi
if [[ "$(stamp_value fingerprint)" != "$(combine_fingerprint "$(sources_fingerprint)" "$(lock_fingerprint)")" ]]; then
echo "warning: the xcframework / UniFFI bindings in generated/ are stale" >&2
echo " (built from $(stamp_value commit); girocrab-core, uniffi-bindgen or Cargo.* changed since)." >&2
echo " swift build may fail with misleading errors – rebuild: $hint" >&2
exit 1
fi
# Strict checks (notarize.sh) only; plain --check stays as the hooks know it.
if [[ $NEED_IOS -eq 1 && "$(stamp_value slices)" != *ios* ]]; then
echo "note: xcframework has no iOS device slice" >&2
exit 1
fi
if [[ $STRICT_ENV -eq 1 ]]; then
if [[ "$(stamp_value profile)" != "release" ]]; then
echo "note: xcframework was not built with the release profile" >&2
exit 1
fi
if [[ "$(stamp_value env)" != "$(env_fingerprint)" ]]; then
echo "note: xcframework was built with a different GIROCRAB_FINTS_PRODUCT_ID" >&2
exit 1
fi
fi
exit 0
fi
# Sources as they are when the build starts: an edit made while it runs leaves
# the stamp stale instead of hiding the change. No stamp until the build
# succeeds, so a failed or interrupted build never looks up to date.
SOURCES_FP="$(sources_fingerprint)"
BUILT_FROM="$(git -C "$ROOT" describe --always --dirty 2>/dev/null || echo unknown)"
rm -f "$STAMP"
CRATE=girocrab_core
FFI_MODULE=girocrab_coreFFI
TARGET_DIR="${GIROCRAB_TARGET_DIR:-$ROOT/target/apple}"
OUT="$ROOT/generated/GiroCrabCorePackage"
BUILD_FLAGS=()
[[ "$PROFILE" == "release" ]] && BUILD_FLAGS+=(--release)
export MACOSX_DEPLOYMENT_TARGET=14.0
export IPHONEOS_DEPLOYMENT_TARGET=17.0
TARGETS=(aarch64-apple-darwin)
if [[ $MACOS_ONLY -eq 0 || $FORCE_IOS -eq 1 ]]; then
TARGETS+=(aarch64-apple-ios)
[[ $NO_SIM -eq 1 ]] || TARGETS+=(aarch64-apple-ios-sim)
fi
# OpenSSL's iOS cross configs call `makedepend` (only to write dependency
# files). macOS doesn't ship it, and the XQuartz copy fails on the iOS SDK.
# A no-op stand-in placed first in PATH is enough for a clean build.
# (Kept in a temp dir: OpenSSL's Makefile can't handle spaces in the path.)
SHIM_DIR="$(mktemp -d "${TMPDIR:-/tmp}/girocrab-shim.XXXXXX")"
printf '#!/bin/sh\nexit 0\n' > "$SHIM_DIR/makedepend"
chmod +x "$SHIM_DIR/makedepend"
export PATH="$SHIM_DIR:$PATH"
trap 'rm -rf "$SHIM_DIR"' EXIT
if command -v rustup >/dev/null 2>&1; then
rustup target add "${TARGETS[@]}" >/dev/null 2>&1 || true
fi
echo "==> Building $CRATE ($PROFILE) for: ${TARGETS[*]}"
for t in "${TARGETS[@]}"; do
# ${arr[@]+"${arr[@]}"}: an empty array (--debug) is "unbound" for bash 3.2 under set -u.
"$CARGO" build -p girocrab-core --lib ${BUILD_FLAGS[@]+"${BUILD_FLAGS[@]}"} --target "$t" --target-dir "$TARGET_DIR"
done
echo "==> Building uniffi-bindgen-swift"
"$CARGO" build -p uniffi-bindgen --bin uniffi-bindgen-swift --release --target-dir "$TARGET_DIR"
BINDGEN="$TARGET_DIR/release/uniffi-bindgen-swift"
FINGERPRINT="$(combine_fingerprint "$SOURCES_FP" "$(lock_fingerprint)")"
MAC_LIB="$TARGET_DIR/aarch64-apple-darwin/$PROFILE/lib$CRATE.a"
GEN="$TARGET_DIR/swift-gen"
rm -rf "$GEN"
mkdir -p "$GEN/Headers" "$GEN/Sources"
echo "==> Generating Swift bindings (library mode)"
"$BINDGEN" "$MAC_LIB" "$GEN/Sources" --swift-sources
"$BINDGEN" "$MAC_LIB" "$GEN/Headers" --headers
"$BINDGEN" "$MAC_LIB" "$GEN/Headers" --modulemap \
--module-name "$FFI_MODULE" --modulemap-filename module.modulemap
echo "==> Creating XCFramework"
rm -rf "$OUT"
mkdir -p "$OUT/Sources/GiroCrabCore"
XCF_ARGS=(-library "$MAC_LIB" -headers "$GEN/Headers")
IOS_LIB="$TARGET_DIR/aarch64-apple-ios/$PROFILE/lib$CRATE.a"
SIM_LIB="$TARGET_DIR/aarch64-apple-ios-sim/$PROFILE/lib$CRATE.a"
INCLUDE_IOS=$(( MACOS_ONLY == 0 || FORCE_IOS == 1 ))
INCLUDE_SIM=$(( INCLUDE_IOS == 1 && NO_SIM == 0 ))
# Fingerprints of the sources the iOS slices in $TARGET_DIR were built from
# (device + simulator, resp. device only).
IOS_STAMP="$TARGET_DIR/ios-slices-$PROFILE.fingerprint"
IOS_DEVICE_STAMP="$TARGET_DIR/ios-device-$PROFILE.fingerprint"
if [[ $INCLUDE_SIM -eq 1 ]]; then
echo "$FINGERPRINT" > "$IOS_STAMP"
echo "$FINGERPRINT" > "$IOS_DEVICE_STAMP"
elif [[ $INCLUDE_IOS -eq 1 ]]; then
echo "$FINGERPRINT" > "$IOS_DEVICE_STAMP"
elif [[ -f "$IOS_LIB" && -f "$SIM_LIB" && "$(cat "$IOS_STAMP" 2>/dev/null)" == "$FINGERPRINT" ]]; then
# Keep iOS slices from an earlier full build of exactly these sources
# (stale slices would fail UniFFI's checksums).
INCLUDE_IOS=1; INCLUDE_SIM=1
echo " (keeping up-to-date iOS slices from the last full build)"
elif [[ -f "$IOS_LIB" && "$(cat "$IOS_DEVICE_STAMP" 2>/dev/null)" == "$FINGERPRINT" ]]; then
INCLUDE_IOS=1
echo " (keeping up-to-date iOS device slice from the last build)"
fi
if [[ $INCLUDE_IOS -eq 1 ]]; then
XCF_ARGS+=(-library "$IOS_LIB" -headers "$GEN/Headers")
fi
if [[ $INCLUDE_SIM -eq 1 ]]; then
XCF_ARGS+=(-library "$SIM_LIB" -headers "$GEN/Headers")
fi
xcodebuild -create-xcframework "${XCF_ARGS[@]}" -output "$OUT/$FFI_MODULE.xcframework" >/dev/null
cp "$GEN/Sources/"*.swift "$OUT/Sources/GiroCrabCore/"
cat > "$OUT/Package.swift" <<'SWIFT'
// swift-tools-version:5.10
// GENERATED by build-xcframework.sh – do not edit.
import PackageDescription
let package = Package(
name: "GiroCrabCorePackage",
platforms: [.macOS(.v14), .iOS(.v17)],
products: [
.library(name: "GiroCrabCore", targets: ["GiroCrabCore"]),
],
targets: [
.binaryTarget(name: "girocrab_coreFFI", path: "girocrab_coreFFI.xcframework"),
.target(
name: "GiroCrabCore",
dependencies: ["girocrab_coreFFI"],
path: "Sources/GiroCrabCore",
linkerSettings: [
.linkedFramework("Security"),
.linkedFramework("SystemConfiguration"),
.linkedFramework("CoreFoundation"),
]
),
]
)
SWIFT
SLICES=macos
[[ $INCLUDE_IOS -eq 1 ]] && SLICES=macos,ios-device
[[ $INCLUDE_SIM -eq 1 ]] && SLICES=macos,ios
{
echo "# Written by build-xcframework.sh; checked by ./build-xcframework.sh --check"
echo "fingerprint=$FINGERPRINT"
echo "commit=$BUILT_FROM"
echo "slices=$SLICES"
echo "profile=$PROFILE"
echo "env=$(env_fingerprint)"
} > "$STAMP"
echo "==> Done: $OUT"