| 6 |
| # jni/prebuild/lib/<ABI>/libdav1d.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 | export SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-946684800}" |
| 14 | export TZ=UTC |
| 15 | export LC_ALL=C |
| 16 | export LANG=C |
| 17 | export PYTHONHASHSEED=0 |
| 18 | export ZERO_AR_DATE=1 |
| 19 | umask 022 |
| 20 |
| 21 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 22 | source "$SCRIPT_DIR/common.sh" |
| 23 |
| 24 | # Keep build flags byte-for-byte equivalent to the merged media script instead |
| 25 | # of inheriting common.sh's generic CFLAGS/CXXFLAGS. |
| 26 | unset CFLAGS CXXFLAGS |
| 27 |
| 28 | require_ndk |
| 29 | require_command meson |
| 30 | require_command python3 |
| 31 |
| 32 | # Meson determines the object ordering passed to the static archiver. Different |
| 33 | # Meson versions can therefore produce byte-different libdav1d.a archives even |
| 34 | # when every .o is identical. Set MESON_VERSION to enforce the same version on |
| 35 | # all build hosts, e.g. MESON_VERSION=1.9.0. |
| 36 | MESON_VERSION="${MESON_VERSION:-}" |
| 37 | DETECTED_MESON_VERSION="$(meson --version | head -n 1)" |
| 38 | if [[ -n "$MESON_VERSION" && "$DETECTED_MESON_VERSION" != "$MESON_VERSION" ]]; then |
| 39 | echo "ERROR: Meson $MESON_VERSION is required for reproducible dav1d builds" >&2 |
| 40 | echo " Found: $DETECTED_MESON_VERSION ($(command -v meson))" >&2 |
| 41 | exit 1 |
| 42 | fi |
| 43 | require_command pkg-config |
| 44 | [[ -n "$NINJA_BIN" && -x "$NINJA_BIN" ]] || { |
| 45 | echo "ERROR: ninja is required" >&2 |
| 46 | exit 1 |
| 47 | } |
| 48 |
| 49 | PROJECT_ROOT="$JNI_DIR" |
| 50 | DAV1D_SOURCE_DIR="${DAV1D_SOURCE_DIR:-$JNI_DIR/third_party/dav1d}" |
| 51 | BUILD_ROOT="${DAV1D_BUILD_ROOT:-$SCRIPT_DIR/build/dav1d}" |
| 52 | WORK_DIR="$BUILD_ROOT/work" |
| 53 | INSTALL_DIR="$BUILD_ROOT/install" |
| 54 |
| 55 | UNWIND_TABLES="${UNWIND_TABLES:-0}" |
| 56 | FRAME_POINTERS="${FRAME_POINTERS:-0}" |
| 57 | FUNCTION_SECTIONS="${FUNCTION_SECTIONS:-1}" |
| 58 | ADDRSIG="${ADDRSIG:-1}" |
| 59 | HIDDEN_VISIBILITY="${HIDDEN_VISIBILITY:-0}" |
| 60 |
| 61 | error() { echo "ERROR: $*" >&2; } |
| 62 |
| 63 | require_bool() { |
| 64 | local name="$1" value="$2" |
| 65 | if [[ "$value" != "0" && "$value" != "1" ]]; then |
| 66 | error "$name must be 0 or 1, got: $value" |
| 67 | exit 1 |
| 68 | fi |
| 69 | } |
| 70 | for name in UNWIND_TABLES FRAME_POINTERS FUNCTION_SECTIONS ADDRSIG HIDDEN_VISIBILITY; do |
| 71 | require_bool "$name" "${!name}" |
| 72 | done |
| 73 |
| 74 | [[ -f "$DAV1D_SOURCE_DIR/meson.build" ]] || { |
| 75 | error "dav1d sources were not found in: $DAV1D_SOURCE_DIR" |
| 76 | exit 1 |
| 77 | } |
| 78 | DAV1D_SOURCE_DIR="$(cd "$DAV1D_SOURCE_DIR" && pwd)" |
| 79 |
| 80 | check_x86_assembler() { |
| 81 | local abi="$1" |
| 82 | if [[ "$abi" == "x86" || "$abi" == "x86_64" ]]; then |
| 83 | if ! command -v nasm >/dev/null 2>&1 && ! command -v yasm >/dev/null 2>&1; then |
| 84 | error "NASM or Yasm is required for $abi." |
| 85 | return 1 |
| 86 | fi |
| 87 | fi |
| 88 | } |
| 89 |
| 90 | COMMON_CFLAGS_BASE="" |
| 91 | COMMON_CFLAGS_BASE+=" -ffile-prefix-map=$PROJECT_ROOT=/src" |
| 92 | COMMON_CFLAGS_BASE+=" -fdebug-prefix-map=$PROJECT_ROOT=/src" |
| 93 | COMMON_CFLAGS_BASE+=" -fmacro-prefix-map=$PROJECT_ROOT=/src" |
| 94 | COMMON_CFLAGS_BASE+=" -ffile-prefix-map=$BUILD_ROOT=/build" |
| 95 | COMMON_CFLAGS_BASE+=" -fdebug-prefix-map=$BUILD_ROOT=/build" |
| 96 | COMMON_CFLAGS_BASE+=" -fmacro-prefix-map=$BUILD_ROOT=/build" |
| 97 | COMMON_CFLAGS_BASE+=" -fdebug-compilation-dir=." |
| 98 | [[ "$FUNCTION_SECTIONS" == "1" ]] && COMMON_CFLAGS_BASE+=" -ffunction-sections -fdata-sections" |
| 99 | [[ "$HIDDEN_VISIBILITY" == "1" ]] && COMMON_CFLAGS_BASE+=" -fvisibility=hidden" |
| 100 | [[ "$UNWIND_TABLES" == "0" ]] && COMMON_CFLAGS_BASE+=" -fno-asynchronous-unwind-tables -fno-unwind-tables" |
| 101 | if [[ "$FRAME_POINTERS" == "1" ]]; then |
| 102 | COMMON_CFLAGS_BASE+=" -fno-omit-frame-pointer" |
| 103 | else |
| 104 | COMMON_CFLAGS_BASE+=" -fomit-frame-pointer" |
| 105 | fi |
| 106 | COMMON_CFLAGS_BASE+=" -g0 -fno-ident" |
| 107 | COMMON_CFLAGS_BASE="${COMMON_CFLAGS_BASE# }" |
| 108 | COMMON_CFLAGS="$COMMON_CFLAGS_BASE" |
| 109 | [[ "$ADDRSIG" == "1" ]] && COMMON_CFLAGS+=" -faddrsig" |
| 110 |
| 111 | write_dav1d_cross_file() { |
| 112 | local abi="$1" file="$2" |
| 113 | local clang_target cpu_family cpu extra_c_args cc shared_c_args flag |
| 114 |
| 115 | case "$abi" in |
| 116 | armeabi-v7a) |
| 117 | clang_target="armv7a-linux-androideabi${API}" |
| 118 | cpu_family="arm" |
| 119 | cpu="armv7-a" |
| 120 | extra_c_args="'-march=armv7-a', '-mthumb', '-mfpu=neon', '-mfloat-abi=softfp'" |
| 121 | ;; |
| 122 | arm64-v8a) |
| 123 | clang_target="aarch64-linux-android${API}" |
| 124 | cpu_family="aarch64" |
| 125 | cpu="armv8-a" |
| 126 | extra_c_args="'-march=armv8-a'" |
| 127 | ;; |
| 128 | x86) |
| 129 | clang_target="i686-linux-android${API}" |
| 130 | cpu_family="x86" |
| 131 | cpu="i686" |
| 132 | extra_c_args="'-march=i686', '-msse3', '-mfpmath=sse'" |
| 133 | ;; |
| 134 | x86_64) |
| 135 | clang_target="x86_64-linux-android${API}" |
| 136 | cpu_family="x86_64" |
| 137 | cpu="x86_64" |
| 138 | extra_c_args="'-march=x86-64', '-msse4.1'" |
| 139 | ;; |
| 140 | *) error "Unsupported ABI for dav1d: $abi"; return 1 ;; |
| 141 | esac |
| 142 |
| 143 | cc="$NDK_TOOLCHAIN/bin/${clang_target}-clang" |
| 144 | [[ -x "$cc" ]] || { error "dav1d compiler was not found: $cc"; return 1; } |
| 145 |
| 146 | shared_c_args="" |
| 147 | for flag in $COMMON_CFLAGS; do |
| 148 | shared_c_args+=", '$flag'" |
| 149 | done |
| 150 |
| 151 | cat > "$file" <<CROSS |
| 152 | [binaries] |
| 153 | c = '$cc' |
| 154 | ar = '$NDK_TOOLCHAIN/bin/llvm-ar' |
| 155 | strip = '$NDK_TOOLCHAIN/bin/llvm-strip' |
| 156 | ranlib = '$NDK_TOOLCHAIN/bin/llvm-ranlib' |
| 157 | nm = '$NDK_TOOLCHAIN/bin/llvm-nm' |
| 158 | objcopy = '$NDK_TOOLCHAIN/bin/llvm-objcopy' |
| 159 | pkg-config = 'pkg-config' |
| 160 |
| 161 | [host_machine] |
| 162 | system = 'android' |
| 163 | cpu_family = '$cpu_family' |
| 164 | cpu = '$cpu' |
| 165 | endian = 'little' |
| 166 |
| 167 | [properties] |
| 168 | needs_exe_wrapper = true |
| 169 |
| 170 | [built-in options] |
| 171 | c_args = [$extra_c_args, '-fPIC'$shared_c_args] |
| 172 | c_link_args = ['-Wl,--gc-sections'] |
| 173 | CROSS |
| 174 | } |
| 175 |
| 176 | verify_dav1d_archive() { |
| 177 | local abi="$1" library="$2" |
| 178 | [[ -f "$library" ]] || { error "Missing dav1d archive for $abi: $library"; return 1; } |
| 179 | if ! "$NDK_TOOLCHAIN/bin/llvm-nm" -g --defined-only "$library" \ |
| 180 | | awk '$NF == "dav1d_open" { found = 1 } END { exit found ? 0 : 1 }'; |
| 181 | then |
| 182 | error "dav1d_open was not found in $library" |
| 183 | return 1 |
| 184 | fi |
| 185 | } |
| 186 |
| 187 |
| 188 | copy_generated_header_overlay() { |
| 189 | local abi="$1" |
| 190 | local installed_root="$2/include" |
| 191 | local destination_root |
| 192 | destination_root="$(abi_include_dir "$abi")" |
| 193 |
| 194 | local installed relative source destination copied=0 |
| 195 | while IFS= read -r -d '' installed; do |
| 196 | relative="${installed#$installed_root/}" |
| 197 | source="$DAV1D_SOURCE_DIR/include/$relative" |
| 198 | if [[ -f "$source" ]] && cmp -s "$source" "$installed"; then |
| 199 | continue |
| 200 | fi |
| 201 | destination="$destination_root/$relative" |
| 202 | mkdir -p "$(dirname "$destination")" |
| 203 | cp -f "$installed" "$destination" |
| 204 | chmod 0644 "$destination" |
| 205 | copied=$((copied + 1)) |
| 206 | echo "==> generated/ABI header: $destination" |
| 207 | done < <(find "$installed_root/dav1d" -type f -name '*.h' -print0) |
| 208 |
| 209 | if [[ "$copied" == "0" ]]; then |
| 210 | echo "==> dav1d $abi: no generated/ABI-dependent public headers" |
| 211 | fi |
| 212 | } |
| 213 |
| 214 |
| 215 | install_pkgconfig_metadata() { |
| 216 | local abi="$1" prefix="$2" |
| 217 | local source_pc="$prefix/lib/pkgconfig/dav1d.pc" |
| 218 | local destination_dir destination overlay source_include |
| 219 | destination_dir="$(abi_pkgconfig_dir "$abi")" |
| 220 | destination="$destination_dir/dav1d.pc" |
| 221 | overlay="$(abi_include_dir "$abi")" |
| 222 | source_include="$DAV1D_SOURCE_DIR/include" |
| 223 |
| 224 | [[ -f "$source_pc" ]] || { error "Missing dav1d pkg-config file: $source_pc"; return 1; } |
| 225 | mkdir -p "$destination_dir" |
| 226 | awk -v libdir="$(abi_output_dir "$abi")" -v includedir="$source_include" -v overlay="$overlay" ' |
| 227 | /^prefix=/ { print "prefix=" libdir; next } |
| 228 | /^libdir=/ { print "libdir=" libdir; next } |
| 229 | /^includedir=/ { print "includedir=" includedir; next } |
| 230 | /^Cflags:/ { print "Cflags: -I" overlay " -I" includedir; next } |
| 231 | { print } |
| 232 | ' "$source_pc" > "$destination" |
| 233 | chmod 0644 "$destination" |
| 234 | echo "==> $destination" |
| 235 | } |
| 236 |
| 237 | build_dav1d_for_abi() { |
| 238 | local abi="$1" prefix build_dir cross_file destination |
| 239 | check_x86_assembler "$abi" |
| 240 |
| 241 | prefix="$INSTALL_DIR/$abi" |
| 242 | build_dir="$WORK_DIR/$abi" |
| 243 | cross_file="$WORK_DIR/crossfiles/$abi.ini" |
| 244 |
| 245 | rm -rf "$build_dir" "$prefix" |
| 246 | mkdir -p "$build_dir" "$prefix" "$(dirname "$cross_file")" |
| 247 | write_dav1d_cross_file "$abi" "$cross_file" |
| 248 |
| 249 | echo |
| 250 | echo "========== dav1d: $abi, API $API ==========" |
| 251 |
| 252 | # Make SDK-provided Ninja visible to Meson on macOS without changing the |
| 253 | # selected backend or any compilation parameter. |
| 254 | local meson_path="$(dirname "$NINJA_BIN"):$PATH" |
| 255 | if ! env PATH="$meson_path" meson setup "$build_dir" "$DAV1D_SOURCE_DIR" \ |
| 256 | --cross-file "$cross_file" \ |
| 257 | --prefix "$prefix" \ |
| 258 | --libdir lib \ |
| 259 | --buildtype release \ |
| 260 | --default-library static \ |
| 261 | -Db_ndebug=true \ |
| 262 | -Db_lto=false \ |
| 263 | -Denable_tools=false \ |
| 264 | -Denable_examples=false \ |
| 265 | -Denable_tests=false \ |
| 266 | -Denable_docs=false \ |
| 267 | -Denable_asm=true; |
| 268 | then |
| 269 | error "dav1d configure failed for $abi." |
| 270 | return 1 |
| 271 | fi |
| 272 |
| 273 | if ! "$NINJA_BIN" -C "$build_dir" -j "$JOBS" 2>&1 | tee "$build_dir/build.log"; then |
| 274 | error "dav1d build failed for $abi. Log: $build_dir/build.log" |
| 275 | return 1 |
| 276 | fi |
| 277 | if ! "$NINJA_BIN" -C "$build_dir" install 2>&1 | tee "$build_dir/install.log"; then |
| 278 | error "dav1d install failed for $abi. Log: $build_dir/install.log" |
| 279 | return 1 |
| 280 | fi |
| 281 |
| 282 | verify_dav1d_archive "$abi" "$prefix/lib/libdav1d.a" |
| 283 | copy_generated_header_overlay "$abi" "$prefix" |
| 284 | install_pkgconfig_metadata "$abi" "$prefix" |
| 285 | [[ -f "$prefix/include/dav1d/dav1d.h" ]] || { error "Missing dav1d headers for $abi"; return 1; } |
| 286 | [[ -f "$prefix/lib/pkgconfig/dav1d.pc" ]] || { error "Missing dav1d.pc for $abi"; return 1; } |
| 287 |
| 288 | destination="$(abi_output_dir "$abi")/libdav1d.a" |
| 289 | copy_archive "$prefix/lib/libdav1d.a" "$destination" |
| 290 | echo "==> $destination" |
| 291 | } |
| 292 |
| 293 | for abi in $ABIS; do rm -rf "$(abi_include_dir "$abi")/dav1d"; done |
| 294 |
| 295 | print_common_config |
| 296 | echo "dav1d source: $DAV1D_SOURCE_DIR" |
| 297 | echo "Meson: $DETECTED_MESON_VERSION ($(command -v meson))" |
| 298 | if [[ -z "$MESON_VERSION" ]]; then |
| 299 | echo "WARNING: MESON_VERSION is not pinned; use the same Meson version on every host for byte-reproducible libdav1d.a" |
| 300 | fi |
| 301 | echo "dav1d build root: $BUILD_ROOT" |
| 302 | echo "dav1d flags: $COMMON_CFLAGS" |
| 303 |
| 304 | rm -rf "$BUILD_ROOT" |
| 305 | mkdir -p "$WORK_DIR" "$INSTALL_DIR" |
| 306 | for abi in $ABIS; do |
| 307 | build_dav1d_for_abi "$abi" |
| 308 | done |
| 309 | rm -rf "$BUILD_ROOT" |
| 310 |
| 311 | echo |
| 312 | echo "dav1d static build completed." |
| 313 |