ref:main
#!/usr/bin/env bash
set -euo pipefail
# Build native libraries for all supported platforms.
# Requires: cross (cargo install cross) for Linux targets.
# macOS targets build natively, Linux targets use cross (Docker).
# Windows is a pre-built artifact (no cross-compilation in this script).
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
RUST_DIR="$SCRIPT_DIR/rust"
NATIVES_DIR="$SCRIPT_DIR/common/src/main/resources/natives"
cd "$RUST_DIR"
# Prefer rustup toolchain over Homebrew for cross-compilation support.
# Homebrew's Rust doesn't ship cross-compilation sysroots.
RUSTUP_TOOLCHAIN="$HOME/.rustup/toolchains/stable-aarch64-apple-darwin/bin"
if [ -x "$RUSTUP_TOOLCHAIN/cargo" ]; then
export PATH="$RUSTUP_TOOLCHAIN:$PATH"
echo "Using rustup cargo: $(which cargo)"
fi
echo "=== Building native libraries ==="
# macOS aarch64 (native on Apple Silicon)
echo "[1/4] macOS aarch64..."
cargo build --release --target aarch64-apple-darwin
mkdir -p "$NATIVES_DIR/macos-aarch64"
cp target/aarch64-apple-darwin/release/libhuorn_minecraft.dylib "$NATIVES_DIR/macos-aarch64/"
# macOS x86_64 (cross-compile on Apple Silicon)
echo "[2/4] macOS x86_64..."
cargo build --release --target x86_64-apple-darwin
mkdir -p "$NATIVES_DIR/macos-x86_64"
cp target/x86_64-apple-darwin/release/libhuorn_minecraft.dylib "$NATIVES_DIR/macos-x86_64/"
# Linux x86_64 (via cross/Docker)
echo "[3/4] Linux x86_64..."
if command -v cross &>/dev/null; then
cross build --release --target x86_64-unknown-linux-gnu
mkdir -p "$NATIVES_DIR/linux-x86_64"
cp target/x86_64-unknown-linux-gnu/release/libhuorn_minecraft.so "$NATIVES_DIR/linux-x86_64/"
else
echo " SKIP: 'cross' not installed (cargo install cross)"
fi
# Linux aarch64 (via cross/Docker)
echo "[4/4] Linux aarch64..."
if command -v cross &>/dev/null; then
cross build --release --target aarch64-unknown-linux-gnu
mkdir -p "$NATIVES_DIR/linux-aarch64"
cp target/aarch64-unknown-linux-gnu/release/libhuorn_minecraft.so "$NATIVES_DIR/linux-aarch64/"
else
echo " SKIP: 'cross' not installed (cargo install cross)"
fi
echo ""
echo "=== Results ==="
find "$NATIVES_DIR" -type f -exec ls -lh {} \;
echo ""
echo "=== JNI class path verification ==="
for dir in "$NATIVES_DIR"/*/; do
platform=$(basename "$dir")
file=$(ls "$dir" 2>/dev/null | head -1)
if [ -n "$file" ]; then
match=$(strings "$dir$file" 2>/dev/null | grep -o "io/fangorn/[a-z]*/nativelib" | head -1)
echo " $platform: $match"
fi
done