#!/usr/bin/env bash # Build Cotton's JNI adapter for the Android ABIs the app ships, or for the # Linux JVM smoke-test host. # # scripts/build-android-core.sh [--abi ] [--host] [--release] # # Android products go to the Gradle source set's jniLibs directory. They are # generated and intentionally never committed. The host product stays in # Cargo's target directory so a JVM smoke test can load it by the path printed # here without copying a Linux binary into an Android package. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" CRATES_DIR="$ROOT/crates" JNI_LIBS_DIR="${COTTON_ANDROID_JNI_LIBS_DIR:-$ROOT/apps/cotton-android/app/src/main/jniLibs}" BUILD_LOCK_SCRIPT="${COTTON_BUILD_LOCK_SCRIPT:-$ROOT/scripts/with-build-lock.sh}" DEFAULT_NDK="/opt/android-sdk/ndk/27.1.12297006" API_LEVEL=28 ABIS=(arm64-v8a armeabi-v7a x86 x86_64) HOST=0 RELEASE=0 REQUESTED_ABI="" while [ $# -gt 0 ]; do case "$1" in --abi) [ $# -ge 2 ] || { echo "build-android-core: --abi needs a value" >&2; exit 2; } REQUESTED_ABI="$2" shift 2 ;; --host) HOST=1; shift ;; --release) RELEASE=1; shift ;; *) echo "usage: build-android-core.sh [--abi ] [--host] [--release]" >&2 exit 2 ;; esac done if [ "$HOST" = 1 ] && [ -n "$REQUESTED_ABI" ]; then echo "build-android-core: --host and --abi cannot be combined" >&2 exit 2 fi [ "$(uname -s)" = "Linux" ] || { echo "build-android-core: Android core builds are supported on Linux only" >&2 exit 1 } if [ -n "$REQUESTED_ABI" ]; then case "$REQUESTED_ABI" in arm64-v8a|armeabi-v7a|x86|x86_64) ABIS=("$REQUESTED_ABI") ;; *) echo "build-android-core: unsupported ABI '$REQUESTED_ABI' (choose arm64-v8a, armeabi-v7a, x86, or x86_64)" >&2 exit 2 ;; esac fi # Cargo's process fan-out is at most six, and six when the caller has not # configured it. A SMALLER value the caller set is kept: in CI it is the build slot's share of the host # (`scripts/ci/build-slot.sh`, THE SHARE — `min(asked, cores / (busy + 1))`), # and a hard six here undid that cap for the whole four-ABI build. If this # process already has a taskset affinity, every Cargo/NDK child inherits it; do # not widen that cap here. android_jobs="${CARGO_BUILD_JOBS:-6}" case "$android_jobs" in ''|*[!0-9]*|0) android_jobs=6 ;; esac [ "$android_jobs" -le 6 ] || android_jobs=6 export CARGO_BUILD_JOBS="$android_jobs" export PATH="$HOME/.cargo/bin:$PATH" # EMPTY-ARRAY EXPANSION, SPELLED FOR bash 3.2 AS WELL AS 5. # # `${profile_args[*]}` and `${profile_args[@]}` on an EMPTY array are an unbound # variable under `set -u` in bash 3.2 — the bash macOS ships — and this script # runs on a developer's Mac whenever its tests do. Seven of those tests could # only ever fail there. So every expansion of these two arrays uses the # `${a[@]+"${a[@]}"}` form, which is empty-safe in both, the same idiom # `sweep-worktree-targets.sh` uses for the same reason. profile_args=() profile_dir=debug if [ "$RELEASE" = 1 ]; then profile_args=(--release) profile_dir=release fi # The one for the human-readable echo, where an empty array must print nothing. profile_shown="${profile_args[*]+${profile_args[*]}}" run_cargo() { BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-cotton-android-core}" \ bash "$BUILD_LOCK_SCRIPT" cargo "$@" } require_ndk() { local ndk="${ANDROID_NDK_HOME:-}" if [ -z "$ndk" ]; then [ -d "$DEFAULT_NDK" ] || { echo "build-android-core: ANDROID_NDK_HOME is unset and the machine NDK is absent at $DEFAULT_NDK" >&2 exit 1 } ndk="$DEFAULT_NDK" fi [ -d "$ndk" ] || { echo "build-android-core: ANDROID_NDK_HOME is not a directory: $ndk" >&2 exit 1 } export ANDROID_NDK_HOME="$ndk" } cd "$CRATES_DIR" command -v cargo >/dev/null || { echo "build-android-core: cargo is not on PATH" >&2; exit 1; } # A worktree may set target-dir in .cargo/config.toml, which beats Cargo's # default just as CARGO_TARGET_DIR does. When neither names one, the lock # wrapper builds in the machine's one shared target (with-build-lock.sh, # scripts/ci/cargo-target-dir.sh); set it here first so Cargo answers with the # directory the build writes to, then ask Cargo rather than guessing. if [ -z "${CARGO_TARGET_DIR:-}" ] && ! grep -qs '^[[:space:]]*target-dir[[:space:]]*=' "$CRATES_DIR/.cargo/config.toml" "$ROOT/.cargo/config.toml"; then CARGO_TARGET_DIR="$(bash "$ROOT/scripts/ci/cargo-target-dir.sh" shared)" export CARGO_TARGET_DIR fi target_dir="$(cargo metadata --locked --no-deps --format-version 1 | jq -r .target_directory)" [ -n "$target_dir" ] && [ "$target_dir" != "null" ] || { echo "build-android-core: cargo metadata named no target directory" >&2 exit 1 } if [ "$HOST" = 1 ]; then echo "[android-core] cargo build --locked -p cotton-android-jni $profile_shown" >&2 run_cargo build --locked -p cotton-android-jni ${profile_args[@]+"${profile_args[@]}"} host_library="$target_dir/$profile_dir/libcotton_android_jni.so" [ -f "$host_library" ] || { echo "[android-core] no host JNI library at $host_library" >&2 exit 1 } printf '%s\n' "$host_library" exit 0 fi require_ndk # Intentionally a plain loop: all four ABI builds run one after another while # holding the same machine-wide mutex. Parallel ABI compilation exceeds this # machine's Android build budget and gains little from Cargo's own cache. for abi in "${ABIS[@]}"; do echo "[android-core] cargo ndk -t $abi -P $API_LEVEL -o $JNI_LIBS_DIR build --locked -p cotton-android-jni $profile_shown" >&2 run_cargo ndk -t "$abi" -P "$API_LEVEL" -o "$JNI_LIBS_DIR" build --locked -p cotton-android-jni ${profile_args[@]+"${profile_args[@]}"} library="$JNI_LIBS_DIR/$abi/libcotton_android_jni.so" [ -f "$library" ] || { echo "[android-core] cargo-ndk did not write $library" >&2 exit 1 } printf '%s\n' "$library" done