Skip to content

Commit 0995305

Browse files
authored
Merge pull request #8497 from freakboy3742/homebrew-isolation
2 parents 5bff2f3 + 96b898c commit 0995305

File tree

5 files changed

+124
-46
lines changed

5 files changed

+124
-46
lines changed

.github/workflows/wheels-dependencies.sh

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
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}
727
fi
8-
export PLAT=$CIBW_ARCHS
28+
PLAT=$CIBW_ARCHS
29+
30+
# Define custom utilities
931
source wheels/multibuild/common_utils.sh
1032
source wheels/multibuild/library_builders.sh
1133
if [ -z "$IS_MACOS" ]; then
@@ -38,35 +60,43 @@ BZIP2_VERSION=1.0.8
3860
LIBXCB_VERSION=1.17.0
3961
BROTLI_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+
4177
function 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

5387
function 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

6699
function 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
137187
fi
138188

139189
wrap_wheel_builder build
140190

191+
# Return to the project root to finish the build
192+
popd > /dev/null
193+
141194
# Append licenses
142195
for filename in wheels/dependency_licenses/*; do
143196
echo -e "\n\n----\n\n$(basename $filename | cut -f 1 -d '.')\n" | cat >> LICENSE

.github/workflows/wheels-test.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
#!/bin/bash
22
set -e
33

4+
# Ensure fribidi is installed by the system.
45
if [[ "$OSTYPE" == "darwin"* ]]; then
5-
brew install fribidi
6-
export PKG_CONFIG_PATH="/usr/local/opt/openblas/lib/pkgconfig"
7-
if [ -f /opt/homebrew/lib/libfribidi.dylib ]; then
8-
sudo cp /opt/homebrew/lib/libfribidi.dylib /usr/local/lib
6+
# If Homebrew is on the path during the build, it may leak into the wheels.
7+
# However, we *do* need Homebrew to provide a copy of fribidi for
8+
# testing purposes so that we can verify the fribidi shim works as expected.
9+
if [[ "$(uname -m)" == "x86_64" ]]; then
10+
HOMEBREW_PREFIX=/usr/local
11+
else
12+
HOMEBREW_PREFIX=/opt/homebrew
913
fi
14+
$HOMEBREW_PREFIX/bin/brew install fribidi
15+
16+
# Add the lib folder for fribidi so that the vendored library can be found.
17+
# Don't use $HOMEWBREW_PREFIX/lib directly - use the lib folder where the
18+
# installed copy of fribidi is cellared. This ensures we don't pick up the
19+
# Homebrew version of any other library that we're dependent on (most notably,
20+
# freetype).
21+
export DYLD_LIBRARY_PATH=$(dirname $(realpath $HOMEBREW_PREFIX/lib/libfribidi.dylib))
1022
elif [ "${AUDITWHEEL_POLICY::9}" == "musllinux" ]; then
1123
apk add curl fribidi
1224
else

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ lib64/
1919
parts/
2020
sdist/
2121
var/
22+
wheelhouse/
2223
*.egg-info/
2324
.installed.cfg
2425
*.egg
@@ -90,5 +91,9 @@ Tests/images/msp
9091
Tests/images/picins
9192
Tests/images/sunraster
9293

94+
# Test and dependency downloads
95+
pillow-depends-main.zip
96+
pillow-test-images.zip
97+
9398
# pyinstaller
9499
*.spec

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,17 @@ version = { attr = "PIL.__version__" }
9494
[tool.cibuildwheel]
9595
before-all = ".github/workflows/wheels-dependencies.sh"
9696
build-verbosity = 1
97+
9798
config-settings = "raqm=enable raqm=vendor fribidi=vendor imagequant=disable"
99+
# Disable platform guessing on macOS
100+
macos.config-settings = "raqm=enable raqm=vendor fribidi=vendor imagequant=disable platform-guessing=disable"
101+
98102
test-command = "cd {project} && .github/workflows/wheels-test.sh"
99103
test-extras = "tests"
100104

105+
[tool.cibuildwheel.macos.environment]
106+
PATH = "$(pwd)/build/deps/darwin/bin:$(dirname $(which python3)):/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin"
107+
101108
[tool.black]
102109
exclude = "wheels/multibuild"
103110

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def _remove_extension(self, name: str) -> None:
448448
def get_macos_sdk_path(self) -> str | None:
449449
try:
450450
sdk_path = (
451-
subprocess.check_output(["xcrun", "--show-sdk-path"])
451+
subprocess.check_output(["xcrun", "--show-sdk-path", "--sdk", "macosx"])
452452
.strip()
453453
.decode("latin1")
454454
)
@@ -606,6 +606,7 @@ def build_extensions(self) -> None:
606606
_add_directory(library_dirs, "/usr/X11/lib")
607607
_add_directory(include_dirs, "/usr/X11/include")
608608

609+
# Add the macOS SDK path.
609610
sdk_path = self.get_macos_sdk_path()
610611
if sdk_path:
611612
_add_directory(library_dirs, os.path.join(sdk_path, "usr", "lib"))

0 commit comments

Comments
 (0)