Skip to content

Commit

Permalink
Try rootless container start for cibuildwheel
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjng committed Apr 16, 2024
1 parent bc946b9 commit 3d96e7d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
# macos-13 is an Intel x86 runner, macos-14 is ARM64.
os: [ubuntu-latest, macos-13, macos-14, windows-latest]

steps:
Expand All @@ -57,6 +58,8 @@ jobs:
CIBW_BEFORE_ALL_LINUX: bash .github/install_bazel.sh
# Grab the rootless Bazel installation inside the container.
CIBW_ENVIRONMENT_LINUX: PATH=$PATH:$HOME/bin
# set a GitHub Actions non-root user to avoid rules_python errors.
CIBW_CONTAINER_ENGINE: "docker; create_args: --user 1001"
CIBW_TEST_COMMAND: python {project}/bindings/python/google_benchmark/example.py

- name: Upload Google Benchmark ${{ matrix.os }} wheels
Expand Down
49 changes: 2 additions & 47 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import contextlib
import os
import platform
import re
import shutil
from pathlib import Path
from typing import Any, Generator
from typing import Any

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 @@ -20,46 +16,6 @@
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."""

Expand Down Expand Up @@ -119,8 +75,7 @@ def bazel_build(self, ext: BazelExtension) -> None:
# C++17 needs macOS 10.14 at minimum
bazel_argv.append("--macos_minimum_os=10.14")

with _maybe_patch_toolchains():
self.spawn(bazel_argv)
self.spawn(bazel_argv)

if IS_WINDOWS:
suffix = ".pyd"
Expand Down

0 comments on commit 3d96e7d

Please sign in to comment.