08 · Groups
scripts/build_mls_xcframework.sh
121 lines · Shell
1#!/usr/bin/env bash
2# build_mls_xcframework.sh
3# Compiles the sophax-mls Rust crate for all iOS targets, generates UniFFI Swift
4# bindings, and assembles an XCFramework at Frameworks/SophaxMLS.xcframework.
5#
6# Requirements:
7# brew install xcodegen (for project rebuild after)
8# rustup targets installed (script installs them if missing)
9# Xcode Command Line Tools with iphoneos SDK
10
11set -euo pipefail
12
13# Use full Xcode if available (required for xcodebuild -create-xcframework).
14if [ -d "/Applications/Xcode.app/Contents/Developer" ]; then
15 export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"
16fi
17
18REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
19RUST_DIR="$REPO_ROOT/rust"
20FRAMEWORKS_DIR="$REPO_ROOT/Frameworks"
21GENERATED_DIR="$REPO_ROOT/Sources/SophaxChatCore/MLS/Generated"
22XCFRAMEWORK="$FRAMEWORKS_DIR/SophaxMLS.xcframework"
23
24LIB_NAME="libsophax_mls.a"
25MODULE_NAME="sophax_mls"
26
27# ── Source Cargo/rustup ──────────────────────────────────────────────────────
28
29source "$HOME/.cargo/env" 2>/dev/null || true
30
31# ── Add targets ──────────────────────────────────────────────────────────────
32
33echo "▶ Adding Rust targets…"
34rustup target add \
35 aarch64-apple-ios \
36 aarch64-apple-ios-sim \
37 x86_64-apple-ios \
38 aarch64-apple-darwin
39
40cd "$RUST_DIR"
41
42# macOS SDK is used as SDKROOT so rustc can find system headers for cross-compilation.
43MACOS_SDK="$(xcrun --sdk macosx --show-sdk-path)"
44
45# ── Build ─────────────────────────────────────────────────────────────────────
46
47echo "▶ Building aarch64-apple-ios (device)…"
48SDKROOT="$MACOS_SDK" \
49 IPHONEOS_DEPLOYMENT_TARGET=17.0 \
50 cargo build --release --lib --target aarch64-apple-ios
51
52echo "▶ Building aarch64-apple-ios-sim (Apple Silicon simulator)…"
53SDKROOT="$MACOS_SDK" \
54 IPHONEOS_DEPLOYMENT_TARGET=17.0 \
55 cargo build --release --lib --target aarch64-apple-ios-sim
56
57echo "▶ Building x86_64-apple-ios (Intel simulator)…"
58SDKROOT="$MACOS_SDK" \
59 IPHONEOS_DEPLOYMENT_TARGET=17.0 \
60 cargo build --release --lib --target x86_64-apple-ios
61
62echo "▶ Building aarch64-apple-darwin (macOS arm64 — needed for swift build / SPM)…"
63cargo build --release --lib --target aarch64-apple-darwin
64
65# ── Fat simulator lib (lipo) ──────────────────────────────────────────────────
66
67SIM_FAT_DIR="$RUST_DIR/target/sim-fat/release"
68mkdir -p "$SIM_FAT_DIR"
69
70echo "▶ Creating fat simulator library (arm64 + x86_64)…"
71lipo -create \
72 "$RUST_DIR/target/aarch64-apple-ios-sim/release/$LIB_NAME" \
73 "$RUST_DIR/target/x86_64-apple-ios/release/$LIB_NAME" \
74 -output "$SIM_FAT_DIR/$LIB_NAME"
75
76# ── Generate Swift bindings ───────────────────────────────────────────────────
77
78echo "▶ Generating UniFFI Swift bindings…"
79mkdir -p "$GENERATED_DIR"
80# Build uniffi-bindgen for host only (no --target flag), then run it
81cargo build --bin uniffi-bindgen
82cargo run --bin uniffi-bindgen -- generate \
83 --library "target/aarch64-apple-ios/release/$LIB_NAME" \
84 --language swift \
85 --out-dir "$GENERATED_DIR"
86
87echo " Generated: $(ls "$GENERATED_DIR")"
88
89# ── Header / modulemap paths ──────────────────────────────────────────────────
90
91HEADERS_DEVICE="$RUST_DIR/target/headers-device"
92HEADERS_SIM="$RUST_DIR/target/headers-sim"
93HEADERS_MACOS="$RUST_DIR/target/headers-macos"
94mkdir -p "$HEADERS_DEVICE" "$HEADERS_SIM" "$HEADERS_MACOS"
95
96for DIR in "$HEADERS_DEVICE" "$HEADERS_SIM" "$HEADERS_MACOS"; do
97 cp "$GENERATED_DIR/${MODULE_NAME}FFI.h" "$DIR/"
98 # Xcode/SPM require the modulemap to be named "module.modulemap" to find it automatically.
99 cp "$GENERATED_DIR/${MODULE_NAME}FFI.modulemap" "$DIR/module.modulemap"
100done
101
102# ── Assemble XCFramework ──────────────────────────────────────────────────────
103
104echo "▶ Assembling XCFramework…"
105rm -rf "$XCFRAMEWORK"
106mkdir -p "$FRAMEWORKS_DIR"
107
108xcodebuild -create-xcframework \
109 -library "$RUST_DIR/target/aarch64-apple-ios/release/$LIB_NAME" \
110 -headers "$HEADERS_DEVICE" \
111 -library "$SIM_FAT_DIR/$LIB_NAME" \
112 -headers "$HEADERS_SIM" \
113 -library "$RUST_DIR/target/aarch64-apple-darwin/release/$LIB_NAME" \
114 -headers "$HEADERS_MACOS" \
115 -output "$XCFRAMEWORK"
116
117echo ""
118echo "✓ SophaxMLS.xcframework built at: $XCFRAMEWORK"
119echo "✓ Swift bindings in: $GENERATED_DIR"
120echo ""
121echo "Next: run 'xcodegen generate' to pick up the new framework."