Skip to content

Commit 0fc7341

Browse files
committed
setup.py: fix macos arm64 wheel builds.
- Move cibuildwheel config to pyproject.toml to support local builds. - Building arm64 instead of universal2 for macos. - Passing --host argument to libusb configure when building under cibuildwheel and the ARCHFLAGS env var is set. The cross-compile architecture is set to the one specified in ARCHFLAGS. - Cleaning libusb before build to force a fresh build.
1 parent 21c6591 commit 0fc7341

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

.github/workflows/build-wheels.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ on:
2222
workflow_dispatch:
2323

2424
jobs:
25+
# Note: cibuildwheel configuration comes from pyproject.toml instead of env vars!
2526
build_wheels:
2627
name: Build wheels on ${{ matrix.os }}
2728
runs-on: ${{ matrix.os }}
@@ -32,14 +33,6 @@ jobs:
3233
- ubuntu-latest
3334
- macos-latest
3435
- windows-latest
35-
env:
36-
CIBW_ARCHS_LINUX: auto aarch64
37-
CIBW_ARCHS_MACOS: x86_64 universal2
38-
CIBW_ARCHS_WINDOWS: auto
39-
CIBW_SKIP: "cp27-* cp34-* cp35-* pp*"
40-
CIBW_BEFORE_BUILD: "python -m pip install --upgrade pip wheel setuptools setuptools_scm"
41-
CIBW_BEFORE_BUILD_MACOS: "brew install autoconf automake libtool m4"
42-
CIBW_BUILD_VERBOSITY: 1
4336

4437
steps:
4538
- uses: actions/checkout@v2

pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ fallback_version = "1.0.0.0"
1313
[tool.mypy]
1414
files = "src/libusb_package"
1515
ignore_missing_imports = true
16+
17+
[tool.cibuildwheel]
18+
before-build = "python -m pip install --upgrade pip wheel setuptools setuptools_scm"
19+
build-verbosity = 1
20+
21+
[tool.cibuildwheel.linux]
22+
archs = ["auto", "aarch64"]
23+
24+
[tool.cibuildwheel.macos]
25+
archs = ["x86_64", "arm64"]
26+
before-build = "brew install autoconf automake libtool m4"
27+
28+
[tool.cibuildwheel.windows]
29+
archs = ["auto"]

setup.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,35 @@ def run(self):
115115
"-Wredundant-decls",
116116
"-Wswitch-enum",
117117
]
118+
extra_configure_args = []
119+
120+
# Special conditions when cibuildwheel is building us.
121+
if os.environ.get('CIBUILDWHEEL') == '1':
122+
if sys.platform == 'darwin':
123+
# Support arm64 cross-compile builds from x86-64 on macOS from cibuildwheel.
124+
archflags = os.environ.get('ARCHFLAGS', '')
125+
if archflags:
126+
# Wrap in exception handler just in case something goes unexpectedly wrong, it
127+
# won't totally break all builds.
128+
try:
129+
arch = archflags.split()[-1]
130+
cflags += [archflags]
131+
extra_configure_args = [f'--host={arch}-apple-darwin']
132+
except Exception as err:
133+
print(f"Warning: failure to extract architecture from ARCHFLAGS='{archflags}' ({err})")
134+
else:
135+
# Don't include libudev (for now) on Linux since it isn't in the CI runner image.
136+
extra_configure_args = ['--disable-udev']
118137

119138
os.environ['CFLAGS'] = ' '.join(cflags)
120139

121140
# Run bootstrap.sh, configure, and make.
122-
123141
try:
142+
self.spawn(['env']) # Dump environment for debugging purposes.
124143
self.spawn(['bash', str(BOOTSTRAP_SCRIPT)])
125-
self.spawn(['bash', str(CONFIGURE_SCRIPT), '--disable-udev'])
126-
self.spawn(['make', f'-j{os.cpu_count() or 4}'])
144+
self.spawn(['bash', str(CONFIGURE_SCRIPT), *extra_configure_args])
145+
self.spawn(['make', 'clean'])
146+
self.spawn(['make', f'-j{os.cpu_count() or 4}', 'all'])
127147
except Exception as err:
128148
# Exception is caught here and reraised as our specific Exception class because the actual
129149
# DistutilsExecError class raised on exceptions appears to be difficult to import to use in

0 commit comments

Comments
 (0)