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
|
|
|
|
from typing import List
|
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
|
|
|
def _get_long_description(fp: str) -> str:
|
|
|
|
with open(fp, "r", encoding="utf-8") as f:
|
|
|
|
return f.read()
|
2020-05-06 16:28:29 +00:00
|
|
|
|
2022-03-08 12:40:14 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
def _get_version(fp: str) -> str:
|
|
|
|
"""Parse a version string from a file."""
|
|
|
|
with open(fp, "r") as f:
|
|
|
|
for line in f:
|
|
|
|
if "__version__" in line:
|
|
|
|
delim = '"'
|
|
|
|
return line.split(delim)[1]
|
|
|
|
raise RuntimeError(f"could not find a version string in file {fp!r}.")
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
def _parse_requirements(fp: str) -> List[str]:
|
|
|
|
with open(fp) as requirements:
|
2020-09-09 08:43:26 +00:00
|
|
|
return [
|
2020-09-11 09:55:18 +00:00
|
|
|
line.rstrip()
|
|
|
|
for line in requirements
|
|
|
|
if not (line.isspace() or line.startswith("#"))
|
2020-09-09 08:43:26 +00:00
|
|
|
]
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def temp_fill_include_path(fp: str):
|
|
|
|
"""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,
|
|
|
|
Path(sysconfig.get_paths()['include']).as_posix(),
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
build_ext.build_ext.run(self)
|
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
def bazel_build(self, ext: BazelExtension):
|
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",
|
|
|
|
str(ext.bazel_target),
|
|
|
|
f"--symlink_prefix={temp_path / 'bazel-'}",
|
|
|
|
f"--compilation_mode={'dbg' if self.debug else 'opt'}",
|
|
|
|
]
|
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)
|
|
|
|
elif IS_MAC and platform.machine() == "x86_64":
|
|
|
|
bazel_argv.append("--macos_minimum_os=10.9")
|
2020-09-09 08:43:26 +00:00
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
# ARCHFLAGS is always 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")
|
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
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
shared_lib_suffix = '.dll' if IS_WINDOWS else '.so'
|
|
|
|
ext_name = ext.target_name + shared_lib_suffix
|
|
|
|
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
|
|
|
|
2023-02-03 09:47:02 +00:00
|
|
|
# explicitly call `bazel shutdown` for graceful exit
|
|
|
|
self.spawn(["bazel", "shutdown"])
|
2022-04-08 14:00:46 +00:00
|
|
|
|
2020-05-06 16:28:29 +00:00
|
|
|
|
|
|
|
setuptools.setup(
|
2020-09-11 09:55:18 +00:00
|
|
|
name="google_benchmark",
|
2023-02-03 09:47:02 +00:00
|
|
|
version=_get_version("bindings/python/google_benchmark/__init__.py"),
|
2020-09-11 09:55:18 +00:00
|
|
|
url="https://github.com/google/benchmark",
|
|
|
|
description="A library to benchmark code snippets.",
|
2023-02-03 09:47:02 +00:00
|
|
|
long_description=_get_long_description("README.md"),
|
2022-03-08 12:40:14 +00:00
|
|
|
long_description_content_type="text/markdown",
|
2020-09-11 09:55:18 +00:00
|
|
|
author="Google",
|
|
|
|
author_email="benchmark-py@google.com",
|
2020-05-06 16:28:29 +00:00
|
|
|
# Contained modules and scripts.
|
2020-09-11 09:55:18 +00:00
|
|
|
package_dir={"": "bindings/python"},
|
|
|
|
packages=setuptools.find_packages("bindings/python"),
|
|
|
|
install_requires=_parse_requirements("bindings/python/requirements.txt"),
|
2020-05-06 16:28:29 +00:00
|
|
|
cmdclass=dict(build_ext=BuildBazelExtension),
|
2020-09-11 09:55:18 +00:00
|
|
|
ext_modules=[
|
|
|
|
BazelExtension(
|
|
|
|
"google_benchmark._benchmark",
|
|
|
|
"//bindings/python/google_benchmark:_benchmark",
|
|
|
|
)
|
|
|
|
],
|
2020-05-06 16:28:29 +00:00
|
|
|
zip_safe=False,
|
|
|
|
# PyPI package information.
|
|
|
|
classifiers=[
|
2020-09-11 09:55:18 +00:00
|
|
|
"Development Status :: 4 - Beta",
|
|
|
|
"Intended Audience :: Developers",
|
|
|
|
"Intended Audience :: Science/Research",
|
|
|
|
"License :: OSI Approved :: Apache Software License",
|
|
|
|
"Programming Language :: Python :: 3.7",
|
|
|
|
"Programming Language :: Python :: 3.8",
|
2022-10-18 10:23:59 +00:00
|
|
|
"Programming Language :: Python :: 3.9",
|
|
|
|
"Programming Language :: Python :: 3.10",
|
|
|
|
"Programming Language :: Python :: 3.11",
|
2020-09-11 09:55:18 +00:00
|
|
|
"Topic :: Software Development :: Testing",
|
|
|
|
"Topic :: System :: Benchmark",
|
2020-05-06 16:28:29 +00:00
|
|
|
],
|
2020-09-11 09:55:18 +00:00
|
|
|
license="Apache 2.0",
|
|
|
|
keywords="benchmark",
|
2020-05-06 16:28:29 +00:00
|
|
|
)
|