diff options
author | Nicholas Junge <nicholas.junge@web.de> | 2024-04-15 18:44:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-15 17:44:09 +0100 |
commit | bc946b919cac6f25a199a526da571638cfde109f (patch) | |
tree | 375de8e2843cc5e0e8df560866cd937bb8170538 /setup.py | |
parent | 185c55d79301f1b3a6505b8432440f7a3994e79a (diff) | |
download | google-benchmark-bc946b919cac6f25a199a526da571638cfde109f.tar.gz google-benchmark-bc946b919cac6f25a199a526da571638cfde109f.zip |
Modernize wheel building job config (#1783)
It is now possible to build Mac wheels on native machines in Github
Actions, so ARM64 Mac wheels are now built and tested on M1 machines.
Also, the artifact up-/download was migrated to v4, which made it
necessary to upload wheels to unique artifact names, and then later
stitch them together again in a subsequent job.
The cross-platform Mac build injection in setup.py was removed,
since it is no longer necessary.
I relanded a monkey-patching of Bazel build files, this time for
MODULE.bazel. This is because `rules_python` does not allow running
as the root user, which is the case in cibuildwheel+Linux (happens
in a Docker container). Since I did not see a quick way of switching
to rootless containers, and did not want to hardcode the config change
(it can apparently cause cache misses and build failures), I inject the
"ignore_root_user_error" flag into the MODULE.bazel file when running
in cibuildwheel on Linux.
Diffstat (limited to 'setup.py')
-rw-r--r-- | setup.py | 63 |
1 files changed, 48 insertions, 15 deletions
@@ -1,14 +1,17 @@ +import contextlib import os import platform +import re import shutil from pathlib import Path -from typing import Any +from typing import Any, Generator import setuptools from setuptools.command import build_ext IS_WINDOWS = platform.system() == "Windows" IS_MAC = platform.system() == "Darwin" +IS_LINUX = platform.system() == "Linux" # hardcoded SABI-related options. Requires that each Python interpreter # (hermetic or not) participating is of the same major-minor version. @@ -17,6 +20,46 @@ py_limited_api = version_tuple >= (3, 12) options = {"bdist_wheel": {"py_limited_api": "cp312"}} if py_limited_api else {} +def is_cibuildwheel() -> bool: + return os.getenv("CIBUILDWHEEL") is not None + + +@contextlib.contextmanager +def _maybe_patch_toolchains() -> Generator[None, None, None]: + """ + Patch rules_python toolchains to ignore root user error + when run in a Docker container on Linux in cibuildwheel. + """ + + def fmt_toolchain_args(matchobj): + suffix = "ignore_root_user_error = True" + callargs = matchobj.group(1) + # toolchain def is broken over multiple lines + if callargs.endswith("\n"): + callargs = callargs + " " + suffix + ",\n" + # toolchain def is on one line. + else: + callargs = callargs + ", " + suffix + return "python.toolchain(" + callargs + ")" + + CIBW_LINUX = is_cibuildwheel() and IS_LINUX + try: + if CIBW_LINUX: + module_bazel = Path("MODULE.bazel") + content: str = module_bazel.read_text() + module_bazel.write_text( + re.sub( + r"python.toolchain\(([\w\"\s,.=]*)\)", + fmt_toolchain_args, + content, + ) + ) + yield + finally: + if CIBW_LINUX: + module_bazel.write_text(content) + + class BazelExtension(setuptools.Extension): """A C/C++ extension that is defined as a Bazel BUILD target.""" @@ -73,21 +116,11 @@ class BuildBazelExtension(build_ext.build_ext): for library_dir in self.library_dirs: bazel_argv.append("--linkopt=/LIBPATH:" + library_dir) elif IS_MAC: - if platform.machine() == "x86_64": - # C++17 needs macOS 10.14 at minimum - bazel_argv.append("--macos_minimum_os=10.14") - - # cross-compilation for Mac ARM64 on GitHub Mac x86 runners. - # ARCHFLAGS is 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") - - elif platform.machine() == "arm64": - bazel_argv.append("--macos_minimum_os=11.0") + # C++17 needs macOS 10.14 at minimum + bazel_argv.append("--macos_minimum_os=10.14") - self.spawn(bazel_argv) + with _maybe_patch_toolchains(): + self.spawn(bazel_argv) if IS_WINDOWS: suffix = ".pyd" |