2023-02-03 09:47:02 +00:00
|
|
|
import contextlib
|
2020-05-06 16:28:29 +00:00
|
|
|
import os
|
2021-11-11 14:51:22 +00:00
|
|
|
import platform
|
2020-05-06 16:28:29 +00:00
|
|
|
import shutil
|
2023-02-03 09:47:02 +00:00
|
|
|
import sysconfig
|
|
|
|
from pathlib import Path
|
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
|
|
|
from typing import 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
|
|
|
PYTHON_INCLUDE_PATH_PLACEHOLDER = "<PYTHON_INCLUDE_PATH>"
|
2020-05-06 16:28:29 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
IS_WINDOWS = platform.system() == "Windows"
|
|
|
|
IS_MAC = platform.system() == "Darwin"
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
@contextlib.contextmanager
|
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 temp_fill_include_path(fp: str) -> Generator[None, None, None]:
|
2023-02-03 09:47:02 +00:00
|
|
|
"""Temporarily set the Python include path in a file."""
|
|
|
|
with open(fp, "r+") as f:
|
|
|
|
try:
|
|
|
|
content = f.read()
|
|
|
|
replaced = content.replace(
|
|
|
|
PYTHON_INCLUDE_PATH_PLACEHOLDER,
|
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
|
|
|
Path(sysconfig.get_paths()["include"]).as_posix(),
|
2023-02-03 09:47:02 +00:00
|
|
|
)
|
|
|
|
f.seek(0)
|
|
|
|
f.write(replaced)
|
|
|
|
f.truncate()
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
# revert to the original content after exit
|
|
|
|
f.seek(0)
|
|
|
|
f.write(content)
|
|
|
|
f.truncate()
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
def __init__(self, name: str, bazel_target: str):
|
|
|
|
super().__init__(name=name, sources=[])
|
|
|
|
|
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."""
|
2023-02-03 09:47:02 +00:00
|
|
|
with temp_fill_include_path("WORKSPACE"):
|
|
|
|
temp_path = Path(self.build_temp)
|
2020-09-09 08:43:26 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
bazel_argv = [
|
|
|
|
"bazel",
|
|
|
|
"build",
|
Switch bindings implementation to `nanobind` (#1526)
* End support for Python 3.7, update cibuildwheel and publish actions
Removes Python 3.7 from the support matrix, since it does not support
PEP590 vectorcalls.
Bumps the `cibuildwheel` and `pypa-publish` actions to their latest
available versions respectively.
* Add nanobind to the Bazel dependencies, add a BUILD file
The build file builds nanobind as a static `cc_library`. Currently,
the git SHA points to HEAD, since some necessary features have not
been included in a release yet.
* Delete pybind11 BUILD file
* Switch bindings implementation to nanobind
Switches over the binding tool to `nanobind` from `pybind11`. Most
changes in the build setup itself were drop-in replacements of existing
code changed to nanobind names, no new concepts needed to be
implemented.
Sets the minimum required macOS to 10.14 for full C++17 support. Also,
to avoid ambiguities in Bazel, build for macOS 11 on Mac ARM64.
* Use Bazel select for linker options
Guards against unknown linker option errors by selecting required
linker options for nanobind only on macOS, where they are relevant.
Other changes:
* Bump cibuildwheel action to v2.12.0
* Bump Bazel for aarch64 linux wheels to 6.0.0
* Remove C++17 flag from build files since it is present in setup.py `bazel build` command
* Bump nanobind commit to current HEAD (TBD: Bump to next stable release)
* Unbreak Windows builds of nanobind-based bindings
Guards compiler options behind a new `select` macro choosing between
MSVC and not MSVC.
Other changes:
* Inject the proper C++17 standard cxxopt in the `setup.py` build
command.
* Bump nanobind to current HEAD.
* Make `macos` a benchmark-wide condition, with public visibility to
allow its use in the nanobind BUILD file.
* Fall back to `nb::implicitly_convertible` for Counter construction
Since `benchmark::Counter` only has a constructor for `double`,
the nanobind `nb::init_implicit` template cannot be used. Therefore,
to support implicit construction from ints, we fall back to the
`nb::implicitly_convertible` template instead.
2023-02-06 13:07:17 +00:00
|
|
|
ext.bazel_target,
|
2023-02-03 09:47:02 +00:00
|
|
|
f"--symlink_prefix={temp_path / 'bazel-'}",
|
|
|
|
f"--compilation_mode={'dbg' if self.debug else 'opt'}",
|
Switch bindings implementation to `nanobind` (#1526)
* End support for Python 3.7, update cibuildwheel and publish actions
Removes Python 3.7 from the support matrix, since it does not support
PEP590 vectorcalls.
Bumps the `cibuildwheel` and `pypa-publish` actions to their latest
available versions respectively.
* Add nanobind to the Bazel dependencies, add a BUILD file
The build file builds nanobind as a static `cc_library`. Currently,
the git SHA points to HEAD, since some necessary features have not
been included in a release yet.
* Delete pybind11 BUILD file
* Switch bindings implementation to nanobind
Switches over the binding tool to `nanobind` from `pybind11`. Most
changes in the build setup itself were drop-in replacements of existing
code changed to nanobind names, no new concepts needed to be
implemented.
Sets the minimum required macOS to 10.14 for full C++17 support. Also,
to avoid ambiguities in Bazel, build for macOS 11 on Mac ARM64.
* Use Bazel select for linker options
Guards against unknown linker option errors by selecting required
linker options for nanobind only on macOS, where they are relevant.
Other changes:
* Bump cibuildwheel action to v2.12.0
* Bump Bazel for aarch64 linux wheels to 6.0.0
* Remove C++17 flag from build files since it is present in setup.py `bazel build` command
* Bump nanobind commit to current HEAD (TBD: Bump to next stable release)
* Unbreak Windows builds of nanobind-based bindings
Guards compiler options behind a new `select` macro choosing between
MSVC and not MSVC.
Other changes:
* Inject the proper C++17 standard cxxopt in the `setup.py` build
command.
* Bump nanobind to current HEAD.
* Make `macos` a benchmark-wide condition, with public visibility to
allow its use in the nanobind BUILD file.
* Fall back to `nb::implicitly_convertible` for Counter construction
Since `benchmark::Counter` only has a constructor for `double`,
the nanobind `nb::init_implicit` template cannot be used. Therefore,
to support implicit construction from ints, we fall back to the
`nb::implicitly_convertible` template instead.
2023-02-06 13:07:17 +00:00
|
|
|
# C++17 is required by nanobind
|
|
|
|
f"--cxxopt={'/std:c++17' if IS_WINDOWS else '-std=c++17'}",
|
2023-02-03 09:47:02 +00:00
|
|
|
]
|
2020-09-09 08:43:26 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
# Link with python*.lib.
|
|
|
|
for library_dir in self.library_dirs:
|
|
|
|
bazel_argv.append("--linkopt=/LIBPATH:" + library_dir)
|
Switch bindings implementation to `nanobind` (#1526)
* End support for Python 3.7, update cibuildwheel and publish actions
Removes Python 3.7 from the support matrix, since it does not support
PEP590 vectorcalls.
Bumps the `cibuildwheel` and `pypa-publish` actions to their latest
available versions respectively.
* Add nanobind to the Bazel dependencies, add a BUILD file
The build file builds nanobind as a static `cc_library`. Currently,
the git SHA points to HEAD, since some necessary features have not
been included in a release yet.
* Delete pybind11 BUILD file
* Switch bindings implementation to nanobind
Switches over the binding tool to `nanobind` from `pybind11`. Most
changes in the build setup itself were drop-in replacements of existing
code changed to nanobind names, no new concepts needed to be
implemented.
Sets the minimum required macOS to 10.14 for full C++17 support. Also,
to avoid ambiguities in Bazel, build for macOS 11 on Mac ARM64.
* Use Bazel select for linker options
Guards against unknown linker option errors by selecting required
linker options for nanobind only on macOS, where they are relevant.
Other changes:
* Bump cibuildwheel action to v2.12.0
* Bump Bazel for aarch64 linux wheels to 6.0.0
* Remove C++17 flag from build files since it is present in setup.py `bazel build` command
* Bump nanobind commit to current HEAD (TBD: Bump to next stable release)
* Unbreak Windows builds of nanobind-based bindings
Guards compiler options behind a new `select` macro choosing between
MSVC and not MSVC.
Other changes:
* Inject the proper C++17 standard cxxopt in the `setup.py` build
command.
* Bump nanobind to current HEAD.
* Make `macos` a benchmark-wide condition, with public visibility to
allow its use in the nanobind BUILD file.
* Fall back to `nb::implicitly_convertible` for Counter construction
Since `benchmark::Counter` only has a constructor for `double`,
the nanobind `nb::init_implicit` template cannot be used. Therefore,
to support implicit construction from ints, we fall back to the
`nb::implicitly_convertible` template instead.
2023-02-06 13:07:17 +00:00
|
|
|
elif IS_MAC:
|
|
|
|
if platform.machine() == "x86_64":
|
|
|
|
# C++17 needs macOS 10.14 at minimum
|
|
|
|
bazel_argv.append("--macos_minimum_os=10.14")
|
2020-09-09 08:43:26 +00:00
|
|
|
|
Switch bindings implementation to `nanobind` (#1526)
* End support for Python 3.7, update cibuildwheel and publish actions
Removes Python 3.7 from the support matrix, since it does not support
PEP590 vectorcalls.
Bumps the `cibuildwheel` and `pypa-publish` actions to their latest
available versions respectively.
* Add nanobind to the Bazel dependencies, add a BUILD file
The build file builds nanobind as a static `cc_library`. Currently,
the git SHA points to HEAD, since some necessary features have not
been included in a release yet.
* Delete pybind11 BUILD file
* Switch bindings implementation to nanobind
Switches over the binding tool to `nanobind` from `pybind11`. Most
changes in the build setup itself were drop-in replacements of existing
code changed to nanobind names, no new concepts needed to be
implemented.
Sets the minimum required macOS to 10.14 for full C++17 support. Also,
to avoid ambiguities in Bazel, build for macOS 11 on Mac ARM64.
* Use Bazel select for linker options
Guards against unknown linker option errors by selecting required
linker options for nanobind only on macOS, where they are relevant.
Other changes:
* Bump cibuildwheel action to v2.12.0
* Bump Bazel for aarch64 linux wheels to 6.0.0
* Remove C++17 flag from build files since it is present in setup.py `bazel build` command
* Bump nanobind commit to current HEAD (TBD: Bump to next stable release)
* Unbreak Windows builds of nanobind-based bindings
Guards compiler options behind a new `select` macro choosing between
MSVC and not MSVC.
Other changes:
* Inject the proper C++17 standard cxxopt in the `setup.py` build
command.
* Bump nanobind to current HEAD.
* Make `macos` a benchmark-wide condition, with public visibility to
allow its use in the nanobind BUILD file.
* Fall back to `nb::implicitly_convertible` for Counter construction
Since `benchmark::Counter` only has a constructor for `double`,
the nanobind `nb::init_implicit` template cannot be used. Therefore,
to support implicit construction from ints, we fall back to the
`nb::implicitly_convertible` template instead.
2023-02-06 13:07:17 +00:00
|
|
|
# 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")
|
2022-01-26 09:13:26 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
self.spawn(bazel_argv)
|
2020-09-10 15:32:25 +00:00
|
|
|
|
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
|
|
|
shared_lib_suffix = ".dll" if IS_WINDOWS else ".so"
|
2023-02-03 09:47:02 +00:00
|
|
|
ext_name = ext.target_name + shared_lib_suffix
|
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
|
|
|
ext_bazel_bin_path = (
|
|
|
|
temp_path / "bazel-bin" / ext.relpath / ext_name
|
|
|
|
)
|
2020-09-11 09:55:18 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
ext_dest_path = Path(self.get_ext_fullpath(ext.name))
|
|
|
|
shutil.copyfile(ext_bazel_bin_path, ext_dest_path)
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
setuptools.setup(
|
|
|
|
cmdclass=dict(build_ext=BuildBazelExtension),
|
2020-09-11 09:55:18 +00:00
|
|
|
ext_modules=[
|
|
|
|
BazelExtension(
|
2023-07-10 09:43:49 +00:00
|
|
|
name="google_benchmark._benchmark",
|
|
|
|
bazel_target="//bindings/python/google_benchmark:_benchmark",
|
2020-09-11 09:55:18 +00:00
|
|
|
)
|
|
|
|
],
|
2020-05-06 16:28:29 +00:00
|
|
|
)
|