2024-04-15 16:44:09 +00:00
|
|
|
import contextlib
|
2020-05-06 16:28:29 +00:00
|
|
|
import os
|
2021-11-11 14:51:22 +00:00
|
|
|
import platform
|
2024-04-15 16:44:09 +00:00
|
|
|
import re
|
2020-05-06 16:28:29 +00:00
|
|
|
import shutil
|
2024-11-06 13:15:22 +00:00
|
|
|
import sys
|
2023-02-03 09:47:02 +00:00
|
|
|
from pathlib import Path
|
2024-04-15 16:44:09 +00:00
|
|
|
from typing import Any, Generator
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
import setuptools
|
|
|
|
from setuptools.command import build_ext
|
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
IS_WINDOWS = platform.system() == "Windows"
|
|
|
|
IS_MAC = platform.system() == "Darwin"
|
2024-04-15 16:44:09 +00:00
|
|
|
IS_LINUX = platform.system() == "Linux"
|
2020-05-06 16:28:29 +00:00
|
|
|
|
2024-03-07 12:28:55 +00:00
|
|
|
# hardcoded SABI-related options. Requires that each Python interpreter
|
|
|
|
# (hermetic or not) participating is of the same major-minor version.
|
2024-11-06 13:15:22 +00:00
|
|
|
py_limited_api = sys.version_info >= (3, 12)
|
2024-03-07 12:28:55 +00:00
|
|
|
options = {"bdist_wheel": {"py_limited_api": "cp312"}} if py_limited_api else {}
|
2023-02-03 09:47:02 +00:00
|
|
|
|
|
|
|
|
2024-04-15 16:44:09 +00:00
|
|
|
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
|
2024-11-06 13:15:22 +00:00
|
|
|
module_bazel = Path("MODULE.bazel")
|
|
|
|
content: str = module_bazel.read_text()
|
2024-04-15 16:44:09 +00:00
|
|
|
try:
|
|
|
|
if CIBW_LINUX:
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-05-06 16:28:29 +00:00
|
|
|
class BazelExtension(setuptools.Extension):
|
2020-09-09 08:43:26 +00:00
|
|
|
"""A C/C++ extension that is defined as a Bazel BUILD target."""
|
2020-05-06 16:28:29 +00:00
|
|
|
|
2024-03-07 12:28:55 +00:00
|
|
|
def __init__(self, name: str, bazel_target: str, **kwargs: Any):
|
|
|
|
super().__init__(name=name, sources=[], **kwargs)
|
2023-02-03 09:47:02 +00:00
|
|
|
|
2020-09-09 08:43:26 +00:00
|
|
|
self.bazel_target = bazel_target
|
2023-02-03 09:47:02 +00:00
|
|
|
stripped_target = bazel_target.split("//")[-1]
|
|
|
|
self.relpath, self.target_name = stripped_target.split(":")
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BuildBazelExtension(build_ext.build_ext):
|
2020-09-09 08:43:26 +00:00
|
|
|
"""A command that runs Bazel to build a C/C++ extension."""
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
for ext in self.extensions:
|
|
|
|
self.bazel_build(ext)
|
2023-10-24 12:04:12 +00:00
|
|
|
super().run()
|
|
|
|
# explicitly call `bazel shutdown` for graceful exit
|
|
|
|
self.spawn(["bazel", "shutdown"])
|
2020-09-09 08:43:26 +00:00
|
|
|
|
2023-12-07 12:35:20 +00:00
|
|
|
def copy_extensions_to_source(self):
|
|
|
|
"""
|
|
|
|
Copy generated extensions into the source tree.
|
|
|
|
This is done in the ``bazel_build`` method, so it's not necessary to
|
|
|
|
do again in the `build_ext` base class.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
Add pre-commit config and GitHub Actions job (#1688)
* Add pre-commit config and GitHub Actions job
Contains the following hooks:
* buildifier - for formatting and linting Bazel files.
* mypy, ruff, isort, black - for Python typechecking, import hygiene,
static analysis, and formatting.
The pylint CI job was changed to be a pre-commit CI job, where pre-commit
is bootstrapped via Python.
Pylint is currently no longer part of the
code checks, but can be re-added if requested. The reason to drop was
that it does not play nicely with pre-commit, and lots of its
functionality and responsibilities are actually covered in ruff.
* Add dev extra to pyproject.toml for development installs
* Clarify that pre-commit contains only Python and Bazel hooks
* Add one-line docstrings to Bazel modules
* Apply buildifier pre-commit fixes to Bazel files
* Apply pre-commit fixes to Python files
* Supply --profile=black to isort to prevent conflicts
* Fix nanobind build file formatting
* Add tooling configs to `pyproject.toml`
In particular, set line length 80 for all Python files.
* Reformat all Python files to line length 80, fix return type annotations
Also ignores the `tools/compare.py` and `tools/gbench/report.py` files
for mypy, since they emit a barrage of errors which we can deal with
later. The errors are mostly related to dynamic classmethod definition.
2023-10-30 15:35:37 +00:00
|
|
|
def bazel_build(self, ext: BazelExtension) -> None:
|
2020-09-09 08:43:26 +00:00
|
|
|
"""Runs the bazel build to create the package."""
|
2024-03-07 12:28:55 +00:00
|
|
|
temp_path = Path(self.build_temp)
|
2024-11-06 13:15:22 +00:00
|
|
|
if py_limited_api:
|
|
|
|
# We only need to know the minimum ABI version,
|
|
|
|
# since it is stable across minor versions by definition.
|
|
|
|
# The value here is calculated as the minimum of a) the minimum
|
|
|
|
# Python version required, and b) the stable ABI version target.
|
|
|
|
# NB: This needs to be kept in sync with [project.requires-python]
|
|
|
|
# in pyproject.toml.
|
|
|
|
python_version = "3.12"
|
|
|
|
else:
|
|
|
|
python_version = "{0}.{1}".format(*sys.version_info[:2])
|
2024-03-07 12:28:55 +00:00
|
|
|
|
|
|
|
bazel_argv = [
|
|
|
|
"bazel",
|
2024-07-30 14:49:33 +00:00
|
|
|
"run",
|
2024-03-07 12:28:55 +00:00
|
|
|
ext.bazel_target,
|
|
|
|
f"--symlink_prefix={temp_path / 'bazel-'}",
|
|
|
|
f"--compilation_mode={'dbg' if self.debug else 'opt'}",
|
|
|
|
# C++17 is required by nanobind
|
|
|
|
f"--cxxopt={'/std:c++17' if IS_WINDOWS else '-std=c++17'}",
|
|
|
|
f"--@rules_python//python/config_settings:python_version={python_version}",
|
|
|
|
]
|
|
|
|
|
|
|
|
if ext.py_limited_api:
|
|
|
|
bazel_argv += ["--@nanobind_bazel//:py-limited-api=cp312"]
|
|
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
# Link with python*.lib.
|
|
|
|
for library_dir in self.library_dirs:
|
|
|
|
bazel_argv.append("--linkopt=/LIBPATH:" + library_dir)
|
|
|
|
elif IS_MAC:
|
2024-04-15 16:44:09 +00:00
|
|
|
# C++17 needs macOS 10.14 at minimum
|
|
|
|
bazel_argv.append("--macos_minimum_os=10.14")
|
2024-03-07 12:28:55 +00:00
|
|
|
|
2024-04-15 16:44:09 +00:00
|
|
|
with _maybe_patch_toolchains():
|
|
|
|
self.spawn(bazel_argv)
|
2024-03-07 12:28:55 +00:00
|
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
suffix = ".pyd"
|
|
|
|
else:
|
|
|
|
suffix = ".abi3.so" if ext.py_limited_api else ".so"
|
|
|
|
|
2024-07-30 14:49:33 +00:00
|
|
|
# copy the Bazel build artifacts into setuptools' libdir,
|
|
|
|
# from where the wheel is built.
|
|
|
|
pkgname = "google_benchmark"
|
|
|
|
pythonroot = Path("bindings") / "python" / "google_benchmark"
|
|
|
|
srcdir = temp_path / "bazel-bin" / pythonroot
|
|
|
|
libdir = Path(self.build_lib) / pkgname
|
|
|
|
for root, dirs, files in os.walk(srcdir, topdown=True):
|
|
|
|
# exclude runfiles directories and children.
|
|
|
|
dirs[:] = [d for d in dirs if "runfiles" not in d]
|
|
|
|
|
|
|
|
for f in files:
|
|
|
|
fp = Path(f)
|
|
|
|
should_copy = False
|
|
|
|
# we do not want the bare .so file included
|
|
|
|
# when building for ABI3, so we require a
|
|
|
|
# full and exact match on the file extension.
|
|
|
|
if "".join(fp.suffixes) == suffix:
|
|
|
|
should_copy = True
|
|
|
|
elif fp.suffix == ".pyi":
|
|
|
|
should_copy = True
|
|
|
|
elif Path(root) == srcdir and f == "py.typed":
|
|
|
|
# copy py.typed, but only at the package root.
|
|
|
|
should_copy = True
|
|
|
|
|
|
|
|
if should_copy:
|
|
|
|
shutil.copyfile(root / fp, libdir / fp)
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
setuptools.setup(
|
|
|
|
cmdclass=dict(build_ext=BuildBazelExtension),
|
2024-07-30 14:49:33 +00:00
|
|
|
package_data={"google_benchmark": ["py.typed", "*.pyi"]},
|
2020-09-11 09:55:18 +00:00
|
|
|
ext_modules=[
|
|
|
|
BazelExtension(
|
2023-07-10 09:43:49 +00:00
|
|
|
name="google_benchmark._benchmark",
|
2024-07-30 14:49:33 +00:00
|
|
|
bazel_target="//bindings/python/google_benchmark:benchmark_stubgen",
|
2024-03-07 12:28:55 +00:00
|
|
|
py_limited_api=py_limited_api,
|
2020-09-11 09:55:18 +00:00
|
|
|
)
|
|
|
|
],
|
2024-03-07 12:28:55 +00:00
|
|
|
options=options,
|
2020-05-06 16:28:29 +00:00
|
|
|
)
|