setup.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # usage: python setup.py install
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. from setuptools import setup, Extension
  8. from setuptools.command.build_ext import build_ext
  9. from setuptools import find_packages
  10. # Convert distutils Windows platform specifiers to CMake -A arguments
  11. PLAT_TO_CMAKE = {
  12. "win32": "Win32",
  13. "win-amd64": "x64",
  14. "win-arm32": "ARM",
  15. "win-arm64": "ARM64",
  16. }
  17. # A CMakeExtension needs a sourcedir instead of a file list.
  18. # The name must be the _single_ output extension from the CMake build.
  19. # If you need multiple extensions, see scikit-build.
  20. class CMakeExtension(Extension):
  21. def __init__(self, name, sourcedir=".."):
  22. Extension.__init__(self, name, sources=[])
  23. self.sourcedir = os.path.abspath(sourcedir)
  24. class CMakeBuild(build_ext):
  25. def build_extension(self, ext):
  26. # self.parallel = 4
  27. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  28. # required for auto-detection & inclusion of auxiliary "native" libs
  29. if not extdir.endswith(os.path.sep):
  30. extdir += os.path.sep
  31. debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
  32. cfg = "Debug" if debug else "Release"
  33. # CMake lets you override the generator - we need to check this.
  34. # Can be set with Conda-Build, for example.
  35. cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
  36. # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
  37. cmake_args = [
  38. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
  39. "-DPYTHON_EXECUTABLE={}".format(sys.executable),
  40. "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm
  41. "-Dbuild_python_api=ON",
  42. ]
  43. build_args = []
  44. # Adding CMake arguments set as environment variable
  45. # (needed e.g. to build for ARM OSx on conda-forge)
  46. if "CMAKE_ARGS" in os.environ:
  47. cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
  48. if self.compiler.compiler_type != "msvc":
  49. # Using Ninja-build since it a) is available as a wheel and b)
  50. # multithreads automatically. MSVC would require all variables be
  51. # exported for Ninja to pick it up, which is a little tricky to do.
  52. # Users can override the generator with CMAKE_GENERATOR in CMake
  53. # 3.15+.
  54. if not cmake_generator:
  55. try:
  56. import ninja # noqa: F401
  57. cmake_args += ["-GNinja"]
  58. except ImportError:
  59. pass
  60. else:
  61. # Single config generators are handled "normally"
  62. single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
  63. # CMake allows an arch-in-generator style for backward compatibility
  64. contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
  65. # Specify the arch if using MSVC generator, but only if it doesn't
  66. # contain a backward-compatibility arch spec already in the
  67. # generator name.
  68. if not single_config and not contains_arch:
  69. cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
  70. # Multi-config generators have a different way to specify configs
  71. if not single_config:
  72. cmake_args += [
  73. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
  74. ]
  75. build_args += ["--config", cfg]
  76. if sys.platform.startswith("darwin"):
  77. # Cross-compile support for macOS - respect ARCHFLAGS if set
  78. archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
  79. if archs:
  80. cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
  81. # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
  82. # across all generators.
  83. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
  84. # self.parallel is a Python 3 only way to set parallel jobs by hand
  85. # using -j in the build_ext call, not supported by pip or PyPA-build.
  86. if hasattr(self, "parallel") and self.parallel:
  87. # CMake 3.12+ only.
  88. build_args += ["-j{}".format(self.parallel)]
  89. if not os.path.exists(self.build_temp):
  90. os.makedirs(self.build_temp)
  91. subprocess.check_call(
  92. ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
  93. )
  94. subprocess.check_call(
  95. ["cmake", "--build", "."] + build_args, cwd=self.build_temp
  96. )
  97. # The information here can also be placed in setup.cfg - better separation of
  98. # logic and declaration, and simpler if you include description/version in a file.
  99. setup(
  100. name="cnstream",
  101. version="6.1.0",
  102. author="Cambricon",
  103. url='https://gitee.com/SolutionSDK/CNStream',
  104. author_email="@cambricon.com",
  105. license='APACHE 2.0',
  106. description="CNStream based on pybind11",
  107. long_description="",
  108. python_requires='>=3.5',
  109. ext_modules=[CMakeExtension("cnstream")],
  110. cmdclass={"build_ext": CMakeBuild},
  111. zip_safe=False,
  112. include_package_data = True,
  113. # extras_require={"test": ["pytest"]},
  114. )