From 6d51a119ff7cacc8f1691c1977e190a25a227847 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Wed, 26 Jan 2022 10:13:26 +0100 Subject: [PATCH] Fix cross compilation for macOS ARM builds in `cibuildwheel` (#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. --- setup.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/setup.py b/setup.py index 4eaccf84..13e4d61f 100644 --- a/setup.py +++ b/setup.py @@ -93,6 +93,12 @@ class BuildBazelExtension(build_ext.build_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'