#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" case "$(uname -s)" in Darwin) ;; *) echo "wrong platform: $(uname -s), iOS build requires macOS" >&2; exit 1;; esac TARGET="${1:-sim}" CARGO_PROFILE="${YRXTALS_IOS_CARGO_PROFILE:-release}" case "$TARGET" in sim) RUST_TARGET="aarch64-apple-ios-sim" SDK_NAME="iphonesimulator" SWIFT_TARGET="arm64-apple-ios17.0-simulator" ;; device) RUST_TARGET="aarch64-apple-ios" SDK_NAME="iphoneos" SWIFT_TARGET="arm64-apple-ios17.0" ;; *) echo "usage: $0 [sim|device]" >&2 exit 2 ;; esac BUILD="$ROOT/build" APP="$BUILD/ios/YrXtals.app" RUST_LIB="/tmp/yr_crystals-target/$RUST_TARGET/$CARGO_PROFILE" SDK="$(xcrun --sdk "$SDK_NAME" --show-sdk-path)" export CC=/usr/bin/clang export CXX=/usr/bin/clang++ export IPHONEOS_DEPLOYMENT_TARGET=17.0 echo "Building Rust staticlib for $RUST_TARGET (profile=$CARGO_PROFILE)..." cargo build --profile "$CARGO_PROFILE" --target "$RUST_TARGET" --lib rm -f "$RUST_LIB/libyr_crystals.dylib" "$RUST_LIB/deps/libyr_crystals.dylib" if [ ! -f "$RUST_LIB/libyr_crystals.a" ]; then echo "ERROR: libyr_crystals.a not found at $RUST_LIB" >&2 exit 1 fi rm -rf "$APP" mkdir -p "$APP" cp "$ROOT/ios/Info.plist" "$APP/Info.plist" bash "$ROOT/scripts/ios/generate-icons.sh" ACTOOL_PARTIAL="$BUILD/ios/actool-partial-info.plist" mkdir -p "$BUILD/ios" echo "Compiling asset catalog..." xcrun actool "$ROOT/ios/Assets.xcassets" \ --compile "$APP" \ --platform "$SDK_NAME" \ --minimum-deployment-target 17.0 \ --app-icon AppIcon \ --output-partial-info-plist "$ACTOOL_PARTIAL" \ --target-device ipad \ --target-device iphone \ >/dev/null if [ -f "$ACTOOL_PARTIAL" ]; then /usr/libexec/PlistBuddy -c "Merge $ACTOOL_PARTIAL" "$APP/Info.plist" 2>/dev/null || true fi RUST_FLAGS=(-import-objc-header "$ROOT/include/yr_xtals.h" -L "$RUST_LIB" -lyr_crystals) SWIFT_DEBUG_FLAGS=() case "$CARGO_PROFILE" in *debug*) SWIFT_DEBUG_FLAGS=(-D DEBUG) ;; esac echo "Compiling Swift (profile=$CARGO_PROFILE)..." xcrun -sdk "$SDK_NAME" swiftc \ -target "$SWIFT_TARGET" \ -sdk "$SDK" \ "${RUST_FLAGS[@]}" \ "${SWIFT_DEBUG_FLAGS[@]}" \ -framework UIKit \ -framework SwiftUI \ -framework QuartzCore \ -framework Metal \ -framework MetalKit \ -framework AVFoundation \ -framework AudioToolbox \ -framework MediaPlayer \ -framework CoreGraphics \ -framework CoreFoundation \ -O \ -o "$APP/YrXtals" \ "$ROOT"/ios/src/*.swift if [ "$TARGET" = "sim" ]; then codesign --force --sign - "$APP" else PROFILE="${YRXTALS_IOS_PROFILE:-/Volumes/External/prvProfiles/All.mobileprovision}" if [ ! -f "$PROFILE" ]; then echo "ERROR: provisioning profile not found at $PROFILE" >&2 echo " set YRXTALS_IOS_PROFILE to point at a valid .mobileprovision" >&2 exit 1 fi cp "$PROFILE" "$APP/embedded.mobileprovision" ENT="$BUILD/ios/entitlements.plist" security cms -D -i "$PROFILE" 2>/dev/null \ | plutil -extract Entitlements xml1 -o "$ENT" - \ || { echo "ERROR: could not extract entitlements from profile" >&2; exit 1; } # pins wildcard application-identifier to the concrete TEAM.bundle.id form. BUNDLE_ID="$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "$APP/Info.plist")" TEAM_ID="$(/usr/libexec/PlistBuddy -c "Print :com.apple.developer.team-identifier" "$ENT" 2>/dev/null || true)" if [ -z "$TEAM_ID" ]; then TEAM_ID="$(/usr/libexec/PlistBuddy -c "Print :application-identifier" "$ENT" | cut -d. -f1)" fi /usr/libexec/PlistBuddy -c "Set :application-identifier ${TEAM_ID}.${BUNDLE_ID}" "$ENT" /usr/libexec/PlistBuddy -c "Delete :keychain-access-groups" "$ENT" 2>/dev/null || true echo "Pinned application-identifier=${TEAM_ID}.${BUNDLE_ID}" TMPDIR_PROF="$(mktemp -d)" PROFILE_PLIST="$TMPDIR_PROF/profile.plist" security cms -D -i "$PROFILE" > "$PROFILE_PLIST" 2>/dev/null PROFILE_SHAS="" for i in 0 1 2 3 4 5 6 7 8 9; do if ! plutil -extract "DeveloperCertificates.$i" raw -o "$TMPDIR_PROF/c$i.b64" "$PROFILE_PLIST" >/dev/null 2>&1; then break fi base64 -D -i "$TMPDIR_PROF/c$i.b64" -o "$TMPDIR_PROF/c$i.cer" sha=$(openssl x509 -inform der -in "$TMPDIR_PROF/c$i.cer" -fingerprint -noout 2>/dev/null \ | sed 's/.*=//;s/://g') PROFILE_SHAS="$PROFILE_SHAS $sha" done KEYCHAIN_SHAS=$(security find-identity -v 2>/dev/null \ | awk '/[0-9A-F]{40}/ {gsub(/[^0-9A-F]/, "", $2); print $2}') IDENTITY="" for s in $PROFILE_SHAS; do if echo "$KEYCHAIN_SHAS" | grep -qi "^$s$"; then IDENTITY="$s" break fi done rm -rf "$TMPDIR_PROF" if [ -z "$IDENTITY" ]; then echo "ERROR: no codesigning identity in your keychain matches any cert in the profile" >&2 echo " profile certs:$PROFILE_SHAS" >&2 exit 1 fi echo "Signing with $IDENTITY..." codesign --force \ --sign "$IDENTITY" \ --entitlements "$ENT" \ --options runtime \ --timestamp=none \ "$APP" fi echo "Built: $APP"