2020-05-07 00:28:29 +08:00
|
|
|
import os
|
|
|
|
import posixpath
|
2021-11-11 22:51:22 +08:00
|
|
|
import platform
|
2020-05-07 00:28:29 +08:00
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from distutils import sysconfig
|
|
|
|
import setuptools
|
|
|
|
from setuptools.command import build_ext
|
|
|
|
|
|
|
|
|
2020-09-09 16:43:26 +08:00
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
2020-05-07 00:28:29 +08:00
|
|
|
|
|
|
|
|
2020-09-11 17:55:18 +08:00
|
|
|
IS_WINDOWS = sys.platform.startswith("win")
|
2020-05-07 00:28:29 +08:00
|
|
|
|
|
|
|
|
2022-03-08 20:40:14 +08:00
|
|
|
with open("README.md", "r", encoding="utf-8") as fp:
|
|
|
|
long_description = fp.read()
|
|
|
|
|
|
|
|
|
2020-05-07 00:28:29 +08:00
|
|
|
def _get_version():
|
2020-09-09 16:43:26 +08:00
|
|
|
"""Parse the version string from __init__.py."""
|
2020-09-11 17:55:18 +08:00
|
|
|
with open(
|
|
|
|
os.path.join(HERE, "bindings", "python", "google_benchmark", "__init__.py")
|
|
|
|
) as init_file:
|
2020-09-09 16:43:26 +08:00
|
|
|
try:
|
|
|
|
version_line = next(
|
2020-09-11 17:55:18 +08:00
|
|
|
line for line in init_file if line.startswith("__version__")
|
|
|
|
)
|
2020-09-09 16:43:26 +08:00
|
|
|
except StopIteration:
|
2020-09-11 17:55:18 +08:00
|
|
|
raise ValueError("__version__ not defined in __init__.py")
|
2020-09-09 16:43:26 +08:00
|
|
|
else:
|
|
|
|
namespace = {}
|
|
|
|
exec(version_line, namespace) # pylint: disable=exec-used
|
2020-09-11 17:55:18 +08:00
|
|
|
return namespace["__version__"]
|
2020-05-07 00:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _parse_requirements(path):
|
2020-09-09 16:43:26 +08:00
|
|
|
with open(os.path.join(HERE, path)) as requirements:
|
|
|
|
return [
|
2020-09-11 17:55:18 +08:00
|
|
|
line.rstrip()
|
|
|
|
for line in requirements
|
|
|
|
if not (line.isspace() or line.startswith("#"))
|
2020-09-09 16:43:26 +08:00
|
|
|
]
|
2020-05-07 00:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BazelExtension(setuptools.Extension):
|
2020-09-09 16:43:26 +08:00
|
|
|
"""A C/C++ extension that is defined as a Bazel BUILD target."""
|
2020-05-07 00:28:29 +08:00
|
|
|
|
2020-09-09 16:43:26 +08:00
|
|
|
def __init__(self, name, bazel_target):
|
|
|
|
self.bazel_target = bazel_target
|
2020-09-11 17:55:18 +08:00
|
|
|
self.relpath, self.target_name = posixpath.relpath(bazel_target, "//").split(
|
|
|
|
":"
|
|
|
|
)
|
2020-09-09 16:43:26 +08:00
|
|
|
setuptools.Extension.__init__(self, name, sources=[])
|
2020-05-07 00:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BuildBazelExtension(build_ext.build_ext):
|
2020-09-09 16:43:26 +08: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)
|
|
|
|
|
|
|
|
def bazel_build(self, ext):
|
|
|
|
"""Runs the bazel build to create the package."""
|
2020-09-11 17:55:18 +08:00
|
|
|
with open("WORKSPACE", "r") as workspace:
|
2020-09-09 16:43:26 +08:00
|
|
|
workspace_contents = workspace.read()
|
|
|
|
|
2020-09-11 17:55:18 +08:00
|
|
|
with open("WORKSPACE", "w") as workspace:
|
|
|
|
workspace.write(
|
|
|
|
re.sub(
|
|
|
|
r'(?<=path = ").*(?=", # May be overwritten by setup\.py\.)',
|
|
|
|
sysconfig.get_python_inc().replace(os.path.sep, posixpath.sep),
|
|
|
|
workspace_contents,
|
|
|
|
)
|
|
|
|
)
|
2020-09-09 16:43:26 +08:00
|
|
|
|
|
|
|
if not os.path.exists(self.build_temp):
|
|
|
|
os.makedirs(self.build_temp)
|
|
|
|
|
|
|
|
bazel_argv = [
|
2020-09-11 17:55:18 +08:00
|
|
|
"bazel",
|
|
|
|
"build",
|
2020-09-09 16:43:26 +08:00
|
|
|
ext.bazel_target,
|
2020-09-11 17:55:18 +08:00
|
|
|
"--symlink_prefix=" + os.path.join(self.build_temp, "bazel-"),
|
|
|
|
"--compilation_mode=" + ("dbg" if self.debug else "opt"),
|
2020-09-09 16:43:26 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
# Link with python*.lib.
|
|
|
|
for library_dir in self.library_dirs:
|
2020-09-11 17:55:18 +08:00
|
|
|
bazel_argv.append("--linkopt=/LIBPATH:" + library_dir)
|
2021-11-11 22:51:22 +08:00
|
|
|
elif sys.platform == "darwin" and platform.machine() == "x86_64":
|
|
|
|
bazel_argv.append("--macos_minimum_os=10.9")
|
2020-09-09 16:43:26 +08:00
|
|
|
|
2022-01-26 17:13:26 +08: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")
|
|
|
|
|
2020-09-10 23:32:25 +08:00
|
|
|
self.spawn(bazel_argv)
|
|
|
|
|
|
|
|
shared_lib_suffix = '.dll' if IS_WINDOWS else '.so'
|
|
|
|
ext_bazel_bin_path = os.path.join(
|
|
|
|
self.build_temp, 'bazel-bin',
|
|
|
|
ext.relpath, ext.target_name + shared_lib_suffix)
|
2020-09-11 17:55:18 +08:00
|
|
|
|
2020-09-10 23:32:25 +08:00
|
|
|
ext_dest_path = self.get_ext_fullpath(ext.name)
|
|
|
|
ext_dest_dir = os.path.dirname(ext_dest_path)
|
|
|
|
if not os.path.exists(ext_dest_dir):
|
|
|
|
os.makedirs(ext_dest_dir)
|
|
|
|
shutil.copyfile(ext_bazel_bin_path, ext_dest_path)
|
2020-05-07 00:28:29 +08:00
|
|
|
|
2022-04-08 22:00:46 +08:00
|
|
|
# explicitly call `bazel shutdown` for graceful exit
|
|
|
|
self.spawn(["bazel", "shutdown"])
|
|
|
|
|
2020-05-07 00:28:29 +08:00
|
|
|
|
|
|
|
setuptools.setup(
|
2020-09-11 17:55:18 +08:00
|
|
|
name="google_benchmark",
|
2020-05-07 00:28:29 +08:00
|
|
|
version=_get_version(),
|
2020-09-11 17:55:18 +08:00
|
|
|
url="https://github.com/google/benchmark",
|
|
|
|
description="A library to benchmark code snippets.",
|
2022-03-08 20:40:14 +08:00
|
|
|
long_description=long_description,
|
|
|
|
long_description_content_type="text/markdown",
|
2020-09-11 17:55:18 +08:00
|
|
|
author="Google",
|
|
|
|
author_email="benchmark-py@google.com",
|
2020-05-07 00:28:29 +08:00
|
|
|
# Contained modules and scripts.
|
2020-09-11 17:55:18 +08:00
|
|
|
package_dir={"": "bindings/python"},
|
|
|
|
packages=setuptools.find_packages("bindings/python"),
|
|
|
|
install_requires=_parse_requirements("bindings/python/requirements.txt"),
|
2020-05-07 00:28:29 +08:00
|
|
|
cmdclass=dict(build_ext=BuildBazelExtension),
|
2020-09-11 17:55:18 +08:00
|
|
|
ext_modules=[
|
|
|
|
BazelExtension(
|
|
|
|
"google_benchmark._benchmark",
|
|
|
|
"//bindings/python/google_benchmark:_benchmark",
|
|
|
|
)
|
|
|
|
],
|
2020-05-07 00:28:29 +08:00
|
|
|
zip_safe=False,
|
|
|
|
# PyPI package information.
|
|
|
|
classifiers=[
|
2020-09-11 17:55:18 +08: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 18:23:59 +08:00
|
|
|
"Programming Language :: Python :: 3.9",
|
|
|
|
"Programming Language :: Python :: 3.10",
|
|
|
|
"Programming Language :: Python :: 3.11",
|
2020-09-11 17:55:18 +08:00
|
|
|
"Topic :: Software Development :: Testing",
|
|
|
|
"Topic :: System :: Benchmark",
|
2020-05-07 00:28:29 +08:00
|
|
|
],
|
2020-09-11 17:55:18 +08:00
|
|
|
license="Apache 2.0",
|
|
|
|
keywords="benchmark",
|
2020-05-07 00:28:29 +08:00
|
|
|
)
|