| 6 |
| # jni/prebuild/lib/<ABI>/libvpx.a |
| 7 | # |
| 8 | # Public source headers remain canonical. Only generated or ABI-dependent |
| 9 | # installed headers are copied to jni/prebuild/include/<ABI>/ as an overlay. |
| 10 |
| 11 | set -Eeuo pipefail |
| 12 |
| 13 | # Preserve the merged build's reproducibility environment before common.sh sets |
| 14 | # its generic defaults. |
| 15 | export SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-946684800}" |
| 16 | export TZ=UTC |
| 17 | export LC_ALL=C |
| 18 | export LANG=C |
| 19 | export PYTHONHASHSEED=0 |
| 20 | export ZERO_AR_DATE=1 |
| 21 | umask 022 |
| 22 |
| 23 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 24 | source "$SCRIPT_DIR/common.sh" |
| 25 |
| 26 | # common.sh exports generic CFLAGS for the other prebuild scripts. libvpx must |
| 27 | # receive exactly the flags from the merged media build through --extra-cflags. |
| 28 | unset CFLAGS CXXFLAGS |
| 29 |
| 30 | require_ndk |
| 31 | require_command make |
| 32 |
| 33 | PROJECT_ROOT="$JNI_DIR" |
| 34 | LIBVPX_SOURCE_DIR="${LIBVPX_SOURCE_DIR:-$JNI_DIR/third_party/libvpx}" |
| 35 | BUILD_ROOT="${LIBVPX_BUILD_ROOT:-$SCRIPT_DIR/build/libvpx}" |
| 36 | WORK_DIR="$BUILD_ROOT/work" |
| 37 | INSTALL_DIR="$BUILD_ROOT/install" |
| 38 |
| 39 | LIBVPX_REALTIME_ONLY="${LIBVPX_REALTIME_ONLY:-1}" |
| 40 | LIBVPX_SMALL="${LIBVPX_SMALL:-1}" |
| 41 | LIBVPX_BETTER_HW_COMPATIBILITY="${LIBVPX_BETTER_HW_COMPATIBILITY:-1}" |
| 42 | LIBVPX_WEBM_IO="${LIBVPX_WEBM_IO:-0}" |
| 43 | LIBVPX_RUNTIME_CPU_DETECT="${LIBVPX_RUNTIME_CPU_DETECT:-auto}" |
| 44 | LIBVPX_ARM_EXTENSIONS="${LIBVPX_ARM_EXTENSIONS:-0}" |
| 45 | LIBVPX_SIZE_LIMIT="${LIBVPX_SIZE_LIMIT:-4096x4096}" |
| 46 |
| 47 | UNWIND_TABLES="${UNWIND_TABLES:-0}" |
| 48 | FRAME_POINTERS="${FRAME_POINTERS:-0}" |
| 49 | FUNCTION_SECTIONS="${FUNCTION_SECTIONS:-1}" |
| 50 | ADDRSIG="${ADDRSIG:-1}" |
| 51 | LIBVPX_ADDRSIG="${LIBVPX_ADDRSIG:-0}" |
| 52 | HIDDEN_VISIBILITY="${HIDDEN_VISIBILITY:-0}" |
| 53 |
| 54 | error() { echo "ERROR: $*" >&2; } |
| 55 |
| 56 | require_bool() { |
| 57 | local name="$1" value="$2" |
| 58 | if [[ "$value" != "0" && "$value" != "1" ]]; then |
| 59 | error "$name must be 0 or 1, got: $value" |
| 60 | exit 1 |
| 61 | fi |
| 62 | } |
| 63 |
| 64 | for name in \ |
| 65 | LIBVPX_REALTIME_ONLY LIBVPX_SMALL LIBVPX_BETTER_HW_COMPATIBILITY \ |
| 66 | LIBVPX_WEBM_IO LIBVPX_ARM_EXTENSIONS UNWIND_TABLES FRAME_POINTERS \ |
| 67 | FUNCTION_SECTIONS ADDRSIG LIBVPX_ADDRSIG HIDDEN_VISIBILITY |
| 68 | do |
| 69 | require_bool "$name" "${!name}" |
| 70 | done |
| 71 | case "$LIBVPX_RUNTIME_CPU_DETECT" in |
| 72 | auto|on|off) ;; |
| 73 | *) error "LIBVPX_RUNTIME_CPU_DETECT must be auto, on or off"; exit 1 ;; |
| 74 | esac |
| 75 |
| 76 | [[ -x "$LIBVPX_SOURCE_DIR/configure" ]] || { |
| 77 | error "libvpx sources were not found in: $LIBVPX_SOURCE_DIR" |
| 78 | exit 1 |
| 79 | } |
| 80 | LIBVPX_SOURCE_DIR="$(cd "$LIBVPX_SOURCE_DIR" && pwd)" |
| 81 |
| 82 | check_x86_assembler() { |
| 83 | local abi="$1" |
| 84 | if [[ "$abi" == "x86" || "$abi" == "x86_64" ]]; then |
| 85 | if ! command -v nasm >/dev/null 2>&1 && ! command -v yasm >/dev/null 2>&1; then |
| 86 | error "NASM or Yasm is required for $abi." |
| 87 | return 1 |
| 88 | fi |
| 89 | fi |
| 90 | } |
| 91 |
| 92 | libvpx_has_option() { |
| 93 | grep -qE "^[[:space:]]*$1[[:space:]]*$" "$LIBVPX_SOURCE_DIR/configure" |
| 94 | } |
| 95 |
| 96 | # Bash 3.2 compatible: mutates the dynamically-scoped local vpx_args array in |
| 97 | # build_libvpx_for_abi, avoiding `local -n` which is unavailable on macOS Bash. |
| 98 | libvpx_toggle() { |
| 99 | local option="$1" enable="$2" |
| 100 | libvpx_has_option "$option" || return 0 |
| 101 | if [[ "$enable" == "1" ]]; then |
| 102 | vpx_args+=("--enable-${option//_/-}") |
| 103 | else |
| 104 | vpx_args+=("--disable-${option//_/-}") |
| 105 | fi |
| 106 | } |
| 107 |
| 108 | COMMON_CFLAGS_BASE="" |
| 109 | COMMON_CFLAGS_BASE+=" -ffile-prefix-map=$PROJECT_ROOT=/src" |
| 110 | COMMON_CFLAGS_BASE+=" -fdebug-prefix-map=$PROJECT_ROOT=/src" |
| 111 | COMMON_CFLAGS_BASE+=" -fmacro-prefix-map=$PROJECT_ROOT=/src" |
| 112 | COMMON_CFLAGS_BASE+=" -ffile-prefix-map=$BUILD_ROOT=/build" |
| 113 | COMMON_CFLAGS_BASE+=" -fdebug-prefix-map=$BUILD_ROOT=/build" |
| 114 | COMMON_CFLAGS_BASE+=" -fmacro-prefix-map=$BUILD_ROOT=/build" |
| 115 | COMMON_CFLAGS_BASE+=" -fdebug-compilation-dir=." |
| 116 | [[ "$FUNCTION_SECTIONS" == "1" ]] && COMMON_CFLAGS_BASE+=" -ffunction-sections -fdata-sections" |
| 117 | [[ "$HIDDEN_VISIBILITY" == "1" ]] && COMMON_CFLAGS_BASE+=" -fvisibility=hidden" |
| 118 | [[ "$UNWIND_TABLES" == "0" ]] && COMMON_CFLAGS_BASE+=" -fno-asynchronous-unwind-tables -fno-unwind-tables" |
| 119 | if [[ "$FRAME_POINTERS" == "1" ]]; then |
| 120 | COMMON_CFLAGS_BASE+=" -fno-omit-frame-pointer" |
| 121 | else |
| 122 | COMMON_CFLAGS_BASE+=" -fomit-frame-pointer" |
| 123 | fi |
| 124 | COMMON_CFLAGS_BASE+=" -g0 -fno-ident" |
| 125 | COMMON_CFLAGS_BASE="${COMMON_CFLAGS_BASE# }" |
| 126 |
| 127 | LIBVPX_COMMON_CFLAGS="$COMMON_CFLAGS_BASE" |
| 128 | LIBVPX_SKIP_STRIP=0 |
| 129 | if [[ "$ADDRSIG" == "1" && "$LIBVPX_ADDRSIG" == "1" ]]; then |
| 130 | LIBVPX_COMMON_CFLAGS+=" -faddrsig" |
| 131 | LIBVPX_SKIP_STRIP=1 |
| 132 | fi |
| 133 |
| 134 |
| 135 | copy_generated_header_overlay() { |
| 136 | local abi="$1" |
| 137 | local installed_root="$2/include" |
| 138 | local destination_root |
| 139 | destination_root="$(abi_include_dir "$abi")" |
| 140 |
| 141 | local installed relative source destination copied=0 |
| 142 | while IFS= read -r -d '' installed; do |
| 143 | relative="${installed#$installed_root/}" |
| 144 | source="$LIBVPX_SOURCE_DIR/$relative" |
| 145 | if [[ -f "$source" ]] && cmp -s "$source" "$installed"; then |
| 146 | continue |
| 147 | fi |
| 148 | destination="$destination_root/$relative" |
| 149 | mkdir -p "$(dirname "$destination")" |
| 150 | cp -f "$installed" "$destination" |
| 151 | chmod 0644 "$destination" |
| 152 | copied=$((copied + 1)) |
| 153 | echo "==> generated/ABI header: $destination" |
| 154 | done < <(find "$installed_root/vpx" -type f -name '*.h' -print0) |
| 155 |
| 156 | if [[ "$copied" == "0" ]]; then |
| 157 | echo "==> libvpx $abi: no generated/ABI-dependent public headers" |
| 158 | fi |
| 159 | } |
| 160 |
| 161 |
| 162 | install_pkgconfig_metadata() { |
| 163 | local abi="$1" prefix="$2" |
| 164 | local source_pc="$prefix/lib/pkgconfig/vpx.pc" |
| 165 | local destination_dir destination overlay |
| 166 | destination_dir="$(abi_pkgconfig_dir "$abi")" |
| 167 | destination="$destination_dir/vpx.pc" |
| 168 | overlay="$(abi_include_dir "$abi")" |
| 169 |
| 170 | [[ -f "$source_pc" ]] || { error "Missing libvpx pkg-config file: $source_pc"; return 1; } |
| 171 | mkdir -p "$destination_dir" |
| 172 | awk -v libdir="$(abi_output_dir "$abi")" -v includedir="$LIBVPX_SOURCE_DIR" -v overlay="$overlay" ' |
| 173 | /^prefix=/ { print "prefix=" libdir; next } |
| 174 | /^exec_prefix=/ { print "exec_prefix=" libdir; next } |
| 175 | /^libdir=/ { print "libdir=" libdir; next } |
| 176 | /^includedir=/ { print "includedir=" includedir; next } |
| 177 | /^Cflags:/ { print "Cflags: -I" overlay " -I" includedir; next } |
| 178 | { print } |
| 179 | ' "$source_pc" > "$destination" |
| 180 | chmod 0644 "$destination" |
| 181 | echo "==> $destination" |
| 182 | } |
| 183 |
| 184 | build_libvpx_for_abi() { |
| 185 | local abi="$1" |
| 186 | local target vpx_target extra_cflags runtime_cpu_detect prefix build_dir cc cxx symbol |
| 187 | local -a configure_env=() |
| 188 | local -a vpx_args=() |
| 189 |
| 190 | case "$abi" in |
| 191 | arm64-v8a) |
| 192 | target="aarch64-linux-android" |
| 193 | vpx_target="arm64-android-gcc" |
| 194 | extra_cflags="-O3 -fPIC -march=armv8-a" |
| 195 | ;; |
| 196 | armeabi-v7a) |
| 197 | target="armv7a-linux-androideabi" |
| 198 | vpx_target="armv7-android-gcc" |
| 199 | extra_cflags="-O3 -fPIC -march=armv7-a -mfloat-abi=softfp -mfpu=neon" |
| 200 | ;; |
| 201 | x86_64) |
| 202 | target="x86_64-linux-android" |
| 203 | vpx_target="x86_64-android-gcc" |
| 204 | extra_cflags="-O3 -fPIC -march=x86-64" |
| 205 | ;; |
| 206 | x86) |
| 207 | target="i686-linux-android" |
| 208 | vpx_target="x86-android-gcc" |
| 209 | extra_cflags="-O3 -fPIC -march=i686 -mssse3 -mfpmath=sse" |
| 210 | ;; |
| 211 | *) error "Unsupported ABI: $abi"; return 1 ;; |
| 212 | esac |
| 213 |
| 214 | extra_cflags="$extra_cflags${LIBVPX_COMMON_CFLAGS:+ $LIBVPX_COMMON_CFLAGS}" |
| 215 | check_x86_assembler "$abi" |
| 216 |
| 217 | prefix="$INSTALL_DIR/$abi" |
| 218 | build_dir="$WORK_DIR/$abi" |
| 219 | cc="$NDK_TOOLCHAIN/bin/${target}${API}-clang" |
| 220 | cxx="$NDK_TOOLCHAIN/bin/${target}${API}-clang++" |
| 221 |
| 222 | rm -rf "$build_dir" "$prefix" |
| 223 | mkdir -p "$build_dir" "$prefix" |
| 224 |
| 225 | echo |
| 226 | echo "========== libvpx: $abi, API $API ==========" |
| 227 |
| 228 | configure_env=( |
| 229 | "CC=$cc" |
| 230 | "CXX=$cxx" |
| 231 | "LD=$cc" |
| 232 | "AR=$NDK_TOOLCHAIN/bin/llvm-ar" |
| 233 | "ARFLAGS=crsD" |
| 234 | "NM=$NDK_TOOLCHAIN/bin/llvm-nm" |
| 235 | "STRIP=$NDK_TOOLCHAIN/bin/llvm-strip" |
| 236 | "READELF=$LLVM_READELF_BIN" |
| 237 | ) |
| 238 | if [[ "$abi" == "arm64-v8a" || "$abi" == "armeabi-v7a" ]]; then |
| 239 | configure_env+=("AS=$cc") |
| 240 | fi |
| 241 |
| 242 | vpx_args=( |
| 243 | --target="$vpx_target" |
| 244 | --prefix="$prefix" |
| 245 | --disable-shared |
| 246 | --enable-static |
| 247 | --enable-pic |
| 248 | --enable-optimizations |
| 249 | --enable-multithread |
| 250 | --disable-examples |
| 251 | --disable-tools |
| 252 | --disable-docs |
| 253 | --disable-unit-tests |
| 254 | --disable-install-bins |
| 255 | --disable-install-docs |
| 256 | --disable-debug |
| 257 | --enable-vp8 |
| 258 | --enable-vp9 |
| 259 | --enable-vp8-decoder |
| 260 | --enable-vp8-encoder |
| 261 | --enable-vp9-decoder |
| 262 | --enable-vp9-encoder |
| 263 | ) |
| 264 |
| 265 | runtime_cpu_detect="$LIBVPX_RUNTIME_CPU_DETECT" |
| 266 | if [[ "$runtime_cpu_detect" == "auto" ]]; then |
| 267 | case "$abi" in |
| 268 | x86|x86_64) runtime_cpu_detect="on" ;; |
| 269 | *) runtime_cpu_detect="off" ;; |
| 270 | esac |
| 271 | fi |
| 272 | if [[ "$runtime_cpu_detect" == "on" ]]; then |
| 273 | vpx_args+=(--enable-runtime-cpu-detect) |
| 274 | else |
| 275 | vpx_args+=(--disable-runtime-cpu-detect) |
| 276 | fi |
| 277 |
| 278 | libvpx_toggle realtime_only "$LIBVPX_REALTIME_ONLY" |
| 279 | libvpx_toggle small "$LIBVPX_SMALL" |
| 280 | libvpx_toggle better_hw_compatibility "$LIBVPX_BETTER_HW_COMPATIBILITY" |
| 281 | libvpx_toggle webm_io "$LIBVPX_WEBM_IO" |
| 282 |
| 283 | case "$abi" in |
| 284 | armeabi-v7a) |
| 285 | libvpx_toggle neon_asm 0 |
| 286 | ;; |
| 287 | arm64-v8a) |
| 288 | libvpx_toggle neon_asm 0 |
| 289 | libvpx_toggle neon_dotprod "$LIBVPX_ARM_EXTENSIONS" |
| 290 | libvpx_toggle neon_i8mm "$LIBVPX_ARM_EXTENSIONS" |
| 291 | libvpx_toggle sve "$LIBVPX_ARM_EXTENSIONS" |
| 292 | libvpx_toggle sve2 "$LIBVPX_ARM_EXTENSIONS" |
| 293 | ;; |
| 294 | esac |
| 295 |
| 296 | [[ -n "$LIBVPX_SIZE_LIMIT" ]] && vpx_args+=(--size-limit="$LIBVPX_SIZE_LIMIT") |
| 297 | vpx_args+=(--extra-cflags="$extra_cflags" --log="$build_dir/config.log") |
| 298 |
| 299 | local tool_shim_dir="$build_dir/tool-shims" |
| 300 | mkdir -p "$tool_shim_dir" |
| 301 | cat > "$tool_shim_dir/readelf" <<EOF |
| 302 | #!/bin/sh |
| 303 | exec "$LLVM_READELF_BIN" "\$@" |
| 304 | EOF |
| 305 | chmod +x "$tool_shim_dir/readelf" |
| 306 |
| 307 | pushd "$build_dir" >/dev/null |
| 308 | if ! env -u AS -u ASFLAGS \ |
| 309 | "PATH=$tool_shim_dir:$PATH" \ |
| 310 | "${configure_env[@]}" \ |
| 311 | "$LIBVPX_SOURCE_DIR/configure" "${vpx_args[@]}"; |
| 312 | then |
| 313 | error "libvpx configure failed for $abi. Log: $build_dir/config.log" |
| 314 | popd >/dev/null |
| 315 | return 1 |
| 316 | fi |
| 317 |
| 318 | # libvpx embeds its full configure command in vpx_codec_build_config(). |
| 319 | # Normalize host/check-out paths in that diagnostic string only; configure |
| 320 | # itself and all compiler arguments above remain untouched. |
| 321 | local vpx_config_c |
| 322 | vpx_config_c="$(find "$build_dir" -type f -name vpx_config.c -print -quit)" |
| 323 | [[ -n "$vpx_config_c" ]] || { |
| 324 | error "Generated vpx_config.c was not found for $abi" |
| 325 | popd >/dev/null |
| 326 | return 1 |
| 327 | } |
| 328 | normalize_generated_metadata_lines "$vpx_config_c" "--target=" || { |
| 329 | popd >/dev/null |
| 330 | return 1 |
| 331 | } |
| 332 |
| 333 | if [[ "$LIBVPX_SKIP_STRIP" == "1" && -f "$build_dir/config.mk" ]]; then |
| 334 | if grep -q '^HAVE_GNU_STRIP=' "$build_dir/config.mk"; then |
| 335 | sed -i.bak 's/^HAVE_GNU_STRIP=.*/HAVE_GNU_STRIP=no/' "$build_dir/config.mk" |
| 336 | rm -f "$build_dir/config.mk.bak" |
| 337 | else |
| 338 | echo 'HAVE_GNU_STRIP=no' >> "$build_dir/config.mk" |
| 339 | fi |
| 340 | fi |
| 341 |
| 342 | if ! make -j"$JOBS" 2>&1 | tee "$build_dir/build.log"; then |
| 343 | error "libvpx build failed for $abi. Log: $build_dir/build.log" |
| 344 | popd >/dev/null |
| 345 | return 1 |
| 346 | fi |
| 347 | if ! make install 2>&1 | tee "$build_dir/install.log"; then |
| 348 | error "libvpx install failed for $abi. Log: $build_dir/install.log" |
| 349 | popd >/dev/null |
| 350 | return 1 |
| 351 | fi |
| 352 | popd >/dev/null |
| 353 |
| 354 | [[ -f "$prefix/lib/libvpx.a" ]] || { error "Missing $prefix/lib/libvpx.a"; return 1; } |
| 355 | [[ -d "$prefix/include/vpx" ]] || { error "Missing $prefix/include/vpx"; return 1; } |
| 356 |
| 357 | copy_generated_header_overlay "$abi" "$prefix" |
| 358 | install_pkgconfig_metadata "$abi" "$prefix" |
| 359 |
| 360 | for symbol in vpx_codec_vp8_cx vpx_codec_vp8_dx vpx_codec_vp9_cx vpx_codec_vp9_dx; do |
| 361 | if ! "$NDK_TOOLCHAIN/bin/llvm-nm" -g --defined-only "$prefix/lib/libvpx.a" \ |
| 362 | | grep -E "[[:space:]]${symbol}$" >/dev/null; |
| 363 | then |
| 364 | error "Missing libvpx symbol for $abi: $symbol" |
| 365 | return 1 |
| 366 | fi |
| 367 | done |
| 368 |
| 369 | local destination="$(abi_output_dir "$abi")/libvpx.a" |
| 370 | copy_archive "$prefix/lib/libvpx.a" "$destination" |
| 371 | echo "==> $destination" |
| 372 | } |
| 373 |
| 374 | for abi in $ABIS; do rm -rf "$(abi_include_dir "$abi")/vpx"; done |
| 375 |
| 376 | print_common_config |
| 377 | echo "libvpx source: $LIBVPX_SOURCE_DIR" |
| 378 | echo "libvpx build root: $BUILD_ROOT" |
| 379 | echo "libvpx flags: $LIBVPX_COMMON_CFLAGS" |
| 380 |
| 381 | rm -rf "$BUILD_ROOT" |
| 382 | mkdir -p "$WORK_DIR" "$INSTALL_DIR" |
| 383 | for abi in $ABIS; do |
| 384 | build_libvpx_for_abi "$abi" |
| 385 | done |
| 386 | rm -rf "$BUILD_ROOT" |
| 387 |
| 388 | echo |
| 389 | echo "libvpx static build completed." |
| 390 |