| 5 | return 0 2>/dev/null || exit 0 |
| 6 | fi |
| 7 | PREBUILD_COMMON_LOADED=1 |
| 8 |
| 9 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | JNI_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 11 | LIB_DIR="${LIB_DIR:-$SCRIPT_DIR/lib}" |
| 12 | INCLUDE_DIR="${INCLUDE_DIR:-$SCRIPT_DIR/include}" |
| 13 | PKGCONFIG_DIR="${PKGCONFIG_DIR:-$SCRIPT_DIR/pkgconfig}" |
| 14 |
| 15 | NDK_VERSION="${NDK_VERSION:-27.2.12479018}" |
| 16 | API="${API:-21}" |
| 17 | ABIS="${ABIS:-arm64-v8a armeabi-v7a x86_64 x86}" |
| 18 |
| 19 | host_os="$(uname -s)" |
| 20 | case "$host_os" in |
| 21 | Darwin) |
| 22 | DEFAULT_ANDROID_SDK_ROOT="$HOME/Library/Android/sdk" |
| 23 | ;; |
| 24 | Linux) |
| 25 | DEFAULT_ANDROID_SDK_ROOT="$HOME/Android/Sdk" |
| 26 | ;; |
| 27 | *) |
| 28 | echo "ERROR: unsupported host OS: $host_os" >&2 |
| 29 | return 1 2>/dev/null || exit 1 |
| 30 | ;; |
| 31 | esac |
| 32 |
| 33 | ANDROID_SDK_ROOT="${ANDROID_SDK_ROOT:-$DEFAULT_ANDROID_SDK_ROOT}" |
| 34 | ANDROID_NDK_HOME="${ANDROID_NDK_HOME:-$ANDROID_SDK_ROOT/ndk/$NDK_VERSION}" |
| 35 | TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" |
| 36 |
| 37 | # Resolve the host-specific LLVM prebuilt directory once for scripts that call |
| 38 | # the NDK compilers directly (libvpx/dav1d/FFmpeg). |
| 39 | case "$host_os" in |
| 40 | Darwin) |
| 41 | case "$(uname -m)" in |
| 42 | arm64) |
| 43 | if [[ -d "$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-arm64" ]]; then |
| 44 | NDK_HOST_TAG="darwin-arm64" |
| 45 | else |
| 46 | NDK_HOST_TAG="darwin-x86_64" |
| 47 | fi |
| 48 | ;; |
| 49 | x86_64) NDK_HOST_TAG="darwin-x86_64" ;; |
| 50 | *) |
| 51 | echo "ERROR: unsupported macOS host architecture: $(uname -m)" >&2 |
| 52 | return 1 2>/dev/null || exit 1 |
| 53 | ;; |
| 54 | esac |
| 55 | ;; |
| 56 | Linux) |
| 57 | case "$(uname -m)" in |
| 58 | x86_64) NDK_HOST_TAG="linux-x86_64" ;; |
| 59 | aarch64|arm64) NDK_HOST_TAG="linux-aarch64" ;; |
| 60 | *) |
| 61 | echo "ERROR: unsupported Linux host architecture: $(uname -m)" >&2 |
| 62 | return 1 2>/dev/null || exit 1 |
| 63 | ;; |
| 64 | esac |
| 65 | ;; |
| 66 | esac |
| 67 | NDK_TOOLCHAIN="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$NDK_HOST_TAG" |
| 68 |
| 69 | # Make C/C++ outputs independent from the absolute checkout path. Clang may |
| 70 | # otherwise embed /Users/... on macOS and /home/... on Linux into object files |
| 71 | # through __FILE__, diagnostics/debug metadata and other compiler-generated data. |
| 72 | # |
| 73 | # Map both the project JNI tree and Android NDK to stable virtual locations on |
| 74 | # every host. This removes differences such as /Users/... vs /home/... not only |
| 75 | # for project sources, but also for NDK headers/sysroot paths recorded by Clang. |
| 76 | REPRODUCIBLE_SOURCE_ROOT="${REPRODUCIBLE_SOURCE_ROOT:-/jni}" |
| 77 | REPRODUCIBLE_NDK_ROOT="${REPRODUCIBLE_NDK_ROOT:-/android-ndk}" |
| 78 |
| 79 | REPRODUCIBLE_PATH_FLAGS="-ffile-prefix-map=$JNI_DIR=$REPRODUCIBLE_SOURCE_ROOT -fdebug-prefix-map=$JNI_DIR=$REPRODUCIBLE_SOURCE_ROOT -fmacro-prefix-map=$JNI_DIR=$REPRODUCIBLE_SOURCE_ROOT -ffile-prefix-map=$ANDROID_NDK_HOME=$REPRODUCIBLE_NDK_ROOT -fdebug-prefix-map=$ANDROID_NDK_HOME=$REPRODUCIBLE_NDK_ROOT -fmacro-prefix-map=$ANDROID_NDK_HOME=$REPRODUCIBLE_NDK_ROOT" |
| 80 |
| 81 | # Produce final static-library objects directly. Avoid post-build strip/objcopy, |
| 82 | # which can invalidate SHT_LLVM_ADDRSIG used by LLD --icf=safe. |
| 83 | PREBUILD_OBJECT_FLAGS="-g0 -fno-ident" |
| 84 |
| 85 | # Preserve caller-provided flags while appending the reproducibility flags. |
| 86 | CFLAGS="${CFLAGS:+$CFLAGS }$REPRODUCIBLE_PATH_FLAGS $PREBUILD_OBJECT_FLAGS" |
| 87 | CXXFLAGS="${CXXFLAGS:+$CXXFLAGS }$REPRODUCIBLE_PATH_FLAGS $PREBUILD_OBJECT_FLAGS" |
| 88 | export CFLAGS CXXFLAGS |
| 89 |
| 90 | # Pass these explicitly to CMake projects. Some projects/cache setups do not |
| 91 | # reliably preserve environment CFLAGS/CXXFLAGS across configure steps. |
| 92 | CMAKE_REPRO_C_FLAGS="$CFLAGS" |
| 93 | CMAKE_REPRO_CXX_FLAGS="$CXXFLAGS" |
| 94 |
| 95 | # Normalize environment data that can otherwise leak into generated files or |
| 96 | # archives. Individual build systems are free to ignore SOURCE_DATE_EPOCH, but |
| 97 | # exporting it is harmless and makes supported tools deterministic. |
| 98 | export SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-1}" |
| 99 | export TZ="${TZ:-UTC}" |
| 100 | export LC_ALL="${LC_ALL:-C}" |
| 101 |
| 102 | if [[ -z "${JOBS:-}" ]]; then |
| 103 | if command -v nproc >/dev/null 2>&1; then |
| 104 | JOBS="$(nproc)" |
| 105 | elif command -v sysctl >/dev/null 2>&1; then |
| 106 | JOBS="$(sysctl -n hw.logicalcpu 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 8)" |
| 107 | else |
| 108 | JOBS=8 |
| 109 | fi |
| 110 | fi |
| 111 |
| 112 | # Convert a dotted Android SDK CMake directory name into a numeric key so the |
| 113 | # newest installed version can be selected without GNU sort -V (not on macOS). |
| 114 | version_key() { |
| 115 | local version="$1" |
| 116 | awk -v version="$version" 'BEGIN { |
| 117 | n = split(version, parts, ".") |
| 118 | for (i = 1; i <= 6; ++i) { |
| 119 | value = (i <= n ? parts[i] + 0 : 0) |
| 120 | printf "%09d", value |
| 121 | } |
| 122 | }' |
| 123 | } |
| 124 |
| 125 | find_latest_sdk_tool() { |
| 126 | local tool_name="$1" |
| 127 | local cmake_root="$ANDROID_SDK_ROOT/cmake" |
| 128 | local dir version key best_key="" best_tool="" |
| 129 |
| 130 | [[ -d "$cmake_root" ]] || return 1 |
| 131 |
| 132 | for dir in "$cmake_root"/*; do |
| 133 | [[ -d "$dir" ]] || continue |
| 134 | [[ -x "$dir/bin/$tool_name" ]] || continue |
| 135 |
| 136 | version="$(basename "$dir")" |
| 137 | key="$(version_key "$version")" |
| 138 | if [[ -z "$best_key" || "$key" > "$best_key" ]]; then |
| 139 | best_key="$key" |
| 140 | best_tool="$dir/bin/$tool_name" |
| 141 | fi |
| 142 | done |
| 143 |
| 144 | [[ -n "$best_tool" ]] || return 1 |
| 145 | printf '%s\n' "$best_tool" |
| 146 | } |
| 147 |
| 148 | resolve_tool() { |
| 149 | local override="$1" |
| 150 | local tool_name="$2" |
| 151 | local path_tool="" |
| 152 | local sdk_tool="" |
| 153 |
| 154 | if [[ -n "$override" ]]; then |
| 155 | printf '%s\n' "$override" |
| 156 | return 0 |
| 157 | fi |
| 158 |
| 159 | path_tool="$(command -v "$tool_name" 2>/dev/null || true)" |
| 160 | if [[ -n "$path_tool" ]]; then |
| 161 | printf '%s\n' "$path_tool" |
| 162 | return 0 |
| 163 | fi |
| 164 |
| 165 | sdk_tool="$(find_latest_sdk_tool "$tool_name" 2>/dev/null || true)" |
| 166 | if [[ -n "$sdk_tool" ]]; then |
| 167 | printf '%s\n' "$sdk_tool" |
| 168 | return 0 |
| 169 | fi |
| 170 |
| 171 | return 1 |
| 172 | } |
| 173 |
| 174 | CMAKE_BIN="$(resolve_tool "${CMAKE_BIN:-}" cmake || true)" |
| 175 | NINJA_BIN="$(resolve_tool "${NINJA_BIN:-}" ninja || true)" |
| 176 |
| 177 | require_command() { |
| 178 | local command_name="$1" |
| 179 | command -v "$command_name" >/dev/null 2>&1 || { |
| 180 | echo "ERROR: $command_name is required" >&2 |
| 181 | return 1 |
| 182 | } |
| 183 | } |
| 184 |
| 185 | require_cmake_ninja() { |
| 186 | [[ -n "$CMAKE_BIN" && -x "$CMAKE_BIN" ]] || { |
| 187 | echo "ERROR: cmake is required" >&2 |
| 188 | echo " Install it, add it to PATH, or install CMake from Android SDK Manager." >&2 |
| 189 | echo " Android SDK searched at: $ANDROID_SDK_ROOT" >&2 |
| 190 | return 1 |
| 191 | } |
| 192 | [[ -n "$NINJA_BIN" && -x "$NINJA_BIN" ]] || { |
| 193 | echo "ERROR: ninja is required" >&2 |
| 194 | echo " Install it, add it to PATH, or install CMake from Android SDK Manager." >&2 |
| 195 | echo " Android SDK searched at: $ANDROID_SDK_ROOT" >&2 |
| 196 | return 1 |
| 197 | } |
| 198 | } |
| 199 |
| 200 | require_ndk() { |
| 201 | [[ -f "$TOOLCHAIN_FILE" ]] || { |
| 202 | echo "ERROR: Android NDK not found: $ANDROID_NDK_HOME" >&2 |
| 203 | echo " Set ANDROID_NDK_HOME, ANDROID_SDK_ROOT, or NDK_VERSION." >&2 |
| 204 | return 1 |
| 205 | } |
| 206 | } |
| 207 |
| 208 | find_ndk_tool() { |
| 209 | local tool_name="$1" |
| 210 | local candidate |
| 211 |
| 212 | for candidate in "$ANDROID_NDK_HOME"/toolchains/llvm/prebuilt/*/bin/"$tool_name"; do |
| 213 | [[ -x "$candidate" ]] || continue |
| 214 | printf '%s\n' "$candidate" |
| 215 | return 0 |
| 216 | done |
| 217 |
| 218 | return 1 |
| 219 | } |
| 220 |
| 221 | LLVM_STRIP_BIN="${LLVM_STRIP_BIN:-$(find_ndk_tool llvm-strip || true)}" |
| 222 | LLVM_OBJCOPY_BIN="${LLVM_OBJCOPY_BIN:-$(find_ndk_tool llvm-objcopy || true)}" |
| 223 | LLVM_READELF_BIN="${LLVM_READELF_BIN:-$(find_ndk_tool llvm-readelf || true)}" |
| 224 |
| 225 | require_llvm_strip() { |
| 226 | [[ -n "$LLVM_STRIP_BIN" && -x "$LLVM_STRIP_BIN" ]] || { |
| 227 | echo "ERROR: llvm-strip was not found in Android NDK: $ANDROID_NDK_HOME" >&2 |
| 228 | return 1 |
| 229 | } |
| 230 | } |
| 231 |
| 232 | require_llvm_objcopy() { |
| 233 | [[ -n "$LLVM_OBJCOPY_BIN" && -x "$LLVM_OBJCOPY_BIN" ]] || { |
| 234 | echo "ERROR: llvm-objcopy was not found in Android NDK: $ANDROID_NDK_HOME" >&2 |
| 235 | return 1 |
| 236 | } |
| 237 | } |
| 238 |
| 239 | strip_archive_debug() { |
| 240 | local archive="$1" |
| 241 | [[ -f "$archive" ]] || { |
| 242 | echo "ERROR: archive not found for stripping: $archive" >&2 |
| 243 | return 1 |
| 244 | } |
| 245 | require_llvm_strip |
| 246 | "$LLVM_STRIP_BIN" --strip-debug "$archive" |
| 247 | } |
| 248 |
| 249 | normalize_archive() { |
| 250 | local archive="$1" |
| 251 | local temporary="${archive}.normalized.$$" |
| 252 |
| 253 | [[ -f "$archive" ]] || { |
| 254 | echo "ERROR: archive not found for normalization: $archive" >&2 |
| 255 | return 1 |
| 256 | } |
| 257 |
| 258 | require_llvm_objcopy |
| 259 | rm -f "$temporary" |
| 260 |
| 261 | # llvm-objcopy understands GNU/LLVM archives directly, so archive member |
| 262 | # ordering and duplicate member names are preserved. Removing .comment |
| 263 | # eliminates host-Clang build metadata (+/-bolt, +/-mlgo) without touching |
| 264 | # code, relocations, or symbols required by the final linker. |
| 265 | if ! "$LLVM_OBJCOPY_BIN" --remove-section=.comment "$archive" "$temporary"; then |
| 266 | rm -f "$temporary" |
| 267 | return 1 |
| 268 | fi |
| 269 |
| 270 | chmod 0644 "$temporary" |
| 271 | mv -f "$temporary" "$archive" |
| 272 | } |
| 273 |
| 274 | finalize_archive() { |
| 275 | local archive="$1" |
| 276 | strip_archive_debug "$archive" |
| 277 | normalize_archive "$archive" |
| 278 | } |
| 279 |
| 280 | abi_output_dir() { |
| 281 | printf '%s/%s\n' "$LIB_DIR" "$1" |
| 282 | } |
| 283 |
| 284 | abi_include_dir() { |
| 285 | printf '%s/%s\n' "$INCLUDE_DIR" "$1" |
| 286 | } |
| 287 |
| 288 | abi_pkgconfig_dir() { |
| 289 | printf '%s/%s\n' "$PKGCONFIG_DIR" "$1" |
| 290 | } |
| 291 |
| 292 | copy_archive() { |
| 293 | local source="$1" |
| 294 | local destination="$2" |
| 295 | mkdir -p "$(dirname "$destination")" |
| 296 | cp "$source" "$destination" |
| 297 | chmod 0644 "$destination" |
| 298 | } |
| 299 |
| 300 | # Rewrite host-specific absolute paths only inside generated diagnostic/build |
| 301 | # metadata. This does not change the real paths passed to compilers/linkers. |
| 302 | normalize_host_paths_in_string() { |
| 303 | local value="$1" |
| 304 |
| 305 | # Replace the full host-specific LLVM prebuilt path before the NDK root so |
| 306 | # linux-x86_64 / darwin-x86_64 cannot leak into embedded config strings. |
| 307 | value="${value//$NDK_TOOLCHAIN/$REPRODUCIBLE_NDK_ROOT/toolchain}" |
| 308 | value="${value//$ANDROID_NDK_HOME/$REPRODUCIBLE_NDK_ROOT}" |
| 309 | value="${value//$JNI_DIR/$REPRODUCIBLE_SOURCE_ROOT}" |
| 310 |
| 311 | printf '%s' "$value" |
| 312 | } |
| 313 |
| 314 | # Normalize only lines containing MARKER. Useful for generated files where the |
| 315 | # build configuration is embedded as a runtime diagnostic string. |
| 316 | normalize_generated_metadata_lines() { |
| 317 | local file="$1" |
| 318 | local marker="$2" |
| 319 | local temporary line normalized found=0 |
| 320 |
| 321 | [[ -f "$file" ]] || { |
| 322 | echo "ERROR: generated metadata file not found: $file" >&2 |
| 323 | return 1 |
| 324 | } |
| 325 |
| 326 | temporary="$(mktemp "${file}.normalized.XXXXXX")" |
| 327 | while IFS= read -r line || [[ -n "$line" ]]; do |
| 328 | if [[ "$line" == *"$marker"* ]]; then |
| 329 | normalized="$(normalize_host_paths_in_string "$line")" |
| 330 | printf '%s\n' "$normalized" >> "$temporary" |
| 331 | found=1 |
| 332 | else |
| 333 | printf '%s\n' "$line" >> "$temporary" |
| 334 | fi |
| 335 | done < "$file" |
| 336 |
| 337 | if [[ "$found" != "1" ]]; then |
| 338 | rm -f "$temporary" |
| 339 | echo "ERROR: metadata marker '$marker' was not found in: $file" >&2 |
| 340 | return 1 |
| 341 | fi |
| 342 |
| 343 | chmod --reference="$file" "$temporary" 2>/dev/null || chmod 0644 "$temporary" |
| 344 | mv -f "$temporary" "$file" |
| 345 | } |
| 346 |
| 347 |
| 348 | command_version_line() { |
| 349 | local label="$1" |
| 350 | local command_name="$2" |
| 351 | shift 2 |
| 352 |
| 353 | local command_path |
| 354 | command_path="$(command -v "$command_name" 2>/dev/null || true)" |
| 355 | if [[ -z "$command_path" ]]; then |
| 356 | printf '%-16s %s\n' "$label" '<not installed>' |
| 357 | return 0 |
| 358 | fi |
| 359 |
| 360 | local version |
| 361 | version="$($command_name "$@" 2>&1 | head -n 1 || true)" |
| 362 | printf '%-16s %s | %s\n' "$label" "$version" "$command_path" |
| 363 | } |
| 364 |
| 365 | write_build_environment() { |
| 366 | local destination="${1:-$SCRIPT_DIR/build/env.txt}" |
| 367 | local clang_bin="$NDK_TOOLCHAIN/bin/clang" |
| 368 |
| 369 | mkdir -p "$(dirname "$destination")" |
| 370 | { |
| 371 | echo "Build environment" |
| 372 | echo "Generated: $(date -u '+%Y-%m-%dT%H:%M:%SZ')" |
| 373 | echo "Host OS: $host_os" |
| 374 | echo "Host arch: $(uname -m)" |
| 375 | echo "ABIs: $ABIS" |
| 376 | echo "API: $API" |
| 377 | echo "Jobs: $JOBS" |
| 378 | echo "Android SDK: $ANDROID_SDK_ROOT" |
| 379 | echo "Android NDK: $ANDROID_NDK_HOME" |
| 380 | echo "NDK version: $NDK_VERSION" |
| 381 | echo "NDK host tag: $NDK_HOST_TAG" |
| 382 | if [[ -x "$clang_bin" ]]; then |
| 383 | printf '%-16s %s | %s\n' "Clang" "$("$clang_bin" --version 2>&1 | head -n 1)" "$clang_bin" |
| 384 | fi |
| 385 | if [[ -n "$CMAKE_BIN" && -x "$CMAKE_BIN" ]]; then |
| 386 | printf '%-16s %s | %s\n' "CMake" "$("$CMAKE_BIN" --version 2>&1 | head -n 1)" "$CMAKE_BIN" |
| 387 | fi |
| 388 | if [[ -n "$NINJA_BIN" && -x "$NINJA_BIN" ]]; then |
| 389 | printf '%-16s %s | %s\n' "Ninja" "$("$NINJA_BIN" --version 2>&1 | head -n 1)" "$NINJA_BIN" |
| 390 | fi |
| 391 | command_version_line "Make" make --version |
| 392 | command_version_line "NASM" nasm -v |
| 393 | command_version_line "Yasm" yasm --version |
| 394 | command_version_line "Meson" meson --version |
| 395 | command_version_line "pkg-config" pkg-config --version |
| 396 | command_version_line "gperf" gperf --version |
| 397 | command_version_line "Python" python3 --version |
| 398 | command_version_line "Rust" rustc --version |
| 399 | command_version_line "Cargo" cargo --version |
| 400 | command_version_line "Go" go version |
| 401 | command_version_line "Perl" perl -v |
| 402 | echo "SOURCE_DATE_EPOCH: ${SOURCE_DATE_EPOCH:-}" |
| 403 | echo "TZ: ${TZ:-}" |
| 404 | echo "LC_ALL: ${LC_ALL:-}" |
| 405 | } > "$destination" |
| 406 |
| 407 | echo "Build environment saved: $destination" |
| 408 | } |
| 409 |
| 410 | print_common_config() { |
| 411 | echo "Host: $host_os" |
| 412 | echo "Jobs: $JOBS" |
| 413 | echo "ABIs: $ABIS" |
| 414 | echo "Library output: $LIB_DIR" |
| 415 | echo "Generated header output: $INCLUDE_DIR" |
| 416 | echo "Pkg-config output: $PKGCONFIG_DIR" |
| 417 | echo "Reproducible source root: $REPRODUCIBLE_SOURCE_ROOT" |
| 418 | echo "Reproducible NDK root: $REPRODUCIBLE_NDK_ROOT" |
| 419 | [[ -n "$CMAKE_BIN" ]] && echo "CMake: $CMAKE_BIN" |
| 420 | [[ -n "$NINJA_BIN" ]] && echo "Ninja: $NINJA_BIN" |
| 421 | [[ -n "$LLVM_STRIP_BIN" ]] && echo "llvm-strip: $LLVM_STRIP_BIN" |
| 422 | [[ -n "$LLVM_OBJCOPY_BIN" ]] && echo "llvm-objcopy: $LLVM_OBJCOPY_BIN" |
| 423 | [[ -n "$LLVM_READELF_BIN" ]] && echo "llvm-readelf: $LLVM_READELF_BIN" |
| 424 | } |
| 425 |