forked from Blosc/python-blosc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
323 lines (277 loc) · 12.1 KB
/
Copy pathsetup.py
File metadata and controls
323 lines (277 loc) · 12.1 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
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
# -*- coding: utf-8 -*-
########################################################################
#
# License: BSD 3-clause
# Created: September 22, 2010
# Author: Francesc Alted - [email protected]
#
########################################################################
# flake8: noqa
from __future__ import print_function
import os
import platform
import re
import sys
import io
from setuptools import Extension
from setuptools import setup
from glob import glob
from distutils.version import LooseVersion
from distutils.command.build_ext import build_ext
from distutils.errors import CompileError
from textwrap import dedent
class BloscExtension(Extension):
"""Allows extension to carry architecture-capable flag options.
Attributes:
avx2_def (Dict[str]: List[str]):
AVX2 support dictionary mapping Extension properties to a
list of values. If compiler is AVX2 capable, then these will
be appended onto the end of the Extension properties.
"""
def __init__(self, *args, **kwargs):
self.avx2_defs = kwargs.pop("avx2_defs", {})
Extension.__init__(self, *args, **kwargs)
class build_ext_posix_avx2(build_ext):
"""build_ext customized to test for AVX2 support in posix compiler.
This is because until distutils has actually started the build
process, we can't be certain what compiler is being used.
If compiler supports, then the avx2_defs dictionary on any given
Extension will be used to extend the other Extension attributes.
"""
def _test_compiler_flags(self, name, flags):
# type: (List[str]) -> Bool
"""Test that a sample program can compile with given flags.
Attr:
flags (List[str]): the flags to test
name (str): An identifier-like name to cache the results as
Returns:
(bool): Whether the compiler accepted the flags(s)
"""
# Look to see if we have a written file to cache the result
success_file = os.path.join(self.build_temp, "_{}_present".format(name))
fail_file = os.path.join(self.build_temp, "_{}_failed".format(name))
if os.path.isfile(success_file):
return True
elif os.path.isfile(fail_file):
return False
# No cache file, try to run the compile
try:
# Write an empty test file
test_file = os.path.join(self.build_temp, "test_{}_empty.c".format(name))
if not os.path.isfile(test_file):
open(test_file, "w").close()
objects = self.compiler.compile(
[test_file], output_dir=self.build_temp, extra_postargs=flags
)
# Write a success marker so we don't need to compile again
open(success_file, 'w').close()
return True
except CompileError:
# Write a failure marker so we don't need to compile again
open(fail_file, 'w').close()
return False
finally:
pass
def build_extensions(self):
# Verify that the compiler supports requested extra flags
if self._test_compiler_flags("avx2", ["-mavx2"]):
# Apply the AVX2 properties to each extension
for extension in self.extensions:
if hasattr(extension, "avx2_defs"):
# Extend an existing attribute with the stored values
for attr, defs in extension.avx2_defs.items():
getattr(extension, attr).extend(defs)
else:
print("AVX2 Unsupported by compiler")
# Call up to the superclass to do the actual build
build_ext.build_extensions(self)
if __name__ == '__main__':
with io.open('README.rst', encoding='utf-8') as f:
long_description = f.read()
import cpuinfo
cpu_info = cpuinfo.get_cpu_info()
########### Check versions ##########
def exit_with_error(message):
print('ERROR: %s' % message)
sys.exit(1)
# Check for Python
if sys.version_info[0] == 2:
if sys.version_info[1] < 7:
exit_with_error("You need Python 2.7 or greater to install blosc!")
elif sys.version_info[0] == 3:
if sys.version_info[1] < 4:
exit_with_error("You need Python 3.4 or greater to install blosc!")
else:
exit_with_error("You need Python 2.7/3.4 or greater to install blosc!")
tests_require = ['numpy', 'psutil']
########### End of checks ##########
# Read the long_description from README.rst
with open('README.rst') as f:
long_description = f.read()
# Blosc version
VERSION = open('VERSION').read().strip()
# Create the version.py file
open('blosc/version.py', 'w').write('__version__ = "%s"\n' % VERSION)
# Global variables
CFLAGS = os.environ.get('CFLAGS', '').split()
LFLAGS = os.environ.get('LFLAGS', '').split()
# Allow setting the Blosc dir if installed in the system
BLOSC_DIR = os.environ.get('BLOSC_DIR', '')
# Check for USE_CODEC environment variables
try:
INCLUDE_LZ4 = os.environ['INCLUDE_LZ4'] == '1'
except KeyError:
INCLUDE_LZ4 = True
try:
INCLUDE_SNAPPY = os.environ['INCLUDE_SNAPPY'] == '1'
except KeyError:
INCLUDE_SNAPPY = False # Snappy is disabled by default
try:
INCLUDE_ZLIB = os.environ['INCLUDE_ZLIB'] == '1'
except KeyError:
INCLUDE_ZLIB = True
try:
INCLUDE_ZSTD = os.environ['INCLUDE_ZSTD'] == '1'
except KeyError:
INCLUDE_ZSTD = True
# Handle --blosc=[PATH] --lflags=[FLAGS] --cflags=[FLAGS]
args = sys.argv[:]
for arg in args:
if arg.find('--blosc=') == 0:
BLOSC_DIR = os.path.expanduser(arg.split('=')[1])
sys.argv.remove(arg)
if arg.find('--lflags=') == 0:
LFLAGS = arg.split('=')[1].split()
sys.argv.remove(arg)
if arg.find('--cflags=') == 0:
CFLAGS = arg.split('=')[1].split()
sys.argv.remove(arg)
# Blosc sources and headers
# To avoid potential namespace collisions use build_clib.py for each codec
# instead of co-compiling all sources files in one setuptools.Extension object.
clibs = [] # for build_clib, libraries TO BE BUILT
# Below are parameters for the Extension object
sources = ["blosc/blosc_extension.c"]
inc_dirs = []
lib_dirs = []
libs = [] # Pre-built libraries ONLY, like python36.so
def_macros = []
builder_class = build_ext # To swap out if we have AVX capability and posix
avx2_defs = {} # Definitions to build extension with if compiler supports AVX2
if BLOSC_DIR != '':
# Using the Blosc library
lib_dirs += [os.path.join(BLOSC_DIR, 'lib')]
inc_dirs += [os.path.join(BLOSC_DIR, 'include')]
libs += ['blosc']
else:
# Configure the Extension
# Compiling everything from included C-Blosc sources
sources += [f for f in glob('c-blosc/blosc/*.c')
if 'avx2' not in f and 'sse2' not in f]
inc_dirs += [os.path.join('c-blosc', 'blosc')]
inc_dirs += glob('c-blosc/internal-complibs/*')
# Codecs to be built with build_clib
if INCLUDE_LZ4:
clibs.append( ('lz4', {'sources': glob('c-blosc/internal-complibs/lz4*/*.c')} ) )
inc_dirs += glob('c-blosc/internal-complibs/lz4*')
def_macros += [('HAVE_LZ4',1)]
# Tried and failed to compile Snappy with gcc using 'cflags' on posix
# setuptools always uses gcc instead of g++, as it only checks for the
# env var 'CC' and not 'CXX'.
if INCLUDE_SNAPPY:
clibs.append( ('snappy', {'sources': glob('c-blosc/internal-complibs/snappy*/*.cc'),
'cflags': ['-std=c++11', '-lstdc++'] } ) )
inc_dirs += glob('c-blosc/internal-complibs/snappy*')
def_macros += [('HAVE_SNAPPY',1)]
if INCLUDE_ZLIB:
clibs.append( ('zlib', {'sources': glob('c-blosc/internal-complibs/zlib*/*.c')} ) )
def_macros += [('HAVE_ZLIB',1)]
if INCLUDE_ZSTD:
clibs.append( ('zstd', {'sources': glob('c-blosc/internal-complibs/zstd*/*/*.c'),
'include_dirs': glob('c-blosc/internal-complibs/zstd*') + glob('c-blosc/internal-complibs/zstd*/common') } ) )
inc_dirs += glob('c-blosc/internal-complibs/zstd*/common')
inc_dirs += glob('c-blosc/internal-complibs/zstd*')
def_macros += [('HAVE_ZSTD',1)]
# Guess SSE2 or AVX2 capabilities
# SSE2
if 'DISABLE_BLOSC_SSE2' not in os.environ and (cpu_info != None) and ('sse2' in cpu_info['flags']):
print('SSE2 detected')
CFLAGS.append('-DSHUFFLE_SSE2_ENABLED')
sources += [f for f in glob('c-blosc/blosc/*.c') if 'sse2' in f]
if os.name == 'posix':
CFLAGS.append('-msse2')
elif os.name == 'nt':
def_macros += [('__SSE2__', 1)]
# AVX2
if 'DISABLE_BLOSC_AVX2' not in os.environ and (cpu_info != None) and ('avx2' in cpu_info['flags']):
if os.name == 'posix':
print("AVX2 detected")
avx2_defs = {
"extra_compile_args": ["-DSHUFFLE_AVX2_ENABLED", "-mavx2"],
"sources": [f for f in glob("c-blosc/blosc/*.c") if "avx2" in f]
}
# The CPU supports it but the compiler might not..
builder_class = build_ext_posix_avx2
elif(os.name == 'nt' and
LooseVersion(platform.python_version()) >= LooseVersion('3.5.0')):
# Neither MSVC2008 for Python 2.7 or MSVC2010 for Python 3.4 have
# sufficient AVX2 support
# Since we don't rely on any special compiler capabilities,
# we don't need to rely on testing the compiler
print('AVX2 detected')
CFLAGS.append('-DSHUFFLE_AVX2_ENABLED')
sources += [f for f in glob('c-blosc/blosc/*.c') if 'avx2' in f]
def_macros += [('__AVX2__', 1)]
# TODO: AVX512
classifiers = dedent("""\
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
Intended Audience :: Information Technology
Intended Audience :: Science/Research
License :: OSI Approved :: BSD License
Programming Language :: Python
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Topic :: Software Development :: Libraries :: Python Modules
Topic :: System :: Archiving :: Compression
Operating System :: Microsoft :: Windows
Operating System :: Unix
""")
setup(name = "blosc",
version = VERSION,
description = 'Blosc data compressor',
long_description = long_description,
classifiers = [c for c in classifiers.split("\n") if c],
author = 'Francesc Alted, Valentin Haenel',
maintainer = 'Francesc Alted, Valentin Haenel',
url = 'http://github.com/blosc/python-blosc',
license = 'https://opensource.org/licenses/BSD-3-Clause',
platforms = ['any'],
libraries = clibs,
ext_modules = [
BloscExtension( "blosc.blosc_extension",
include_dirs=inc_dirs,
define_macros=def_macros,
sources=sources,
library_dirs=lib_dirs,
libraries=libs,
extra_link_args=LFLAGS,
extra_compile_args=CFLAGS,
avx2_defs=avx2_defs
),
],
tests_require=tests_require,
zip_safe=False,
packages = ['blosc'],
cmdclass={'build_ext': builder_class},
)
elif __name__ == '__mp_main__':
# This occurs from `cpuinfo 4.0.0` using multiprocessing to interrogate the
# CPUID flags
# https://github.com/workhorsy/py-cpuinfo/issues/108
pass