You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.py 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import io
  2. import os
  3. import sys
  4. import time
  5. import re
  6. import subprocess
  7. from setuptools import setup, find_packages, Extension
  8. from setuptools.command.build_ext import build_ext
  9. def find_version():
  10. with io.open("CMakeLists.txt", encoding="utf8") as f:
  11. version_file = f.read()
  12. version_major = re.findall(r"NCNN_VERSION_MAJOR (.+?)", version_file)
  13. version_minor = re.findall(r"NCNN_VERSION_MINOR (.+?)", version_file)
  14. if version_major and version_minor:
  15. if sys.platform == "darwin":
  16. ncnn_version = time.strftime("%Y.%m.%d", time.localtime())
  17. else:
  18. ncnn_version = time.strftime("%Y%m%d", time.localtime())
  19. return version_major[0] + "." + version_minor[0] + "." + ncnn_version
  20. raise RuntimeError("Unable to find version string.")
  21. # Convert distutils Windows platform specifiers to CMake -A arguments
  22. PLAT_TO_CMAKE = {
  23. "win32": "Win32",
  24. "win-amd64": "x64",
  25. "win-arm32": "ARM",
  26. "win-arm64": "ARM64",
  27. }
  28. # A CMakeExtension needs a sourcedir instead of a file list.
  29. # The name must be the _single_ output extension from the CMake build.
  30. # If you need multiple extensions, see scikit-build.
  31. class CMakeExtension(Extension):
  32. def __init__(self, name, sourcedir=""):
  33. Extension.__init__(self, name, sources=[])
  34. self.sourcedir = os.path.abspath(sourcedir)
  35. class CMakeBuild(build_ext):
  36. def build_extension(self, ext):
  37. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  38. extdir = os.path.join(extdir, "ncnn")
  39. # required for auto-detection of auxiliary "native" libs
  40. if not extdir.endswith(os.path.sep):
  41. extdir += os.path.sep
  42. cfg = "Debug" if self.debug else "Release"
  43. # CMake lets you override the generator - we need to check this.
  44. # Can be set with Conda-Build, for example.
  45. cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
  46. # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
  47. # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
  48. # from Python.
  49. cmake_args = [
  50. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
  51. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={}".format(extdir),
  52. "-DPYTHON_EXECUTABLE={}".format(sys.executable),
  53. "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm
  54. "-DNCNN_PYTHON=ON",
  55. "-DNCNN_BUILD_BENCHMARK=OFF",
  56. "-DNCNN_BUILD_EXAMPLES=OFF",
  57. "-DNCNN_BUILD_TOOLS=OFF",
  58. ]
  59. build_args = []
  60. if self.compiler.compiler_type != "msvc":
  61. # Using Ninja-build since it a) is available as a wheel and b)
  62. # multithreads automatically. MSVC would require all variables be
  63. # exported for Ninja to pick it up, which is a little tricky to do.
  64. # Users can override the generator with CMAKE_GENERATOR in CMake
  65. # 3.15+.
  66. if not cmake_generator:
  67. cmake_args += ["-GNinja"]
  68. else:
  69. # Single config generators are handled "normally"
  70. single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
  71. # CMake allows an arch-in-generator style for backward compatibility
  72. contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
  73. # Specify the arch if using MSVC generator, but only if it doesn't
  74. # contain a backward-compatibility arch spec already in the
  75. # generator name.
  76. if not single_config and not contains_arch:
  77. cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
  78. # Multi-config generators have a different way to specify configs
  79. if not single_config:
  80. cmake_args += [
  81. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
  82. ]
  83. build_args += ["--config", cfg]
  84. # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
  85. # across all generators.
  86. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
  87. # self.parallel is a Python 3 only way to set parallel jobs by hand
  88. # using -j in the build_ext call, not supported by pip or PyPA-build.
  89. if hasattr(self, "parallel") and self.parallel:
  90. # CMake 3.12+ only.
  91. build_args += ["-j{}".format(self.parallel)]
  92. if not os.path.exists(self.build_temp):
  93. os.makedirs(self.build_temp)
  94. subprocess.check_call(
  95. ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
  96. )
  97. subprocess.check_call(
  98. ["cmake", "--build", "."] + build_args, cwd=self.build_temp
  99. )
  100. if sys.version_info < (3, 0):
  101. sys.exit("Sorry, Python < 3.0 is not supported")
  102. requirements = ["numpy", "tqdm", "requests", "portalocker", "opencv-python"]
  103. with io.open("README.md", encoding="utf-8") as h:
  104. long_description = h.read()
  105. setup(
  106. name="ncnn",
  107. version=find_version(),
  108. author="nihui",
  109. author_email="nihuini@tencent.com",
  110. maintainer="caishanli",
  111. maintainer_email="caishanli25@gmail.com",
  112. description="ncnn is a high-performance neural network inference framework optimized for the mobile platform",
  113. long_description=long_description,
  114. long_description_content_type="text/markdown",
  115. url="https://github.com/Tencent/ncnn",
  116. classifiers=[
  117. "Programming Language :: C++",
  118. "Programming Language :: Python :: 3",
  119. "Programming Language :: Python :: 3.5",
  120. "Programming Language :: Python :: 3.6",
  121. "Programming Language :: Python :: 3.7",
  122. "Programming Language :: Python :: 3.8",
  123. "Programming Language :: Python :: 3.9",
  124. "License :: OSI Approved :: BSD License",
  125. "Operating System :: OS Independent",
  126. ],
  127. license="BSD-3",
  128. python_requires=">=3.5",
  129. packages=find_packages("python"),
  130. package_dir={"": "python"},
  131. install_requires=requirements,
  132. ext_modules=[CMakeExtension("ncnn")],
  133. cmdclass={"build_ext": CMakeBuild},
  134. )