chessbaseLinux/iosApp/scripts/build-bindings.sh
2026-12-01 00:56:28 +03:00

95 lines
3.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
IOS="$ROOT/iosApp"
GENERATED="$IOS/Generated/MobileAPI"
TARGET_DIR="${CARGO_TARGET_DIR:-$ROOT/target}"
HOST_OS="$(uname -s)"
case "$HOST_OS" in
Darwin) HOST_LIB="$TARGET_DIR/debug/libmobile_api.dylib" ;;
Linux) HOST_LIB="$TARGET_DIR/debug/libmobile_api.so" ;;
*)
printf 'Unsupported host for binding generation: %s\n' "$HOST_OS" >&2
exit 1
;;
esac
mkdir -p "$GENERATED"
cd "$ROOT"
# UniFFI reads metadata from the host library; no iOS artifact is used for this step.
cargo build --locked -p mobile-api --lib
cargo run -q --locked -p mobile-api --features bindgen --bin uniffi-bindgen -- \
generate --no-format --language swift \
--library "$HOST_LIB" --out-dir "$GENERATED"
# Swift/Clang discovers this fixed name through SWIFT_INCLUDE_PATHS.
cp "$GENERATED/MobileAPIFFI.modulemap" "$GENERATED/module.modulemap"
printf 'Swift UniFFI bindings: %s\n' "$GENERATED"
if [[ "$HOST_OS" != "Darwin" ]]; then
printf '%s\n' 'Host is not macOS: skipped iOS targets and XCFramework (no iOS build was attempted).'
exit 0
fi
for tool in rustup xcrun xcodebuild lipo; do
if ! command -v "$tool" >/dev/null 2>&1; then
printf 'Required tool is missing: %s\n' "$tool" >&2
exit 1
fi
done
# Override for a quicker local build, for example:
# CP_IOS_TARGETS="aarch64-apple-ios aarch64-apple-ios-sim" ./scripts/build-bindings.sh
read -r -a TARGETS <<< "${CP_IOS_TARGETS:-aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios}"
if [[ "${#TARGETS[@]}" -eq 0 ]]; then
printf '%s\n' 'CP_IOS_TARGETS must not be empty' >&2
exit 1
fi
export IPHONEOS_DEPLOYMENT_TARGET="${IPHONEOS_DEPLOYMENT_TARGET:-16.0}"
DEVICE_LIB=""
SIM_LIBS=()
for target in "${TARGETS[@]}"; do
case "$target" in
aarch64-apple-ios|aarch64-apple-ios-sim|x86_64-apple-ios) ;;
*) printf 'Unsupported iOS target: %s\n' "$target" >&2; exit 1 ;;
esac
rustup target add "$target"
cargo check --locked -p mobile-api --target "$target"
# The shared Cargo manifest keeps Android's cdylib. This per-target rustc invocation
# additionally emits the self-contained static archive required by XCFramework.
cargo rustc --locked --release -p mobile-api --lib --target "$target" --crate-type staticlib
library="$TARGET_DIR/$target/release/libmobile_api.a"
if [[ ! -f "$library" ]]; then
printf 'Expected static library was not produced: %s\n' "$library" >&2
exit 1
fi
if [[ "$target" == "aarch64-apple-ios" ]]; then
DEVICE_LIB="$library"
else
SIM_LIBS+=("$library")
fi
done
if [[ -z "$DEVICE_LIB" || "${#SIM_LIBS[@]}" -eq 0 ]]; then
printf '%s\n' 'Need aarch64-apple-ios and at least one simulator target for an XCFramework.' >&2
exit 1
fi
WORK="$TARGET_DIR/ios-xcframework"
HEADERS="$WORK/Headers"
mkdir -p "$HEADERS" "$WORK/simulator"
cp "$GENERATED/MobileAPIFFI.h" "$GENERATED/module.modulemap" "$HEADERS/"
SIM_LIB="$WORK/simulator/libmobile_api.a"
lipo -create "${SIM_LIBS[@]}" -output "$SIM_LIB"
XCFRAMEWORK="$IOS/Frameworks/MobileAPI.xcframework"
rm -rf "$XCFRAMEWORK"
xcodebuild -create-xcframework \
-library "$DEVICE_LIB" -headers "$HEADERS" \
-library "$SIM_LIB" -headers "$HEADERS" \
-output "$XCFRAMEWORK"
printf 'XCFramework: %s\n' "$XCFRAMEWORK"