Elffuss BraidHub

kikocis/ncnn public

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