#!/bin/zsh # Builds FFmpeg.xcframework for TimeBolt iPad: the LGPL FFmpeg libraries plus the ffmpeg command-line # tool compiled as a library (its main() renamed to ffmpeg_main, see patches/), as a dynamic framework # per platform so the LGPL relinking requirement is met. # # ./build.sh builds every slice (iOS device, iOS simulator, macOS) and the xcframework # ./build.sh ios one slice: ios | iossim | macos # ./build.sh xcframework only assembles the xcframework from slices already built # # Output: out/FFmpeg.xcframework, out// (prefix with include/ and lib/), build// (objects) # Requirements: Xcode 15+ with the iOS and macOS SDKs, curl, tar, patch. No nasm/yasm (arm64 only). set -euo pipefail FFMPEG_VERSION="8.1.3" FFMPEG_SHA256="7138d28c96d9d3e3af4ee3d8cad72741f8ffb40da90c1112235dea3ecd3178a3" IOS_MIN="17.0" MACOS_MIN="14.0" FRAMEWORK_ID="com.quinston.timebolt.FFmpeg" HERE="$(cd "$(dirname "$0")" && pwd)" DOWNLOADS="$HERE/downloads" SRC_ROOT="$HERE/src" SRC="$SRC_ROOT/ffmpeg-$FFMPEG_VERSION" BUILD="$HERE/build" OUT="$HERE/out" JOBS="$(sysctl -n hw.ncpu)" log() { print -P "%F{cyan}[ffmpeg-build]%f $*"; } die() { print -P "%F{red}[ffmpeg-build] $*%f" >&2; exit 1; } fetch_source() { mkdir -p "$DOWNLOADS" "$SRC_ROOT" local tarball="$DOWNLOADS/ffmpeg-$FFMPEG_VERSION.tar.xz" if [[ ! -f "$tarball" ]]; then log "downloading ffmpeg-$FFMPEG_VERSION.tar.xz" curl -sSL -o "$tarball" "https://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.xz" fi local sum; sum="$(shasum -a 256 "$tarball" | cut -d' ' -f1)" [[ "$sum" == "$FFMPEG_SHA256" ]] || die "checksum mismatch for $tarball: $sum" if [[ ! -d "$SRC" ]]; then log "extracting" tar xf "$tarball" -C "$SRC_ROOT" fi if [[ ! -f "$SRC/.timebolt-patched" ]]; then for p in "$HERE"/patches/*.patch; do [[ -f "$p" ]] || continue log "applying $(basename "$p")" patch -p1 -d "$SRC" < "$p" done touch "$SRC/.timebolt-patched" fi } # slice -> sdk name, clang -target triple, framework platform string for Info.plist slice_sdk() { case "$1" in ios) echo iphoneos;; iossim) echo iphonesimulator;; macos) echo macosx;; *) die "unknown slice $1";; esac } slice_target() { case "$1" in ios) echo "arm64-apple-ios$IOS_MIN";; iossim) echo "arm64-apple-ios$IOS_MIN-simulator";; macos) echo "arm64-apple-macos$MACOS_MIN";; esac } slice_platform() { case "$1" in ios) echo iPhoneOS;; iossim) echo iPhoneSimulator;; macos) echo MacOSX;; esac } slice_minkey() { case "$1" in macos) echo LSMinimumSystemVersion;; *) echo MinimumOSVersion;; esac } slice_min() { case "$1" in macos) echo "$MACOS_MIN";; *) echo "$IOS_MIN";; esac } build_slice() { local slice="$1" local sdk; sdk="$(slice_sdk "$slice")" local target; target="$(slice_target "$slice")" local sdkpath; sdkpath="$(xcrun --sdk "$sdk" --show-sdk-path)" local cc; cc="$(xcrun --sdk "$sdk" -f clang)" local prefix="$OUT/$slice" local objdir="$BUILD/$slice" pushd "$HERE" >/dev/null if [[ "${TB_RESUME:-0}" == 1 && -f "$objdir/ffbuild/config.mak" ]]; then log "resuming $slice: libraries already configured and built" cd "$objdir" else log "configuring $slice ($target)" rm -rf "$objdir" "$prefix" mkdir -p "$objdir" "$prefix" cd "$objdir" # LGPL only: no --enable-gpl, no --enable-nonfree, so no libx264. Encoding uses VideoToolbox. # Programs: ffmpeg is "enabled" so make knows its object list, but only the objects are built # (main() is renamed by the patch, so the executable itself never links). "$SRC/configure" \ --prefix="$prefix" \ --enable-cross-compile --target-os=darwin --arch=arm64 \ --cc="$cc" --sysroot="$sdkpath" \ --extra-cflags="-target $target -fPIC -fno-common -DTB_FFMPEG_LIBRARY=1" \ --extra-ldflags="-target $target" \ --enable-static --disable-shared --enable-pic \ --disable-doc --disable-debug --disable-stripping \ --enable-ffmpeg --disable-ffplay --disable-ffprobe \ --disable-avdevice --disable-network --disable-securetransport \ --disable-sdl2 --disable-xlib --disable-coreimage \ --disable-lzma --disable-bzlib --disable-iconv \ --enable-zlib --enable-videotoolbox --enable-audiotoolbox --enable-pthreads \ --disable-gpl --disable-nonfree \ > "$objdir/configure.log" 2>&1 || { tail -40 "$objdir/configure.log"; die "configure failed for $slice (see $objdir/configure.log and ffbuild/config.log)"; } log "building libraries for $slice" # Libraries and headers only: the ffmpeg program itself can no longer link (no main), by design. make -j"$JOBS" install-libs install-headers > "$objdir/make.log" 2>&1 || { tail -40 "$objdir/make.log"; die "make failed for $slice"; } fi log "building fftools objects for $slice" # The object list exactly as make would link ffmpeg_g, taken from make's own database. local objs # Printed by make itself so nested variables ($(OBJS-resman), $(RESOBJS)) are expanded. printf 'tb-print-objs:\n\t@echo $(OBJS-ffmpeg)\n' > "$objdir/tb-print-objs.mk" objs="$(make -s -f Makefile -f tb-print-objs.mk tb-print-objs | tr -s ' ' | sed 's/^ //;s/ $//')" [[ -n "$objs" ]] || die "could not read OBJS-ffmpeg from make" make -j"$JOBS" ${=objs} >> "$objdir/make.log" 2>&1 || { tail -40 "$objdir/make.log"; die "fftools objects failed for $slice"; } # The shim that wraps ffmpeg_main() (shim/ffmpeg_library.c), built with the tool's own include paths. "$cc" -target "$target" -isysroot "$sdkpath" -O2 -fPIC -fno-common -DTB_FFMPEG_LIBRARY=1 \ -I"$objdir" -I"$SRC" \ -c "$HERE/shim/ffmpeg_library.c" -o "$objdir/fftools/ffmpeg_library.o" xcrun --sdk "$sdk" libtool -static -o "$prefix/lib/libffmpegtools.a" ${=objs} "$objdir/fftools/ffmpeg_library.o" echo "$objs fftools/ffmpeg_library.o" > "$prefix/lib/ffmpegtools.objs" popd >/dev/null make_framework "$slice" } # One dynamic FFmpeg.framework per slice: all static libraries loaded whole, plus the system frameworks # the videotoolbox/audiotoolbox code and zlib need. make_framework() { local slice="$1" local sdk; sdk="$(slice_sdk "$slice")" local target; target="$(slice_target "$slice")" local sdkpath; sdkpath="$(xcrun --sdk "$sdk" --show-sdk-path)" local cc; cc="$(xcrun --sdk "$sdk" -f clang)" local prefix="$OUT/$slice" local fw="$prefix/FFmpeg.framework" log "linking FFmpeg.framework for $slice" rm -rf "$fw" mkdir -p "$fw/Headers" "$fw/Modules" local libs=() for l in libffmpegtools libavfilter libavformat libavcodec libswscale libswresample libavutil; do libs+=("-force_load" "$prefix/lib/$l.a") done "$cc" -target "$target" -isysroot "$sdkpath" -dynamiclib \ -install_name "@rpath/FFmpeg.framework/FFmpeg" \ -compatibility_version 1.0 -current_version 1.0 \ -o "$fw/FFmpeg" "${libs[@]}" \ -lz -lm -lpthread \ -framework CoreFoundation -framework CoreMedia -framework CoreVideo \ -framework VideoToolbox -framework AudioToolbox -framework Security \ -Wl,-dead_strip_dylibs # Public headers: the libav* headers the Swift wrapper needs, plus the fftools entry points. cp -R "$prefix/include/"* "$fw/Headers/" cp "$HERE/shim/ffmpeg_main.h" "$HERE/shim/FFmpeg.h" "$fw/Headers/" cp "$HERE/shim/module.modulemap" "$fw/Modules/module.modulemap" python3 "$HERE/shim/rewrite-includes.py" "$fw/Headers" local platform; platform="$(slice_platform "$slice")" local minkey; minkey="$(slice_minkey "$slice")" local minver; minver="$(slice_min "$slice")" cat > "$fw/Info.plist" < CFBundleDevelopmentRegionen CFBundleExecutableFFmpeg CFBundleIdentifier$FRAMEWORK_ID CFBundleInfoDictionaryVersion6.0 CFBundleNameFFmpeg CFBundlePackageTypeFMWK CFBundleShortVersionString$FFMPEG_VERSION CFBundleVersion1 CFBundleSupportedPlatforms$platform $minkey$minver PLIST # macOS frameworks are expected in the Versions/A layout; a flat framework still loads, and # xcodebuild accepts it for -create-xcframework. Kept flat for simplicity across slices. du -sh "$fw/FFmpeg" | awk '{print "[ffmpeg-build] binary size: " $1}' } make_xcframework() { log "assembling FFmpeg.xcframework" rm -rf "$OUT/FFmpeg.xcframework" local args=() for slice in ios iossim macos; do [[ -d "$OUT/$slice/FFmpeg.framework" ]] && args+=("-framework" "$OUT/$slice/FFmpeg.framework") done (( ${#args[@]} )) || die "no slices built" xcodebuild -create-xcframework "${args[@]}" -output "$OUT/FFmpeg.xcframework" cp "$SRC/COPYING.LGPLv2.1" "$OUT/FFmpeg.xcframework/LICENSE.LGPLv2.1" du -sh "$OUT/FFmpeg.xcframework" } case "${1:-all}" in all) fetch_source; for s in ios iossim macos; do build_slice "$s"; done; make_xcframework ;; ios|iossim|macos) fetch_source; build_slice "$1" ;; xcframework) make_xcframework ;; *) die "usage: $0 [all|ios|iossim|macos|xcframework]" ;; esac log "done"