#!/usr/bin/env bash# Bootstrap the Android Rust lane. This script installs only cargo-ndk into its# user cache; the Android SDK and NDK are provisioned read-only on the runner.set -euo pipefailoriginal_cwd="$(pwd -P)"script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"repo_root="$(cd "$script_dir/../.." && pwd -P)"if [ "$(uname -s)" != "Linux" ]; then echo "setup-android: Linux only" >&2exit1fi# The runner's Rust tools live here; do this before any rustup/cargo call.PATH="$HOME/.cargo/bin:$PATH"# The workflow pins JAVA_HOME. Refuse a broken value rather than validating a# system Java while Gradle later receives the broken JAVA_HOME.if [ -n "${JAVA_HOME:-}" ]; then [ -x "$JAVA_HOME/bin/java" ] && [ -x "$JAVA_HOME/bin/javac" ] || { echo "setup-android: JAVA_HOME must contain executable java and javac" >&2exit1 } PATH="$JAVA_HOME/bin:$PATH"fiif [ -n "${ANDROID_HOME:-}" ] && [ -n "${ANDROID_SDK_ROOT:-}" ] && [ "$ANDROID_HOME" != "$ANDROID_SDK_ROOT" ]; then echo "setup-android: ANDROID_HOME and ANDROID_SDK_ROOT disagree" >&2exit1fiANDROID_HOME="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/opt/android-sdk}}"ANDROID_SDK_ROOT="$ANDROID_HOME"ANDROID_NDK_HOME="${ANDROID_NDK_HOME:-$ANDROID_HOME/ndk/27.1.12297006}"api_directory="$ANDROID_HOME/platforms/android-36"ndk_clang="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"[ -d "$api_directory" ] || { echo "setup-android: Android API 36 is missing at $api_directory" >&2exit1}[ -x "$ndk_clang" ] || { echo "setup-android: Android NDK 27 clang is missing at $ndk_clang" >&2exit1}java_version="$(java -version 2>&1 | head -n 1)" || { echo "setup-android: java could not be run" >&2exit1}javac_version="$(javac -version 2>&1)" || { echo "setup-android: javac could not be run" >&2exit1}printf '%s\n'"$java_version" | grep -Eq 'version "21([.][0-9]+)*([+][0-9]+)?"' || { echo "setup-android: JDK 21 is required (java: $java_version)" >&2exit1}printf '%s\n'"$javac_version" | grep -Eq '^javac 21([.][0-9]+)*([+][0-9]+)?$' || { echo "setup-android: JDK 21 is required (javac: $javac_version)" >&2exit1}[ -d "$repo_root/crates" ] || { echo "setup-android: crates directory is missing below $repo_root" >&2exit1}export ANDROID_HOME ANDROID_SDK_ROOT ANDROID_NDK_HOME PATH# At most six, and fewer when the caller says so — under `build-slot.sh run` the# caller's value is the slot's share, and a hard six here undid that cap (see# build-android-core.sh).android_jobs="${CARGO_BUILD_JOBS:-6}"case"$android_jobs"in''|*[!0-9]*|0) android_jobs=6 ;; esac[ "$android_jobs" -le 6 ] || android_jobs=6export CARGO_BUILD_JOBS="$android_jobs"# The Linux Rust job provisions this persistent layout, not ~/.cargo. Recognize# that same installation when the runner's base PATH does not publish it.if [ -n "${CARGO_HOME:-}" ]; then PATH="$CARGO_HOME/bin:$PATH"fiif ! command -v rustup >/dev/null 2>&1 || ! command -v cargo >/dev/null 2>&1; then ci_rust_root="$HOME/.cache/cottondb/rust"if [ ! -x "$ci_rust_root/cargo/bin/rustup" ] || [ ! -x "$ci_rust_root/cargo/bin/cargo" ]; then echo "setup-android: Rust is required on PATH or in the Linux CI Rust cache at $ci_rust_root" >&2exit1fi echo "setup-android: using the Linux CI Rust cache at $ci_rust_root"export CARGO_HOME="$ci_rust_root/cargo" RUSTUP_HOME="$ci_rust_root/rustup" PATH="$CARGO_HOME/bin:$PATH"fiexport PATH# rustup must run where the repository's rust-toolchain.toml is authoritative.cd "$repo_root/crates"# EVERY rustup call this repository makes takes the same lock. A `target add` is# a WRITE to `~/.rustup`, and on a machine running more than one of this# workflow's jobs a concurrent `target list --installed` reads that directory# mid-write and comes back exit 0 with a partial list (run #158).# shellcheck source=../lib/rustup-lock.sh. "$repo_root/scripts/lib/rustup-lock.sh"for target in \ aarch64-linux-android \ armv7-linux-androideabi \ i686-linux-android \ x86_64-linux-androiddo with_rustup_lock rustup target add "$target"donecargo_ndk_version="4.1.2"cargo_ndk_root="$HOME/.cache/cottondb/android/cargo-ndk-$cargo_ndk_version"cargo_ndk_bin="$cargo_ndk_root/bin"cached_cargo_ndk="$cargo_ndk_bin/cargo-ndk"has_cargo_ndk_version() { [ "$("$1" --version 2>/dev/null || true)" = "cargo-ndk $cargo_ndk_version" ]}has_cargo_ndk_on_path() { [ "$(cargo ndk --version 2>/dev/null || true)" = "cargo-ndk $cargo_ndk_version" ]}path_cargo_ndk="$(command -v cargo-ndk 2>/dev/null || true)"cargo_ndk_publish_path=""if [ -x "$cached_cargo_ndk" ] && has_cargo_ndk_version "$cached_cargo_ndk"; then PATH="$cargo_ndk_bin:$PATH" cargo_ndk_publish_path="$cargo_ndk_bin"elif [ -n "$path_cargo_ndk" ] && has_cargo_ndk_version "$path_cargo_ndk"; then# A runner-provided exact version is already isolated from the SDK/NDK setup. cargo_ndk_publish_path="$(dirname "$path_cargo_ndk")" :elif has_cargo_ndk_on_path; then# Cargo can also find a runner-provided subcommand without a cargo-ndk# executable being directly visible on PATH. :else mkdir -p "$cargo_ndk_root" BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-ci-android-cargo-ndk}" \ bash "$repo_root/scripts/with-build-lock.sh" \ cargo install --locked -j "$CARGO_BUILD_JOBS" --root "$cargo_ndk_root" \ cargo-ndk --version "$cargo_ndk_version" [ -x "$cached_cargo_ndk" ] || { echo "setup-android: cargo-ndk installation produced no executable at $cached_cargo_ndk" >&2exit1 } PATH="$cargo_ndk_bin:$PATH" cargo_ndk_publish_path="$cargo_ndk_bin"fiexport PATHif ! has_cargo_ndk_on_path; then echo "setup-android: cargo-ndk $cargo_ndk_version is required" >&2exit1fiif [ -n "${GITHUB_PATH:-}" ] && [ -n "$cargo_ndk_publish_path" ]; then printf '%s\n'"$cargo_ndk_publish_path" >> "$GITHUB_PATH"ficd "$original_cwd"if [ "$#" -eq 0 ]; thenexit0fiexec "$@"