#!/usr/bin/env bash # Build from this checkout and install a standalone CLI for the current user. set -euo pipefail usage() { cat <<'EOF' Usage: scripts/install-native.sh [--bin-dir DIR] Build and install ai-engineering and the aie shortcut (Linux/macOS). Default destination: $HOME/.local/bin. Run again to update. Uses GRAALVM_HOME, a compatible JAVA_HOME, or installs the GraalVM pinned in .sdkmanrc through SDKMAN (bootstrapped when absent). Requires JDK 25 with Native Image and the platform's C/C++ toolchain. Downloads require internet. Does not install system packages or edit shell profiles. Options: --bin-dir DIR Installation directory (no sudo needed for the default). -h, --help Show this help. EOF } die() { printf 'Error: %s\n' "$*" >&2; exit 1; } log() { printf '\n==> %s\n' "$*"; } bin_dir="${HOME:?HOME must be set}/.local/bin" while [[ $# -gt 0 ]]; do case "$1" in --bin-dir) [[ $# -ge 2 && -n "$2" && "$2" != --* ]] || die '--bin-dir requires a directory.' bin_dir=$2 shift 2 ;; -h|--help) usage; exit 0 ;; *) usage >&2; die "Unknown argument: $1" ;; esac done project_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P) platform=$(uname -s) architecture=$(uname -m) case "$platform/$architecture" in Linux/x86_64|Linux/aarch64|Linux/arm64|Darwin/arm64) ;; Darwin/x86_64) die 'The pinned GraalVM does not support macOS Intel. Use the JVM build (see README.md).' ;; *) die "Unsupported platform: $platform/$architecture (requires Linux x64/ARM64 or macOS Apple Silicon)." ;; esac case "$bin_dir" in /*) ;; *) bin_dir="$PWD/$bin_dir" ;; esac for name in ai-engineering aie; do [[ ! -d "$bin_dir/$name" ]] || die "Installation target is a directory: $bin_dir/$name" done work_dir=$(mktemp -d "${TMPDIR:-/tmp}/aie-native.XXXXXXXX") stage_dir='' cleanup() { rm -rf -- "$work_dir" if [[ -n "$stage_dir" ]]; then rm -rf -- "$stage_dir"; fi } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM toolchain_help() { if [[ "$platform" == Darwin ]]; then printf 'Install Apple Command Line Tools: xcode-select --install\n' >&2 else cat >&2 <<'EOF' Install the C/C++ toolchain and zlib development headers, then retry: Debian/Ubuntu: sudo apt-get install build-essential zlib1g-dev Fedora/RHEL: sudo dnf install gcc gcc-c++ glibc-devel zlib-devel libstdc++-static EOF fi } log 'Checking native build prerequisites' if [[ "$platform" == Darwin ]]; then compiler=clang if ! xcode-select -p >/dev/null 2>&1; then toolchain_help die 'Apple Command Line Tools are missing.' fi else compiler=gcc fi if ! command -v "$compiler" >/dev/null 2>&1; then toolchain_help die "Missing compiler: $compiler" fi cat > "$work_dir/check.c" <<'EOF' #include #include int main(void) { return zlibVersion() == NULL; } EOF link_flags=(-lz) if [[ "$platform" == Linux ]]; then link_flags+=(-lstdc++); fi if ! "$compiler" "$work_dir/check.c" -o "$work_dir/check" "${link_flags[@]}"; then toolchain_help die 'The compiler, system headers or native libraries are incomplete.' fi compatible_graalvm() { local candidate=$1 java_version native_version [[ -x "$candidate/bin/java" && -x "$candidate/bin/native-image" ]] || return 1 java_version=$("$candidate/bin/java" -version 2>&1) || return 1 native_version=$("$candidate/bin/native-image" --version 2>&1) || return 1 [[ "$java_version" =~ version\ \"25[.\"] && "$native_version" =~ 25[.\ ] ]] } if [[ -n "${GRAALVM_HOME:-}" ]]; then compatible_graalvm "$GRAALVM_HOME" || die 'GRAALVM_HOME must contain GraalVM JDK 25 with Native Image.' graalvm_dir=$GRAALVM_HOME elif [[ -n "${JAVA_HOME:-}" ]] && compatible_graalvm "$JAVA_HOME"; then graalvm_dir=$JAVA_HOME else # Read just the pinned value; do not execute repository configuration. pinned_java=$(sed -n 's/^java=//p' "$project_dir/.sdkmanrc" | tr -d '\r') [[ -n "$pinned_java" && "$pinned_java" != *$'\n'* ]] || die 'Expected one java= version in .sdkmanrc.' export SDKMAN_DIR="${SDKMAN_DIR:-$HOME/.sdkman}" graalvm_dir="$SDKMAN_DIR/candidates/java/$pinned_java" if ! compatible_graalvm "$graalvm_dir"; then for dependency in curl zip unzip; do command -v "$dependency" >/dev/null 2>&1 || die "Install $dependency before bootstrapping GraalVM with SDKMAN." done if [[ ! -s "$SDKMAN_DIR/bin/sdkman-init.sh" ]]; then log "Installing SDKMAN in $SDKMAN_DIR" curl --fail --show-error --silent --location --retry 3 \ 'https://get.sdkman.io?ci=true&rcupdate=false' -o "$work_dir/sdkman-install.sh" bash "$work_dir/sdkman-install.sh" fi log "Installing GraalVM $pinned_java through SDKMAN" # SDKMAN shell functions are not compatible with nounset/errexit. # Isolate their shell options and decline changing an existing default JDK. ( set +eu source "$SDKMAN_DIR/bin/sdkman-init.sh" || exit 1 sdkman_auto_answer=false sdk install java "$pinned_java" <<< 'n' ) || die "SDKMAN could not install $pinned_java. Configure GRAALVM_HOME with a compatible GraalVM JDK 25 and retry." fi compatible_graalvm "$graalvm_dir" || die "Invalid GraalVM installation: $graalvm_dir" fi graalvm_dir=$(cd -- "$graalvm_dir" && pwd -P) export JAVA_HOME="$graalvm_dir" export GRAALVM_HOME="$graalvm_dir" export PATH="$JAVA_HOME/bin:$PATH" log "Building with $JAVA_HOME" ( cd -- "$project_dir" # Explicitly select this JDK for Gradle toolchain discovery as well as its JVM. ./gradlew --no-daemon --console=plain \ "-Dorg.gradle.java.home=$JAVA_HOME" \ "-Porg.gradle.java.installations.paths=$JAVA_HOME" \ -Porg.gradle.java.installations.auto-detect=false \ -Porg.gradle.java.installations.auto-download=false nativeCompile ) native_binary="$project_dir/build/native/nativeCompile/ai-engineering" [[ -x "$native_binary" ]] || die "Build did not produce an executable: $native_binary" log "Installing in $bin_dir" mkdir -p -- "$bin_dir" bin_dir=$(cd -- "$bin_dir" && pwd -P) # Stage on the destination filesystem. Rename replaces old binaries/symlinks # without following a previous symlink back into this checkout. stage_dir=$(mktemp -d "$bin_dir/.aie-install.XXXXXXXX") install -m 755 "$native_binary" "$stage_dir/ai-engineering" "$stage_dir/ai-engineering" --help >/dev/null ln -s ai-engineering "$stage_dir/aie" mv -f -- "$stage_dir/ai-engineering" "$bin_dir/ai-engineering" mv -f -- "$stage_dir/aie" "$bin_dir/aie" log "Installed: $bin_dir/ai-engineering (shortcut: $bin_dir/aie)" case ":$PATH:" in *":$bin_dir:"*) ;; *) printf 'Add this directory to your shell profile and current session:\n export PATH=%q:"$PATH"\n' "$bin_dir" ;; esac printf 'Try: %q --help\n' "$bin_dir/aie"