Skip to content

Commit

Permalink
build(macos): Enable cross compiling libpq across macOS architectures
Browse files Browse the repository at this point in the history
The GitHub Actions runners look like they're only 1 year away from the
last macOS x86_64 platform being removed. Get ahead of the game and
build x86_64 on arm64.
  • Loading branch information
bwoodsend authored and dvarrazzo committed Jan 5, 2025
1 parent c8eead8 commit 117f30e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
21 changes: 4 additions & 17 deletions .github/workflows/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,15 @@ jobs:
# }}}

macos: # {{{
runs-on: macos-${{ matrix.macver }}
runs-on: macos-latest
if: true

strategy:
fail-fast: false
matrix:
# These archs require an Apple M1 runner: [arm64, universal2]
arch: [x86_64]
arch: [x86_64, arm64]
pyver: [cp39, cp310, cp311, cp312, cp313]
macver: ["13"]
include:
- arch: arm64
pyver: cp310
macver: "14"
- arch: arm64
pyver: cp311
macver: "14"
- arch: arm64
pyver: cp312
macver: "14"
- arch: arm64
pyver: cp313
macver: "14"

steps:
- name: Checkout repos
Expand All @@ -171,6 +157,7 @@ jobs:
env:
CIBW_BUILD: ${{matrix.pyver}}-macosx_${{matrix.arch}}
CIBW_ARCHS_MACOS: ${{matrix.arch}}
MACOSX_ARCHITECTURE: ${{matrix.arch}}
CIBW_BEFORE_ALL_MACOS: ./scripts/build/wheel_macos_before_all.sh
CIBW_TEST_COMMAND: >-
export PYTHONPATH={project} &&
Expand All @@ -184,7 +171,7 @@ jobs:
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: macos-${{matrix.pyver}}-macos-${{matrix.macver}}_${{matrix.arch}}
name: macos-${{matrix.pyver}}-macos-${{matrix.arch}}
path: ./wheelhouse/*.whl

# }}}
Expand Down
52 changes: 35 additions & 17 deletions scripts/build/build_libpq.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ case "$ID" in
# it, force use of system curl.
brew uninstall --force --ignore-dependencies openssl gettext
curl="/usr/bin/curl"
# The deployment target should be <= to that of the oldest supported Python version.
if [ -z "$MACOSX_ARCHITECTURE" ]; then
MACOSX_ARCHITECTURE="$(uname -m)"
fi
# Set the deployment target to be <= to that of the oldest supported Python version.
# e.g. https://www.python.org/downloads/release/python-380/
if [ "$(uname -m)" == "x86_64" ]; then
if [ "$MACOSX_ARCHITECTURE" == "x86_64" ]; then
export MACOSX_DEPLOYMENT_TARGET=10.9
else
export MACOSX_DEPLOYMENT_TARGET=11.0
Expand All @@ -83,6 +86,22 @@ case "$ID" in
;;
esac


if [ "$ID" == "macos" ]; then
make_configure_standard_flags=( \
--prefix=${LIBPQ_BUILD_PREFIX} \
"CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ -arch $MACOSX_ARCHITECTURE" \
"LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib -arch $MACOSX_ARCHITECTURE" \
)
else
make_configure_standard_flags=( \
--prefix=${LIBPQ_BUILD_PREFIX} \
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ \
LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib \
)
fi


if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then

# Build openssl if needed
Expand All @@ -95,8 +114,14 @@ if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then

cd "${openssl_dir}"

./config --prefix=${LIBPQ_BUILD_PREFIX} --openssldir=${LIBPQ_BUILD_PREFIX} \
zlib -fPIC shared
options=(--prefix=${LIBPQ_BUILD_PREFIX} --openssldir=${LIBPQ_BUILD_PREFIX} \
zlib -fPIC shared)
if [ -z "$MACOSX_ARCHITECTURE" ]; then
./config $options
else
./configure "darwin64-$MACOSX_ARCHITECTURE-cc" $options
fi

make depend
make
else
Expand All @@ -120,9 +145,7 @@ if [ "$ID" == "macos" ]; then
| tar xzf -

cd "${krb5_dir}"

./configure --prefix=${LIBPQ_BUILD_PREFIX} \
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
./configure "${make_configure_standard_flags[@]}"
make
else
cd "${krb5_dir}"
Expand All @@ -144,9 +167,7 @@ if [ "$ID" == "macos" ]; then
| tar xzf -

cd "${gettext_dir}"

./configure --prefix=${LIBPQ_BUILD_PREFIX} --disable-java \
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
./configure --disable-java "${make_configure_standard_flags[@]}"
make -C gettext-runtime all
else
cd "${gettext_dir}"
Expand Down Expand Up @@ -174,8 +195,7 @@ if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then
cd "${sasl_dir}"

autoreconf -i
./configure --prefix=${LIBPQ_BUILD_PREFIX} --disable-macos-framework \
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
./configure "${make_configure_standard_flags[@]}" --disable-macos-framework
make
else
cd "${sasl_dir}"
Expand All @@ -202,8 +222,7 @@ if [ "$ID" == "centos" ] || [ "$ID" == "macos" ]; then

cd "${ldap_dir}"

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

make depend
make -C libraries/liblutil/
Expand Down Expand Up @@ -244,10 +263,9 @@ if [ ! -d "${postgres_dir}" ]; then
# Often needed, but currently set by the workflow
# export LD_LIBRARY_PATH="${LIBPQ_BUILD_PREFIX}/lib"

./configure --prefix=${LIBPQ_BUILD_PREFIX} --sysconfdir=/etc/postgresql-common \
./configure "${make_configure_standard_flags[@]}" --sysconfdir=/etc/postgresql-common \
--with-gssapi --with-openssl --with-pam --with-ldap \
--without-readline --without-icu \
CPPFLAGS=-I${LIBPQ_BUILD_PREFIX}/include/ LDFLAGS=-L${LIBPQ_BUILD_PREFIX}/lib
--without-readline --without-icu
make -C src/interfaces/libpq
make -C src/bin/pg_config
make -C src/include
Expand Down

0 comments on commit 117f30e

Please sign in to comment.