ncnn
/
setup.py
212 строк · 8.2 Кб
1import io2import os3import sys4import time5import re6import subprocess7
8from setuptools import setup, find_packages, Extension9from setuptools.command.build_ext import build_ext10from setuptools.command.install import install11
12
13def find_version():14with io.open("CMakeLists.txt", encoding="utf8") as f:15version_file = f.read()16
17version_major = re.findall(r"NCNN_VERSION_MAJOR (.+?)", version_file)18version_minor = re.findall(r"NCNN_VERSION_MINOR (.+?)", version_file)19
20if version_major and version_minor:21ncnn_version = time.strftime("%Y%m%d", time.localtime())22
23return version_major[0] + "." + version_minor[0] + "." + ncnn_version24raise 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):43user_options = install.user_options + [44('vulkan=', None, 'Enable the usage of Vulkan.'),45]46def initialize_options(self):47install.initialize_options(self)48self.vulkan = None49
50def finalize_options(self):51install.finalize_options(self)52
53def run(self):54install.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):68def __init__(self, name, sourcedir=""):69Extension.__init__(self, name, sources=[])70self.sourcedir = os.path.abspath(sourcedir)71
72
73class CMakeBuild(build_ext):74def build_extension(self, ext):75extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))76extdir = os.path.join(extdir, "ncnn")77
78# required for auto-detection of auxiliary "native" libs79if not extdir.endswith(os.path.sep):80extdir += os.path.sep81
82cfg = "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.86cmake_generator = os.environ.get("CMAKE_GENERATOR", "")87
88# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON89# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code90# from Python.91cmake_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 harm96"-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]104if Vulkan_LIBRARY != "":105cmake_args.append("-DVulkan_LIBRARY=" + Vulkan_LIBRARY)106if CMAKE_TOOLCHAIN_FILE != "":107cmake_args.append("-DCMAKE_TOOLCHAIN_FILE=" + CMAKE_TOOLCHAIN_FILE)108if PLATFORM != "":109cmake_args.append("-DPLATFORM=" + PLATFORM)110if ARCHS != "":111cmake_args.append("-DARCHS=" + ARCHS)112if DEPLOYMENT_TARGET != "":113cmake_args.append("-DDEPLOYMENT_TARGET=" + DEPLOYMENT_TARGET)114if OpenMP_C_FLAGS != "":115cmake_args.append("-DOpenMP_C_FLAGS=" + OpenMP_C_FLAGS)116if OpenMP_CXX_FLAGS != "":117cmake_args.append("-DOpenMP_CXX_FLAGS=" + OpenMP_CXX_FLAGS)118if OpenMP_C_LIB_NAMES != "":119cmake_args.append("-DOpenMP_C_LIB_NAMES=" + OpenMP_C_LIB_NAMES)120if OpenMP_CXX_LIB_NAMES != "":121cmake_args.append("-DOpenMP_CXX_LIB_NAMES=" + OpenMP_CXX_LIB_NAMES)122if OpenMP_libomp_LIBRARY != "":123cmake_args.append("-DOpenMP_libomp_LIBRARY=" + OpenMP_libomp_LIBRARY)124if ENABLE_BITCODE != "":125cmake_args.append("-DENABLE_BITCODE=" + ENABLE_BITCODE)126if ENABLE_ARC != "":127cmake_args.append("-DENABLE_ARC=" + ENABLE_ARC)128if ENABLE_VISIBILITY != "":129cmake_args.append("-DENABLE_VISIBILITY=" + ENABLE_VISIBILITY)130
131build_args = []132
133if self.compiler.compiler_type == "msvc":134# Single config generators are handled "normally"135single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})136
137# CMake allows an arch-in-generator style for backward compatibility138contains_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't141# contain a backward-compatibility arch spec already in the142# generator name.143if not single_config and not contains_arch:144cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]145
146# Multi-config generators have a different way to specify configs147if not single_config:148cmake_args += [149"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)150]151build_args += ["--config", cfg]152
153# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level154# across all generators.155if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:156# self.parallel is a Python 3 only way to set parallel jobs by hand157# using -j in the build_ext call, not supported by pip or PyPA-build.158if hasattr(self, "parallel") and self.parallel:159# CMake 3.12+ only.160build_args += ["-j{}".format(self.parallel)]161else:162build_args += ["-j4"]163
164if not os.path.exists(self.build_temp):165os.makedirs(self.build_temp)166
167subprocess.check_call(168["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp169)170subprocess.check_call(171["cmake", "--build", "."] + build_args, cwd=self.build_temp172)173
174
175if sys.version_info < (3, 0):176sys.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:181long_description = h.read()182
183setup(184name="ncnn",185version=find_version(),186author="nihui",187author_email="nihuini@tencent.com",188maintainer="caishanli",189maintainer_email="caishanli25@gmail.com",190description="ncnn is a high-performance neural network inference framework optimized for the mobile platform",191long_description=long_description,192long_description_content_type="text/markdown",193url="https://github.com/Tencent/ncnn",194classifiers=[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],205license="BSD-3",206python_requires=">=3.5",207packages=find_packages("python"),208package_dir={"": "python"},209install_requires=requirements,210ext_modules=[CMakeExtension("ncnn")],211cmdclass={'install': InstallCommand, "build_ext": CMakeBuild},212)
213