|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Build script for generating Kotlin bindings using UniFFI |
| 4 | +# |
| 5 | +# This script: |
| 6 | +# 1. Builds the lni library with uniffi feature |
| 7 | +# 2. Uses uniffi-bindgen to generate Kotlin bindings from the shared library |
| 8 | +# 3. Optionally builds for Android targets |
| 9 | +# |
| 10 | +# Usage: ./build.sh [--release] [--android] |
| 11 | +# ./build.sh --release --android # Build for Android in release mode |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 16 | +ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 17 | +EXAMPLE_DIR="$SCRIPT_DIR/example" |
| 18 | + |
| 19 | +# Parse arguments |
| 20 | +BUILD_TYPE="debug" |
| 21 | +BUILD_ANDROID=false |
| 22 | + |
| 23 | +for arg in "$@"; do |
| 24 | + case $arg in |
| 25 | + --release) |
| 26 | + BUILD_TYPE="release" |
| 27 | + ;; |
| 28 | + --android) |
| 29 | + BUILD_ANDROID=true |
| 30 | + ;; |
| 31 | + esac |
| 32 | +done |
| 33 | + |
| 34 | +echo "Building LNI library with UniFFI feature ($BUILD_TYPE)..." |
| 35 | + |
| 36 | +cd "$ROOT_DIR" |
| 37 | + |
| 38 | +# Build for host platform (needed for uniffi-bindgen) |
| 39 | +if [ "$BUILD_TYPE" == "release" ]; then |
| 40 | + cargo build --package lni --features uniffi --release |
| 41 | + LIB_PATH="$ROOT_DIR/target/release" |
| 42 | +else |
| 43 | + cargo build --package lni --features uniffi |
| 44 | + LIB_PATH="$ROOT_DIR/target/debug" |
| 45 | +fi |
| 46 | + |
| 47 | +# Build for Android targets if requested |
| 48 | +if [ "$BUILD_ANDROID" = true ]; then |
| 49 | + echo "" |
| 50 | + echo "Building for Android targets..." |
| 51 | + |
| 52 | + # Android target configurations: (rust_target, jni_dir) |
| 53 | + ANDROID_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 | + # Create jniLibs directories |
| 61 | + JNILIBS_DIR="$EXAMPLE_DIR/app/src/main/jniLibs" |
| 62 | + |
| 63 | + for target_pair in "${ANDROID_TARGETS[@]}"; do |
| 64 | + RUST_TARGET="${target_pair%%:*}" |
| 65 | + JNI_DIR="${target_pair##*:}" |
| 66 | + |
| 67 | + echo " Building for $RUST_TARGET..." |
| 68 | + |
| 69 | + if [ "$BUILD_TYPE" == "release" ]; then |
| 70 | + cargo build --package lni --features uniffi --release --target "$RUST_TARGET" |
| 71 | + ANDROID_LIB_PATH="$ROOT_DIR/target/$RUST_TARGET/release/liblni.so" |
| 72 | + else |
| 73 | + cargo build --package lni --features uniffi --target "$RUST_TARGET" |
| 74 | + ANDROID_LIB_PATH="$ROOT_DIR/target/$RUST_TARGET/debug/liblni.so" |
| 75 | + fi |
| 76 | + |
| 77 | + # Copy to jniLibs |
| 78 | + mkdir -p "$JNILIBS_DIR/$JNI_DIR" |
| 79 | + cp "$ANDROID_LIB_PATH" "$JNILIBS_DIR/$JNI_DIR/" |
| 80 | + echo " Copied to $JNILIBS_DIR/$JNI_DIR/liblni.so" |
| 81 | + done |
| 82 | + |
| 83 | + echo "Android builds complete!" |
| 84 | +fi |
| 85 | + |
| 86 | +# Find the shared library (Linux: .so, macOS: .dylib) |
| 87 | +if [ -f "$LIB_PATH/liblni.so" ]; then |
| 88 | + LIB_FILE="$LIB_PATH/liblni.so" |
| 89 | +elif [ -f "$LIB_PATH/liblni.dylib" ]; then |
| 90 | + LIB_FILE="$LIB_PATH/liblni.dylib" |
| 91 | +else |
| 92 | + echo "Error: Could not find liblni.so or liblni.dylib in $LIB_PATH" |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +echo "Found library: $LIB_FILE" |
| 97 | + |
| 98 | +# Build the uniffi-bindgen tool |
| 99 | +echo "Building uniffi-bindgen..." |
| 100 | +cargo build --package lni-kotlin-bindgen |
| 101 | + |
| 102 | +# Create output directory |
| 103 | +OUTPUT_DIR="$SCRIPT_DIR/src/main/kotlin" |
| 104 | +mkdir -p "$OUTPUT_DIR" |
| 105 | + |
| 106 | +echo "Generating Kotlin bindings..." |
| 107 | +cargo run --package lni-kotlin-bindgen -- generate --library "$LIB_FILE" --language kotlin --out-dir "$OUTPUT_DIR" |
| 108 | + |
| 109 | +echo "" |
| 110 | +echo "Kotlin bindings generated successfully in: $OUTPUT_DIR" |
| 111 | +echo "" |
| 112 | +echo "Generated files:" |
| 113 | +ls -la "$OUTPUT_DIR" |
0 commit comments