1313PY2 = sys .version_info [0 ] == 2
1414
1515
16+ # determine CPU support for SSE2 and AVX2
17+ cpu_info = cpuinfo .get_cpu_info ()
18+ have_sse2 = 'sse2' in cpu_info ['flags' ]
19+ have_avx2 = 'avx2' in cpu_info ['flags' ]
20+ disable_sse2 = 'DISABLE_NUMCODECS_SSE2' in os .environ
21+ disable_avx2 = 'DISABLE_NUMCODECS_AVX2' in os .environ
22+
23+
24+ # setup common compile arguments
25+ have_cflags = 'CFLAGS' in os .environ
26+ base_compile_args = list ()
27+ if have_cflags :
28+ # respect compiler options set by user
29+ pass
30+ elif os .name == 'posix' :
31+ if disable_sse2 :
32+ base_compile_args .append ('-mno-sse2' )
33+ elif have_sse2 :
34+ base_compile_args .append ('-msse2' )
35+ if disable_avx2 :
36+ base_compile_args .append ('-mno-avx2' )
37+ elif have_avx2 :
38+ base_compile_args .append ('-mavx2' )
39+ # workaround lack of support for "inline" in MSVC when building for Python 2.7 64-bit
40+ if PY2 and os .name == 'nt' :
41+ base_compile_args .append ('-Dinline=__inline' )
42+
43+
1644try :
1745 from Cython .Build import cythonize
1846except ImportError :
@@ -34,23 +62,22 @@ def error(*msg):
3462def blosc_extension ():
3563 info ('setting up Blosc extension' )
3664
37- # setup blosc extension
38- blosc_sources = []
39- extra_compile_args = []
40- include_dirs = []
65+ extra_compile_args = list (base_compile_args )
4166 define_macros = []
4267
43- # generic setup
44- blosc_sources += [f for f in glob ('c-blosc/blosc/*.c' )
45- if 'avx2' not in f and 'sse2' not in f ]
68+ # setup blosc sources
69+ blosc_sources = [f for f in glob ('c-blosc/blosc/*.c' )
70+ if 'avx2' not in f and 'sse2' not in f ]
71+ include_dirs = [os .path .join ('c-blosc' , 'blosc' )]
72+
73+ # add internal complibs
4674 blosc_sources += glob ('c-blosc/internal-complibs/lz4*/*.c' )
4775 blosc_sources += glob ('c-blosc/internal-complibs/snappy*/*.cc' )
4876 blosc_sources += glob ('c-blosc/internal-complibs/zlib*/*.c' )
4977 blosc_sources += glob ('c-blosc/internal-complibs/zstd*/common/*.c' )
5078 blosc_sources += glob ('c-blosc/internal-complibs/zstd*/compress/*.c' )
5179 blosc_sources += glob ('c-blosc/internal-complibs/zstd*/decompress/*.c' )
5280 blosc_sources += glob ('c-blosc/internal-complibs/zstd*/dictBuilder/*.c' )
53- include_dirs += [os .path .join ('c-blosc' , 'blosc' )]
5481 include_dirs += [d for d in glob ('c-blosc/internal-complibs/*' )
5582 if os .path .isdir (d )]
5683 include_dirs += [d for d in glob ('c-blosc/internal-complibs/*/*' )
@@ -61,33 +88,25 @@ def blosc_extension():
6188 ('HAVE_ZSTD' , 1 )]
6289 # define_macros += [('CYTHON_TRACE', '1')]
6390
64- # determine CPU support for SSE2 and AVX2
65- cpu_info = cpuinfo .get_cpu_info ()
66-
6791 # SSE2
68- if 'sse2' in cpu_info [ 'flags' ] and 'DISABLE_NUMCODECS_SSE2' not in os . environ :
69- info ('building with SSE2 support' )
92+ if have_sse2 and not disable_sse2 :
93+ info ('compiling Blosc extension with SSE2 support' )
7094 extra_compile_args .append ('-DSHUFFLE_SSE2_ENABLED' )
7195 blosc_sources += [f for f in glob ('c-blosc/blosc/*.c' ) if 'sse2' in f ]
72- if os .name == 'posix' :
73- extra_compile_args .append ('-msse2' )
74- elif os .name == 'nt' :
96+ if os .name == 'nt' :
7597 define_macros += [('__SSE2__' , 1 )]
98+ else :
99+ info ('compiling Blosc extension without SSE2 support' )
76100
77101 # AVX2
78- if 'avx2' in cpu_info [ 'flags' ] and 'DISABLE_NUMCODECS_AVX2' not in os . environ :
79- info ('building with AVX2 support' )
102+ if have_avx2 and not disable_avx2 :
103+ info ('compiling Blosc extension with AVX2 support' )
80104 extra_compile_args .append ('-DSHUFFLE_AVX2_ENABLED' )
81105 blosc_sources += [f for f in glob ('c-blosc/blosc/*.c' ) if 'avx2' in f ]
82- if os .name == 'posix' :
83- extra_compile_args .append ('-mavx2' )
84- elif os .name == 'nt' :
106+ if os .name == 'nt' :
85107 define_macros += [('__AVX2__' , 1 )]
86-
87- # workaround lack of support for "inline" in MSVC when building for Python
88- # 2.7 64-bit
89- if PY2 and os .name == 'nt' :
90- extra_compile_args .append ('-Dinline=__inline' )
108+ else :
109+ info ('compiling Blosc extension without AVX2 support' )
91110
92111 if have_cython :
93112 sources = ['numcodecs/blosc.pyx' ]
@@ -114,7 +133,7 @@ def zstd_extension():
114133 info ('setting up Zstandard extension' )
115134
116135 zstd_sources = []
117- extra_compile_args = []
136+ extra_compile_args = list ( base_compile_args )
118137 include_dirs = []
119138 define_macros = []
120139
@@ -123,18 +142,12 @@ def zstd_extension():
123142 zstd_sources += glob ('c-blosc/internal-complibs/zstd*/compress/*.c' )
124143 zstd_sources += glob ('c-blosc/internal-complibs/zstd*/decompress/*.c' )
125144 zstd_sources += glob ('c-blosc/internal-complibs/zstd*/dictBuilder/*.c' )
126-
127145 include_dirs += [d for d in glob ('c-blosc/internal-complibs/zstd*' )
128146 if os .path .isdir (d )]
129147 include_dirs += [d for d in glob ('c-blosc/internal-complibs/zstd*/*' )
130148 if os .path .isdir (d )]
131149 # define_macros += [('CYTHON_TRACE', '1')]
132150
133- # workaround lack of support for "inline" in MSVC when building for Python
134- # 2.7 64-bit
135- if PY2 and os .name == 'nt' :
136- extra_compile_args .append ('-Dinline=__inline' )
137-
138151 if have_cython :
139152 sources = ['numcodecs/zstd.pyx' ]
140153 else :
@@ -159,7 +172,7 @@ def zstd_extension():
159172def lz4_extension ():
160173 info ('setting up LZ4 extension' )
161174
162- extra_compile_args = []
175+ extra_compile_args = list ( base_compile_args )
163176 define_macros = []
164177
165178 # setup sources - use LZ4 bundled in blosc
@@ -168,11 +181,6 @@ def lz4_extension():
168181 include_dirs += ['numcodecs' ]
169182 # define_macros += [('CYTHON_TRACE', '1')]
170183
171- # workaround lack of support for "inline" in MSVC when building for Python
172- # 2.7 64-bit
173- if PY2 and os .name == 'nt' :
174- extra_compile_args .append ('-Dinline=__inline' )
175-
176184 if have_cython :
177185 sources = ['numcodecs/lz4.pyx' ]
178186 else :
@@ -197,14 +205,18 @@ def lz4_extension():
197205def compat_extension ():
198206 info ('setting up compat extension' )
199207
208+ extra_compile_args = list (base_compile_args )
209+
200210 if have_cython :
201211 sources = ['numcodecs/compat_ext.pyx' ]
202212 else :
203213 sources = ['numcodecs/compat_ext.c' ]
204214
205215 # define extension module
206216 extensions = [
207- Extension ('numcodecs.compat_ext' , sources = sources ),
217+ Extension ('numcodecs.compat_ext' ,
218+ sources = sources ,
219+ extra_compile_args = extra_compile_args ),
208220 ]
209221
210222 if have_cython :
0 commit comments