-
Notifications
You must be signed in to change notification settings - Fork 23
/
setup.py
327 lines (284 loc) · 11 KB
/
setup.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from distutils.command.build_ext import build_ext
from distutils import sysconfig
#from distutils.core import setup
from setuptools import setup, Extension
import os, sys
from os import chdir, getcwd
from os.path import abspath, dirname, split
import shlex, glob
from subprocess import check_output
import re
import argparse
if sys.platform == 'win32' and sys.version_info[0] < 3:
def samefile(path1, path2):
return (os.path.normcase(os.path.normpath(path1)) ==
os.path.normcase(os.path.normpath(path2)))
else:
from os.path import samefile
# Pre-parse and remove the '--target' argument from sys.argv since we
# need it here already.
parser = argparse.ArgumentParser()
parser.add_argument('--target', default='all')
parser.add_argument('-j', default=None)
values, rest = parser.parse_known_args()
DIST_TARGET = values.target
PARALLEL = values.j
sys.argv = sys.argv[:1] + rest
#
# DyND is a namespace package that contains the type module dynd.ndt and
# the array/callables module dynd.nd. These are the supported install
# methods together with the resulting directory hierarchies:
#
# 1) DIST_TARGET = 'all':
# dynd-x.y.z/dynd/ndt/*
# dynd-x.y.z/dynd/nd/*
#
# 2) DIST_TARGET = 'ndt':
# dynd.ndt-x.y.z/dynd/ndt/*
#
# 3) DIST_TARGET = 'nd':
# dynd.nd-x.y.z/dynd/nd/*
#
# In this case also add dynd.ndt to requirements.txt.
#
# A single Python install should use either option 1) OR option 2) OR
# option 2) followed by option 3). All options require a clean build
# directory. At the minimum build/, dist/ and dynd.egg* must be removed.
#
class Target():
def __init__(self, target):
if target not in ['ndt', 'nd', 'all']:
raise ValueError("unknown distribution target: '%s'" % target)
self.target = target
def get_name(self):
if self.target == 'ndt':
return 'dynd.ndt'
elif self.target == 'nd':
return 'dynd.nd'
else:
return 'dynd'
def get_packages(self):
ndt = {'dynd', 'dynd.common', 'dynd.ndt', 'dynd.ndt.test'}
nd = {'dynd', 'dynd.nd', 'dynd.nd.test'}
if self.target == 'ndt':
return list(ndt)
elif self.target == 'nd':
return list(nd)
else:
return list(ndt | nd)
def get_package_data(self):
ndt = {'*.pxd', 'ndt/*.pxd', 'include/*.hpp', 'cpp/*.pxd',
'cpp/types/*.pxd'}
nd = {'*.pxd', 'nd/*.pxd', 'include/*.hpp', 'include/kernels/*.hpp',
'cpp/*.pxd', 'cpp/eval/*.pxd', 'cpp/func/*.pxd'}
if self.target == 'ndt':
return {'dynd.ndt': list(ndt)}
elif self.target == 'nd':
return {'dynd.nd': list(nd)}
else:
return {'dynd': list(ndt | nd)}
def get_extension_names(self):
ndt = ['config', os.path.join('ndt', 'type'),
os.path.join('ndt', 'json')]
nd = [os.path.join('nd', 'array'), os.path.join('nd', 'callable'),
os.path.join('nd', 'functional'), os.path.join('nd', 'registry')]
if self.target == 'ndt':
return ndt
elif self.target == 'nd':
return nd
else:
return ndt + nd
def get_library_names(self, build_type):
if sys.platform != 'win32':
ndt = glob.glob('libdynd/libdyndt.*')
nd = glob.glob('libdynd/libdynd.*')
else:
ndt = glob.glob('libdynd/%s/libdyndt.*' % build_type)
nd = glob.glob('libdynd/%s/libdynd.*' % build_type)
if self.target == 'ndt':
return ndt
elif self.target == 'nd':
return nd
else:
return ndt + nd
def get_requirements(self):
reqs = open('dev-requirements.txt').read().strip().split('\n')
if self.target == 'nd':
reqs.append('dynd.ndt')
return reqs
target = Target(DIST_TARGET)
# Check if we're running 64-bit Python
is_64_bit = sys.maxsize > 2**32
# Check if this is a debug build of Python.
if hasattr(sys, 'gettotalrefcount'):
build_type = 'Debug'
else:
build_type = 'Release'
class cmake_build_ext(build_ext):
description = "Build the C-extension for dynd-python with CMake"
user_options = [('extra-cmake-args=', None, 'extra arguments for CMake')]
def get_ext_path(self, name):
# Get the package directory from build_py
build_py = self.get_finalized_command('build_py')
package_dir = build_py.get_package_dir('dynd')
# This is the name of the dynd-python C-extension
suffix = sysconfig.get_config_var('EXT_SUFFIX')
if (suffix is None):
suffix = sysconfig.get_config_var('SO')
filename = name + suffix
return os.path.join(package_dir, filename)
def get_ext_built(self, name):
suffix = sysconfig.get_config_var('SO')
return os.path.join('dynd', *os.path.split(name)) + suffix
def initialize_options(self):
build_ext.initialize_options(self)
self.extra_cmake_args = ''
def run(self):
global build_type
# We don't call the origin build_ext, instead ignore that
# default behavior and call cmake for DyND's one C-extension.
# The directory containing this setup.py
source = dirname(abspath(__file__))
# The staging directory for the module being built
if self.inplace:
# In `python setup.py develop` mode, always build C++ code
# in the 'build-dev' dir.
build_temp = os.path.join(os.getcwd(), 'build-dev')
else:
build_temp = os.path.join(os.getcwd(), self.build_temp)
build_lib = os.path.join(os.getcwd(), self.build_lib)
# Change to the build directory
saved_cwd = getcwd()
if not os.path.isdir(build_temp):
self.mkpath(build_temp)
chdir(build_temp)
# Detect if we built elsewhere
if os.path.isfile('CMakeCache.txt'):
cachefile = open('CMakeCache.txt', 'r')
cachedir = re.search('CMAKE_CACHEFILE_DIR:INTERNAL=(.*)', cachefile.read()).group(1)
cachefile.close()
if not samefile(cachedir, build_temp):
return
pyexe_option = '-DPYTHON_EXECUTABLE=%s' % sys.executable
install_lib_option = '-DDYND_INSTALL_LIB=ON'
static_lib_option = ''
build_tests_option = ''
inplace_build_option = ''
# If libdynd is checked out into the libdynd subdir,
# we want to build libdynd as part of dynd-python, not
# separately like the default does.
libdynd_in_tree = os.path.isfile(
os.path.join(source, 'libdynd/include/dynd/array.hpp'))
if libdynd_in_tree:
install_lib_option = '-DDYND_INSTALL_LIB=OFF'
build_tests_option = '-DDYND_BUILD_TESTS=OFF'
# If the build is done inplace, require libdynd be included in the build
if self.inplace:
if not libdynd_in_tree:
raise RuntimeError('For an in-tree build with'
' "python setup.py develop",'
' libdynd must be checked out in the'
' dynd-python subdirectory')
# Definitely want the tests in 'develop' mode
build_tests_option = '-DDYND_BUILD_TESTS=ON'
# This option causes the cmake config to copy the binaries into the
# tree every time they are built
inplace_build_option = '-DDYND_PYTHON_INPLACE_BUILD=ON'
# Enable debug info
if build_type == 'Release':
build_type = 'RelWithDebInfo'
extra_cmake_args = shlex.split(self.extra_cmake_args)
cmake_command = ['cmake'] + extra_cmake_args + [pyexe_option,
install_lib_option, build_tests_option,
inplace_build_option, static_lib_option]
if sys.platform != 'win32':
cmake_command.append(source)
self.spawn(cmake_command)
if PARALLEL:
self.spawn(['make', '-j%d' % int(PARALLEL)])
else:
self.spawn(['make'])
else:
if "-G" not in self.extra_cmake_args:
cmake_generator = 'Visual Studio 14 2015'
if is_64_bit:
cmake_generator += ' Win64'
cmake_command += ['-G', cmake_generator]
cmake_command.append(source)
self.spawn(cmake_command)
# Do the build
self.spawn(['cmake', '--build', '.', '--config', build_type])
import shutil
if not self.inplace:
if install_lib_option.split('=')[1] == 'OFF':
for name in target.get_library_names(build_type):
short_name = split(name)[1]
shutil.move(name, os.path.join(build_lib, 'dynd', short_name))
# Move the built C-extension to the place expected by the Python build
self._found_names = []
for name in target.get_extension_names():
built_path = self.get_ext_built(name)
print(os.getcwd())
if os.path.exists(built_path):
ext_path = os.path.join(build_lib, self.get_ext_path(name))
if os.path.exists(ext_path):
os.remove(ext_path)
self.mkpath(os.path.dirname(ext_path))
print('Moving built DyND C-extension', built_path,
'to build path', ext_path)
shutil.move(self.get_ext_built(name), ext_path)
self._found_names.append(name)
else:
raise RuntimeError('DyND C-extension failed to build:',
os.path.abspath(built_path))
chdir(saved_cwd)
def get_names(self):
return self._found_names
def get_outputs(self):
# Just the C extensions
return [self.get_ext_path(name) for name in self.get_names()]
# Get the version number to use from git
ver = check_output(['git', 'describe', '--dirty',
'--always', '--match', 'v*']).decode('ascii').strip('\n')
# Same processing as in __init__.py
if '.' in ver:
vlst = ver.lstrip('v').split('.')
vlst = vlst[:-1] + vlst[-1].split('-')
if len(vlst) > 3:
# The 4th one may not be, so trap it
try:
# Zero pad the dev version #, so it sorts lexicographically
vlst[3] = 'dev%03d' % int(vlst[3])
# increment the third version number, so
# the '.dev##' versioning convention works
vlst[2] = str(int(vlst[2]) + 1)
except ValueError:
pass
ver = '.'.join(vlst[:4])
# Can't use the local version on PyPI, so just exclude this part
# + '+' + '.'.join(vlst[4:])
else:
ver = '.'.join(vlst)
setup(
name = target.get_name(),
description = 'Python exposure of DyND',
version = ver,
author = 'DyND Developers',
author_email = 'libdynd-dev@googlegroups.com',
license = 'BSD',
url = 'http://libdynd.org',
packages = target.get_packages(),
package_data = target.get_package_data(),
# build_ext is overridden to call cmake, the Extension is just
# needed so things like bdist_wheel understand what's going on.
ext_modules = [Extension(target.get_name(), sources=[])],
# This includes both build and install requirements. Setuptools' setup_requires
# option does not actually install things, so isn't actually helpful...
install_requires = target.get_requirements(),
classifiers = [
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
],
cmdclass = {'build_ext': cmake_build_ext},
)