Louis Dionne | 6c39722 | 2021-07-12 13:15:34 -0400 | [diff] [blame^] | 1 | import os |
| 2 | import posixpath |
| 3 | import re |
| 4 | import shutil |
| 5 | import sys |
| 6 | |
| 7 | from distutils import sysconfig |
| 8 | import setuptools |
| 9 | from setuptools.command import build_ext |
| 10 | |
| 11 | |
| 12 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 13 | |
| 14 | |
| 15 | IS_WINDOWS = sys.platform.startswith("win") |
| 16 | |
| 17 | |
| 18 | def _get_version(): |
| 19 | """Parse the version string from __init__.py.""" |
| 20 | with open( |
| 21 | os.path.join(HERE, "bindings", "python", "google_benchmark", "__init__.py") |
| 22 | ) as init_file: |
| 23 | try: |
| 24 | version_line = next( |
| 25 | line for line in init_file if line.startswith("__version__") |
| 26 | ) |
| 27 | except StopIteration: |
| 28 | raise ValueError("__version__ not defined in __init__.py") |
| 29 | else: |
| 30 | namespace = {} |
| 31 | exec(version_line, namespace) # pylint: disable=exec-used |
| 32 | return namespace["__version__"] |
| 33 | |
| 34 | |
| 35 | def _parse_requirements(path): |
| 36 | with open(os.path.join(HERE, path)) as requirements: |
| 37 | return [ |
| 38 | line.rstrip() |
| 39 | for line in requirements |
| 40 | if not (line.isspace() or line.startswith("#")) |
| 41 | ] |
| 42 | |
| 43 | |
| 44 | class BazelExtension(setuptools.Extension): |
| 45 | """A C/C++ extension that is defined as a Bazel BUILD target.""" |
| 46 | |
| 47 | def __init__(self, name, bazel_target): |
| 48 | self.bazel_target = bazel_target |
| 49 | self.relpath, self.target_name = posixpath.relpath(bazel_target, "//").split( |
| 50 | ":" |
| 51 | ) |
| 52 | setuptools.Extension.__init__(self, name, sources=[]) |
| 53 | |
| 54 | |
| 55 | class BuildBazelExtension(build_ext.build_ext): |
| 56 | """A command that runs Bazel to build a C/C++ extension.""" |
| 57 | |
| 58 | def run(self): |
| 59 | for ext in self.extensions: |
| 60 | self.bazel_build(ext) |
| 61 | build_ext.build_ext.run(self) |
| 62 | |
| 63 | def bazel_build(self, ext): |
| 64 | """Runs the bazel build to create the package.""" |
| 65 | with open("WORKSPACE", "r") as workspace: |
| 66 | workspace_contents = workspace.read() |
| 67 | |
| 68 | with open("WORKSPACE", "w") as workspace: |
| 69 | workspace.write( |
| 70 | re.sub( |
| 71 | r'(?<=path = ").*(?=", # May be overwritten by setup\.py\.)', |
| 72 | sysconfig.get_python_inc().replace(os.path.sep, posixpath.sep), |
| 73 | workspace_contents, |
| 74 | ) |
| 75 | ) |
| 76 | |
| 77 | if not os.path.exists(self.build_temp): |
| 78 | os.makedirs(self.build_temp) |
| 79 | |
| 80 | bazel_argv = [ |
| 81 | "bazel", |
| 82 | "build", |
| 83 | ext.bazel_target, |
| 84 | "--symlink_prefix=" + os.path.join(self.build_temp, "bazel-"), |
| 85 | "--compilation_mode=" + ("dbg" if self.debug else "opt"), |
| 86 | ] |
| 87 | |
| 88 | if IS_WINDOWS: |
| 89 | # Link with python*.lib. |
| 90 | for library_dir in self.library_dirs: |
| 91 | bazel_argv.append("--linkopt=/LIBPATH:" + library_dir) |
| 92 | |
| 93 | self.spawn(bazel_argv) |
| 94 | |
| 95 | shared_lib_suffix = '.dll' if IS_WINDOWS else '.so' |
| 96 | ext_bazel_bin_path = os.path.join( |
| 97 | self.build_temp, 'bazel-bin', |
| 98 | ext.relpath, ext.target_name + shared_lib_suffix) |
| 99 | |
| 100 | ext_dest_path = self.get_ext_fullpath(ext.name) |
| 101 | ext_dest_dir = os.path.dirname(ext_dest_path) |
| 102 | if not os.path.exists(ext_dest_dir): |
| 103 | os.makedirs(ext_dest_dir) |
| 104 | shutil.copyfile(ext_bazel_bin_path, ext_dest_path) |
| 105 | |
| 106 | |
| 107 | setuptools.setup( |
| 108 | name="google_benchmark", |
| 109 | version=_get_version(), |
| 110 | url="https://github.com/google/benchmark", |
| 111 | description="A library to benchmark code snippets.", |
| 112 | author="Google", |
| 113 | author_email="benchmark-py@google.com", |
| 114 | # Contained modules and scripts. |
| 115 | package_dir={"": "bindings/python"}, |
| 116 | packages=setuptools.find_packages("bindings/python"), |
| 117 | install_requires=_parse_requirements("bindings/python/requirements.txt"), |
| 118 | cmdclass=dict(build_ext=BuildBazelExtension), |
| 119 | ext_modules=[ |
| 120 | BazelExtension( |
| 121 | "google_benchmark._benchmark", |
| 122 | "//bindings/python/google_benchmark:_benchmark", |
| 123 | ) |
| 124 | ], |
| 125 | zip_safe=False, |
| 126 | # PyPI package information. |
| 127 | classifiers=[ |
| 128 | "Development Status :: 4 - Beta", |
| 129 | "Intended Audience :: Developers", |
| 130 | "Intended Audience :: Science/Research", |
| 131 | "License :: OSI Approved :: Apache Software License", |
| 132 | "Programming Language :: Python :: 3.6", |
| 133 | "Programming Language :: Python :: 3.7", |
| 134 | "Programming Language :: Python :: 3.8", |
| 135 | "Topic :: Software Development :: Testing", |
| 136 | "Topic :: System :: Benchmark", |
| 137 | ], |
| 138 | license="Apache 2.0", |
| 139 | keywords="benchmark", |
| 140 | ) |