-
Notifications
You must be signed in to change notification settings - Fork 26
Build and publish macOS wheels via cibuildwheel #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexHill
wants to merge
2
commits into
casacore:master
Choose a base branch
from
AlexHill:macos-wheels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/usr/bin/env bash | ||
| # One-time (per runner) setup for building macOS wheels with cibuildwheel. | ||
| # Installs the non-Python-specific casacore dependencies via Homebrew and | ||
| # downloads the sources that cibw_before_build_macos.sh compiles once per | ||
| # Python version, mirroring docker/py_wheel.docker in the casacore repo. | ||
| set -euxo pipefail | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/cibw_env.sh" | ||
|
|
||
| # bison: casacore needs >= 3 and macOS ships 2.3. flex ships with macOS. | ||
| # No BLAS package: casacore links the Accelerate framework instead. | ||
| brew install bison ccache cfitsio fftw gcc gsl hdf5 libdeflate wcslib | ||
| ccache -M 1G | ||
|
|
||
| mkdir -p "$CIBW_SRC" | ||
| cd "$CIBW_SRC" | ||
|
|
||
| # Boost source: Boost.Python must be compiled per Python version. | ||
| boost_underscored=${CIBW_BOOST_VERSION//./_} | ||
| curl -fsSL --retry 3 \ | ||
| "https://archives.boost.io/release/${CIBW_BOOST_VERSION}/source/boost_${boost_underscored}.tar.bz2" \ | ||
| -o boost.tar.bz2 | ||
| tar xjf boost.tar.bz2 | ||
|
|
||
| # casacore source: libcasa_python3 must be compiled per Python version. | ||
| curl -fsSL --retry 3 \ | ||
| "https://github.com/casacore/casacore/archive/refs/tags/v${CIBW_CASACORE_VERSION}.tar.gz" \ | ||
| -o casacore.tar.gz | ||
| tar xzf casacore.tar.gz | ||
|
|
||
| # Measures data, bundled into the wheels; see CIBW_DATA in cibw_env.sh. | ||
| mkdir -p "$CIBW_DATA" | ||
| curl -fsSL --retry 3 https://www.astron.nl/iers/WSRT_Measures.ztar -o measures.tgz | ||
| tar xzf measures.tgz -C "$CIBW_DATA" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/usr/bin/env bash | ||
| # Per-Python-version build of Boost.Python and casacore for macOS wheels. | ||
| # cibuildwheel runs this before building each wheel, with the target Python | ||
| # first on PATH as `python`. It is the macOS analogue of docker/py_wheel.docker | ||
| # in the casacore repo: libcasa_python3 embeds the CPython ABI, so both | ||
| # libraries are rebuilt per Python version into CIBW_PREFIX, where the | ||
| # python-casacore build finds them via CMAKE_PREFIX_PATH. | ||
| # | ||
| # As in the Linux wheel images, libcasa_python3 must not link a real | ||
| # libpython, or the wheels would only work with one specific interpreter: | ||
| # Python3_LIBRARY is pointed at Boost.Python and Python symbols are left | ||
| # undefined, to be resolved from the host interpreter at import time. | ||
| set -euxo pipefail | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/cibw_env.sh" | ||
|
|
||
| BREW_PREFIX=$(brew --prefix) | ||
| export PATH="$BREW_PREFIX/opt/bison/bin:$PATH" | ||
| NCPU=$(sysctl -n hw.ncpu) | ||
|
|
||
| PYTHON=$(command -v python) | ||
| PY_VER=$("$PYTHON" -c 'import sys; print("%d.%d" % sys.version_info[:2])') | ||
| PY_XY=${PY_VER//./} | ||
| PY_INCLUDE=$("$PYTHON" -c 'import sysconfig; print(sysconfig.get_path("include"))') | ||
|
|
||
| rm -rf "$CIBW_PREFIX" | ||
|
|
||
| # ---- Boost.Python against the target Python ------------------------------ | ||
| # numpy is installed only afterwards, so that b2 skips boost_numpy, matching | ||
| # the build order in docker/py_wheel.docker. | ||
| cd "$CIBW_SRC/boost_${CIBW_BOOST_VERSION//./_}" | ||
| b2_args=() | ||
| if command -v ccache >/dev/null; then | ||
| # Route b2 through ccache too, via the darwin toolset so Boost.Python | ||
| # keeps its macOS-specific linking (no libpython; see the header). | ||
| echo "using darwin : : ccache clang++ ;" > user-config.jam | ||
| b2_args=(--user-config=user-config.jam) | ||
| fi | ||
| ./bootstrap.sh --prefix="$CIBW_PREFIX" \ | ||
| --with-libraries=python \ | ||
| --with-python="$PYTHON" \ | ||
| --with-python-version="$PY_VER" | ||
| # b2 cannot derive the include dir from a virtualenv Python; the same | ||
| # workaround as in docker/py_wheel.docker. | ||
| ./b2 -j"$NCPU" "${b2_args[@]}" cxxflags="-fPIC -I$PY_INCLUDE" install | ||
|
|
||
| # ---- casacore against the target Python ---------------------------------- | ||
| "$PYTHON" -m pip install numpy | ||
|
|
||
| # Flags taken from casacore's own recipes: docker/py_wheel.docker and its | ||
| # Homebrew formulae. | ||
| casacore_args=( | ||
| -DCMAKE_BUILD_TYPE=Release | ||
| -DCMAKE_INSTALL_PREFIX="$CIBW_PREFIX" | ||
| -DBUILD_TESTING=OFF | ||
| -DBUILD_PYTHON=OFF | ||
| -DBUILD_PYTHON3=ON | ||
| -DPython3_EXECUTABLE="$PYTHON" | ||
| -DPython3_INCLUDE_DIR="$PY_INCLUDE" | ||
| # Boost.Python stands in for libpython; see the header comment. | ||
| -DPython3_LIBRARY="$CIBW_PREFIX/lib/libboost_python${PY_XY}.dylib" | ||
| -DPORTABLE=TRUE | ||
| -DUSE_PCH=FALSE | ||
| -DUSE_OPENMP=OFF | ||
| -DUSE_FFTW3=ON | ||
| -DUSE_HDF5=ON | ||
| -DDATA_DIR="$CIBW_DATA" | ||
| ) | ||
|
|
||
| # macOS-specific additions, not present in the casacore recipes. | ||
| macos_args=( | ||
| # Find the Homebrew-installed dependencies. | ||
| -DCMAKE_PREFIX_PATH="$BREW_PREFIX" | ||
| # Find the per-Python Boost built above. | ||
| -DBoost_ROOT="$CIBW_PREFIX" | ||
| # Allow undefined symbols, so that Python symbols resolve from the host | ||
| # interpreter at import time. ELF linkers allow this by default, which is | ||
| # why the Linux recipe needs no such flag. | ||
| -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-undefined,dynamic_lookup" | ||
| ) | ||
|
|
||
| # ccache makes the repeated casacore builds cheap after the first. | ||
| CCACHE_ARGS=() | ||
| if command -v ccache >/dev/null; then | ||
| CCACHE_ARGS=( | ||
| -DCMAKE_C_COMPILER_LAUNCHER=ccache | ||
| -DCMAKE_CXX_COMPILER_LAUNCHER=ccache | ||
| ) | ||
| fi | ||
|
|
||
| rm -rf "$CIBW_SRC/casacore-build" | ||
| cmake -S "$CIBW_SRC/casacore-$CIBW_CASACORE_VERSION" -B "$CIBW_SRC/casacore-build" \ | ||
| "${casacore_args[@]}" "${macos_args[@]}" "${CCACHE_ARGS[@]}" | ||
| cmake --build "$CIBW_SRC/casacore-build" --parallel "$NCPU" | ||
| cmake --install "$CIBW_SRC/casacore-build" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/usr/bin/env bash | ||
| # Shared configuration for the macOS cibuildwheel scripts. On Linux the | ||
| # equivalent values are frozen into the per-Python wheel images built from | ||
| # docker/py_wheel.docker in the casacore repo; macOS has no such images, so | ||
| # the before-* scripts build the dependencies at CI time. | ||
|
|
||
| # Dependency versions. | ||
| export CIBW_BOOST_VERSION="1.91.0" | ||
| export CIBW_CASACORE_VERSION="3.8.1" | ||
|
|
||
| # Everything under one root, so a single CMAKE_PREFIX_PATH finds both installs. | ||
| # CIBW_PREFIX, CIBW_DATA and CCACHE_DIR are duplicated as literals in | ||
| # pyproject.toml; CCACHE_DIR also appears in build_release.yml. | ||
| export CIBW_ROOT="$HOME/cibw" | ||
| export CIBW_SRC="$CIBW_ROOT/src" # extracted boost + casacore sources | ||
| export CIBW_PREFIX="$CIBW_ROOT/local" # install prefix for boost + casacore | ||
| export CCACHE_DIR="$CIBW_ROOT/ccache" # persisted across CI runs | ||
| # The leaf directory must be named "data": python-casacore installs it into | ||
| # the wheel by basename, and casacore/__init__.py expects casacore/data. | ||
| export CIBW_DATA="$CIBW_ROOT/measures/data" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an alternative, you could consider to define the environment variables in
pyproject.toml(in the sectioncibuildwheel, using theenvironmentkey/value pair) and export them in the GitHub workflow, instead of sourcingcibw_env.sh. I wrote the following job for a different project that does just that:The down-side to this solution is that it is harder to run
cibuildwheeloutside of the GitHub workflow, because there's no longer acibw_env.shfile that you can source.