#!/usr/bin/env bash # Offline-friendly helper to make a JDK available for benchmark builds. # Order of preference: # 1) Respect an existing JAVA_HOME when it contains javac. # 2) Use javac from PATH when present. # 3) Extract a vendored archive (jdk-21.0.1.tar.gz) into .jdk/ and use it. ensure_bench_jdk() { # Re-use an explicitly provided JAVA_HOME when it already has javac. if [[ -n "${JAVA_HOME:-}" && -x "${JAVA_HOME}/bin/javac" ]]; then export PATH="${JAVA_HOME}/bin:${PATH}" return 0 fi # Use any javac already on PATH. if command -v javac >/dev/null 2>&1; then return 0 fi local script_dir bench_root cache_dir archive_dir archive_path candidate script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" bench_root="$(cd "${script_dir}/../.." && pwd)" repo_root="$(cd "${bench_root}/../.." && pwd)" cache_dir="${bench_root}/.jdk" archive_dir="${cache_dir}/jdk-21.0.1+12" # Prefer an archive co-located with this script; fall back to the repo copy. for candidate in \ "${script_dir}/jdk-21.0.1.tar.gz" \ "${repo_root}/src/Sdk/StellaOps.Sdk.Generator/tools/jdk-21.0.1.tar.gz" do if [[ -f "${candidate}" ]]; then archive_path="${candidate}" break fi done if [[ -z "${archive_path:-}" ]]; then echo "[ensure_jdk] No JDK found. Set JAVA_HOME or place jdk-21.0.1.tar.gz under tools/java/." >&2 return 1 fi mkdir -p "${cache_dir}" if [[ ! -d "${archive_dir}" ]]; then tar -xzf "${archive_path}" -C "${cache_dir}" fi if [[ ! -x "${archive_dir}/bin/javac" ]]; then echo "[ensure_jdk] Extracted archive but javac not found under ${archive_dir}" >&2 return 1 fi export JAVA_HOME="${archive_dir}" export PATH="${JAVA_HOME}/bin:${PATH}" } # Allow running as a script for quick verification. if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then if ensure_bench_jdk; then java -version fi fi