-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
100 lines (86 loc) · 3.54 KB
/
setup.py
File metadata and controls
100 lines (86 loc) · 3.54 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
import multiprocessing
import os
import platform
import subprocess
import sys
import shutil
from setuptools import find_packages
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.command.clean import clean as clean_ori
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
# don't invoke the original build_ext for this special extension
super().__init__(name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class clean(clean_ori):
def run(self):
import glob
import re
with open('.gitignore', 'r') as f:
ignores = f.read()
pat = re.compile(r'^#( BEGIN NOT-CLEAN-FILES )?')
for wildcard in filter(None, ignores.split('\n')):
match = pat.match(wildcard)
if match:
if match.group(1):
# Marker is found and stop reading .gitignore.
break
# Ignore lines which begin with '#'.
else:
for filename in glob.glob(wildcard):
try:
os.remove(filename)
except OSError:
shutil.rmtree(filename, ignore_errors=True)
class CMakeBuild(build_ext):
def run(self):
try:
_ = subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(ext.name for ext in self.extensions))
try:
import torch
except ImportError:
sys.stderr.write("Pytorch is required to build this package\n")
sys.exit(-1)
self.pytorch_dir = os.path.dirname(torch.__file__)
self.python_exe = subprocess.check_output(["which", "python"]).decode().strip()
for ext in self.extensions:
self.build_cmake(ext)
def build_cmake(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
"-DCMAKE_PREFIX_PATH={}".format(self.pytorch_dir),
# "-DPYTHON_EXECUTABLE:FILEPATH={}".format(self.python_exe),
# "-DPYTHON_EXECUTABLE=".format(sys.executable),
]
config = "Debug" if self.debug else "Release"
build_args = ["--config", config]
if platform.system() == "Darwin":
cmake_args += ["-DCMAKE_OSX_DEPLOYMENT_TARGET=10.9"]
if platform.system() == "Windows":
cmake_args += ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(config.upper(), extdir)]
if sys.maxsize > 2 ** 32:
cmake_args += ["-A", "x64"]
build_args += ["--", "/m"]
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + config]
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
cwd = os.getcwd()
os.chdir(os.path.dirname(extdir))
self.spawn(["cmake", ext.sourcedir] + cmake_args)
if not self.dry_run:
self.spawn(["cmake", "--build", ".", "--", "-j{}".format(multiprocessing.cpu_count())])
os.chdir(cwd)
setup(
name="sparse_extension",
version="0.0.1",
packages=["sparse_turbo"],
license="MIT",
ext_modules=[CMakeExtension("_C")],
cmdclass={"build_ext": CMakeBuild,
"clean": clean}
)