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

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