From 1e65578ad4b64e9c5d6be73ee640dae85252c852 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 11 Jul 2025 09:48:23 +0800 Subject: [PATCH 1/5] Add build instructions for libavif on iOS. --- .ci/requirements-cibw.txt | 2 +- .github/workflows/wheels-dependencies.sh | 31 ++++++++++++++++++------ checks/check_wheel.py | 3 +-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.ci/requirements-cibw.txt b/.ci/requirements-cibw.txt index e1eb52eb8ae..823671828f7 100644 --- a/.ci/requirements-cibw.txt +++ b/.ci/requirements-cibw.txt @@ -1 +1 @@ -cibuildwheel==3.0.1 +cibuildwheel==3.1.2 diff --git a/.github/workflows/wheels-dependencies.sh b/.github/workflows/wheels-dependencies.sh index 4519271b9d3..1c2e5926272 100755 --- a/.github/workflows/wheels-dependencies.sh +++ b/.github/workflows/wheels-dependencies.sh @@ -202,14 +202,24 @@ function build_libavif { -DCMAKE_CXX_FLAGS_MINSIZEREL="-Oz -DNDEBUG -flto" \ -DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,-S,-x,-dead_strip_dylibs" \ ) + if [[ -z "$IOS_SDK" ]]; then + libavif_cmake_flags=( + $libavif_cmake_flags + -DBUILD_SHARED_LIBS=ON + ) + fi else if [[ "$MB_ML_VER" == 2014 ]] && [[ "$PLAT" == "x86_64" ]]; then build_type=Release fi - libavif_cmake_flags=(-DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,--strip-all,-z,relro,-z,now") + libavif_cmake_flags=( + -DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,--strip-all,-z,relro,-z,now" \ + -DBUILD_SHARED_LIBS=ON \ + ) fi local out_dir=$(fetch_unpack https://github.com/AOMediaCodec/libavif/archive/refs/tags/v$LIBAVIF_VERSION.tar.gz libavif-$LIBAVIF_VERSION.tar.gz) + # CONFIG_AV1_HIGHBITDEPTH=0 is a flag for libaom (included as a subproject # of libavif) that disables support for encoding high bit depth images. (cd $out_dir \ @@ -217,7 +227,6 @@ function build_libavif { -DCMAKE_INSTALL_PREFIX=$BUILD_PREFIX \ -DCMAKE_INSTALL_LIBDIR=$BUILD_PREFIX/lib \ -DCMAKE_INSTALL_NAME_DIR=$BUILD_PREFIX/lib \ - -DBUILD_SHARED_LIBS=ON \ -DAVIF_LIBSHARPYUV=LOCAL \ -DAVIF_LIBYUV=LOCAL \ -DAVIF_CODEC_AOM=LOCAL \ @@ -229,8 +238,17 @@ function build_libavif { -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ -DCMAKE_BUILD_TYPE=$build_type \ "${libavif_cmake_flags[@]}" \ - . \ - && make install) + $HOST_CMAKE_FLAGS . ) + + if [[ -n "$IOS_SDK" ]]; then + # libavif's CMake configuration generates a meson cross file... but it + # doesn't work for iOS cross-compilation. Copy in Pillow-generated + # meson-cross config to replace the cmake-generated version. + cp $WORKDIR/meson-cross.txt $out_dir/crossfile-apple.meson + fi + + (cd $out_dir && make install) + touch libavif-stamp } @@ -268,10 +286,7 @@ function build { build_tiff fi - if [[ -z "$IOS_SDK" ]]; then - # Short term workaround; don't build libavif on iOS - build_libavif - fi + build_libavif build_libpng build_lcms2 build_openjpeg diff --git a/checks/check_wheel.py b/checks/check_wheel.py index 3d806eb71e2..937722c4bab 100644 --- a/checks/check_wheel.py +++ b/checks/check_wheel.py @@ -25,8 +25,7 @@ def test_wheel_modules() -> None: elif sys.platform == "ios": # tkinter is not available on iOS - # libavif is not available on iOS (for now) - expected_modules -= {"tkinter", "avif"} + expected_modules.remove("tkinter") assert set(features.get_supported_modules()) == expected_modules From c305b54a97ec94cf508e5b1b817dd72a856d5cc9 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 30 Jul 2025 14:28:11 +0800 Subject: [PATCH 2/5] Correct escaping for libavif build flags. --- .github/workflows/wheels-dependencies.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/wheels-dependencies.sh b/.github/workflows/wheels-dependencies.sh index 1c2e5926272..2a1082aca5d 100755 --- a/.github/workflows/wheels-dependencies.sh +++ b/.github/workflows/wheels-dependencies.sh @@ -204,7 +204,7 @@ function build_libavif { ) if [[ -z "$IOS_SDK" ]]; then libavif_cmake_flags=( - $libavif_cmake_flags + "${libavif_cmake_flags[@]}" \ -DBUILD_SHARED_LIBS=ON ) fi From cd4c416d4c798db7504e489ed8beb14184357e5b Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 1 Aug 2025 12:30:57 +0800 Subject: [PATCH 3/5] Disable libavif on x86_64 simulators. --- .github/workflows/wheels-dependencies.sh | 7 ++++++- checks/check_wheel.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/wheels-dependencies.sh b/.github/workflows/wheels-dependencies.sh index 2a1082aca5d..d1b60e3ffc4 100755 --- a/.github/workflows/wheels-dependencies.sh +++ b/.github/workflows/wheels-dependencies.sh @@ -286,7 +286,12 @@ function build { build_tiff fi - build_libavif + if [[ -z "$IOS_SDK" ]] || [[ "$PLAT" == "arm64" ]]; then + # Building libavif for x86_64 iOS simulator isn't currently possible + # because it requires the use of nasm, which doesn't create + # iOS-compatible binaries. + build_libavif + fi build_libpng build_lcms2 build_openjpeg diff --git a/checks/check_wheel.py b/checks/check_wheel.py index 937722c4bab..4e305ceb4d5 100644 --- a/checks/check_wheel.py +++ b/checks/check_wheel.py @@ -27,6 +27,10 @@ def test_wheel_modules() -> None: # tkinter is not available on iOS expected_modules.remove("tkinter") + # libavif is not available on x86_64 iOS simulators + if platform.machine() == "x86_64": + expected_modules.remove("avif") + assert set(features.get_supported_modules()) == expected_modules From d042ce3b1908e2f1d3fb8c2fbdb9558b53c69cd8 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 1 Aug 2025 12:42:49 +0800 Subject: [PATCH 4/5] Simplify shared build config. --- .github/workflows/wheels-dependencies.sh | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/wheels-dependencies.sh b/.github/workflows/wheels-dependencies.sh index d1b60e3ffc4..e062bea6695 100755 --- a/.github/workflows/wheels-dependencies.sh +++ b/.github/workflows/wheels-dependencies.sh @@ -191,6 +191,7 @@ function build_libavif { fi local build_type=MinSizeRel + local build_shared=ON local lto=ON local libavif_cmake_flags @@ -202,20 +203,14 @@ function build_libavif { -DCMAKE_CXX_FLAGS_MINSIZEREL="-Oz -DNDEBUG -flto" \ -DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,-S,-x,-dead_strip_dylibs" \ ) - if [[ -z "$IOS_SDK" ]]; then - libavif_cmake_flags=( - "${libavif_cmake_flags[@]}" \ - -DBUILD_SHARED_LIBS=ON - ) + if [[ -n "$IOS_SDK" ]]; then + build_shared=OFF fi else if [[ "$MB_ML_VER" == 2014 ]] && [[ "$PLAT" == "x86_64" ]]; then build_type=Release fi - libavif_cmake_flags=( - -DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,--strip-all,-z,relro,-z,now" \ - -DBUILD_SHARED_LIBS=ON \ - ) + libavif_cmake_flags=(-DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,--strip-all,-z,relro,-z,now") fi local out_dir=$(fetch_unpack https://github.com/AOMediaCodec/libavif/archive/refs/tags/v$LIBAVIF_VERSION.tar.gz libavif-$LIBAVIF_VERSION.tar.gz) @@ -227,6 +222,7 @@ function build_libavif { -DCMAKE_INSTALL_PREFIX=$BUILD_PREFIX \ -DCMAKE_INSTALL_LIBDIR=$BUILD_PREFIX/lib \ -DCMAKE_INSTALL_NAME_DIR=$BUILD_PREFIX/lib \ + -DBUILD_SHARED_LIBS=$build_shared \ -DAVIF_LIBSHARPYUV=LOCAL \ -DAVIF_LIBYUV=LOCAL \ -DAVIF_CODEC_AOM=LOCAL \ From 41b83dc0d90df381a17089120251f6026e16935e Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Sat, 2 Aug 2025 11:51:59 +1000 Subject: [PATCH 5/5] For iOS x86_64, do not use dav1d and build aom without optimizations (#8) Co-authored-by: Andrew Murray --- .github/workflows/wheels-dependencies.sh | 21 +++++++++++---------- checks/check_wheel.py | 4 ---- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.github/workflows/wheels-dependencies.sh b/.github/workflows/wheels-dependencies.sh index e062bea6695..d58c6512669 100755 --- a/.github/workflows/wheels-dependencies.sh +++ b/.github/workflows/wheels-dependencies.sh @@ -186,7 +186,7 @@ function build_libavif { python3 -m pip install meson ninja - if [[ "$PLAT" == "x86_64" ]] || [ -n "$SANITIZER" ]; then + if ([[ "$PLAT" == "x86_64" ]] && [[ -z "$IOS_SDK" ]]) || [ -n "$SANITIZER" ]; then build_simple nasm 2.16.03 https://www.nasm.us/pub/nasm/releasebuilds/2.16.03 fi @@ -196,7 +196,7 @@ function build_libavif { local libavif_cmake_flags - if [ -n "$IS_MACOS" ]; then + if [[ -n "$IS_MACOS" ]]; then lto=OFF libavif_cmake_flags=( -DCMAKE_C_FLAGS_MINSIZEREL="-Oz -DNDEBUG -flto" \ @@ -212,6 +212,14 @@ function build_libavif { fi libavif_cmake_flags=(-DCMAKE_SHARED_LINKER_FLAGS_INIT="-Wl,--strip-all,-z,relro,-z,now") fi + if [[ -n "$IOS_SDK" ]] && [[ "$PLAT" == "x86_64" ]]; then + libavif_cmake_flags+=(-DAOM_TARGET_CPU=generic) + else + libavif_cmake_flags+=( + -DAVIF_CODEC_AOM_DECODE=OFF \ + -DAVIF_CODEC_DAV1D=LOCAL + ) + fi local out_dir=$(fetch_unpack https://github.com/AOMediaCodec/libavif/archive/refs/tags/v$LIBAVIF_VERSION.tar.gz libavif-$LIBAVIF_VERSION.tar.gz) @@ -227,8 +235,6 @@ function build_libavif { -DAVIF_LIBYUV=LOCAL \ -DAVIF_CODEC_AOM=LOCAL \ -DCONFIG_AV1_HIGHBITDEPTH=0 \ - -DAVIF_CODEC_AOM_DECODE=OFF \ - -DAVIF_CODEC_DAV1D=LOCAL \ -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=$lto \ -DCMAKE_C_VISIBILITY_PRESET=hidden \ -DCMAKE_CXX_VISIBILITY_PRESET=hidden \ @@ -282,12 +288,7 @@ function build { build_tiff fi - if [[ -z "$IOS_SDK" ]] || [[ "$PLAT" == "arm64" ]]; then - # Building libavif for x86_64 iOS simulator isn't currently possible - # because it requires the use of nasm, which doesn't create - # iOS-compatible binaries. - build_libavif - fi + build_libavif build_libpng build_lcms2 build_openjpeg diff --git a/checks/check_wheel.py b/checks/check_wheel.py index 4e305ceb4d5..937722c4bab 100644 --- a/checks/check_wheel.py +++ b/checks/check_wheel.py @@ -27,10 +27,6 @@ def test_wheel_modules() -> None: # tkinter is not available on iOS expected_modules.remove("tkinter") - # libavif is not available on x86_64 iOS simulators - if platform.machine() == "x86_64": - expected_modules.remove("avif") - assert set(features.get_supported_modules()) == expected_modules