From 117f30e9ac1f8bfde3a532be3fad984e76903e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A9nainn=20Woodsend?= Date: Sun, 5 Jan 2025 14:51:07 +0000 Subject: [PATCH] build(macos): Enable cross compiling libpq across macOS architectures 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. --- .github/workflows/packages.yml | 21 +++----------- scripts/build/build_libpq.sh | 52 +++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/.github/workflows/packages.yml b/.github/workflows/packages.yml index 94db07be5..c69489cd9 100644 --- a/.github/workflows/packages.yml +++ b/.github/workflows/packages.yml @@ -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 @@ -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} && @@ -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 # }}} diff --git a/scripts/build/build_libpq.sh b/scripts/build/build_libpq.sh index 076b99788..afd3eb5c8 100755 --- a/scripts/build/build_libpq.sh +++ b/scripts/build/build_libpq.sh @@ -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 @@ -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 @@ -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 @@ -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}" @@ -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}" @@ -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}" @@ -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/ @@ -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