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 9.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import io
  2. import os
  3. import sys
  4. import time
  5. import re
  6. import shutil
  7. import subprocess
  8. from setuptools import setup, find_packages, Extension
  9. from setuptools.command.build_ext import build_ext
  10. from setuptools.command.install import install
  11. def find_version():
  12. with io.open("CMakeLists.txt", encoding="utf8") as f:
  13. version_file = f.read()
  14. version_major = re.findall(r"NCNN_VERSION_MAJOR (.+?)", version_file)
  15. version_minor = re.findall(r"NCNN_VERSION_MINOR (.+?)", version_file)
  16. if version_major and version_minor:
  17. ncnn_version = time.strftime("%Y%m%d", time.localtime())
  18. return version_major[0] + "." + version_minor[0] + "." + ncnn_version
  19. raise RuntimeError("Unable to find version string.")
  20. # Parse environment variables
  21. Vulkan_LIBRARY = os.environ.get("Vulkan_LIBRARY", "")
  22. CMAKE_TOOLCHAIN_FILE = os.environ.get("CMAKE_TOOLCHAIN_FILE", "")
  23. PLATFORM = os.environ.get("PLATFORM", "")
  24. ARCHS = os.environ.get("ARCHS", "")
  25. DEPLOYMENT_TARGET = os.environ.get("DEPLOYMENT_TARGET", "")
  26. OpenMP_C_FLAGS = os.environ.get("OpenMP_C_FLAGS", "")
  27. OpenMP_CXX_FLAGS = os.environ.get("OpenMP_CXX_FLAGS", "")
  28. OpenMP_C_LIB_NAMES = os.environ.get("OpenMP_C_LIB_NAMES", "")
  29. OpenMP_CXX_LIB_NAMES = os.environ.get("OpenMP_CXX_LIB_NAMES", "")
  30. OpenMP_libomp_LIBRARY = os.environ.get("OpenMP_libomp_LIBRARY", "")
  31. ENABLE_BITCODE = os.environ.get("ENABLE_BITCODE", "")
  32. ENABLE_ARC = os.environ.get("ENABLE_ARC", "")
  33. ENABLE_VISIBILITY = os.environ.get("ENABLE_VISIBILITY", "")
  34. # Parse variables from command line with setup.py install
  35. class InstallCommand(install):
  36. user_options = install.user_options + [
  37. ('vulkan=', None, 'Enable the usage of Vulkan.'),
  38. ]
  39. def initialize_options(self):
  40. install.initialize_options(self)
  41. self.vulkan = None
  42. def finalize_options(self):
  43. install.finalize_options(self)
  44. def run(self):
  45. install.run(self)
  46. # Convert distutils Windows platform specifiers to CMake -A arguments
  47. PLAT_TO_CMAKE = {
  48. "win32": "Win32",
  49. "win-amd64": "x64",
  50. "win-arm32": "ARM",
  51. "win-arm64": "ARM64",
  52. }
  53. # A CMakeExtension needs a sourcedir instead of a file list.
  54. # The name must be the _single_ output extension from the CMake build.
  55. # If you need multiple extensions, see scikit-build.
  56. class CMakeExtension(Extension):
  57. def __init__(self, name, sourcedir=""):
  58. Extension.__init__(self, name, sources=[])
  59. self.sourcedir = os.path.abspath(sourcedir)
  60. class CMakeBuild(build_ext):
  61. def build_extension(self, ext):
  62. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  63. extdir = os.path.join(extdir, "ncnn")
  64. # required for auto-detection of auxiliary "native" libs
  65. if not extdir.endswith(os.path.sep):
  66. extdir += os.path.sep
  67. cfg = "Debug" if self.debug else "Release"
  68. # CMake lets you override the generator - we need to check this.
  69. # Can be set with Conda-Build, for example.
  70. cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
  71. # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
  72. # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
  73. # from Python.
  74. cmake_args = [
  75. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
  76. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={}".format(extdir),
  77. "-DPYTHON_EXECUTABLE={}".format(sys.executable),
  78. "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm
  79. "-DNCNN_PYTHON=ON",
  80. "-DNCNN_VULKAN=ON",
  81. "-DNCNN_DISABLE_RTTI=OFF",
  82. "-DNCNN_DISABLE_EXCEPTION=OFF",
  83. "-DNCNN_BUILD_BENCHMARK=OFF",
  84. "-DNCNN_BUILD_EXAMPLES=OFF",
  85. "-DNCNN_BUILD_TOOLS=OFF",
  86. ]
  87. if Vulkan_LIBRARY != "":
  88. cmake_args.append("-DVulkan_LIBRARY=" + Vulkan_LIBRARY)
  89. if CMAKE_TOOLCHAIN_FILE != "":
  90. cmake_args.append("-DCMAKE_TOOLCHAIN_FILE=" + CMAKE_TOOLCHAIN_FILE)
  91. if PLATFORM != "":
  92. cmake_args.append("-DPLATFORM=" + PLATFORM)
  93. if ARCHS != "":
  94. cmake_args.append("-DARCHS=" + ARCHS)
  95. if DEPLOYMENT_TARGET != "":
  96. cmake_args.append("-DDEPLOYMENT_TARGET=" + DEPLOYMENT_TARGET)
  97. if OpenMP_C_FLAGS != "":
  98. cmake_args.append("-DOpenMP_C_FLAGS=" + OpenMP_C_FLAGS)
  99. if OpenMP_CXX_FLAGS != "":
  100. cmake_args.append("-DOpenMP_CXX_FLAGS=" + OpenMP_CXX_FLAGS)
  101. if OpenMP_C_LIB_NAMES != "":
  102. cmake_args.append("-DOpenMP_C_LIB_NAMES=" + OpenMP_C_LIB_NAMES)
  103. if OpenMP_CXX_LIB_NAMES != "":
  104. cmake_args.append("-DOpenMP_CXX_LIB_NAMES=" + OpenMP_CXX_LIB_NAMES)
  105. if OpenMP_libomp_LIBRARY != "":
  106. cmake_args.append("-DOpenMP_libomp_LIBRARY=" + OpenMP_libomp_LIBRARY)
  107. if ENABLE_BITCODE != "":
  108. cmake_args.append("-DENABLE_BITCODE=" + ENABLE_BITCODE)
  109. if ENABLE_ARC != "":
  110. cmake_args.append("-DENABLE_ARC=" + ENABLE_ARC)
  111. if ENABLE_VISIBILITY != "":
  112. cmake_args.append("-DENABLE_VISIBILITY=" + ENABLE_VISIBILITY)
  113. build_args = []
  114. if self.compiler.compiler_type == "msvc":
  115. # Single config generators are handled "normally"
  116. single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
  117. # CMake allows an arch-in-generator style for backward compatibility
  118. contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
  119. # Specify the arch if using MSVC generator, but only if it doesn't
  120. # contain a backward-compatibility arch spec already in the
  121. # generator name.
  122. if not single_config and not contains_arch:
  123. cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
  124. # Multi-config generators have a different way to specify configs
  125. if not single_config:
  126. cmake_args += [
  127. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
  128. ]
  129. build_args += ["--config", cfg]
  130. # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
  131. # across all generators.
  132. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
  133. # self.parallel is a Python 3 only way to set parallel jobs by hand
  134. # using -j in the build_ext call, not supported by pip or PyPA-build.
  135. if hasattr(self, "parallel") and self.parallel:
  136. # CMake 3.12+ only.
  137. build_args += ["-j{}".format(self.parallel)]
  138. else:
  139. # Automatically set parallel jobs based on CPU core count
  140. cpu_count = os.cpu_count() or 1
  141. build_args += ["-j{}".format(cpu_count)]
  142. if not os.path.exists(self.build_temp):
  143. os.makedirs(self.build_temp)
  144. subprocess.check_call(
  145. ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
  146. )
  147. subprocess.check_call(
  148. ["cmake", "--build", "."] + build_args, cwd=self.build_temp
  149. )
  150. if sys.version_info < (3, 0):
  151. sys.exit("Sorry, Python < 3.0 is not supported")
  152. requirements = ["numpy", "tqdm", "requests", "portalocker", "opencv-python"]
  153. setup_requires = []
  154. if shutil.which("cmake") is None:
  155. setup_requires += ["cmake>=3.12"]
  156. if shutil.which("ninja") is None:
  157. setup_requires += ["ninja; sys_platform != 'win32'"]
  158. with io.open("README.md", encoding="utf-8") as h:
  159. long_description = h.read()
  160. setup(
  161. name="ncnn",
  162. version=find_version(),
  163. author="nihui",
  164. author_email="nihuini@tencent.com",
  165. maintainer="caishanli",
  166. maintainer_email="caishanli25@gmail.com",
  167. description="ncnn is a high-performance neural network inference framework optimized for the mobile platform",
  168. long_description=long_description,
  169. long_description_content_type="text/markdown",
  170. url="https://github.com/Tencent/ncnn",
  171. classifiers=[
  172. "Programming Language :: C++",
  173. "Programming Language :: Python :: 3",
  174. "Programming Language :: Python :: 3.6",
  175. "Programming Language :: Python :: 3.7",
  176. "Programming Language :: Python :: 3.8",
  177. "Programming Language :: Python :: 3.9",
  178. "Programming Language :: Python :: 3.10",
  179. "Programming Language :: Python :: 3.11",
  180. "Programming Language :: Python :: 3.12",
  181. "Programming Language :: Python :: 3.13",
  182. "License :: OSI Approved :: BSD License",
  183. "Operating System :: OS Independent",
  184. "Topic :: Scientific/Engineering :: Artificial Intelligence",
  185. ],
  186. license="BSD-3",
  187. python_requires=">=3.5",
  188. packages=find_packages("python"),
  189. package_dir={"": "python"},
  190. setup_requires=setup_requires,
  191. install_requires=requirements,
  192. ext_modules=[CMakeExtension("ncnn")],
  193. cmdclass={'install': InstallCommand, "build_ext": CMakeBuild},
  194. )