Files
mpv-natives/ci/build-libmpv-macos.sh
david ea42f65e46 build: stop depending on the flaky GNU tarball front hosts
download.savannah.gnu.org and ftpmirror.gnu.org (which share one front
host) intermittently time out from the GitHub runners, which has already
broken the freetype and libiconv steps. Route the affected dependencies
around that host:

- freetype is now fetched via git (gitlab.freedesktop.org, pinned tag
  VER-2-14-1) like the other git dependencies, instead of a savanna
  tarball that no longer has a working host.
- libiconv now tries several independent GNU mirrors, the first one that
  serves it winning.
- gettar() generalizes to accept a list of fallback URLs so any future
  tarball dependency can name its mirrors too.
2026-10-06 09:13:07 +02:00

327 lines
12 KiB
Bash

#!/bin/bash -e
set -e
# (The shebang -e is not honored when the workflow invokes us via `bash file`,
# so set -e is repeated here to guarantee fail-fast regardless of caller.)
#
# Build a single self-contained libmpv dylib for macOS for one architecture
# (arm64 natively, or x86_64 via clang --target=x86_64-apple-darwin).
# The workflow runs this script twice on one arm64 runner and merges the two
# dylibs into a universal binary with lipo. (GitHub's macOS runners are all
# arm64 now, so x86_64 must be cross-compiled.)
#
# Recipe: every dependency is built STATIC and linked into the one dylib
# (same approach as ci/build-libmpv-mingw64.sh for Windows). No iconv (macOS
# ships one), no zlib (macOS ships one, universal), no icu (harfbuzz built
# without it - the runner's brew icu is arm64-only and must not leak into
# the x86_64 slice). Vulkan is intentionally NOT built: jlibmpv renders
# through an externally supplied OpenGL context (JOGL + render API).
# CoreAudio is the audio output; VideoToolbox provides hardware decode.
#
# All third-party sources are pinned to the latest stable tag that existed on
# the mpv v0.41.0 release date (2025-12-21), so a rebuild reproduces the same
# binary instead of drifting to git HEAD.
# Target architecture: arm64 (native) or x86_64 (cross).
ARCH=${ARCH:-arm64}
# The mpv source tree is the current working directory (checked out by CI at
# MPV_REF). Everything is built next to it under macos_prefix-<arch>/.
prefix_dir=$PWD/macos_prefix-$ARCH
mkdir -p "$prefix_dir"
wget="wget -nc --progress=bar:force --tries=3 --timeout=60 --waitretry=5"
# Deployment target shared by every slice (Oldest macOS we still support:
# Big Sur, the first arm64 release and the floor of mpv's Swift @available
# guards). Lowering it keeps all features: mpv gates them on the SDK version,
# not on this min. Raising it is only needed if a dependency uses a newer API
# without an @available guard (that fails the build in CI, not at runtime).
MACOS_MIN=11.0
# Cross-compile target for the x86_64 slice (arm64 is a plain native build).
TARGET_FLAG=""
if [ "$ARCH" = "x86_64" ]; then
TARGET_FLAG="--target=x86_64-apple-darwin"
fi
# Swift does not inherit the clang --target above: swiftc would otherwise
# compile for the host architecture (arm64) even in the x86_64 slice, so the
# generated Application/AppHub objects land in the wrong slice and the
# universal dylib cannot dlopen on Intel. mpv's swiftc invocation takes its
# own target triple (which also carries the deployment version), so both the
# architecture and the min OS are pinned here in one flag.
SWIFT_TARGET="${ARCH}-apple-macosx${MACOS_MIN}"
# Route compilation through ccache (brew-installed by the workflow) so the
# two-arch build (arm64 + x86_64) reuses cached objects across rebuilds.
CCACHE=${CCACHE:-ccache}
export CC="$CCACHE cc${TARGET_FLAG:+ $TARGET_FLAG}"
export CXX="$CCACHE c++${TARGET_FLAG:+ $TARGET_FLAG}"
export AR=ar
export NM=nm
export RANLIB=ranlib
# -mmacosx-version-min applies to every C/ObjC object mpv builds (natively and
# cross) and keeps all statically linked dependencies in step; the linker
# takes the max of the objects it links, so forgetting it here would leave
# the whole dylib pinned to the runner's SDK minimum (15) instead of 11.
export CFLAGS="-O2 -pipe -Wall -mmacosx-version-min=${MACOS_MIN}"
export CXXFLAGS="-O2 -pipe -Wall -mmacosx-version-min=${MACOS_MIN}"
export LDFLAGS="-mmacosx-version-min=${MACOS_MIN}"
# Prefix for the static deps we build ourselves, added additively so the
# system pkg-config files (zlib, ...) are still visible.
export PKG_CONFIG_PATH="$prefix_dir/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
# --- Pinned dependency versions -------------------------------------------
# Centralized in ci/versions.env (single source of truth shared with the other
# platforms and with CI). Bump mpv + deps there.
. "$(dirname "$0")/versions.env"
# Static everywhere: every dependency becomes a .a archive that gets linked
# into the single libmpv dylib. (mpv's own script ships shared libs instead.)
commonflags="--disable-shared --enable-static"
# Meson cross file for the x86_64 slice. arm64 is a plain native build.
MESON_EXTRA=""
if [ "$ARCH" = "x86_64" ]; then
cat >"$prefix_dir/crossfile" <<EOF
[built-in options]
buildtype = 'release'
[binaries]
c = ['cc', '--target=x86_64-apple-darwin']
cpp = ['c++', '--target=x86_64-apple-darwin']
objc = ['cc', '--target=x86_64-apple-darwin']
ar = 'ar'
strip = 'strip'
pkgconfig = 'pkg-config'
pkg-config = 'pkg-config'
nasm = 'nasm'
[host_machine]
system = 'darwin'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'
EOF
MESON_EXTRA="--cross-file $prefix_dir/crossfile"
fi
function builddir {
[ -d "$1/builddir" ] && rm -rf "$1/builddir"
mkdir -p "$1/builddir"
pushd "$1/builddir"
}
function makeplusinstall {
if [ -f build.ninja ]; then
ninja
ninja install
else
make -j$(sysctl -n hw.ncpu)
make install
fi
}
# Download a release tarball, trying each URL in turn (a tarball usually
# exists on several independent mirrors; the first one that serves it wins).
function gettar {
local name="${1##*/}"
[ -d "${name%%.*}" ] && return 0
local url
for url in "$@"; do
if $wget "$url" && tar -xaf "$name"; then
return 0
fi
echo "Download of $name from $url failed, trying next mirror"
rm -f "$name"
done
echo "Error: could not download $name from any of: $*"
return 1
}
# Clone a git dependency at a pinned tag (shallow, tag-checked-out).
# Submodules are fetched shallow too: libplacebo needs its bundled `glad`.
function gitpin {
local url=$1 dest=$2 tag=$3
[ -d "$dest" ] || git clone --depth=1 --branch "$tag" \
--recurse-submodules --shallow-submodules "$url" "$dest"
pushd "$dest"
git checkout --quiet "$tag"
popd
}
function build_if_missing {
local name=${1//-/_}
local mark_var=_${name}_mark
local mark_file=$prefix_dir/${!mark_var}
[ -e "$mark_file" ] && return 0
echo "::group::Building $1"
_$name
echo "::endgroup::"
if [ ! -e "$mark_file" ]; then
echo "Error: Build of $1 completed but $mark_file was not created."
return 2
fi
}
## mpv's dependencies (all static; macOS ships zlib and iconv in the system)
_dav1d () {
gitpin https://code.videolan.org/videolan/dav1d.git dav1d "$DAV1D_VER"
builddir dav1d
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-Denable_{tools,tests}=false -Ddefault_library=static
makeplusinstall
popd
}
_dav1d_mark=lib/libdav1d.a
_lcms2 () {
gitpin https://github.com/mm2/Little-CMS.git lcms2 "$LCMS2_VER"
builddir lcms2
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-Dtests=disabled -Dutils=false -Ddefault_library=static
makeplusinstall
popd
}
_lcms2_mark=lib/liblcms2.a
_ffmpeg () {
gitpin https://github.com/FFmpeg/FFmpeg.git ffmpeg "$FFMPEG_VER"
builddir ffmpeg
# No --enable-gpl: we only need LGPL-available features (see the linux
# script for the rationale) so the resulting libmpv stays LGPL and can be
# embedded via JNA without taint.
# --arch is explicit: ffmpeg would otherwise auto-detect aarch64 from
# uname -m on the runner even for the cross-compiled x86_64 slice.
args=(
--prefix="$prefix_dir" --pkg-config=pkg-config --target-os=darwin
--arch="$ARCH"
$commonflags
--cc="$CC" --cxx="$CXX"
--disable-{doc,programs}
--enable-muxer=spdif --enable-encoder=mjpeg,png --enable-libdav1d
)
../configure "${args[@]}"
makeplusinstall
popd
}
_ffmpeg_mark=lib/libavcodec.a
_freetype () {
# freetype moved to gitlab and publishes no release tarballs (the savanna
# tarball host is down), so clone the pinned tag like the other git deps.
gitpin https://gitlab.freedesktop.org/freetype/freetype.git freetype "$FREETYPE_TAG"
builddir freetype
# zlib stays (universal system lib); every other third-party provider is
# disabled so no arm64-only brew package can leak into the x86_64 slice.
# harfbuzz off: we link our own static harfbuzz into libmpv, not FT's
# hook (and it would pull brew's build in again).
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-Dzlib=enabled -Dbrotli=disabled -Dbzip2=disabled -Dpng=disabled \
-Dharfbuzz=disabled -Ddefault_library=static
makeplusinstall
popd
}
_freetype_mark=lib/libfreetype.a
_fribidi () {
gettar "https://github.com/fribidi/fribidi/releases/download/v${FRIBIDI_VER}/fribidi-${FRIBIDI_VER}.tar.xz"
builddir fribidi-${FRIBIDI_VER}
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-D{tests,docs}=false -Ddefault_library=static
makeplusinstall
popd
}
_fribidi_mark=lib/libfribidi.a
_harfbuzz () {
gettar "https://github.com/harfbuzz/harfbuzz/releases/download/${HARFBUZZ_VER}/harfbuzz-${HARFBUZZ_VER}.tar.xz"
builddir harfbuzz-${HARFBUZZ_VER}
# icu disabled: the runner's brew icu is arm64-only and would poison the
# x86_64 slice; harfbuzz shapes fine without it (built-in unicode data).
# coretext is the Apple font provider (system framework, universal).
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-Dtests=disabled -Dutilities=disabled -Dicu=disabled \
-Dcoretext=enabled -Ddefault_library=static
makeplusinstall
popd
}
_harfbuzz_mark=lib/libharfbuzz.a
_libass () {
gitpin https://github.com/libass/libass.git libass "$LIBASS_VER"
builddir libass
# CoreText is the system font provider (framework, universal); fontconfig
# is off so the runner's arm64-only brew package can't leak into the
# x86_64 slice. asm off for determinism across both slices.
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-Dfontconfig=disabled -Dcoretext=enabled -Dasm=disabled \
-Ddefault_library=static
makeplusinstall
popd
}
_libass_mark=lib/libass.a
for x in dav1d lcms2; do
build_if_missing $x
done
for x in ffmpeg freetype fribidi harfbuzz libass; do
build_if_missing $x
done
_libplacebo () {
gitpin https://code.videolan.org/videolan/libplacebo.git libplacebo "$LIBPLACEBO_VER"
builddir libplacebo
# opengl enabled (the only renderer jlibmpv uses); vulkan/d3d11 off;
# xxhash off (optional) so the arm64-only brew package can't leak into the
# x86_64 slice.
meson setup .. $MESON_EXTRA --buildtype release -Dprefix="$prefix_dir" \
-Ddemos=false -Dopengl=enabled -Dd3d11=disabled \
-Dvulkan=disabled -Dlcms=disabled -Dxxhash=disabled \
-Ddefault_library=static
makeplusinstall
popd
}
_libplacebo_mark=lib/libplacebo.a
build_if_missing libplacebo
## mpv
build=macos_build-$ARCH
rm -rf $build
# libmpv itself is the ONE shared object (default_library=shared), linked
# against the static dependency archives built above. No CLI player, no
# tests, no lua/javascript, no vulkan. OpenGL render API stays (cocoa +
# gl-cocoa are the macOS backend for it); coreaudio is the audio output;
# videotoolbox gives hardware decode.
meson setup $build $MESON_EXTRA \
--buildtype release -Dstrip=true \
-Ddefault_library=shared \
-Dlibmpv=true \
-Dgpl=false \
-Dcplayer=false \
-Dtests=false \
-Dlua=disabled \
-Djavascript=disabled \
-Dshaderc=disabled \
-Dspirv-cross=disabled \
-Dvulkan=disabled \
-Dswift-flags="--target=${SWIFT_TARGET}" \
-Dcocoa=enabled \
-Dgl-cocoa=enabled \
-Dcoreaudio=enabled \
-Dvideotoolbox-gl=enabled
meson compile -C $build
## Collect the self-contained dylib
mkdir -p artifact-$ARCH
LIBMPV_DYLIB=$(find $build -maxdepth 1 -name "libmpv.2.dylib" | head -n 1)
[ -n "$LIBMPV_DYLIB" ] || { echo "ERROR: libmpv dylib not found in $build"; ls -l $build; exit 1; }
cp -pv "$LIBMPV_DYLIB" artifact-$ARCH/
echo "=== artifact-$ARCH contents ==="
ls -l artifact-$ARCH
echo "=== libmpv-2.dylib ($ARCH) load commands (should be system frameworks only) ==="
otool -arch $ARCH "artifact-$ARCH/libmpv.2.dylib" -L