Skip to content

Commit 5509e01

Browse files
authored
Merge pull request #1755 from bwoodsend/fix-macos-deployment-target
ci(macos): Avoid linking against Homebrew
2 parents 3b684f9 + 6cd0fbd commit 5509e01

File tree

5 files changed

+136
-62
lines changed

5 files changed

+136
-62
lines changed

.github/workflows/packages.yml

Lines changed: 13 additions & 24 deletions
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:
@@ -134,55 +132,46 @@ jobs:
134132
# }}}
135133

136134
macos: # {{{
137-
runs-on: macos-${{ matrix.macver }}
135+
runs-on: macos-latest
138136
if: true
139137

140138
strategy:
141139
fail-fast: false
142140
matrix:
143141
# These archs require an Apple M1 runner: [arm64, universal2]
144-
arch: [x86_64]
142+
arch: [x86_64, arm64]
145143
pyver: [cp39, cp310, cp311, cp312, cp313]
146-
macver: ["13"]
147-
include:
148-
- arch: arm64
149-
pyver: cp310
150-
macver: "14"
151-
- arch: arm64
152-
pyver: cp311
153-
macver: "14"
154-
- arch: arm64
155-
pyver: cp312
156-
macver: "14"
157-
- arch: arm64
158-
pyver: cp313
159-
macver: "14"
160144

161145
steps:
162146
- name: Checkout repos
163147
uses: actions/checkout@v4
164148

149+
- name: Cache libpq build
150+
uses: actions/cache@v4
151+
with:
152+
path: /tmp/libpq.build
153+
key: libpq-${{ env.LIBPQ_VERSION }}-macos-${{ matrix.arch }}
154+
165155
- name: Build wheels
166156
uses: pypa/cibuildwheel@v2.22.0
167157
env:
168158
CIBW_BUILD: ${{matrix.pyver}}-macosx_${{matrix.arch}}
169159
CIBW_ARCHS_MACOS: ${{matrix.arch}}
160+
MACOSX_ARCHITECTURE: ${{matrix.arch}}
170161
CIBW_BEFORE_ALL_MACOS: ./scripts/build/wheel_macos_before_all.sh
171162
CIBW_TEST_COMMAND: >-
172163
export PYTHONPATH={project} &&
173164
python -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')"
174165
CIBW_ENVIRONMENT: >-
175-
MACOSX_DEPLOYMENT_TARGET=${{ matrix.macver }}.0
176166
PG_VERSION=16
177167
PACKAGE_NAME=psycopg2-binary
178168
PSYCOPG2_TESTDB=postgres
179-
PSYCOPG2_TEST_FAST=1
180-
PATH="/usr/local/opt/postgresql@${PG_VERSION}/bin:$PATH"
169+
PATH="/tmp/libpq.build/bin:$PATH"
181170
182171
- name: Upload artifacts
183172
uses: actions/upload-artifact@v4
184173
with:
185-
name: macos-${{matrix.pyver}}-macos-${{matrix.macver}}_${{matrix.arch}}
174+
name: macos-${{matrix.pyver}}-macos-${{matrix.arch}}
186175
path: ./wheelhouse/*.whl
187176

188177
# }}}

scripts/build/build_libpq.sh

Lines changed: 115 additions & 36 deletions
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,21 +12,39 @@ 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+
1518
# last release: https://openldap.org/software/download/
16-
ldap_version="2.6.3"
19+
ldap_version="2.6.8"
1720

1821
# last release: https://github.com/cyrusimap/cyrus-sasl/releases
1922
sasl_version="2.1.28"
2023

2124
export LIBPQ_BUILD_PREFIX=${LIBPQ_BUILD_PREFIX:-/tmp/libpq.build}
2225

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

28-
source /etc/os-release
29-
3048
case "$ID" in
3149
centos)
3250
yum update -y
@@ -38,39 +56,101 @@ case "$ID" in
3856
apk add --no-cache zlib-dev krb5-dev linux-pam-dev openldap-dev openssl-dev
3957
;;
4058

59+
macos)
60+
brew install automake m4 libtool
61+
# If available, libpq seemingly insists on linking against homebrew's
62+
# openssl no matter what so remove it. Since homebrew's curl depends on
63+
# it, force use of system curl.
64+
brew uninstall --force --ignore-dependencies openssl gettext curl
65+
if [ -z "${MACOSX_ARCHITECTURE:-}" ]; then
66+
MACOSX_ARCHITECTURE="$(uname -m)"
67+
fi
68+
# Set the deployment target to be <= to that of the oldest supported Python version.
69+
# e.g. https://www.python.org/downloads/release/python-380/
70+
if [ "$MACOSX_ARCHITECTURE" == "x86_64" ]; then
71+
export MACOSX_DEPLOYMENT_TARGET=10.9
72+
else
73+
export MACOSX_DEPLOYMENT_TARGET=11.0
74+
fi
75+
;;
76+
4177
*)
4278
echo "$0: unexpected Linux distribution: '$ID'" >&2
4379
exit 1
4480
;;
4581
esac
4682

47-
if [ "$ID" == "centos" ]; then
83+
84+
if [ "$ID" == "macos" ]; then
85+
make_configure_standard_flags=( \
86+
--prefix=${LIBPQ_BUILD_PREFIX} \
87+
"CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ -arch $MACOSX_ARCHITECTURE" \
88+
"LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib -arch $MACOSX_ARCHITECTURE" \
89+
)
90+
else
91+
make_configure_standard_flags=( \
92+
--prefix=${LIBPQ_BUILD_PREFIX} \
93+
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ \
94+
LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib \
95+
)
96+
fi
97+
98+
99+
if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
48100

49101
# Build openssl if needed
50102
openssl_tag="OpenSSL_${openssl_version//./_}"
51103
openssl_dir="openssl-${openssl_tag}"
52-
if [ ! -d "${openssl_dir}" ]; then curl -sL \
104+
if [ ! -d "${openssl_dir}" ]; then
105+
curl -sL \
53106
https://github.com/openssl/openssl/archive/${openssl_tag}.tar.gz \
54107
| tar xzf -
55108

56-
cd "${openssl_dir}"
109+
pushd "${openssl_dir}"
110+
111+
options=(--prefix=${LIBPQ_BUILD_PREFIX} --openssldir=${LIBPQ_BUILD_PREFIX} \
112+
zlib -fPIC shared)
113+
if [ -z "${MACOSX_ARCHITECTURE:-}" ]; then
114+
./config $options
115+
else
116+
./configure "darwin64-$MACOSX_ARCHITECTURE-cc" $options
117+
fi
57118

58-
./config --prefix=${LIBPQ_BUILD_PREFIX} --openssldir=${LIBPQ_BUILD_PREFIX} \
59-
zlib -fPIC shared
60119
make depend
61120
make
62121
else
63-
cd "${openssl_dir}"
122+
pushd "${openssl_dir}"
64123
fi
65124

66125
# Install openssl
67126
make install_sw
68-
cd ..
127+
popd
128+
129+
fi
130+
131+
132+
if [ "$ID" == "macos" ]; then
133+
134+
# Build kerberos if needed
135+
krb5_dir="krb5-${krb5_version}/src"
136+
if [ ! -d "${krb5_dir}" ]; then
137+
curl -sL "https://kerberos.org/dist/krb5/${krb5_version%.*}/krb5-${krb5_version}.tar.gz" \
138+
| tar xzf -
139+
140+
pushd "${krb5_dir}"
141+
./configure "${make_configure_standard_flags[@]}"
142+
make
143+
else
144+
pushd "${krb5_dir}"
145+
fi
146+
147+
make install
148+
popd
69149

70150
fi
71151

72152

73-
if [ "$ID" == "centos" ]; then
153+
if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
74154

75155
# Build libsasl2 if needed
76156
# The system package (cyrus-sasl-devel) causes an amazing error on i686:
@@ -83,26 +163,25 @@ if [ "$ID" == "centos" ]; then
83163
https://github.com/cyrusimap/cyrus-sasl/archive/${sasl_tag}.tar.gz \
84164
| tar xzf -
85165

86-
cd "${sasl_dir}"
166+
pushd "${sasl_dir}"
87167

88168
autoreconf -i
89-
./configure --prefix=${LIBPQ_BUILD_PREFIX} \
90-
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
169+
./configure "${make_configure_standard_flags[@]}" --disable-macos-framework
91170
make
92171
else
93-
cd "${sasl_dir}"
172+
pushd "${sasl_dir}"
94173
fi
95174

96175
# Install libsasl2
97176
# requires missing nroff to build
98177
touch saslauthd/saslauthd.8
99178
make install
100-
cd ..
179+
popd
101180

102181
fi
103182

104183

105-
if [ "$ID" == "centos" ]; then
184+
if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
106185

107186
# Build openldap if needed
108187
ldap_tag="${ldap_version}"
@@ -112,25 +191,24 @@ if [ "$ID" == "centos" ]; then
112191
https://www.openldap.org/software/download/OpenLDAP/openldap-release/openldap-${ldap_tag}.tgz \
113192
| tar xzf -
114193

115-
cd "${ldap_dir}"
194+
pushd "${ldap_dir}"
116195

117-
./configure --prefix=${LIBPQ_BUILD_PREFIX} --enable-backends=no --enable-null \
118-
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
196+
./configure "${make_configure_standard_flags[@]}" --enable-backends=no --enable-null
119197

120198
make depend
121199
make -C libraries/liblutil/
122200
make -C libraries/liblber/
123201
make -C libraries/libldap/
124202
else
125-
cd "${ldap_dir}"
203+
pushd "${ldap_dir}"
126204
fi
127205

128206
# Install openldap
129207
make -C libraries/liblber/ install
130208
make -C libraries/libldap/ install
131209
make -C include/ install
132-
chmod +x ${LIBPQ_BUILD_PREFIX}/lib/{libldap,liblber}*.so*
133-
cd ..
210+
chmod +x ${LIBPQ_BUILD_PREFIX}/lib/{libldap,liblber}*.${library_suffix}*
211+
popd
134212

135213
fi
136214

@@ -143,32 +221,33 @@ if [ ! -d "${postgres_dir}" ]; then
143221
https://github.com/postgres/postgres/archive/${postgres_tag}.tar.gz \
144222
| tar xzf -
145223

146-
cd "${postgres_dir}"
224+
pushd "${postgres_dir}"
147225

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 .*'\
226+
if [ "$ID" != "macos" ]; then
227+
# Match the default unix socket dir default with what defined on Ubuntu and
228+
# Red Hat, which seems the most common location
229+
sed -i 's|#define DEFAULT_PGSOCKET_DIR .*'\
151230
'|#define DEFAULT_PGSOCKET_DIR "/var/run/postgresql"|' \
152-
src/include/pg_config_manual.h
231+
src/include/pg_config_manual.h
232+
fi
153233

154234
# Often needed, but currently set by the workflow
155235
# export LD_LIBRARY_PATH="${LIBPQ_BUILD_PREFIX}/lib"
156236

157-
./configure --prefix=${LIBPQ_BUILD_PREFIX} --sysconfdir=/etc/postgresql-common \
237+
./configure "${make_configure_standard_flags[@]}" --sysconfdir=/etc/postgresql-common \
158238
--with-gssapi --with-openssl --with-pam --with-ldap \
159-
--without-readline --without-icu \
160-
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
239+
--without-readline --without-icu
161240
make -C src/interfaces/libpq
162241
make -C src/bin/pg_config
163242
make -C src/include
164243
else
165-
cd "${postgres_dir}"
244+
pushd "${postgres_dir}"
166245
fi
167246

168247
# Install libpq
169248
make -C src/interfaces/libpq install
170249
make -C src/bin/pg_config install
171250
make -C src/include install
172-
cd ..
251+
popd
173252

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

scripts/build/wheel_macos_before_all.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ set -x
1111
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1212
prjdir="$( cd "${dir}/../.." && pwd )"
1313

14+
# Build dependency libraries
15+
"${prjdir}/scripts/build/build_libpq.sh"
16+
17+
# Show dependency tree
18+
otool -L /tmp/libpq.build/lib/*.dylib
19+
1420
brew install gnu-sed postgresql@${PG_VERSION}
1521
brew link --overwrite postgresql@${PG_VERSION}
1622

tests/test_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def test_non_block_after_notification(self):
504504
raise Exception("Unexpected result from poll: %r", state)
505505
polls += 1
506506

507-
self.assert_(polls >= 8, polls)
507+
self.assert_(polls >= 5, polls)
508508

509509
def test_poll_noop(self):
510510
self.conn.poll()

tests/test_green.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def wait(conn):
152152
""")
153153

154154
polls = stub.polls.count(POLL_READ)
155-
self.assert_(polls > 8, polls)
155+
self.assert_(polls > 6, polls)
156156

157157

158158
class CallbackErrorTestCase(ConnectingTestCase):

0 commit comments

Comments
 (0)