1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 # Build the Rust FFI bridge for React Native targets.
5 #
6 # Usage:
7 # ./scripts/build-rust.sh ios # Build for iOS device + simulator
8 # ./scripts/build-rust.sh android # Build for Android architectures
9 # ./scripts/build-rust.sh all # Build for all targets
10
11 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
12 RUST_DIR="$SCRIPT_DIR/../rust"
13 IOS_XCFRAMEWORK_OUT="$SCRIPT_DIR/../ios/rust/OndeReactNative.xcframework"
14 ANDROID_OUT="$SCRIPT_DIR/../android/src/main/jniLibs"
15 IOS_DEPLOYMENT_TARGET="${IOS_DEPLOYMENT_TARGET:-16.0}"
16
17 build_ios() {
18 echo "=== Building Rust for iOS ==="
19
20 # Device (arm64)
21 echo " → aarch64-apple-ios"
22 IPHONEOS_DEPLOYMENT_TARGET="$IOS_DEPLOYMENT_TARGET" \
23 cargo build --manifest-path "$RUST_DIR/Cargo.toml" \
24 --target aarch64-apple-ios --release
25
26 # Simulator (arm64 Apple Silicon)
27 echo " → aarch64-apple-ios-sim"
28 IPHONEOS_DEPLOYMENT_TARGET="$IOS_DEPLOYMENT_TARGET" \
29 cargo build --manifest-path "$RUST_DIR/Cargo.toml" \
30 --target aarch64-apple-ios-sim --release
31
32 # Build the XCFramework directly from the cargo output — no staging dirs needed.
33 # CocoaPods picks the correct slice (device vs simulator) automatically.
34 echo " → Creating XCFramework"
35 rm -rf "$IOS_XCFRAMEWORK_OUT"
36 mkdir -p "$(dirname "$IOS_XCFRAMEWORK_OUT")"
37 xcodebuild -create-xcframework \
38 -library "$RUST_DIR/target/aarch64-apple-ios/release/libonde_react_native.a" \
39 -library "$RUST_DIR/target/aarch64-apple-ios-sim/release/libonde_react_native.a" \
40 -output "$IOS_XCFRAMEWORK_OUT"
41
42 echo " ✓ iOS XCFramework created at ios/rust/OndeReactNative.xcframework"
43 echo " ✓ Built with IPHONEOS_DEPLOYMENT_TARGET=$IOS_DEPLOYMENT_TARGET"
44 }
45
46 build_android() {
47 echo "=== Building Rust for Android ==="
48
49 # Verify NDK is available
50 if [ -z "${ANDROID_NDK_HOME:-}" ]; then
51 echo "Error: ANDROID_NDK_HOME is not set."
52 echo "Set it to your Android NDK installation path, e.g.:"
53 echo " export ANDROID_NDK_HOME=\$HOME/Library/Android/sdk/ndk/<version>"
54 exit 1
55 fi
56
57 TARGETS=(
58 "aarch64-linux-android:arm64-v8a"
59 "armv7-linux-androideabi:armeabi-v7a"
60 "x86_64-linux-android:x86_64"
61 "i686-linux-android:x86"
62 )
63
64 for entry in "${TARGETS[@]}"; do
65 RUST_TARGET="${entry%%:*}"
66 ABI="${entry##*:}"
67
68 echo " → $RUST_TARGET ($ABI)"
69 cargo build --manifest-path "$RUST_DIR/Cargo.toml" \
70 --target "$RUST_TARGET" --release
71
72 mkdir -p "$ANDROID_OUT/$ABI"
73 cp "$RUST_DIR/target/$RUST_TARGET/release/libonde_react_native.so" \
74 "$ANDROID_OUT/$ABI/libonde_react_native.so"
75 done
76
77 echo " ✓ Android shared libraries copied to android/src/main/jniLibs/"
78 }
79
80 case "${1:-all}" in
81 ios) build_ios ;;
82 android) build_android ;;
83 all) build_ios; build_android ;;
84 *)
85 echo "Usage: $0 {ios|android|all}"
86 exit 1
87 ;;
88 esac
89
90 echo ""
91 echo "Build complete."