#!/usr/bin/env bash # build_ffmpeg_lgpl.sh [--tarball ] [--jobs N] # # Builds the FFmpeg the Enchant mac app ships, exactly: the source tarball and configure line in # ffmpeg_lgpl.conf (next to this script), shared dylibs with @rpath install names, installed to # . Used by scripts/fetch_deps_mac.sh (--ffmpeg-lgpl) and shipped unchanged in the # LGPL source package (make_ffmpeg_source_package.sh) — so anyone holding that package rebuilds # the same libraries and can drop them into Enchant.app/Contents/Frameworks (see its README). # # Needs: macOS arm64, Xcode command line tools, Homebrew pkgconf + dav1d + libvpx + opus. # Without --tarball it downloads FFmpeg from ffmpeg.org; either way the sha256 must match. set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" # shellcheck source=ffmpeg_lgpl.conf . "$HERE/ffmpeg_lgpl.conf" DEST=""; TARBALL=""; JOBS="${FF_JOBS:-8}" while [ $# -gt 0 ]; do case "$1" in --tarball) shift; TARBALL="${1:?}";; --jobs) shift; JOBS="${1:?}";; -h|--help) sed -n '2,11p' "$0"; exit 0;; *) [ -z "$DEST" ] && DEST="$1" || { echo "unknown argument: $1" >&2; exit 2; };; esac; shift done [ -n "$DEST" ] || { echo "usage: $0 [--tarball file] [--jobs N]" >&2; exit 2; } case "$DEST" in /*) ;; *) DEST="$(pwd)/$DEST";; esac WORK="$(mktemp -d "${TMPDIR:-/tmp}/enchant-ffmpeg.XXXXXX")" trap 'rm -rf "$WORK"' EXIT export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:/usr/bin:/bin:/usr/sbin:/sbin" # arm64 brew, never /usr/local for b in "${FF_LGPL_BREW_DEPS[@]}"; do brew list --versions "$b" >/dev/null 2>&1 || { echo "[ffmpeg] brew install $b"; brew install "$b"; } done if [ -z "$TARBALL" ]; then echo "[ffmpeg] download $FF_LGPL_URL" curl -fSL --retry 3 -o "$WORK/ffmpeg.tar.xz" "$FF_LGPL_URL" TARBALL="$WORK/ffmpeg.tar.xz" fi got="$(shasum -a 256 "$TARBALL" | cut -d' ' -f1)" [ "$got" = "$FF_LGPL_SHA" ] || { echo "[ffmpeg] ERROR: sha256 $got != $FF_LGPL_SHA ($TARBALL)" >&2; exit 1; } mkdir -p "$WORK/src" "$WORK/root" tar -xf "$TARBALL" -C "$WORK/src" --strip-components=1 echo "[ffmpeg] configure: ${FF_LGPL_CONFIGURE[*]}" ( cd "$WORK/src" && ./configure "${FF_LGPL_CONFIGURE[@]}" > "$WORK/configure.log" 2>&1 ) \ || { tail -30 "$WORK/configure.log"; echo "[ffmpeg] ERROR: configure failed" >&2; exit 1; } grep -q "^License: LGPL version 2.1 or later" "$WORK/configure.log" \ || { echo "[ffmpeg] ERROR: configure did not come out LGPL 2.1+ — refusing to build" >&2; exit 1; } echo "[ffmpeg] make -j$JOBS (~5 min on M-series)" ( cd "$WORK/src" && make -j"$JOBS" > "$WORK/make.log" 2>&1 && make install DESTDIR="$WORK/root" > "$WORK/install.log" 2>&1 ) \ || { tail -30 "$WORK/make.log"; echo "[ffmpeg] ERROR: build failed" >&2; exit 1; } STAGE="$WORK/root/enchant/ffmpeg-lgpl" # Relocatable pkg-config: nothing inside may name the fake prefix or this machine. for pc in "$STAGE"/lib/pkgconfig/*.pc; do sed -i '' -e 's|^prefix=.*|prefix=${pcfiledir}/../..|' \ -e 's|^libdir=.*|libdir=${prefix}/lib|' \ -e 's|^includedir=.*|includedir=${prefix}/include|' "$pc" done mkdir -p "$STAGE/share/ffmpeg-licenses" cp "$WORK/src/LICENSE.md" "$WORK/src/COPYING.LGPLv2.1" "$STAGE/share/ffmpeg-licenses/" { echo "FFmpeg $FF_LGPL_VER, LGPL version 2.1 or later" echo "source: $FF_LGPL_URL" echo "sha256: $FF_LGPL_SHA" echo "configure: ${FF_LGPL_CONFIGURE[*]}" echo "built by: scripts/build_ffmpeg_lgpl.sh (Enchant), $(date -u +%Y-%m-%d)" } > "$STAGE/share/ffmpeg-licenses/BUILD_INFO.txt" rm -rf "$DEST"; mkdir -p "$(dirname "$DEST")" mv "$STAGE" "$DEST" echo "[ffmpeg] OK -> $DEST"