Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

140 строки
4.4 KiB

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