generated from jamesnulliu/VSC-Python-Project-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
111 lines (93 loc) · 3.2 KB
/
setup.py
File metadata and controls
111 lines (93 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from pathlib import Path
import shutil
import sys
import os
import subprocess
from setuptools import find_namespace_packages, setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
from wheel.bdist_wheel import bdist_wheel
# Name of your package; Must match the directory name under `CSRC_DIR`:
PKG_NAME = "pmpp"
# Path to the directory of setup.py file:
SETUP_DIR = Path(__file__).parent.absolute()
# Where to create the cmake build directory:
BUILD_DIR = Path(SETUP_DIR, "build")
# Path to the c/c++ source directory:
CSRC_DIR = Path(SETUP_DIR, "csrc")
# Where to install the op library:
TORCH_OPS_DIR = Path(SETUP_DIR, PKG_NAME, "_torch_ops")
class CMakeExtension(Extension):
def __init__(self, name, source_dir, build_dir, install_dir):
Extension.__init__(self, name, sources=[])
# C/C++ source directory
self.source_dir = Path(source_dir).absolute()
# Build directory
self.build_dir = Path(build_dir).absolute()
# Lib installation directory
self.install_dir = Path(install_dir).absolute()
class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake must be installed")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext: CMakeExtension):
build_args = [
"-S",
ext.source_dir,
"-B",
ext.build_dir,
"Release",
]
# If Current Platform is Windows
if sys.platform == "win32":
subprocess.check_call(
[R"scripts\msvc-bash.bat", R"csrc\scripts\build.sh"]
+ build_args
+ ["--prune-env-path"]
)
else:
subprocess.check_call(["bash", "scripts/build.sh"] + build_args)
install_args = [
"--install",
ext.build_dir,
"--prefix",
ext.install_dir,
]
subprocess.check_call(["cmake"] + install_args)
class BuildPy(build_py):
def run(self):
self.run_command("build_ext")
super().run()
class BDistWheel(bdist_wheel):
def run(self):
self.run_command("build_py")
super().run()
dist_dir = Path("build", "dist")
dist_dir.mkdir(exist_ok=True)
wheel_dir = Path(self.dist_dir)
wheels = list(wheel_dir.glob("*.whl"))
if wheels:
wheel_file = wheels[0]
shutil.copy2(wheel_file, dist_dir / wheel_file.name)
# Command class
CMD_CLASS = {"build_ext": CMakeBuild, "build_py": BuildPy}
if os.environ.get("BDIST_WHEEL", None) in ["1", "true", "True", "ON", "on"]:
CMD_CLASS.update({"bdist_wheel": BDistWheel})
setup(
ext_modules=[
CMakeExtension(
name=f"{PKG_NAME}._torch_ops",
source_dir=CSRC_DIR,
build_dir=BUILD_DIR,
install_dir=TORCH_OPS_DIR,
)
],
cmdclass=CMD_CLASS,
packages=find_namespace_packages(where="."),
package_dir={"pmpp": "./pmpp"},
package_data={"pmpp": ["_torch_ops/lib/*.so", "_torch_ops/lib/*.dll"]},
)