Skip to content

Commit

Permalink
Merge pull request #257 from Cadair/macos_arm64_cross
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadair authored Dec 5, 2024
2 parents e772a71 + e140092 commit 9f1f432
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
- run: python -m pip install PyYAML click
- run: echo $LOAD_BUILD_TARGETS_SCRIPT | base64 --decode > load_build_targets.py
env:
LOAD_BUILD_TARGETS_SCRIPT: aW1wb3J0IGpzb24KaW1wb3J0IG9zCmltcG9ydCByZQoKaW1wb3J0IGNsaWNrCmltcG9ydCB5YW1sCgpNQUNISU5FX1RZUEUgPSB7CiAgICAibGludXgiOiAidWJ1bnR1LWxhdGVzdCIsCiAgICAibWFjb3MiOiAibWFjb3MtMTIiLAogICAgIndpbmRvd3MiOiAid2luZG93cy1sYXRlc3QiLAp9Ck0xX1JVTk5FUiA9ICJtYWNvcy0xNCIKCkNJQldfQlVJTEQgPSBvcy5lbnZpcm9uLmdldCgiQ0lCV19CVUlMRCIsICIqIikKQ0lCV19BUkNIUyA9IG9zLmVudmlyb24uZ2V0KCJDSUJXX0FSQ0hTIiwgImF1dG8iKQoKCkBjbGljay5jb21tYW5kKCkKQGNsaWNrLm9wdGlvbigiLS10YXJnZXRzIiwgZGVmYXVsdD0iIikKZGVmIGxvYWRfYnVpbGRfdGFyZ2V0cyh0YXJnZXRzKToKICAgICIiIlNjcmlwdCB0byBsb2FkIGNpYnVpbGR3aGVlbCB0YXJnZXRzIGZvciBHaXRIdWIgQWN0aW9ucyB3b3JrZmxvdy4iIiIKICAgICMgTG9hZCBsaXN0IG9mIHRhcmdldHMKICAgIHRhcmdldHMgPSB5YW1sLmxvYWQodGFyZ2V0cywgTG9hZGVyPXlhbWwuQmFzZUxvYWRlcikKICAgIHByaW50KGpzb24uZHVtcHModGFyZ2V0cywgaW5kZW50PTIpK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2V0IikKICAgIHJldHVybiB7CiAgICAgICAgInRhcmdldCI6IHRhcmdldCwKICAgICAgICAicnVucy1vbiI6IGdldF9vcyh0YXJnZXQpLAogICAgICAgICJDSUJXX0JVSUxEIjogZ2V0X2NpYndfYnVpbGQodGFyZ2V0KSwKICAgICAgICAiQ0lCV19BUkNIUyI6IGdldF9jaWJ3X2FyY2hzKHRhcmdldCksCiAgICAgICAgImFydGlmYWN0LW5hbWUiOiBnZXRfYXJ0aWZhY3RfbmFtZSh0YXJnZXQpLAogICAgICAgICoqZXh0cmFfdGFyZ2V0X2FyZ3MsCiAgICB9CgoKaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoKICAgIGxvYWRfYnVpbGRfdGFyZ2V0cygpCg==
- id: set-outputs
run: python load_build_targets.py --targets "${{ inputs.targets }}"
shell: sh
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/test_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ jobs:
test_command: pytest --pyargs test_package
targets: |
- linux
- cp311-macosx_x86_64
- cp31?-macos*
- target: cp311-macosx_x86_64
runs-on: macos-13
- cp312-macosx_arm64
- cp313-macosx_universal2
- cp3?-win_amd64
- cp312-manylinux_aarch64
- cp313-manylinux_x86_64
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
envs: |
- linux: py312-inputs-linux
- macos: py311-inputs-macos
- macos: py39-inputs-macos
- windows: py310-inputs-windows
toxargs: '-v'
runs-on: windows-2019
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions docs/source/publish.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ Any other target is assumed to be a value for the ``CIBW_BUILD``
environment variable (e.g. ``cp3?-macosx_x86_64``). In this case the OS
to run cibuildwheel on is extracted from the target.

Targets that end with ``aarch64``, ``arm64`` and ``universal2`` are also
supported without any additional configuration of cibuildwheel.
Targets which end with non-native architectures such as ``aarch64`` on linux or
``x86_64`` on macos are supported and will be emulated (on linux) or cross
compiled.

**Note:** ``targets`` is a *string* and must be specified as a
literal block scalar using the ``|``. (Without the ``|``, it must also
Expand All @@ -57,6 +58,15 @@ To not build any wheels:
targets: ''
For additional configuration extra arguments can be passed by making a target a dictionary.
An example of this is specifying the runner for a target, such as building macos x86_64 wheels on native x86_64 runners:

.. code:: yaml
targets:
- target: cp311-macosx_x86_64
runs-on: macos-13
sdist
^^^^^

Expand Down
37 changes: 28 additions & 9 deletions tools/load_build_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

MACHINE_TYPE = {
"linux": "ubuntu-latest",
"macos": "macos-12",
"macos": "macos-latest",
"windows": "windows-latest",
}
M1_RUNNER = "macos-14"

CIBW_BUILD = os.environ.get("CIBW_BUILD", "*")
CIBW_ARCHS = os.environ.get("CIBW_ARCHS", "auto")
Expand All @@ -37,10 +36,7 @@ def load_build_targets(targets):

def get_os(target):
if "macos" in target:
if get_cibw_archs(target) in {"arm64", "universal2"}:
return M1_RUNNER
else:
return MACHINE_TYPE["macos"]
return MACHINE_TYPE["macos"]
if "win" in target:
return MACHINE_TYPE["windows"]
return MACHINE_TYPE["linux"]
Expand All @@ -53,9 +49,32 @@ def get_cibw_build(target):


def get_cibw_archs(target):
for arch in ["aarch64", "ppc64le", "s390x", "armv7l", "arm64", "universal2"]:
if target.endswith(arch):
return arch
"""
Handle non-native architectures
cibw allows running non-native builds on various platforms:
https://cibuildwheel.pypa.io/en/stable/options/#archs
This logic overrides the "auto" flag based on OS and a list of supported
non-native arch if a non-native arch is given for a particular platform in
targets, rather than the user having to do this manually.
"""
platform_archs = {
# We now cross compile x86_64 on arm64 by default
"macos": ["universal2", "x86_64"],
# This is a list of supported eumulated arches on linux
"linux": ["aarch64", "ppc64le", "s390x", "armv7l"],
}
for platform, archs in platform_archs.items():
if platform in target:
for arch in archs:
if target.endswith(arch):
return arch

# If no explict arch has been specified build both arm64 and x86_64 on macos
if "macos" in target:
return os.environ.get("CIBW_ARCHS", "arm64 x86_64")

return CIBW_ARCHS


Expand Down
2 changes: 1 addition & 1 deletion tools/tox_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def get_matrix_item(env, global_libraries, global_string_parameters,
# if Python is <3.10 we can't use macos-latest which is arm64
try:
if Version(item["python_version"]) < Version('3.10') and item["os"] == "macos-latest":
item["os"] = "macos-12"
item["os"] = "macos-13"
except InvalidVersion:
# python_version might be for example 'pypy-3.10' which won't parse
pass
Expand Down

0 comments on commit 9f1f432

Please sign in to comment.