11#! /bin/bash
2- # Define custom utilities
3- # Test for macOS with [ -n "$IS_MACOS" ]
4- if [ -z " $IS_MACOS " ]; then
5- export MB_ML_LIBC=${AUDITWHEEL_POLICY:: 9}
6- export MB_ML_VER=${AUDITWHEEL_POLICY: 9}
2+
3+ # Setup that needs to be done before multibuild utils are invoked
4+ PROJECTDIR=$( pwd)
5+ if [[ " $( uname -s) " == " Darwin" ]]; then
6+ # Safety check - macOS builds require that CIBW_ARCHS is set, and that it
7+ # only contains a single value (even though cibuildwheel allows multiple
8+ # values in CIBW_ARCHS).
9+ if [[ -z " $CIBW_ARCHS " ]]; then
10+ echo " ERROR: Pillow macOS builds require CIBW_ARCHS be defined."
11+ exit 1
12+ fi
13+ if [[ " $CIBW_ARCHS " == * " " * ]]; then
14+ echo " ERROR: Pillow macOS builds only support a single architecture in CIBW_ARCHS."
15+ exit 1
16+ fi
17+
18+ # Build macOS dependencies in `build/darwin`
19+ # Install them into `build/deps/darwin`
20+ WORKDIR=$( pwd) /build/darwin
21+ BUILD_PREFIX=$( pwd) /build/deps/darwin
22+ else
23+ # Build prefix will default to /usr/local
24+ WORKDIR=$( pwd) /build
25+ MB_ML_LIBC=${AUDITWHEEL_POLICY:: 9}
26+ MB_ML_VER=${AUDITWHEEL_POLICY: 9}
727fi
8- export PLAT=$CIBW_ARCHS
28+ PLAT=$CIBW_ARCHS
29+
30+ # Define custom utilities
931source wheels/multibuild/common_utils.sh
1032source wheels/multibuild/library_builders.sh
1133if [ -z " $IS_MACOS " ]; then
@@ -38,35 +60,43 @@ BZIP2_VERSION=1.0.8
3860LIBXCB_VERSION=1.17.0
3961BROTLI_VERSION=1.1.0
4062
63+ function build_pkg_config {
64+ if [ -e pkg-config-stamp ]; then return ; fi
65+ # This essentially duplicates the Homebrew recipe
66+ ORIGINAL_CFLAGS=$CFLAGS
67+ CFLAGS=" $CFLAGS -Wno-int-conversion"
68+ build_simple pkg-config 0.29.2 https://pkg-config.freedesktop.org/releases tar.gz \
69+ --disable-debug --disable-host-tool --with-internal-glib \
70+ --with-pc-path=$BUILD_PREFIX /share/pkgconfig:$BUILD_PREFIX /lib/pkgconfig \
71+ --with-system-include-path=$( xcrun --show-sdk-path --sdk macosx) /usr/include
72+ CFLAGS=$ORIGINAL_CFLAGS
73+ export PKG_CONFIG=$BUILD_PREFIX /bin/pkg-config
74+ touch pkg-config-stamp
75+ }
76+
4177function build_brotli {
78+ if [ -e brotli-stamp ]; then return ; fi
4279 local cmake=$( get_modern_cmake)
4380 local out_dir=$( fetch_unpack https://github.com/google/brotli/archive/v$BROTLI_VERSION .tar.gz brotli-$BROTLI_VERSION .tar.gz)
4481 (cd $out_dir \
45- && $cmake -DCMAKE_INSTALL_PREFIX=$BUILD_PREFIX -DCMAKE_INSTALL_NAME_DIR=$BUILD_PREFIX /lib . \
82+ && $cmake -DCMAKE_INSTALL_PREFIX=$BUILD_PREFIX -DCMAKE_INSTALL_LIBDIR= $BUILD_PREFIX /lib - DCMAKE_INSTALL_NAME_DIR=$BUILD_PREFIX /lib . \
4683 && make install)
47- if [[ " $MB_ML_LIBC " == " manylinux" ]]; then
48- cp /usr/local/lib64/libbrotli* /usr/local/lib
49- cp /usr/local/lib64/pkgconfig/libbrotli* /usr/local/lib/pkgconfig
50- fi
84+ touch brotli-stamp
5185}
5286
5387function build_harfbuzz {
88+ if [ -e harfbuzz-stamp ]; then return ; fi
5489 python3 -m pip install meson ninja
5590
5691 local out_dir=$( fetch_unpack https://github.com/harfbuzz/harfbuzz/releases/download/$HARFBUZZ_VERSION /$HARFBUZZ_VERSION .tar.xz harfbuzz-$HARFBUZZ_VERSION .tar.xz)
5792 (cd $out_dir \
58- && meson setup build --buildtype=release -Dfreetype=enabled -Dglib=disabled)
93+ && meson setup build --prefix= $BUILD_PREFIX --libdir= $BUILD_PREFIX /lib -- buildtype=release -Dfreetype=enabled -Dglib=disabled)
5994 (cd $out_dir /build \
6095 && meson install)
61- if [[ " $MB_ML_LIBC " == " manylinux" ]]; then
62- cp /usr/local/lib64/libharfbuzz* /usr/local/lib
63- fi
96+ touch harfbuzz-stamp
6497}
6598
6699function build {
67- if [[ -n " $IS_MACOS " ]] && [[ " $CIBW_ARCHS " == " arm64" ]]; then
68- sudo chown -R runner /usr/local
69- fi
70100 build_xz
71101 if [ -z " $IS_ALPINE " ] && [ -z " $IS_MACOS " ]; then
72102 yum remove -y zlib-devel
@@ -78,16 +108,24 @@ function build {
78108 build_simple xorgproto 2024.1 https://www.x.org/pub/individual/proto
79109 build_simple libXau 1.0.11 https://www.x.org/pub/individual/lib
80110 build_simple libpthread-stubs 0.5 https://xcb.freedesktop.org/dist
81- if [[ " $CIBW_ARCHS " == " arm64" ]]; then
82- cp /usr/local/share/pkgconfig/xcb-proto.pc /usr/local/lib/pkgconfig
83- fi
84111 else
85- sed s/\$ {pc_sysrootdir\} // /usr/local/ share/pkgconfig/xcb-proto.pc > /usr/local /lib/pkgconfig/xcb-proto.pc
112+ sed s/\$ {pc_sysrootdir\} // $BUILD_PREFIX / share/pkgconfig/xcb-proto.pc > $BUILD_PREFIX /lib/pkgconfig/xcb-proto.pc
86113 fi
87114 build_simple libxcb $LIBXCB_VERSION https://www.x.org/releases/individual/lib
88115
89116 build_libjpeg_turbo
90- build_tiff
117+ if [ -n " $IS_MACOS " ]; then
118+ # Custom tiff build to include jpeg; by default, configure won't include
119+ # headers/libs in the custom macOS prefix. Explicitly disable webp,
120+ # libdeflate and zstd, because on x86_64 macs, it will pick up the
121+ # Homebrew versions of those libraries from /usr/local.
122+ build_simple tiff $TIFF_VERSION https://download.osgeo.org/libtiff tar.gz \
123+ --with-jpeg-include-dir=$BUILD_PREFIX /include --with-jpeg-lib-dir=$BUILD_PREFIX /lib \
124+ --disable-webp --disable-libdeflate --disable-zstd
125+ else
126+ build_tiff
127+ fi
128+
91129 build_libpng
92130 build_lcms2
93131 build_openjpeg
@@ -112,32 +150,47 @@ function build {
112150 build_harfbuzz
113151}
114152
153+ # Perform all dependency builds in the build subfolder.
154+ mkdir -p $WORKDIR
155+ pushd $WORKDIR > /dev/null
156+
115157# Any stuff that you need to do before you start building the wheels
116158# Runs in the root directory of this repository.
117- curl -fsSL -o pillow-depends-main.zip https://github.com/python-pillow/pillow-depends/archive/main.zip
118- untar pillow-depends-main.zip
119-
120- if [[ -n " $IS_MACOS " ]]; then
121- # libdeflate may cause a minimum target error when repairing the wheel
122- # libtiff and libxcb cause a conflict with building libtiff and libxcb
123- # libxau and libxdmcp cause an issue on macOS < 11
124- # remove cairo to fix building harfbuzz on arm64
125- # remove lcms2 and libpng to fix building openjpeg on arm64
126- # remove jpeg-turbo to avoid inclusion on arm64
127- # remove webp and zstd to avoid inclusion on x86_64
128- # curl from brew requires zstd, use system curl
129- brew remove --ignore-dependencies libpng libtiff libxcb libxau libxdmcp curl cairo lcms2 zstd
130- if [[ " $CIBW_ARCHS " == " arm64" ]]; then
131- brew remove --ignore-dependencies jpeg-turbo
132- else
133- brew remove --ignore-dependencies libdeflate webp
159+ if [[ ! -d $WORKDIR /pillow-depends-main ]]; then
160+ if [[ ! -f $PROJECTDIR /pillow-depends-main.zip ]]; then
161+ echo " Download pillow dependency sources..."
162+ curl -fSL -o $PROJECTDIR /pillow-depends-main.zip https://github.com/python-pillow/pillow-depends/archive/main.zip
134163 fi
164+ echo " Unpacking pillow dependency sources..."
165+ untar $PROJECTDIR /pillow-depends-main.zip
166+ fi
135167
136- brew install pkg-config
168+ if [[ -n " $IS_MACOS " ]]; then
169+ # Homebrew (or similar packaging environments) install can contain some of
170+ # the libraries that we're going to build. However, they may be compiled
171+ # with a MACOSX_DEPLOYMENT_TARGET that doesn't match what we want to use,
172+ # and they may bring in other dependencies that we don't want. The same will
173+ # be true of any other locations on the path. To avoid conflicts, strip the
174+ # path down to the bare minimum (which, on macOS, won't include any
175+ # development dependencies).
176+ export PATH=" $BUILD_PREFIX /bin:$( dirname $( which python3) ) :/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin"
177+ export CMAKE_PREFIX_PATH=$BUILD_PREFIX
178+
179+ # Ensure the basic structure of the build prefix directory exists.
180+ mkdir -p " $BUILD_PREFIX /bin"
181+ mkdir -p " $BUILD_PREFIX /lib"
182+
183+ # Ensure pkg-config is available
184+ build_pkg_config
185+ # Ensure cmake is available
186+ python3 -m pip install cmake
137187fi
138188
139189wrap_wheel_builder build
140190
191+ # Return to the project root to finish the build
192+ popd > /dev/null
193+
141194# Append licenses
142195for filename in wheels/dependency_licenses/* ; do
143196 echo -e " \n\n----\n\n$( basename $filename | cut -f 1 -d ' .' ) \n" | cat >> LICENSE
0 commit comments