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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. # Single config generators are handled "normally"
  62. single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
  63. # CMake allows an arch-in-generator style for backward compatibility
  64. contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
  65. # Specify the arch if using MSVC generator, but only if it doesn't
  66. # contain a backward-compatibility arch spec already in the
  67. # generator name.
  68. if not single_config and not contains_arch:
  69. cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
  70. # Multi-config generators have a different way to specify configs
  71. if not single_config:
  72. cmake_args += [
  73. "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
  74. ]
  75. build_args += ["--config", cfg]
  76. # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
  77. # across all generators.
  78. if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
  79. # self.parallel is a Python 3 only way to set parallel jobs by hand
  80. # using -j in the build_ext call, not supported by pip or PyPA-build.
  81. if hasattr(self, "parallel") and self.parallel:
  82. # CMake 3.12+ only.
  83. build_args += ["-j{}".format(self.parallel)]
  84. else:
  85. build_args += ["-j4"]
  86. if not os.path.exists(self.build_temp):
  87. os.makedirs(self.build_temp)
  88. subprocess.check_call(
  89. ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
  90. )
  91. subprocess.check_call(
  92. ["cmake", "--build", "."] + build_args, cwd=self.build_temp
  93. )
  94. if sys.version_info < (3, 0):
  95. sys.exit("Sorry, Python < 3.0 is not supported")
  96. requirements = ["numpy", "tqdm", "requests", "portalocker", "opencv-python"]
  97. with io.open("README.md", encoding="utf-8") as h:
  98. long_description = h.read()
  99. setup(
  100. name="ncnn",
  101. version=find_version(),
  102. author="nihui",
  103. author_email="nihuini@tencent.com",
  104. maintainer="caishanli",
  105. maintainer_email="caishanli25@gmail.com",
  106. description="ncnn is a high-performance neural network inference framework optimized for the mobile platform",
  107. long_description=long_description,
  108. long_description_content_type="text/markdown",
  109. url="https://github.com/Tencent/ncnn",
  110. classifiers=[
  111. "Programming Language :: C++",
  112. "Programming Language :: Python :: 3",
  113. "Programming Language :: Python :: 3.6",
  114. "Programming Language :: Python :: 3.7",
  115. "Programming Language :: Python :: 3.8",
  116. "Programming Language :: Python :: 3.9",
  117. "Programming Language :: Python :: 3.10",
  118. "License :: OSI Approved :: BSD License",
  119. "Operating System :: OS Independent",
  120. ],
  121. license="BSD-3",
  122. python_requires=">=3.5",
  123. packages=find_packages("python"),
  124. package_dir={"": "python"},
  125. install_requires=requirements,
  126. ext_modules=[CMakeExtension("ncnn")],
  127. cmdclass={"build_ext": CMakeBuild},
  128. )