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