SPB Git

Repository Public License

build.sh 15.1 KB 286 lines shellscript

1#!/bin/bash2# ORIX Convert — EmbeddedEngines/ffmpeg/build.sh3# Description: Builds the bundled LGPL ffmpeg + ffprobe (arm64, macOS 15+) from pinned source tarballs. Third-party4#              codecs are linked statically; the result links only system libraries and frameworks.5# Author: Simon-Pierre Boucher — contact@spboucher.ai6#7# Usage:  EmbeddedEngines/ffmpeg/build.sh            build everything (skips steps already done)8#         EmbeddedEngines/ffmpeg/build.sh --clean    remove src/, work/, prefix/ first9#10# Output: EmbeddedEngines/tools/ffmpeg, EmbeddedEngines/tools/ffprobe (stripped, ad-hoc signed by the linker; the11#         release script re-signs them with Developer ID + Hardened Runtime — no entitlement is needed).12#13# Build tools taken from this Mac: Xcode command-line tools (clang, make, ar, python3), plus cmake and ninja14# (Homebrew, by absolute path — /opt/homebrew is never on PATH nor on any search path while building, so no15# Homebrew library can be picked up). pkgconf and meson are not assumed: they are built/unpacked from pinned16# tarballs into work/tools and used only at build time.17set -euo pipefail18export LC_ALL=C LANG=C1920REAL="$(cd "$(dirname "$0")" && pwd)"21# Several libraries (and FFmpeg itself, see `ffmpeg -buildconf`) embed their configure line in the binary. So that the22# result is the same on every machine and carries no user name, the whole build runs through a fixed symlink to this23# directory. One build at a time per machine.24STABLE="/tmp/orix-convert-ffmpeg"25rm -f "$STABLE"; ln -s "$REAL" "$STABLE"26trap 'rm -f "$STABLE"' EXIT27HERE="$STABLE"28SRC="$HERE/src"; DL="$SRC/dl"; WORK="$HERE/work"; PREFIX="$HERE/prefix"; TOOLS="$WORK/tools"29OUT="$REAL/../tools"30JOBS="$(sysctl -n hw.ncpu)"3132if [[ "${1:-}" == "--clean" ]]; then rm -rf "$SRC" "$WORK" "$PREFIX"; fi33mkdir -p "$DL" "$WORK" "$PREFIX" "$TOOLS/bin" "$OUT"3435[[ "$(uname -m)" == "arm64" ]] || { echo "This build is arm64 only." >&2; exit 1; }3637# ---- pinned sources: name | version | url | sha256 --------------------------------------------------------------38SOURCES=(39"ffmpeg|9.1|https://ffmpeg.org/releases/ffmpeg-9.1.tar.xz|1a4d394ae63d190a526cd052eb4415c156b5c5e2df37efab41a7de0b8fde2433"40"lame|3.100|https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz|ddfe36cab873794038ae2c1210557ad34857a4b6bdc515785d1da9e175b1da1e"41"opus|1.6.1|https://downloads.xiph.org/releases/opus/opus-1.6.1.tar.gz|6ffcb593207be92584df15b32466ed64bbec99109f007c82205f0194572411a1"42"libogg|1.3.6|https://downloads.xiph.org/releases/ogg/libogg-1.3.6.tar.xz|5c8253428e181840cd20d41f3ca16557a9cc04bad4a3d04cce84808677fa1061"43"libvorbis|1.3.7|https://downloads.xiph.org/releases/vorbis/libvorbis-1.3.7.tar.xz|b33cc4934322bcbf6efcbacf49e3ca01aadbea4114ec9589d1b1e9d20f72954b"44"libvpx|1.17.0|https://github.com/webmproject/libvpx/archive/refs/tags/v1.17.0.tar.gz|1020f184046187baa2985dbde38e0691f49c44088bca7a1842b0236c6081dc0a"45"SVT-AV1|v4.3.0|https://gitlab.com/AOMediaCodec/SVT-AV1/-/archive/v4.3.0/SVT-AV1-v4.3.0.tar.gz|95ce8fc2381ba707e65ac8b9b2e165915bfbe9cd4d2f7b0f58d64ce31bd98f6d"46"dav1d|1.5.5|https://downloads.videolan.org/pub/videolan/dav1d/1.5.5/dav1d-1.5.5.tar.xz|ca96b1d0ba3f01f55de7231d775892b64994ba061aaeeaaa4f662a390b3abf9e"47"zimg|3.0.6|https://github.com/sekrit-twc/zimg/archive/refs/tags/release-3.0.6.tar.gz|be89390f13a5c9b2388ce0f44a5e89364a20c1c57ce46d382b1fcc3967057577"48"meson|1.6.1|https://github.com/mesonbuild/meson/releases/download/1.6.1/meson-1.6.1.tar.gz|1eca49eb6c26d58bbee67fd3337d8ef557c0804e30a6d16bfdf269db997464de"49"pkgconf|3.0.11|https://distfiles.ariadne.space/pkgconf/pkgconf-3.0.11.tar.xz|d0cd841bc8e55b9da888106c00dc256d05b7d1d8e5a46f4be4eb4c5125bd2dc4"50)5152archive_name() { # name version url -> file name in dl/53  case "$3" in *.tar.xz) echo "$1-${2#v}.tar.xz" ;; *) echo "$1-${2#v}.tar.gz" ;; esac54}5556fetch_all() {57  local entry name version url sha file got58  for entry in "${SOURCES[@]}"; do59    IFS='|' read -r name version url sha <<<"$entry"60    file="$DL/$(archive_name "$name" "$version" "$url")"61    [[ "$name" == "SVT-AV1" ]] && file="$DL/SVT-AV1-$version.tar.gz"62    if [[ ! -f "$file" ]]; then63      echo ">> download $name $version"64      /usr/bin/curl -fsSL --retry 3 -o "$file.part" "$url"65      mv "$file.part" "$file"66    fi67    got="$(/usr/bin/shasum -a 256 "$file" | /usr/bin/awk '{print $1}')"68    if [[ "$got" != "$sha" ]]; then69      echo "SHA-256 mismatch for $file: expected $sha, got $got" >&2; exit 170    fi71  done72}7374# Unpacks an archive into its own fresh directory under work/ and prints that directory.75unpack() { # archive-file dirname76  local dest="$WORK/$2"77  if [[ ! -d "$dest" ]]; then78    mkdir -p "$dest.tmp"79    /usr/bin/tar -xf "$DL/$1" -C "$dest.tmp" --strip-components 180    mv "$dest.tmp" "$dest"81  fi82  echo "$dest"83}8485done_marker() { [[ -f "$WORK/.done-$1" ]]; }86mark_done() { touch "$WORK/.done-$1"; }8788fetch_all8990# ---- isolated build environment ---------------------------------------------------------------------------------91CMAKE_BIN="$(command -v cmake || true)"; NINJA_BIN="$(command -v ninja || true)"92[[ -x "$CMAKE_BIN" && -x "$NINJA_BIN" ]] || { echo "cmake and ninja are required to build (brew install cmake ninja)." >&2; exit 1; }93ln -sf "$CMAKE_BIN" "$TOOLS/bin/cmake"; ln -sf "$NINJA_BIN" "$TOOLS/bin/ninja"9495export PATH="$TOOLS/bin:/usr/bin:/bin:/usr/sbin:/sbin"96export MACOSX_DEPLOYMENT_TARGET=15.097export SDKROOT="$(/usr/bin/xcrun --sdk macosx --show-sdk-path)"98export CC=clang CXX=clang++99ARCHFLAGS="-arch arm64 -mmacosx-version-min=15.0"100# -ffile-prefix-map keeps the build directory out of the objects (reproducibility).101export CFLAGS="-O2 $ARCHFLAGS -ffile-prefix-map=$HERE=."102export CXXFLAGS="$CFLAGS"103export LDFLAGS="$ARCHFLAGS"104export PKG_CONFIG="$TOOLS/bin/pkgconf"105export PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig"106unset PKG_CONFIG_PATH CPATH LIBRARY_PATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH DYLD_LIBRARY_PATH || true107export ZERO_AR_DATE=1 SOURCE_DATE_EPOCH=1767225600 TZ=UTC108109# ---- build-time tools ---------------------------------------------------------------------------------------------110if ! done_marker pkgconf; then111  echo ">> pkgconf (build tool)"112  d="$(unpack pkgconf-3.0.11.tar.xz pkgconf)"113  (cd "$d" && ./configure --prefix="$TOOLS" --disable-shared --enable-static >/dev/null && make -j"$JOBS" >/dev/null && make install >/dev/null)114  mark_done pkgconf115fi116MESON_DIR="$(unpack meson-1.6.1.tar.gz meson)"117meson() { /usr/bin/python3 "$MESON_DIR/meson.py" "$@"; }118119# ---- codec libraries (static) ---------------------------------------------------------------------------------------120if ! done_marker lame; then121  echo ">> libmp3lame 3.100 (LGPL-2.0-or-later)"122  d="$(unpack lame-3.100.tar.gz lame)"123  (cd "$d" && ./configure --prefix="$PREFIX" --disable-shared --enable-static --disable-frontend --disable-decoder \124      --disable-gtktest --enable-nasm=no >/dev/null && make -j"$JOBS" >/dev/null && make install >/dev/null)125  mark_done lame126fi127128if ! done_marker opus; then129  echo ">> libopus 1.6.1 (BSD-3-Clause)"130  d="$(unpack opus-1.6.1.tar.gz opus)"131  (cd "$d" && ./configure --prefix="$PREFIX" --disable-shared --enable-static --disable-doc --disable-extra-programs >/dev/null \132      && make -j"$JOBS" >/dev/null && make install >/dev/null)133  mark_done opus134fi135136if ! done_marker libogg; then137  echo ">> libogg 1.3.6 (BSD-3-Clause)"138  d="$(unpack libogg-1.3.6.tar.xz libogg)"139  (cd "$d" && ./configure --prefix="$PREFIX" --disable-shared --enable-static >/dev/null && make -j"$JOBS" >/dev/null && make install >/dev/null)140  mark_done libogg141fi142143if ! done_marker libvorbis; then144  echo ">> libvorbis 1.3.7 (BSD-3-Clause)"145  d="$(unpack libvorbis-1.3.7.tar.xz libvorbis)"146  # The 2020 configure script passes -force_cpusubtype_ALL, which current Apple linkers reject on arm64.147  /usr/bin/sed -i '' 's/-force_cpusubtype_ALL//g' "$d/configure"148  (cd "$d" && ./configure --prefix="$PREFIX" --disable-shared --enable-static --disable-oggtest --disable-docs --disable-examples \149      --with-ogg="$PREFIX" >/dev/null && make -j"$JOBS" >/dev/null && make install >/dev/null)150  mark_done libvorbis151fi152153if ! done_marker libvpx; then154  echo ">> libvpx 1.17.0 (BSD-3-Clause)"155  d="$(unpack libvpx-1.17.0.tar.gz libvpx)"156  mkdir -p "$WORK/libvpx-build"157  (cd "$WORK/libvpx-build" && "$d/configure" --prefix="$PREFIX" --target=arm64-darwin24-gcc --disable-shared --enable-static \158      --enable-pic --enable-vp8 --enable-vp9 --enable-vp9-highbitdepth --disable-examples --disable-tools --disable-docs \159      --disable-unit-tests --extra-cflags="-mmacosx-version-min=15.0" >/dev/null && make -j"$JOBS" >/dev/null && make install >/dev/null)160  mark_done libvpx161fi162163if ! done_marker svtav1; then164  echo ">> SVT-AV1 4.3.0 (BSD-3-Clause-Clear + AOM patent licence)"165  d="$(unpack SVT-AV1-v4.3.0.tar.gz svtav1)"166  cmake -S "$d" -B "$WORK/svtav1-build" -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PREFIX" \167      -DBUILD_SHARED_LIBS=OFF -DBUILD_APPS=OFF -DBUILD_TESTING=OFF -DCMAKE_OSX_ARCHITECTURES=arm64 \168      -DCMAKE_OSX_DEPLOYMENT_TARGET=15.0 -DCMAKE_INSTALL_LIBDIR=lib >/dev/null169  cmake --build "$WORK/svtav1-build" -j "$JOBS" >/dev/null170  cmake --install "$WORK/svtav1-build" >/dev/null171  mark_done svtav1172fi173174if ! done_marker dav1d; then175  echo ">> dav1d 1.5.5 (BSD-2-Clause)"176  d="$(unpack dav1d-1.5.5.tar.xz dav1d)"177  meson setup "$WORK/dav1d-build" "$d" --prefix="$PREFIX" --libdir=lib --buildtype=release --default-library=static \178      -Denable_tools=false -Denable_tests=false >/dev/null179  ninja -C "$WORK/dav1d-build" >/dev/null180  ninja -C "$WORK/dav1d-build" install >/dev/null181  mark_done dav1d182fi183184if ! done_marker zimg; then185  echo ">> zimg 3.0.6 (WTFPL)"186  # zimg's release archive has no generated configure script; the library is a plain set of C++ sources, compiled187  # here directly with the same definitions its Makefile.am uses for an ARM NEON build.188  d="$(unpack zimg-3.0.6.tar.gz zimg)"189  b="$WORK/zimg-build"; rm -rf "$b"; mkdir -p "$b"190  i=0191  while IFS= read -r f; do192    i=$((i + 1))193    clang++ -std=c++17 $CXXFLAGS -fvisibility=hidden -DNDEBUG -DZIMG_ARM -I"$d/src/zimg" -c "$f" -o "$b/$(printf '%03d' $i)-$(basename "${f%.cpp}").o" &194    if (( i % JOBS == 0 )); then wait; fi195  done < <(/usr/bin/find "$d/src/zimg" -name '*.cpp' -not -path '*/x86/*' | /usr/bin/sort)196  wait197  rm -f "$PREFIX/lib/libzimg.a"; mkdir -p "$PREFIX/lib/pkgconfig" "$PREFIX/include"198  /usr/bin/ar rcs "$PREFIX/lib/libzimg.a" "$b"/*.o199  cp "$d/src/zimg/api/zimg.h" "$PREFIX/include/zimg.h"200  cat > "$PREFIX/lib/pkgconfig/zimg.pc" <<EOF201prefix=$PREFIX202libdir=\${prefix}/lib203includedir=\${prefix}/include204Name: zimg205Description: Scaling, colorspace conversion, and dithering library206Version: 3.0.6207Libs: -L\${libdir} -lzimg208Libs.private: -lc++209Cflags: -I\${includedir}210EOF211  mark_done zimg212fi213214# ---- FFmpeg (LGPL-2.1-or-later configuration) -------------------------------------------------------------------------215ENCODERS="h264_videotoolbox,hevc_videotoolbox,prores_videotoolbox,prores_ks,libsvtav1,libvpx_vp8,libvpx_vp9,mpeg4,mpeg2video,mpeg1video,wmv2,wmv1,msmpeg4v3,flv,mjpeg,png,apng,gif,rawvideo,aac,aac_at,alac,flac,libmp3lame,libopus,libvorbis,wmav2,wmav1,mp2,ac3,pcm_s16le,pcm_s24le,pcm_s32le,pcm_f32le,pcm_f64le,pcm_s16be,pcm_s24be,pcm_s32be,pcm_f32be,pcm_f64be,pcm_s8,pcm_u8,pcm_alaw,pcm_mulaw,subrip,srt,ass,ssa,webvtt,movtext,text,wrapped_avframe"216MUXERS="mp4,mov,ipod,matroska,webm,avi,mpeg1system,mpeg2vob,mpeg2dvd,mpegts,flv,asf,ogg,opus,oga,flac,wav,aiff,caf,mp3,adts,mp2,ac3,srt,ass,webvtt,gif,apng,null,md5,framemd5,hash,framehash,streamhash,framecrc,crc,image2,image2pipe,rawvideo,pcm_s16le,pcm_s24le,pcm_s32le,pcm_f32le,ffmetadata"217PROTOCOLS="file,pipe"218219FFMPEG_CONFIGURE=(220  --prefix="$STABLE/prefix" --arch=arm64 --target-os=darwin --cc=clang --cxx=clang++221  --pkg-config="$STABLE/work/tools/bin/pkgconf" --pkg-config-flags=--static222  --extra-cflags="-mmacosx-version-min=15.0 -I$STABLE/prefix/include -ffile-prefix-map=$STABLE=."223  --extra-ldflags="-mmacosx-version-min=15.0 -L$STABLE/prefix/lib" --extra-libs=-liconv224  --disable-shared --enable-static --enable-pic --disable-debug --disable-doc225  --disable-autodetect --disable-network --disable-avdevice226  --disable-ffplay --enable-ffmpeg --enable-ffprobe227  --disable-protocols --enable-protocol="$PROTOCOLS"228  --disable-encoders --enable-encoder="$ENCODERS"229  --disable-muxers --enable-muxer="$MUXERS"230  --enable-videotoolbox --enable-audiotoolbox --enable-zlib --enable-iconv231  --enable-libsvtav1 --enable-libdav1d --enable-libvpx --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libzimg232)233234if ! done_marker ffmpeg; then235  echo ">> FFmpeg 9.1 (LGPL-2.1-or-later: no --enable-gpl, no --enable-nonfree, no --enable-version3)"236  d="$(unpack ffmpeg-9.1.tar.xz ffmpeg)"237  mkdir -p "$WORK/ffmpeg-build"238  (cd "$STABLE/work/ffmpeg-build" && "$STABLE/work/ffmpeg/configure" "${FFMPEG_CONFIGURE[@]}" && make -j"$JOBS" >/dev/null && make install >/dev/null)239  mark_done ffmpeg240fi241242# ---- licence texts (committed; shipped in the app's acknowledgements) -------------------------------------------------------243LIC="$REAL/licenses"; mkdir -p "$LIC"244copy_license() { [[ -f "$WORK/$1" ]] && cp "$WORK/$1" "$LIC/$2"; }245copy_license ffmpeg/COPYING.LGPLv2.1 FFmpeg-COPYING.LGPLv2.1.txt246copy_license ffmpeg/LICENSE.md FFmpeg-LICENSE.md247copy_license lame/COPYING LAME-COPYING.txt248copy_license opus/COPYING opus-COPYING.txt249copy_license libogg/COPYING libogg-COPYING.txt250copy_license libvorbis/COPYING libvorbis-COPYING.txt251copy_license libvpx/LICENSE libvpx-LICENSE.txt252copy_license libvpx/PATENTS libvpx-PATENTS.txt253copy_license svtav1/LICENSE.md SVT-AV1-LICENSE.md254copy_license svtav1/PATENTS.md SVT-AV1-PATENTS.md255copy_license dav1d/COPYING dav1d-COPYING.txt256copy_license zimg/COPYING zimg-COPYING.txt257258# ---- install + verify ---------------------------------------------------------------------------------------------------259for tool in ffmpeg ffprobe; do260  cp "$PREFIX/bin/$tool" "$OUT/$tool.new"261  /usr/bin/strip -x "$OUT/$tool.new"262  /usr/bin/codesign --force --sign - "$OUT/$tool.new" 2>/dev/null263  mv "$OUT/$tool.new" "$OUT/$tool"264done265266fail=0267for tool in ffmpeg ffprobe; do268  if /usr/bin/otool -L "$OUT/$tool" | /usr/bin/tail -n +2 | /usr/bin/grep -v -E '^[[:space:]]+(/usr/lib/|/System/Library/)'; then269    echo "ERROR: $tool links a non-system library (see above)." >&2; fail=1270  fi271  [[ "$(/usr/bin/lipo -archs "$OUT/$tool")" == "arm64" ]] || { echo "ERROR: $tool is not arm64-only." >&2; fail=1; }272done273cfg="$("$OUT/ffmpeg" -hide_banner -buildconf)"274if echo "$cfg" | /usr/bin/grep -q -E -e '--enable-(gpl|nonfree|version3)'; then echo "ERROR: not an LGPL-2.1 configuration." >&2; fail=1; fi275"$OUT/ffmpeg" -hide_banner -L 2>&1 | /usr/bin/grep -q 'GNU Lesser General Public' || { echo "ERROR: ffmpeg -L does not report LGPL." >&2; fail=1; }276if "$OUT/ffmpeg" -hide_banner -protocols 2>/dev/null | /usr/bin/grep -q -E '^\s*(http|https|tcp|udp|rtmp|rtp|tls|ftp|concat|subfile|data|crypto)$'; then277  echo "ERROR: unexpected protocol compiled in." >&2; fail=1278fi279[[ $fail -eq 0 ]] || exit 1280281echo282echo "Built:"283/bin/ls -l "$OUT/ffmpeg" "$OUT/ffprobe" | /usr/bin/awk '{printf "  %s  %d bytes (%.1f MB)\n", $NF, $5, $5/1048576}'284/usr/bin/shasum -a 256 "$OUT/ffmpeg" "$OUT/ffprobe" | /usr/bin/sed 's/^/  /'285echo "Configuration: $("$OUT/ffmpeg" -hide_banner -version | /usr/bin/sed -n 's/^configuration: //p')"286
Files main
Menu

Theme