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.4 kB

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