benchmark/setup.py
Nicholas Junge 986423a62d
Some checks failed
bazel / bazel.${{ matrix.os }}.${{ matrix.bzlmod && 'bzlmod' || 'no_bzlmod' }} (false, macos-latest) (push) Has been cancelled
bazel / bazel.${{ matrix.os }}.${{ matrix.bzlmod && 'bzlmod' || 'no_bzlmod' }} (false, ubuntu-latest) (push) Has been cancelled
bazel / bazel.${{ matrix.os }}.${{ matrix.bzlmod && 'bzlmod' || 'no_bzlmod' }} (false, windows-latest) (push) Has been cancelled
bazel / bazel.${{ matrix.os }}.${{ matrix.bzlmod && 'bzlmod' || 'no_bzlmod' }} (true, macos-latest) (push) Has been cancelled
bazel / bazel.${{ matrix.os }}.${{ matrix.bzlmod && 'bzlmod' || 'no_bzlmod' }} (true, ubuntu-latest) (push) Has been cancelled
bazel / bazel.${{ matrix.os }}.${{ matrix.bzlmod && 'bzlmod' || 'no_bzlmod' }} (true, windows-latest) (push) Has been cancelled
build-and-test-min-cmake / ${{ matrix.os }}.min-cmake (macos-latest) (push) Has been cancelled
build-and-test-min-cmake / ${{ matrix.os }}.min-cmake (ubuntu-latest) (push) Has been cancelled
build-and-test-perfcounters / ${{ matrix.os }}.${{ matrix.build_type }} (Debug, ubuntu-20.04) (push) Has been cancelled
build-and-test-perfcounters / ${{ matrix.os }}.${{ matrix.build_type }} (Debug, ubuntu-22.04) (push) Has been cancelled
build-and-test-perfcounters / ${{ matrix.os }}.${{ matrix.build_type }} (Release, ubuntu-20.04) (push) Has been cancelled
build-and-test-perfcounters / ${{ matrix.os }}.${{ matrix.build_type }} (Release, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, clang++, shared, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, clang++, shared, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, clang++, shared, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, clang++, static, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, clang++, static, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, clang++, static, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, g++, shared, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, g++, shared, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, g++, shared, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, g++, static, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, g++, static, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Debug, g++, static, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, clang++, shared, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, clang++, shared, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, clang++, shared, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, clang++, static, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, clang++, static, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, clang++, static, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, g++, shared, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, g++, shared, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, g++, shared, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, g++, static, macos-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, g++, static, ubuntu-20.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.compiler }} (Release, g++, static, ubuntu-22.04) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Debug, Visual Studio 16 2019, shared, VS-16-2019, windows-2019) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Debug, Visual Studio 16 2019, static, VS-16-2019, windows-2019) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Debug, Visual Studio 17 2022, shared, VS-17-2022, windows-2022) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Debug, Visual Studio 17 2022, static, VS-17-2022, windows-2022) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Release, Visual Studio 16 2019, shared, VS-16-2019, windows-2019) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Release, Visual Studio 16 2019, static, VS-16-2019, windows-2019) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Release, Visual Studio 17 2022, shared, VS-17-2022, windows-2022) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} (x64, Release, Visual Studio 17 2022, static, VS-17-2022, windows-2022) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, shared, map[arch:i686 compiler:clang++ family:LLVM msystem:CLANG32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, shared, map[arch:i686 compiler:g++ family:GNU msystem:MINGW32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, shared, map[arch:x86_64 compiler:clang++ family:LLVM msystem:CLANG64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, shared, map[arch:x86_64 compiler:g++ family:GNU msystem:MINGW64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, shared, map[arch:x86_64 compiler:g++ family:GNU msystem:UCRT64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, static, map[arch:i686 compiler:clang++ family:LLVM msystem:CLANG32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, static, map[arch:i686 compiler:g++ family:GNU msystem:MINGW32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, static, map[arch:x86_64 compiler:clang++ family:LLVM msystem:CLANG64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, static, map[arch:x86_64 compiler:g++ family:GNU msystem:MINGW64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Debug, static, map[arch:x86_64 compiler:g++ family:GNU msystem:UCRT64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, shared, map[arch:i686 compiler:clang++ family:LLVM msystem:CLANG32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, shared, map[arch:i686 compiler:g++ family:GNU msystem:MINGW32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, shared, map[arch:x86_64 compiler:clang++ family:LLVM msystem:CLANG64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, shared, map[arch:x86_64 compiler:g++ family:GNU msystem:MINGW64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, shared, map[arch:x86_64 compiler:g++ family:GNU msystem:UCRT64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, static, map[arch:i686 compiler:clang++ family:LLVM msystem:CLANG32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, static, map[arch:i686 compiler:g++ family:GNU msystem:MINGW32], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, static, map[arch:x86_64 compiler:clang++ family:LLVM msystem:CLANG64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, static, map[arch:x86_64 compiler:g++ family:GNU msystem:MINGW64], windows-latest) (push) Has been cancelled
build-and-test / ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msys2.msystem }} (Release, static, map[arch:x86_64 compiler:g++ family:GNU msystem:UCRT64], windows-latest) (push) Has been cancelled
clang-format-lint / check-clang-format (push) Has been cancelled
clang-tidy / run-clang-tidy (push) Has been cancelled
doxygen / Build HTML documentation (push) Has been cancelled
python + Bazel pre-commit checks / pre-commit (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (Debug, asan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (Debug, msan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (Debug, tsan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (Debug, ubsan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (RelWithDebInfo, asan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (RelWithDebInfo, msan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (RelWithDebInfo, tsan) (push) Has been cancelled
sanitizer / ${{ matrix.sanitizer }}.${{ matrix.build_type }} (RelWithDebInfo, ubsan) (push) Has been cancelled
test-bindings / Test GBM Python bindings on ${{ matrix.os }} (macos-latest) (push) Has been cancelled
test-bindings / Test GBM Python bindings on ${{ matrix.os }} (ubuntu-latest) (push) Has been cancelled
test-bindings / Test GBM Python bindings on ${{ matrix.os }} (windows-latest) (push) Has been cancelled
Bump oldest supported Python to 3.10, eliminate setuptools-scm (#1842)
* Supply MacOS deployment target to delocate, use build+uv frontend

This shaves off multiple minutes from the wheel builds alone.

Also revert to trusted publishing for wheel uploads as it is now set up.

* Bump oldest supported Python to 3.10, eliminate setuptools-scm

The version is now a string again, under the same attribute as it was
before. This is a pragmatic decision in order to be able to upload wheels
again, possibly directly from main.

We could in the future also set the Python version to a development version
if we want to avoid accidental uploads of `main`.

* Add a note on supported Python versions in the docs

Also fixes the setuptools failure observed in the latest CI by pinning
to the last version before v73 until the problem is identified and resolved.
2024-09-04 17:42:07 +01:00

170 lines
5.8 KiB
Python

import contextlib
import os
import platform
import re
import shutil
from pathlib import Path
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.
version_tuple = tuple(int(i) for i in platform.python_version_tuple())
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."""
def __init__(self, name: str, bazel_target: str, **kwargs: Any):
super().__init__(name=name, sources=[], **kwargs)
self.bazel_target = bazel_target
stripped_target = bazel_target.split("//")[-1]
self.relpath, self.target_name = stripped_target.split(":")
class BuildBazelExtension(build_ext.build_ext):
"""A command that runs Bazel to build a C/C++ extension."""
def run(self):
for ext in self.extensions:
self.bazel_build(ext)
super().run()
# explicitly call `bazel shutdown` for graceful exit
self.spawn(["bazel", "shutdown"])
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
def bazel_build(self, ext: BazelExtension) -> None:
"""Runs the bazel build to create the package."""
temp_path = Path(self.build_temp)
# omit the patch version to avoid build errors if the toolchain is not
# yet registered in the current @rules_python version.
# patch version differences should be fine.
python_version = ".".join(platform.python_version_tuple()[:2])
bazel_argv = [
"bazel",
"run",
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:
# C++17 needs macOS 10.14 at minimum
bazel_argv.append("--macos_minimum_os=10.14")
with _maybe_patch_toolchains():
self.spawn(bazel_argv)
if IS_WINDOWS:
suffix = ".pyd"
else:
suffix = ".abi3.so" if ext.py_limited_api else ".so"
# 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)
setuptools.setup(
cmdclass=dict(build_ext=BuildBazelExtension),
package_data={"google_benchmark": ["py.typed", "*.pyi"]},
ext_modules=[
BazelExtension(
name="google_benchmark._benchmark",
bazel_target="//bindings/python/google_benchmark:benchmark_stubgen",
py_limited_api=py_limited_api,
)
],
options=options,
)