88from setuptools .errors import CCompilerError , ExecError , PlatformError
99from distutils import ccompiler
1010from distutils .command .clean import clean
11+ import pkgconfig
1112
1213# determine CPU support for SSE2 and AVX2
1314cpu_info = cpuinfo .get_cpu_info ()
1617have_avx2 = 'avx2' in flags
1718disable_sse2 = 'DISABLE_NUMCODECS_SSE2' in os .environ
1819disable_avx2 = 'DISABLE_NUMCODECS_AVX2' in os .environ
20+ use_system_libraries = 'NUMCODECS_USE_SYSTEM_LIBS' in os .environ
1921
2022# setup common compile arguments
2123have_cflags = 'CFLAGS' in os .environ
@@ -48,8 +50,8 @@ def error(*msg):
4850 print ('[numcodecs]' , * msg , ** kwargs )
4951
5052
51- def blosc_extension ():
52- info ('setting up Blosc extension' )
53+ def _blosc_extension_with_vendored_libs ():
54+ info ('setting up Blosc extension from vendored sources ' )
5355
5456 extra_compile_args = base_compile_args .copy ()
5557 define_macros = []
@@ -124,8 +126,73 @@ def blosc_extension():
124126 return extensions
125127
126128
127- def zstd_extension ():
128- info ('setting up Zstandard extension' )
129+ def _blosc_extension_with_system_libs ():
130+ info ('setting up Blosc extension with system libraries' )
131+
132+ extra_compile_args = base_compile_args .copy ()
133+
134+ blosc_package_configuration = pkgconfig .parse ("blosc" )
135+
136+ define_macros = blosc_package_configuration ["define_macros" ]
137+ include_dirs = blosc_package_configuration ["include_dirs" ]
138+ libraries = blosc_package_configuration ["libraries" ]
139+ library_dirs = blosc_package_configuration ["library_dirs" ]
140+
141+ # remove minizip because Python.h 3.8 tries to include crypt.h
142+ include_dirs = [d for d in include_dirs if 'minizip' not in d ]
143+ define_macros += [
144+ ('HAVE_LZ4' , 1 ),
145+ # ('HAVE_SNAPPY', 1),
146+ ('HAVE_ZLIB' , 1 ),
147+ ('HAVE_ZSTD' , 1 ),
148+ ]
149+ # define_macros += [('CYTHON_TRACE', '1')]
150+
151+ # SSE2
152+ if have_sse2 and not disable_sse2 :
153+ info ('compiling Blosc extension with SSE2 support' )
154+ extra_compile_args .append ('-DSHUFFLE_SSE2_ENABLED' )
155+ if os .name == 'nt' :
156+ define_macros += [('__SSE2__' , 1 )]
157+ else :
158+ info ('compiling Blosc extension without SSE2 support' )
159+
160+ # AVX2
161+ if have_avx2 and not disable_avx2 :
162+ info ('compiling Blosc extension with AVX2 support' )
163+ extra_compile_args .append ('-DSHUFFLE_AVX2_ENABLED' )
164+ if os .name == 'nt' :
165+ define_macros += [('__AVX2__' , 1 )]
166+ else :
167+ info ('compiling Blosc extension without AVX2 support' )
168+
169+ sources = ['numcodecs/blosc.pyx' ]
170+
171+ # define extension module
172+ extensions = [
173+ Extension (
174+ 'numcodecs.blosc' ,
175+ sources = sources ,
176+ include_dirs = include_dirs ,
177+ define_macros = define_macros ,
178+ extra_compile_args = extra_compile_args ,
179+ libraries = libraries ,
180+ library_dirs = library_dirs ,
181+ ),
182+ ]
183+
184+ return extensions
185+
186+
187+ def blosc_extension ():
188+ if use_system_libraries :
189+ return _blosc_extension_with_system_libs ()
190+ else :
191+ return _blosc_extension_with_vendored_libs ()
192+
193+
194+ def _zstd_extension_with_vendored_sources ():
195+ info ('setting up Zstandard extension from vendored sources' )
129196
130197 zstd_sources = []
131198 extra_compile_args = base_compile_args .copy ()
@@ -166,8 +233,46 @@ def zstd_extension():
166233 return extensions
167234
168235
169- def lz4_extension ():
170- info ('setting up LZ4 extension' )
236+ def _zstd_extension_with_system_libs ():
237+ info ('setting up Zstandard extension with system libraries' )
238+
239+ extra_compile_args = base_compile_args .copy ()
240+
241+ zstd_package_configuration = pkgconfig .parse ("libzstd" )
242+ include_dirs = zstd_package_configuration ["include_dirs" ]
243+ define_macros = zstd_package_configuration ["define_macros" ]
244+ libraries = zstd_package_configuration ["libraries" ]
245+ library_dirs = zstd_package_configuration ["library_dirs" ]
246+
247+ # define_macros += [('CYTHON_TRACE', '1')]
248+
249+ sources = ['numcodecs/zstd.pyx' ]
250+
251+ # define extension module
252+ extensions = [
253+ Extension (
254+ 'numcodecs.zstd' ,
255+ sources = sources ,
256+ include_dirs = include_dirs ,
257+ define_macros = define_macros ,
258+ extra_compile_args = extra_compile_args ,
259+ libraries = libraries ,
260+ library_dirs = library_dirs ,
261+ ),
262+ ]
263+
264+ return extensions
265+
266+
267+ def zstd_extension ():
268+ if use_system_libraries :
269+ return _zstd_extension_with_system_libs ()
270+ else :
271+ return _zstd_extension_with_vendored_sources ()
272+
273+
274+ def _lz4_extension_with_vendored_sources ():
275+ info ('setting up LZ4 extension from vendored sources' )
171276
172277 extra_compile_args = base_compile_args .copy ()
173278 define_macros = []
@@ -194,6 +299,45 @@ def lz4_extension():
194299 return extensions
195300
196301
302+ def _lz4_extension_with_system_libs ():
303+ info ('setting up LZ4 extension with system libraries' )
304+
305+ extra_compile_args = base_compile_args .copy ()
306+
307+ lz4_package_configuration = pkgconfig .parse ("liblz4" )
308+ include_dirs = lz4_package_configuration ["include_dirs" ]
309+ define_macros = lz4_package_configuration ["define_macros" ]
310+ libraries = lz4_package_configuration ["libraries" ]
311+ library_dirs = lz4_package_configuration ["library_dirs" ]
312+
313+ include_dirs += ['numcodecs' ]
314+ # define_macros += [('CYTHON_TRACE', '1')]
315+
316+ sources = ['numcodecs/lz4.pyx' ]
317+
318+ # define extension module
319+ extensions = [
320+ Extension (
321+ 'numcodecs.lz4' ,
322+ sources = sources ,
323+ include_dirs = include_dirs ,
324+ define_macros = define_macros ,
325+ extra_compile_args = extra_compile_args ,
326+ libraries = libraries ,
327+ library_dirs = library_dirs ,
328+ ),
329+ ]
330+
331+ return extensions
332+
333+
334+ def lz4_extension ():
335+ if use_system_libraries :
336+ return _lz4_extension_with_system_libs ()
337+ else :
338+ return _lz4_extension_with_vendored_sources ()
339+
340+
197341def vlen_extension ():
198342 info ('setting up vlen extension' )
199343 import numpy
0 commit comments