Skip to content

Commit

Permalink
Add monkeypatch for root error ignore on Linux in cibuildwheel
Browse files Browse the repository at this point in the history
Pretty terrible, but easier than figuring out rootless containers
in Github CI.
  • Loading branch information
nicholasjng committed Apr 15, 2024
1 parent 8adb828 commit a93df62
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ jobs:
CIBW_ARCHS_LINUX: auto64 aarch64
CIBW_ARCHS_WINDOWS: auto64
CIBW_BEFORE_ALL_LINUX: bash .github/install_bazel.sh
# Rootless requirement of rules_python, see
# https://github.com/bazelbuild/rules_python/pull/713 .
CIBW_CONTAINER_ENGINE: podman
# Grab the rootless Bazel installation inside the container.
CIBW_ENVIRONMENT_LINUX: PATH=$PATH:$HOME/bin
CIBW_TEST_COMMAND: python {project}/bindings/python/google_benchmark/example.py
Expand Down
4 changes: 0 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ python.toolchain(
is_default = True,
python_version = "3.12",
)
use_repo(
python,
python = "python_versions",
)

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip", dev_dependency = True)
pip.parse(
Expand Down
37 changes: 35 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +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.
Expand All @@ -16,6 +20,34 @@
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.
"""
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(.*)",
r"python.toolchain(\1, ignore_root_user_error = True)",
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."""

Expand Down Expand Up @@ -75,7 +107,8 @@ def bazel_build(self, ext: BazelExtension) -> None:
# 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"
Expand Down

0 comments on commit a93df62

Please sign in to comment.