1 #!/usr/bin/env bash
2 # build-jvm.sh
3 #
4 # Builds the Onde Rust crate as a shared library for the host JVM platform
5 # and copies the output into lib/src/jvmMain/resources/native/<os-arch>/ for
6 # bundling in the JVM JAR.
7 #
8 # This script detects the host OS and architecture automatically. For
9 # cross-compilation, set ONDE_TARGET_TRIPLE explicitly.
10 #
11 # Prerequisites
12 # -------------
13 # 1. Rust stable toolchain installed
14 # 2. On macOS: Xcode command line tools (for Metal backend)
15 # 3. On Linux: standard build tools (gcc, etc.)
16 #
17 # Usage
18 # -----
19 # ./scripts/build-jvm.sh # host platform, release
20 # ./scripts/build-jvm.sh --debug # host platform, debug
21 # ONDE_TARGET_TRIPLE=x86_64-apple-darwin ./scripts/build-jvm.sh # cross-compile
22
23 set -euo pipefail
24
25 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
26 SDK_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
27 REPO_ROOT="$(cd "$SDK_DIR/../.." && pwd)"
28 RESOURCES_DIR="$SDK_DIR/lib/src/jvmMain/resources/native"
29
30 PROFILE="release"
31 for arg in "$@"; do
32 if [[ "$arg" == "--debug" ]]; then
33 PROFILE="debug"
34 fi
35 done
36
37 # ── Detect host platform ──────────────────────────────────────────────────────
38
39 detect_platform() {
40 local os arch
41
42 case "$(uname -s)" in
43 Darwin) os="macos" ;;
44 Linux) os="linux" ;;
45 *) echo "Error: unsupported OS: $(uname -s)"; exit 1 ;;
46 esac
47
48 case "$(uname -m)" in
49 arm64|aarch64) arch="aarch64" ;;
50 x86_64|amd64) arch="x86_64" ;;
51 *) echo "Error: unsupported architecture: $(uname -m)"; exit 1 ;;
52 esac
53
54 echo "$os-$arch"
55 }
56
57 # Map our platform label to a Rust target triple
58 platform_to_triple() {
59 case "$1" in
60 macos-aarch64) echo "aarch64-apple-darwin" ;;
61 macos-x86_64) echo "x86_64-apple-darwin" ;;
62 linux-aarch64) echo "aarch64-unknown-linux-gnu" ;;
63 linux-x86_64) echo "x86_64-unknown-linux-gnu" ;;
64 *) echo "Error: unknown platform: $1"; exit 1 ;;
65 esac
66 }
67
68 # Map our platform label to the shared library extension
69 platform_ext() {
70 case "$1" in
71 macos-*) echo "dylib" ;;
72 linux-*) echo "so" ;;
73 *) echo "Error: unknown platform: $1"; exit 1 ;;
74 esac
75 }
76
77 PLATFORM="$(detect_platform)"
78 TARGET_TRIPLE="${ONDE_TARGET_TRIPLE:-$(platform_to_triple "$PLATFORM")}"
79 LIB_EXT="$(platform_ext "$PLATFORM")"
80 MACOS_DEPLOYMENT_TARGET="${MACOS_DEPLOYMENT_TARGET:-14.0}"
81
82 echo "=== Building Onde for JVM (profile: $PROFILE) ==="
83 echo "Platform: $PLATFORM"
84 echo "Rust target: $TARGET_TRIPLE"
85 echo "Repository root: $REPO_ROOT"
86
87 # ── Build ──────────────────────────────────────────────────────────────────────
88
89 cd "$REPO_ROOT"
90
91 CARGO_ARGS=(build --manifest-path Cargo.toml --target "$TARGET_TRIPLE")
92 if [[ "$PROFILE" == "release" ]]; then
93 CARGO_ARGS+=(--release)
94 fi
95
96 echo "→ cargo ${CARGO_ARGS[*]}"
97 if [[ "$PLATFORM" == macos-* ]]; then
98 MACOSX_DEPLOYMENT_TARGET="$MACOS_DEPLOYMENT_TARGET" cargo "${CARGO_ARGS[@]}"
99 else
100 cargo "${CARGO_ARGS[@]}"
101 fi
102
103 # ── Copy to resources ──────────────────────────────────────────────────────────
104
105 SRC="$REPO_ROOT/target/$TARGET_TRIPLE/$PROFILE/libonde.$LIB_EXT"
106 DST_DIR="$RESOURCES_DIR/$PLATFORM"
107 DST="$DST_DIR/libonde.$LIB_EXT"
108
109 if [[ ! -f "$SRC" ]]; then
110 echo "Error: expected output not found: $SRC"
111 exit 1
112 fi
113
114 mkdir -p "$DST_DIR"
115 cp "$SRC" "$DST"
116 echo "✓ $PLATFORM -> $DST"
117
118 echo
119 echo "JVM build complete."
120 echo "Native library copied to: $DST"
121 echo
122 echo "To build the JVM JAR with the bundled native library:"
123 echo " cd $SDK_DIR && ./gradlew :lib:jvmJar"