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

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