setup.py 5.73 KB
Newer Older
ashawkey's avatar
init  
ashawkey committed
1
import os
ashawkey's avatar
ashawkey committed
2
3
4
import re
import subprocess
from pkg_resources import parse_version
ashawkey's avatar
init  
ashawkey committed
5
6
7
8
9
10
11
from setuptools import setup
from torch.utils.cpp_extension import BuildExtension, CUDAExtension

_src_path = os.path.dirname(os.path.abspath(__file__))

# ref: https://github.com/sxyu/sdf/blob/master/setup.py
def find_eigen(min_ver=(3, 3, 0)):
ashawkey's avatar
ashawkey committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	"""Helper to find or download the Eigen C++ library"""
	import re, os
	try_paths = [
		'/usr/include/eigen3',
		'/usr/local/include/eigen3',
		os.path.expanduser('~/.local/include/eigen3'),
		'C:/Program Files/eigen3',
		'C:/Program Files (x86)/eigen3',
	]
	WORLD_VER_STR = "#define EIGEN_WORLD_VERSION"
	MAJOR_VER_STR = "#define EIGEN_MAJOR_VERSION"
	MINOR_VER_STR = "#define EIGEN_MINOR_VERSION"
	EIGEN_WEB_URL = 'https://gitlab.com/libeigen/eigen/-/archive/3.3.7/eigen-3.3.7.tar.bz2'
	TMP_EIGEN_FILE = 'tmp_eigen.tar.bz2'
ashawkey's avatar
ashawkey committed
26
	TMP_EIGEN_DIR = './eigen3.3.7'
ashawkey's avatar
ashawkey committed
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	min_ver_str = '.'.join(map(str, min_ver))

	eigen_path = None
	for path in try_paths:
		macros_path = os.path.join(path, 'Eigen/src/Core/util/Macros.h')
		if os.path.exists(macros_path):
			macros = open(macros_path, 'r').read().split('\n')
			world_ver, major_ver, minor_ver = None, None, None
			for line in macros:
				if line.startswith(WORLD_VER_STR):
					world_ver = int(line[len(WORLD_VER_STR):])
				elif line.startswith(MAJOR_VER_STR):
					major_ver = int(line[len(MAJOR_VER_STR):])
				elif line.startswith(MINOR_VER_STR):
					minor_ver = int(line[len(MINOR_VER_STR):])
			if world_ver is None or major_ver is None or minor_ver is None:
				print('Failed to parse macros file', macros_path)
			else:
				ver = (world_ver, major_ver, minor_ver)
				ver_str = '.'.join(map(str, ver))
				if ver < min_ver:
					print('Found unsuitable Eigen version', ver_str, 'at',
						  path, '(need >= ' + min_ver_str + ')')
				else:
					print('Found Eigen version', ver_str, 'at', path)
					eigen_path = path
					break

	if eigen_path is None:
		try:
			import urllib.request
			print("Couldn't find Eigen locally, downloading...")
			req = urllib.request.Request(
				EIGEN_WEB_URL,
				data=None,
				headers={
					'User-Agent':
					'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
				})

			with urllib.request.urlopen(req) as resp,\
				  open(TMP_EIGEN_FILE, 'wb') as file:
				data = resp.read()
				file.write(data)
			import tarfile
			tar = tarfile.open(TMP_EIGEN_FILE)
			tar.extractall()
			tar.close()

			eigen_path = TMP_EIGEN_DIR
			os.remove(TMP_EIGEN_FILE)
		except:
			print('Download failed, failed to find Eigen')

	if eigen_path is not None:
		print('Found eigen at', eigen_path)

	return eigen_path


if os.name == "nt":

	# find cl.exe
	def find_cl_path():
		import glob
		for executable in ["Program Files (x86)", "Program Files"]:
			for edition in ["Enterprise", "Professional", "BuildTools", "Community"]:
				paths = sorted(glob.glob(f"C:\\{executable}\\Microsoft Visual Studio\\*\\{edition}\\VC\\Tools\\MSVC\\*\\bin\\Hostx64\\x64"), reverse=True)
				if paths:
					return paths[0]

	# If cl.exe is not on path, try to find it.
	if os.system("where cl.exe >nul 2>nul") != 0:
		cl_path = find_cl_path()
		if cl_path is None:
			raise RuntimeError("Could not locate a supported Microsoft Visual C++ installation")
		os.environ["PATH"] += ";" + cl_path
	else:
		# cl.exe was found in PATH, so we can assume that the user is already in a developer command prompt
		# In this case, BuildExtensions requires the following environment variable to be set such that it
		# won't try to activate a developer command prompt a second time.
		os.environ["DISTUTILS_USE_SDK"] = "1"

cpp_standard = 14

# Get CUDA version and make sure the targeted compute capability is compatible
if os.system("nvcc --version") == 0:
	nvcc_out = subprocess.check_output(["nvcc", "--version"]).decode()
	cuda_version = re.search(r"release (\S+),", nvcc_out)

	if cuda_version:
		cuda_version = parse_version(cuda_version.group(1))
		print(f"Detected CUDA version {cuda_version}")
		if cuda_version >= parse_version("11.0"):
			cpp_standard = 17

print(f"Targeting C++ standard {cpp_standard}")


base_nvcc_flags = [
	f"-std=c++{cpp_standard}",
	"--extended-lambda",
ashawkey's avatar
init  
ashawkey committed
129
	"--expt-relaxed-constexpr",
ashawkey's avatar
ashawkey committed
130
131
132
133
134
	# The following definitions must be undefined
	# since TCNN requires half-precision operation.
	"-U__CUDA_NO_HALF_OPERATORS__",
	"-U__CUDA_NO_HALF_CONVERSIONS__",
	"-U__CUDA_NO_HALF2_OPERATORS__",
ashawkey's avatar
init  
ashawkey committed
135
136
137
]

if os.name == "posix":
ashawkey's avatar
ashawkey committed
138
139
140
141
142
	base_cflags = [f"-std=c++{cpp_standard}"]
	base_nvcc_flags += [
		"-Xcompiler=-Wno-float-conversion",
		"-Xcompiler=-fno-strict-aliasing",
	]
ashawkey's avatar
init  
ashawkey committed
143
elif os.name == "nt":
ashawkey's avatar
ashawkey committed
144
	base_cflags = [f"/std:c++{cpp_standard}"]
ashawkey's avatar
init  
ashawkey committed
145
146
147
148
149
150
151
152
153
154

'''
Usage:
python setup.py build_ext --inplace # build extensions locally, do not install (only can be used from the parent directory)
python setup.py install # build extensions and install (copy) to PATH.
pip install . # ditto but better (e.g., dependency & metadata handling)
python setup.py develop # build extensions and install (symbolic) to PATH.
pip install -e . # ditto but better (e.g., dependency & metadata handling)
'''
setup(
ashawkey's avatar
ashawkey committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
	name='cubvh', # package name, import this to use python API
	version='0.1.0',
	description='CUDA BVH implementation',
	url='https://github.com/ashawkey/cubvh',
	author='kiui',
	author_email='ashawkey1999@gmail.com',
	packages=['cubvh'],
	ext_modules=[
		CUDAExtension(
			name='_cubvh', # extension name, import this to use CUDA API
			sources=[os.path.join(_src_path, 'src', f) for f in [
				'bvh.cu',
				'api.cu',
				'bindings.cpp',
			]],
			include_dirs=[
				os.path.join(_src_path, 'include'),
				find_eigen(),
			],
			extra_compile_args={
				'cxx': base_cflags,
				'nvcc': base_nvcc_flags,
			}
		),
	],
	cmdclass={
		'build_ext': BuildExtension,
	},
	install_requires=[
		'ninja',
		'pybind11',
		'trimesh',
		'torch',
		'numpy',
	],
190
)