Skip to content

Commit

Permalink
Fix cross compilation for macOS ARM builds in cibuildwheel (google#…
Browse files Browse the repository at this point in the history
…1334)

This commit contains a fix for macOS ARM64 wheel buils in Google Benchmark's wheel building CI.

Previously, while `cibuildwheel` itself properly identified the need for cross-compilations and produced valid ARM platform wheels, the included shared library containing the Python bindings
built by `bazel` was built for x86, resulting in immediate errors upon import.

To fix this, logic was added to the setup.py file that adds the "--cpu=darwin_arm64" and "--macos_cpus=arm64" switches to the `bazel build` command if
1) The current system platform is macOS Darwin running on the x86_64 architecture, and
2) The ARCHFLAGS environment variable, set by wheel build systems like conda and cibuildwheel, contains the tag "arm64".

This way, bazel correctly sets the target CPU to ARM64, and produces functional wheels for the macOS ARM line of CPUs.
  • Loading branch information
nicholasjng authored Jan 26, 2022
1 parent d0fbf8a commit 6d51a11
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def bazel_build(self, ext):
elif sys.platform == "darwin" and platform.machine() == "x86_64":
bazel_argv.append("--macos_minimum_os=10.9")

# ARCHFLAGS is always set by cibuildwheel before macOS wheel builds.
archflags = os.getenv("ARCHFLAGS", "")
if "arm64" in archflags:
bazel_argv.append("--cpu=darwin_arm64")
bazel_argv.append("--macos_cpus=arm64")

self.spawn(bazel_argv)

shared_lib_suffix = '.dll' if IS_WINDOWS else '.so'
Expand Down

0 comments on commit 6d51a11

Please sign in to comment.