Skip to content

Commit 96dbaa3

Browse files
committed
ci(macos): Avoid linking against homebrew
Homebrew binaries are always compiled for exactly the version they're installed on making them very un-portable. When a wheel is "repaired" by cibuildwheel, delocate-wheel pulls in _psycopg's dependencies (libpq.dylib, libssl.dylib and libcrypto.dylib) which, on a GitHub Actions macOS 14 runner, are provided by Homebrew and are therefore only macOS >= 14 compatible. The resultant wheel is therefore incompatible with all but the latest macOS versions. Build all dependencies from source so that we can set the deployment target to something sensible. Fixes #1753.
1 parent e83754a commit 96dbaa3

File tree

3 files changed

+122
-35
lines changed

3 files changed

+122
-35
lines changed

.github/workflows/packages.yml

+13-16
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ on:
55

66
env:
77
PIP_BREAK_SYSTEM_PACKAGES: "1"
8+
LIBPQ_VERSION: "16.0"
9+
OPENSSL_VERSION: "1.1.1w"
810

911
jobs:
1012
build-sdist:
11-
if: true
13+
if: false
1214
strategy:
1315
fail-fast: false
1416
matrix:
@@ -58,16 +60,12 @@ jobs:
5860
build-linux:
5961
if: true
6062

61-
env:
62-
LIBPQ_VERSION: "16.0"
63-
OPENSSL_VERSION: "1.1.1w"
64-
6563
strategy:
6664
fail-fast: false
6765
matrix:
6866
platform: [manylinux, musllinux]
69-
arch: [x86_64, i686, aarch64, ppc64le]
70-
pyver: [cp38, cp39, cp310, cp311, cp312, cp313]
67+
arch: [x86_64] #, i686, aarch64, ppc64le]
68+
pyver: [cp38] #, cp39, cp310, cp311, cp312, cp313]
7169

7270
runs-on: ubuntu-latest
7371
steps:
@@ -140,18 +138,12 @@ jobs:
140138
matrix:
141139
# These archs require an Apple M1 runner: [arm64, universal2]
142140
arch: [x86_64]
143-
pyver: [cp38, cp39, cp310, cp311, cp312, cp313]
141+
pyver: [cp38, cp313]
144142
macver: ["12"]
145143
include:
146144
- arch: arm64
147145
pyver: cp310
148146
macver: "14"
149-
- arch: arm64
150-
pyver: cp311
151-
macver: "14"
152-
- arch: arm64
153-
pyver: cp312
154-
macver: "14"
155147
- arch: arm64
156148
pyver: cp313
157149
macver: "14"
@@ -160,6 +152,12 @@ jobs:
160152
- name: Checkout repos
161153
uses: actions/checkout@v4
162154

155+
- name: Build dependencies
156+
run: ./scripts/build/build_libpq.sh
157+
158+
- name: Show dependency tree
159+
run: otool -L /tmp/libpq.build/lib/*.dylib
160+
163161
- name: Build wheels
164162
uses: pypa/[email protected]
165163
env:
@@ -170,12 +168,11 @@ jobs:
170168
export PYTHONPATH={project} &&
171169
python -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')"
172170
CIBW_ENVIRONMENT: >-
173-
MACOSX_DEPLOYMENT_TARGET=${{ matrix.macver }}.0
174171
PG_VERSION=16
175172
PACKAGE_NAME=psycopg2-binary
176173
PSYCOPG2_TESTDB=postgres
177174
PSYCOPG2_TEST_FAST=1
178-
PATH="/usr/local/opt/postgresql@${PG_VERSION}/bin:$PATH"
175+
PATH="/tmp/libpq.build/bin:$PATH"
179176
180177
- name: Upload artifacts
181178
uses: actions/upload-artifact@v4

.github/workflows/tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99

1010
jobs:
1111
tests:
12+
if: false
1213
name: Unit tests run
1314
runs-on: ubuntu-latest
1415

scripts/build/build_libpq.sh

+108-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# Build a modern version of libpq and depending libs from source on Centos 5
3+
# Build a modern version of libpq and depending libs from source on Centos 5, Alpine or macOS
44

55
set -euo pipefail
66
set -x
@@ -12,30 +12,69 @@ postgres_version="${LIBPQ_VERSION}"
1212
# last release: https://www.openssl.org/source/
1313
openssl_version="${OPENSSL_VERSION}"
1414

15+
# last release: https://kerberos.org/dist/
16+
krb5_version="1.21.3"
17+
18+
# last release: https://www.gnu.org/software/gettext/
19+
gettext_version="0.22.5"
20+
1521
# last release: https://openldap.org/software/download/
16-
ldap_version="2.6.3"
22+
ldap_version="2.6.8"
1723

1824
# last release: https://github.com/cyrusimap/cyrus-sasl/releases
1925
sasl_version="2.1.28"
2026

2127
export LIBPQ_BUILD_PREFIX=${LIBPQ_BUILD_PREFIX:-/tmp/libpq.build}
2228

23-
if [[ -f "${LIBPQ_BUILD_PREFIX}/lib/libpq.so" ]]; then
29+
case "$(uname)" in
30+
Darwin)
31+
ID=macos
32+
library_suffix=dylib
33+
;;
34+
35+
Linux)
36+
source /etc/os-release
37+
library_suffix=so
38+
;;
39+
40+
*)
41+
echo "$0: unexpected Operating system: '$(uname)'" >&2
42+
exit 1
43+
;;
44+
esac
45+
46+
if [[ -f "${LIBPQ_BUILD_PREFIX}/lib/libpq.${library_suffix}" ]]; then
2447
echo "libpq already available: build skipped" >&2
2548
exit 0
2649
fi
2750

28-
source /etc/os-release
29-
3051
case "$ID" in
3152
centos)
3253
yum update -y
3354
yum install -y zlib-devel krb5-devel pam-devel
55+
curl="$(which curl)"
3456
;;
3557

3658
alpine)
3759
apk upgrade
3860
apk add --no-cache zlib-dev krb5-dev linux-pam-dev openldap-dev openssl-dev
61+
curl="$(which curl)"
62+
;;
63+
64+
macos)
65+
brew install automake
66+
# If available, libpq seemingly insists on linking against homebrew's
67+
# openssl no matter what so remove it. Since homebrew's curl depends on
68+
# it, force use of system curl.
69+
brew uninstall --force --ignore-dependencies openssl m4
70+
curl="/usr/bin/curl"
71+
# The deployment target should be <= to that of the oldest supported Python version.
72+
# e.g. https://www.python.org/downloads/release/python-380/
73+
if [ "$(uname -m)" == "x86_64" ]; then
74+
export MACOSX_DEPLOYMENT_TARGET=10.9
75+
else
76+
export MACOSX_DEPLOYMENT_TARGET=11.0
77+
fi
3978
;;
4079

4180
*)
@@ -44,12 +83,12 @@ case "$ID" in
4483
;;
4584
esac
4685

47-
if [ "$ID" == "centos" ]; then
86+
if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
4887

4988
# Build openssl if needed
5089
openssl_tag="OpenSSL_${openssl_version//./_}"
5190
openssl_dir="openssl-${openssl_tag}"
52-
if [ ! -d "${openssl_dir}" ]; then curl -sL \
91+
if [ ! -d "${openssl_dir}" ]; then "$curl" -sL \
5392
https://github.com/openssl/openssl/archive/${openssl_tag}.tar.gz \
5493
| tar xzf -
5594

@@ -70,7 +109,55 @@ if [ "$ID" == "centos" ]; then
70109
fi
71110

72111

73-
if [ "$ID" == "centos" ]; then
112+
if [ "$ID" == "macos" ]; then
113+
114+
# Build kerberos if needed
115+
krb5_dir="krb5-${krb5_version}/src"
116+
if [ ! -d "${krb5_dir}" ]; then
117+
"$curl" -sL \
118+
curl -sL "https://kerberos.org/dist/krb5/$(echo 1.21.3 | grep -oE '\d+\.\d+')/krb5-${krb5_version}.tar.gz" \
119+
| tar xzf -
120+
121+
cd "${krb5_dir}"
122+
123+
./configure --prefix=${LIBPQ_BUILD_PREFIX} \
124+
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
125+
make
126+
else
127+
cd "${krb5_dir}"
128+
fi
129+
130+
make install
131+
cd ../..
132+
133+
fi
134+
135+
136+
if [ "$ID" == "macos" ]; then
137+
138+
# Build gettext if needed
139+
gettext_dir="gettext-${gettext_version}"
140+
if [ ! -d "${gettext_dir}" ]; then
141+
"$curl" -sL \
142+
curl -sL "https://ftp.gnu.org/pub/gnu/gettext/gettext-${gettext_version}.tar.gz" \
143+
| tar xzf -
144+
145+
cd "${gettext_dir}"
146+
147+
./configure --prefix=${LIBPQ_BUILD_PREFIX} \
148+
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
149+
make -C gettext-runtime all
150+
else
151+
cd "${gettext_dir}"
152+
fi
153+
154+
make -C gettext-runtime install
155+
cd ..
156+
157+
fi
158+
159+
160+
if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
74161

75162
# Build libsasl2 if needed
76163
# The system package (cyrus-sasl-devel) causes an amazing error on i686:
@@ -79,14 +166,14 @@ if [ "$ID" == "centos" ]; then
79166
sasl_tag="cyrus-sasl-${sasl_version}"
80167
sasl_dir="cyrus-sasl-${sasl_tag}"
81168
if [ ! -d "${sasl_dir}" ]; then
82-
curl -sL \
169+
"$curl" -sL \
83170
https://github.com/cyrusimap/cyrus-sasl/archive/${sasl_tag}.tar.gz \
84171
| tar xzf -
85172

86173
cd "${sasl_dir}"
87174

88175
autoreconf -i
89-
./configure --prefix=${LIBPQ_BUILD_PREFIX} \
176+
./configure --prefix=${LIBPQ_BUILD_PREFIX} --disable-macos-framework \
90177
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
91178
make
92179
else
@@ -102,13 +189,13 @@ if [ "$ID" == "centos" ]; then
102189
fi
103190

104191

105-
if [ "$ID" == "centos" ]; then
192+
if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
106193

107194
# Build openldap if needed
108195
ldap_tag="${ldap_version}"
109196
ldap_dir="openldap-${ldap_tag}"
110197
if [ ! -d "${ldap_dir}" ]; then
111-
curl -sL \
198+
"$curl" -sL \
112199
https://www.openldap.org/software/download/OpenLDAP/openldap-release/openldap-${ldap_tag}.tgz \
113200
| tar xzf -
114201

@@ -129,7 +216,7 @@ if [ "$ID" == "centos" ]; then
129216
make -C libraries/liblber/ install
130217
make -C libraries/libldap/ install
131218
make -C include/ install
132-
chmod +x ${LIBPQ_BUILD_PREFIX}/lib/{libldap,liblber}*.so*
219+
chmod +x ${LIBPQ_BUILD_PREFIX}/lib/{libldap,liblber}*.${library_suffix}*
133220
cd ..
134221

135222
fi
@@ -139,17 +226,19 @@ fi
139226
postgres_tag="REL_${postgres_version//./_}"
140227
postgres_dir="postgres-${postgres_tag}"
141228
if [ ! -d "${postgres_dir}" ]; then
142-
curl -sL \
229+
"$curl" -sL \
143230
https://github.com/postgres/postgres/archive/${postgres_tag}.tar.gz \
144231
| tar xzf -
145232

146233
cd "${postgres_dir}"
147234

148-
# Match the default unix socket dir default with what defined on Ubuntu and
149-
# Red Hat, which seems the most common location
150-
sed -i 's|#define DEFAULT_PGSOCKET_DIR .*'\
235+
if [ "$ID" != "macos" ]; then
236+
# Match the default unix socket dir default with what defined on Ubuntu and
237+
# Red Hat, which seems the most common location
238+
sed -i 's|#define DEFAULT_PGSOCKET_DIR .*'\
151239
'|#define DEFAULT_PGSOCKET_DIR "/var/run/postgresql"|' \
152-
src/include/pg_config_manual.h
240+
src/include/pg_config_manual.h
241+
fi
153242

154243
# Often needed, but currently set by the workflow
155244
# export LD_LIBRARY_PATH="${LIBPQ_BUILD_PREFIX}/lib"
@@ -171,4 +260,4 @@ make -C src/bin/pg_config install
171260
make -C src/include install
172261
cd ..
173262

174-
find ${LIBPQ_BUILD_PREFIX} -name \*.so.\* -type f -exec strip --strip-unneeded {} \;
263+
find ${LIBPQ_BUILD_PREFIX} -name \*.${library_suffix}.\* -type f -exec strip --strip-unneeded {} \;

0 commit comments

Comments
 (0)