Skip to content

Commit d43e5fe

Browse files
bwoodsenddvarrazzo
authored andcommitted
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 3b684f9 commit d43e5fe

File tree

2 files changed

+117
-25
lines changed

2 files changed

+117
-25
lines changed

.github/workflows/packages.yml

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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
sdist: # {{{
@@ -59,10 +61,6 @@ jobs:
5961
linux: # {{{
6062
if: true
6163

62-
env:
63-
LIBPQ_VERSION: "16.0"
64-
OPENSSL_VERSION: "1.1.1w"
65-
6664
strategy:
6765
fail-fast: false
6866
matrix:
@@ -162,6 +160,12 @@ jobs:
162160
- name: Checkout repos
163161
uses: actions/checkout@v4
164162

163+
- name: Build dependencies
164+
run: ./scripts/build/build_libpq.sh
165+
166+
- name: Show dependency tree
167+
run: otool -L /tmp/libpq.build/lib/*.dylib
168+
165169
- name: Build wheels
166170
uses: pypa/[email protected]
167171
env:
@@ -172,12 +176,11 @@ jobs:
172176
export PYTHONPATH={project} &&
173177
python -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')"
174178
CIBW_ENVIRONMENT: >-
175-
MACOSX_DEPLOYMENT_TARGET=${{ matrix.macver }}.0
176179
PG_VERSION=16
177180
PACKAGE_NAME=psycopg2-binary
178181
PSYCOPG2_TESTDB=postgres
179182
PSYCOPG2_TEST_FAST=1
180-
PATH="/usr/local/opt/postgresql@${PG_VERSION}/bin:$PATH"
183+
PATH="/tmp/libpq.build/bin:$PATH"
181184
182185
- name: Upload artifacts
183186
uses: actions/upload-artifact@v4

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 m4 libtool
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 gettext
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} --disable-java \
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)