Elffuss BraidHub

kikocis/ncnn public

master · 6.6 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
12def set_version():
13 pnnx_version = time.strftime("%Y%m%d", time.localtime())
14 return pnnx_version
15
16# Parse environment variables
17TORCH_INSTALL_DIR = os.environ.get("TORCH_INSTALL_DIR", "")
18TORCHVISION_INSTALL_DIR = os.environ.get("TORCHVISION_INSTALL_DIR", "")
19PROTOBUF_INCLUDE_DIR = os.environ.get("PROTOBUF_INCLUDE_DIR", "")
20PROTOBUF_LIBRARIES = os.environ.get("PROTOBUF_LIBRARIES", "")
21PROTOBUF_PROTOC_EXECUTABLE = os.environ.get("PROTOBUF_PROTOC_EXECUTABLE", "")
22CMAKE_BUILD_TYPE = os.environ.get("CMAKE_BUILD_TYPE", "")
23PNNX_BUILD_WITH_STATIC_CRT = os.environ.get("PNNX_BUILD_WITH_STATIC_CRT", "")
24PNNX_WHEEL_WITHOUT_BUILD = os.environ.get("PNNX_WHEEL_WITHOUT_BUILD", "")
25
26
27# Convert distutils Windows platform specifiers to CMake -A arguments
28PLAT_TO_CMAKE = {
29 "win32": "Win32",
30 "win-amd64": "x64",
31 "win-arm32": "ARM",
32 "win-arm64": "ARM64",
33}
34
35# A CMakeExtension needs a sourcedir instead of a file list.
36# The name must be the _single_ output extension from the CMake build.
37# If you need multiple extensions, see scikit-build.
38class CMakeExtension(Extension):
39 def __init__(self, name, sourcedir=""):
40 Extension.__init__(self, name, sources=[])
41 self.sourcedir = os.path.abspath(sourcedir)
42
43class CMakeBuild(build_ext):
44 def build_extension(self, ext):
45 extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
46 extdir = os.path.join(extdir, "pnnx")
47
48 # required for auto-detection of auxiliary "native" libs
49 if not extdir.endswith(os.path.sep):
50 extdir += os.path.sep
51
52 cfg = "Debug" if self.debug else "Release"
53
54 # CMake lets you override the generator - we need to check this.
55 # Can be set with Conda-Build, for example.
56 cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
57
58 # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
59 # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
60 # from Python.
61 cmake_args = [
62 "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={}".format(extdir),
63 "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE={}".format(extdir),
64 "-DPython3_EXECUTABLE={}".format(sys.executable),
65 "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm
66 ]
67
68 if TORCH_INSTALL_DIR != "":
69 cmake_args.append("-DTorch_INSTALL_DIR=" + TORCH_INSTALL_DIR)
70 if TORCHVISION_INSTALL_DIR != "":
71 cmake_args.append("-DTorchVision_INSTALL_DIR=" + TORCHVISION_INSTALL_DIR)
72 if PROTOBUF_INCLUDE_DIR != "":
73 cmake_args.append("-DProtobuf_INCLUDE_DIR=" + PROTOBUF_INCLUDE_DIR)
74 if PROTOBUF_LIBRARIES != "":
75 cmake_args.append("-DProtobuf_LIBRARIES=" + PROTOBUF_LIBRARIES)
76 if PROTOBUF_PROTOC_EXECUTABLE != "":
77 cmake_args.append("-DProtobuf_PROTOC_EXECUTABLE=" + PROTOBUF_PROTOC_EXECUTABLE)
78 if CMAKE_BUILD_TYPE != "":
79 cmake_args.append("-DCMAKE_BUILD_TYPE=" + CMAKE_BUILD_TYPE)
80 if PNNX_BUILD_WITH_STATIC_CRT != "":
81 cmake_args.append("-DPNNX_BUILD_WITH_STATIC_CRT=" + PNNX_BUILD_WITH_STATIC_CRT)
82
83 build_args = []
84
85 if self.compiler.compiler_type == "msvc":
86 # Single config generators are handled "normally"
87 single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
88
89 # CMake allows an arch-in-generator style for backward compatibility
90 contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
91
92 # Specify the arch if using MSVC generator, but only if it doesn't
93 # contain a backward-compatibility arch spec already in the
94 # generator name.
95 if not single_config and not contains_arch:
96 cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
97
98 # Multi-config generators have a different way to specify configs
99 if not single_config:
100 cmake_args += [
101 "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
102 ]
103 build_args += ["--config", cfg]
104
105 # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
106 # across all generators.
107 if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
108 # self.parallel is a Python 3 only way to set parallel jobs by hand
109 # using -j in the build_ext call, not supported by pip or PyPA-build.
110 if hasattr(self, "parallel") and self.parallel:
111 # CMake 3.12+ only.
112 build_args += ["-j{}".format(self.parallel)]
113 else:
114 build_args += ["-j2"]
115
116 if not os.path.exists(self.build_temp):
117 os.makedirs(self.build_temp)
118
119 if not (PNNX_WHEEL_WITHOUT_BUILD == "ON"):
120 subprocess.check_call(
121 ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
122 )
123 subprocess.check_call(
124 ["cmake", "--build", "."] + build_args, cwd=self.build_temp
125 )
126 else:
127 pass
128
129if sys.version_info < (3, 0):
130 sys.exit("Sorry, Python < 3.0 is not supported")
131
132requirements = ["torch"]
133
134with io.open("README.md", encoding="utf-8") as h:
135 long_description = h.read()
136
137setup(
138 name="pnnx",
139 version=set_version(),
140 author="nihui",
141 author_email="nihuini@tencent.com",
142 description="pnnx is an open standard for PyTorch model interoperability.",
143 long_description=long_description,
144 long_description_content_type="text/markdown",
145 url="https://github.com/Tencent/ncnn/tree/master/tools/pnnx",
146 classifiers=[
147 "Programming Language :: C++",
148 "Programming Language :: Python :: 3",
149 "Programming Language :: Python :: 3.7",
150 "Programming Language :: Python :: 3.8",
151 "Programming Language :: Python :: 3.9",
152 "Programming Language :: Python :: 3.10",
153 "Programming Language :: Python :: 3.11",
154 "License :: OSI Approved :: BSD License",
155 "Operating System :: OS Independent",
156 ],
157 license="BSD-3",
158 python_requires=">=3.7",
159 packages=find_packages(),
160 package_data={"pnnx": ["pnnx", "pnnx.exe"]},
161 package_dir={"": "."},
162 install_requires=requirements,
163 ext_modules=None if PNNX_WHEEL_WITHOUT_BUILD == 'ON' else [CMakeExtension("pnnx", "..")],
164 cmdclass={"build_ext": CMakeBuild},
165 entry_points={"console_scripts": ["pnnx=pnnx:pnnx"]},
166)