-
Notifications
You must be signed in to change notification settings - Fork 88
/
setup.py
385 lines (300 loc) · 10.8 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import os
import sys
from distutils import ccompiler
from distutils.command.clean import clean
from distutils.sysconfig import customize_compiler
from glob import glob
import cpuinfo
from Cython.Distutils.build_ext import new_build_ext as build_ext
from setuptools import Extension, setup
from setuptools.errors import CCompilerError, ExecError, PlatformError
# determine CPU support for SSE2 and AVX2
cpu_info = cpuinfo.get_cpu_info()
flags = cpu_info.get('flags', [])
have_sse2 = 'sse2' in flags
have_avx2 = 'avx2' in flags
disable_sse2 = 'DISABLE_NUMCODECS_SSE2' in os.environ
disable_avx2 = 'DISABLE_NUMCODECS_AVX2' in os.environ
# setup common compile arguments
have_cflags = 'CFLAGS' in os.environ
base_compile_args = []
if have_cflags:
# respect compiler options set by user
pass
elif os.name == 'posix':
if disable_sse2:
base_compile_args.append('-mno-sse2')
elif have_sse2:
base_compile_args.append('-msse2')
if disable_avx2:
base_compile_args.append('-mno-avx2')
elif have_avx2:
base_compile_args.append('-mavx2')
# On macOS, force libc++ in case the system tries to use `stdlibc++`.
# The latter is often absent from modern macOS systems.
if sys.platform == 'darwin':
base_compile_args.append('-stdlib=libc++')
def info(*msg):
kwargs = dict(file=sys.stdout)
print('[numcodecs]', *msg, **kwargs)
def error(*msg):
kwargs = dict(file=sys.stderr)
print('[numcodecs]', *msg, **kwargs)
def blosc_extension():
info('setting up Blosc extension')
extra_compile_args = base_compile_args.copy()
define_macros = []
# setup blosc sources
blosc_sources = [f for f in glob('c-blosc/blosc/*.c') if 'avx2' not in f and 'sse2' not in f]
include_dirs = [os.path.join('c-blosc', 'blosc')]
# add internal complibs
blosc_sources += glob('c-blosc/internal-complibs/lz4*/*.c')
blosc_sources += glob('c-blosc/internal-complibs/snappy*/*.cc')
blosc_sources += glob('c-blosc/internal-complibs/zlib*/*.c')
blosc_sources += glob('c-blosc/internal-complibs/zstd*/common/*.c')
blosc_sources += glob('c-blosc/internal-complibs/zstd*/compress/*.c')
blosc_sources += glob('c-blosc/internal-complibs/zstd*/decompress/*.c')
blosc_sources += glob('c-blosc/internal-complibs/zstd*/dictBuilder/*.c')
include_dirs += [d for d in glob('c-blosc/internal-complibs/*') if os.path.isdir(d)]
include_dirs += [d for d in glob('c-blosc/internal-complibs/*/*') if os.path.isdir(d)]
include_dirs += [d for d in glob('c-blosc/internal-complibs/*/*/*') if os.path.isdir(d)]
# remove minizip because Python.h 3.8 tries to include crypt.h
include_dirs = [d for d in include_dirs if 'minizip' not in d]
define_macros += [
('HAVE_LZ4', 1),
# ('HAVE_SNAPPY', 1),
('HAVE_ZLIB', 1),
('HAVE_ZSTD', 1),
]
# define_macros += [('CYTHON_TRACE', '1')]
# SSE2
if have_sse2 and not disable_sse2:
info('compiling Blosc extension with SSE2 support')
extra_compile_args.append('-DSHUFFLE_SSE2_ENABLED')
blosc_sources += [f for f in glob('c-blosc/blosc/*.c') if 'sse2' in f]
if os.name == 'nt':
define_macros += [('__SSE2__', 1)]
else:
info('compiling Blosc extension without SSE2 support')
# AVX2
if have_avx2 and not disable_avx2:
info('compiling Blosc extension with AVX2 support')
extra_compile_args.append('-DSHUFFLE_AVX2_ENABLED')
blosc_sources += [f for f in glob('c-blosc/blosc/*.c') if 'avx2' in f]
if os.name == 'nt':
define_macros += [('__AVX2__', 1)]
else:
info('compiling Blosc extension without AVX2 support')
# include assembly files
if cpuinfo.platform.machine() == 'x86_64':
extra_objects = [
S[:-1] + 'o' for S in glob("c-blosc/internal-complibs/zstd*/decompress/*amd64.S")
]
else:
extra_objects = []
sources = ['numcodecs/blosc.pyx']
# define extension module
extensions = [
Extension(
'numcodecs.blosc',
sources=sources + blosc_sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
extra_objects=extra_objects,
),
]
return extensions
def zstd_extension():
info('setting up Zstandard extension')
zstd_sources = []
extra_compile_args = base_compile_args.copy()
include_dirs = []
define_macros = []
# setup sources - use zstd bundled in blosc
zstd_sources += glob('c-blosc/internal-complibs/zstd*/common/*.c')
zstd_sources += glob('c-blosc/internal-complibs/zstd*/compress/*.c')
zstd_sources += glob('c-blosc/internal-complibs/zstd*/decompress/*.c')
zstd_sources += glob('c-blosc/internal-complibs/zstd*/dictBuilder/*.c')
include_dirs += [d for d in glob('c-blosc/internal-complibs/zstd*') if os.path.isdir(d)]
include_dirs += [d for d in glob('c-blosc/internal-complibs/zstd*/*') if os.path.isdir(d)]
# define_macros += [('CYTHON_TRACE', '1')]
sources = ['numcodecs/zstd.pyx']
# include assembly files
if cpuinfo.platform.machine() == 'x86_64':
extra_objects = [
S[:-1] + 'o' for S in glob("c-blosc/internal-complibs/zstd*/decompress/*amd64.S")
]
else:
extra_objects = []
# define extension module
extensions = [
Extension(
'numcodecs.zstd',
sources=sources + zstd_sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
extra_objects=extra_objects,
),
]
return extensions
def lz4_extension():
info('setting up LZ4 extension')
extra_compile_args = base_compile_args.copy()
define_macros = []
# setup sources - use LZ4 bundled in blosc
lz4_sources = glob('c-blosc/internal-complibs/lz4*/*.c')
include_dirs = [d for d in glob('c-blosc/internal-complibs/lz4*') if os.path.isdir(d)]
include_dirs += ['numcodecs']
# define_macros += [('CYTHON_TRACE', '1')]
sources = ['numcodecs/lz4.pyx']
# define extension module
extensions = [
Extension(
'numcodecs.lz4',
sources=sources + lz4_sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
),
]
return extensions
def vlen_extension():
info('setting up vlen extension')
import numpy
extra_compile_args = base_compile_args.copy()
define_macros = []
# setup sources
include_dirs = ['numcodecs', numpy.get_include()]
# define_macros += [('CYTHON_TRACE', '1')]
sources = ['numcodecs/vlen.pyx']
# define extension module
extensions = [
Extension(
'numcodecs.vlen',
sources=sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
),
]
return extensions
def fletcher_extension():
info('setting up fletcher32 extension')
extra_compile_args = base_compile_args.copy()
define_macros = []
# setup sources
include_dirs = ['numcodecs']
# define_macros += [('CYTHON_TRACE', '1')]
sources = ['numcodecs/fletcher32.pyx']
# define extension module
extensions = [
Extension(
'numcodecs.fletcher32',
sources=sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
),
]
return extensions
def jenkins_extension():
info('setting up jenkins extension')
extra_compile_args = base_compile_args.copy()
define_macros = []
# setup sources
include_dirs = ['numcodecs']
define_macros += [('CYTHON_TRACE', '1')]
sources = ['numcodecs/jenkins.pyx']
# define extension module
extensions = [
Extension(
'numcodecs.jenkins',
sources=sources,
include_dirs=include_dirs,
define_macros=define_macros,
extra_compile_args=extra_compile_args,
),
]
return extensions
def compat_extension():
info('setting up compat extension')
extra_compile_args = base_compile_args.copy()
sources = ['numcodecs/compat_ext.pyx']
# define extension module
extensions = [
Extension(
'numcodecs.compat_ext',
sources=sources,
extra_compile_args=extra_compile_args,
),
]
return extensions
def shuffle_extension():
info('setting up shuffle extension')
extra_compile_args = base_compile_args.copy()
sources = ['numcodecs/_shuffle.pyx']
# define extension module
extensions = [
Extension('numcodecs._shuffle', sources=sources, extra_compile_args=extra_compile_args),
]
return extensions
if sys.platform == 'win32':
ext_errors = (CCompilerError, ExecError, PlatformError, IOError, ValueError)
else:
ext_errors = (CCompilerError, ExecError, PlatformError)
class BuildFailed(Exception):
pass
class ve_build_ext(build_ext):
# This class allows C extension building to fail.
def run(self):
try:
if cpuinfo.platform.machine() == 'x86_64':
S_files = glob('c-blosc/internal-complibs/zstd*/decompress/*amd64.S')
compiler = ccompiler.new_compiler()
customize_compiler(compiler)
compiler.src_extensions.append('.S')
compiler.compile(S_files)
build_ext.run(self)
except PlatformError as e:
error(e)
raise BuildFailed from e
def build_extension(self, ext):
try:
build_ext.build_extension(self, ext)
except ext_errors as e:
error(e)
raise BuildFailed from e
class Sclean(clean):
# Clean up .o files created by .S files
def run(self):
if cpuinfo.platform.machine() == 'x86_64':
o_files = glob('c-blosc/internal-complibs/zstd*/decompress/*amd64.o')
for f in o_files:
os.remove(f)
clean.run(self)
def run_setup(with_extensions):
if with_extensions:
ext_modules = (
blosc_extension()
+ zstd_extension()
+ lz4_extension()
+ compat_extension()
+ shuffle_extension()
+ vlen_extension()
+ fletcher_extension()
+ jenkins_extension()
)
cmdclass = dict(build_ext=ve_build_ext, clean=Sclean)
else:
ext_modules = []
cmdclass = {}
setup(
ext_modules=ext_modules,
cmdclass=cmdclass,
)
if __name__ == '__main__':
is_pypy = hasattr(sys, 'pypy_translation_info')
with_extensions = not is_pypy and 'DISABLE_NUMCODECS_CEXT' not in os.environ
run_setup(with_extensions)