-
Notifications
You must be signed in to change notification settings - Fork 25
/
setup.py
1319 lines (1063 loc) · 43.6 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2024 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
from __future__ import annotations
__authors__ = ["V.A. Sole", "T. Vincent"]
__license__ = "MIT"
__date__ = "05/12/2022"
from functools import lru_cache
from glob import glob
from pathlib import Path
import itertools
import logging
import os
import sys
import sysconfig
import tempfile
import platform
from functools import cached_property
from setuptools import setup, Distribution, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
from setuptools.command.build_clib import build_clib
from setuptools.command.build import build
from setuptools.errors import CompileError
from wheel.bdist_wheel import bdist_wheel
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
import cpuinfo
except ModuleNotFoundError:
raise ModuleNotFoundError("py-cpuinfo is required to build hdf5plugin, please install it.")
except Exception: # cpuinfo raises Exception for unsupported architectures
logger.warning("Architecture is not supported by cpuinfo")
cpuinfo = None
cpuinfo_parse_arch = None
else:
from cpuinfo.cpuinfo import _parse_arch as cpuinfo_parse_arch
class BDistWheel(bdist_wheel):
"""Override bdist_wheel to handle as pure python package"""
def get_tag(self):
"""Override the python and abi tag generation"""
return self.python_tag, "none", super().get_tag()[-1]
# Probe host capabilities and manage build config
def get_compiler(compiler):
"""Returns an initialized compiler
Taken from https://github.com/pypa/setuptools/issues/2806#issuecomment-961805789
"""
build_ext = Distribution().get_command_obj("build_ext")
build_ext.compiler = compiler
build_ext.finalize_options()
# register an extension to ensure a compiler is created
build_ext.extensions = [Extension("ignored", ["ignored.c"])]
# disable building fake extensions
build_ext.build_extensions = lambda: None
# run to populate self.compiler
build_ext.run()
return build_ext.compiler
def check_compile_flags(compiler, *flags, extension='.c', source=None):
"""Try to compile an empty file to check for compiler args
:param distutils.ccompiler.CCompiler compiler: The compiler to use
:param flags: Flags argument to pass to compiler
:param str extension: Source file extension (default: '.c')
:param source: Source code to compile (default: dummy C function)
:returns: Whether or not compilation was successful
:rtype: bool
"""
if source is None:
source = 'int main (int argc, char **argv) { return 0; }\n'
with tempfile.TemporaryDirectory() as tmp_dir:
# Create empty source file
tmp_file = Path(tmp_dir) / f"source{extension}"
tmp_file.write_text(source)
try:
compiler.compile([str(tmp_file)], output_dir=tmp_dir, extra_postargs=list(flags))
except CompileError:
return False
else:
return True
def has_cpu_flag(flag: str) -> bool:
"""Is given flag available on the current (x86) CPU"""
if cpuinfo is None:
logger.warning("Cannot get CPU flags")
return False
return flag in cpuinfo.get_cpu_info().get('flags', [])
class HostConfig:
"""Probe and describe host configuration
For differences in native build option across architectures, see
https://gcc.gnu.org/onlinedocs/gcc/Submodel-Options.html#Submodel-Options
"""
ARCH = cpuinfo_parse_arch(platform.machine().lower())[0] if cpuinfo_parse_arch is not None else None
"""Machine architecture description from cpuinfo parser"""
def __init__(self, compiler=None):
self.__compiler = get_compiler(compiler)
# Set architecture specific compile args
if self.ARCH in ('X86_32', 'X86_64', 'MIPS_64'):
self.native_compile_args = ("-march=native",)
elif self.ARCH in ('ARM_7', 'ARM_8', 'PPC_64'):
self.native_compile_args = ("-mcpu=native",)
else:
self.native_compile_args = ()
if self.ARCH in ('X86_32', 'X86_64'):
self.sse2_compile_args = ('-msse2',) # /arch:SSE2 is on by default
else:
self.sse2_compile_args = ()
if self.ARCH in ('X86_32', 'X86_64'):
self.ssse3_compile_args = ('-mssse3',) # There is no /arch:SSSE3
else:
self.ssse3_compile_args = ()
if self.ARCH in ('X86_32', 'X86_64'):
self.avx2_compile_args = ('-mavx2', '/arch:AVX2')
else:
self.avx2_compile_args = ()
if self.ARCH in ('X86_32', 'X86_64'):
self.avx512_compile_args = ('-mavx512f', '-mavx512bw', '/arch:AVX512')
else:
self.avx512_compile_args = ()
def get_shared_lib_extension(self) -> str:
"""Returns shared library file extension"""
if sys.platform == 'win32':
return '.dll'
elif sys.platform == 'linux':
return '.so'
elif sys.platform == 'darwin':
return '.dylib'
else: # Return same value as used by build_ext.get_ext_filename
return sysconfig.get_config_var('EXT_SUFFIX')
def has_cpp11(self) -> bool:
"""Check C++11 availability on host"""
if self.__compiler.compiler_type == 'msvc':
return True
return check_compile_flags(self.__compiler, '-std=c++11', extension='.cc')
def has_cpp14(self) -> bool:
"""Check C++14 availability on host"""
if self.__compiler.compiler_type == 'msvc':
return True
return check_compile_flags(self.__compiler, '-std=c++14', extension='.cc')
@lru_cache()
def get_cpp20_flag(self) -> str | None:
"""Returns C++20 compiler flag or None if not supported"""
if self.__compiler.compiler_type == 'msvc':
# C++20 available since Visual Studio C++ 2019 v16.11
# Lack of support of the compilation flag do not raise an error
# So instead check the _MSVC_LANG macro
# See: https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=msvc-170#c-standards-support
is_available = check_compile_flags(
self.__compiler,
'/std:c++20',
extension='.cc',
source="""
#if _MSVC_LANG != 202002L
#error C++20 is not supported
#endif
int main (int argc, char **argv) { return 0; }
""",
)
return "/std:c++20" if is_available else None
# -std=c++20 if clang>=10 or gcc>=10 else -std=c++2a
for flag in ('-std=c++20', '-std=c++2a'):
if check_compile_flags(self.__compiler, flag, extension='.cc'):
break
else: # Check failed for both flags
return None
if sys.platform != 'darwin':
return flag
# Check macos min version >= 10.13:
# 10.13 does not fully support C++20, but is enough to build the SPERR library
is_available = check_compile_flags(
self.__compiler,
flag,
extension='.cc',
source="""
#include "AvailabilityMacros.h"
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_13
#error C++20 is not supported: macOS 10.13 at least is required
#endif
int main (int argc, char **argv) { return 0; }
""",
)
return flag if is_available else None
def has_cpp20(self) -> bool:
"""Check C++20 availability on host"""
return self.get_cpp20_flag() is not None
def _has_x86_simd(self, *flags) -> bool:
"""Check x86 SIMD availability on host"""
if self.ARCH not in ('X86_32', 'X86_64'):
return False
if not all(has_cpu_flag(flag) for flag in flags):
return False
if self.__compiler.compiler_type == "msvc":
return True
return check_compile_flags(self.__compiler, *(f"-m{flag}" for flag in flags))
def has_sse2(self) -> bool:
"""Check SSE2 availability on host"""
return self._has_x86_simd('sse2')
def has_ssse3(self) -> bool:
"""Check SSSE3 availability on host"""
return self._has_x86_simd('ssse3')
def has_avx2(self) -> bool:
"""Check AVX2 availability on host"""
return self._has_x86_simd('avx2')
def has_avx512(self) -> bool:
"""Check AVX512 "F" and "BW" instruction sets availability on host"""
return self._has_x86_simd('avx512f', 'avx512bw')
def has_openmp(self) -> bool:
"""Check OpenMP availability on host"""
if sys.platform == 'darwin':
return False
prefix = '/' if self.__compiler.compiler_type == 'msvc' else '-f'
return check_compile_flags(self.__compiler, prefix + 'openmp')
def has_native(self) -> bool:
"""Returns native build option availability on host"""
if len(self.native_compile_args) > 0:
return check_compile_flags(self.__compiler, *self.native_compile_args)
return False
class BuildConfig:
"""Describe build configuration"""
def __init__(self, config_file: Path, compiler=None):
self.__config_file = config_file
self.__host_config = HostConfig(compiler)
self.embedded_filters = []
@staticmethod
def get_hdf5_library_dirs() -> list[str]:
"""Returns list of HDF5 library directories"""
if sys.platform != 'win32':
return []
hdf5_dir = os.environ.get("HDF5PLUGIN_HDF5_DIR", "src/hdf5")
return [f"{hdf5_dir}/lib"]
@staticmethod
def get_hdf5_include_dirs() -> list[str]:
"""Returns list of HDF5 include directories"""
try:
hdf5_dir = os.environ["HDF5PLUGIN_HDF5_DIR"]
except KeyError:
hdf5_dir = "src/hdf5"
else: # HDF5 directory define by environment variable
return [f"{hdf5_dir}/include"]
# Add folder containing H5pubconf.h
if sys.platform == 'win32':
folder = 'windows'
elif sys.platform == 'darwin':
folder = 'darwin'
else:
folder = 'linux'
return [f"{hdf5_dir}/include", f"{hdf5_dir}/include/{folder}"]
@property
def filter_file_extension(self) -> str:
"""File extension to use for filter shared libraries"""
return self.__host_config.get_shared_lib_extension()
@property
def compile_args(self) -> tuple[str, ...]:
"""Returns compile args to use"""
compile_args = []
if self.use_sse2:
compile_args.extend(self.__host_config.sse2_compile_args)
if self.use_ssse3:
compile_args.extend(self.__host_config.ssse3_compile_args)
if self.use_avx2:
compile_args.extend(self.__host_config.avx2_compile_args)
if self.use_avx512:
compile_args.extend(self.__host_config.avx512_compile_args)
if self.use_native:
compile_args.extend(self.__host_config.native_compile_args)
return tuple(compile_args)
@property
def cpp20_compile_arg(self) -> str | None:
"""Returns C++20 compilation flag or None if not supported"""
return self.__host_config.get_cpp20_flag()
@lru_cache(maxsize=None)
def _is_enabled(self, feature: str) -> bool:
"""Returns True if given compilation feature is enabled.
It first checks if the corresponding HDF5PLUGIN_* environment variable is set.
If it is not set, it checks if both the compiler and the host support the feature.
"""
assert feature in ("cpp11", "cpp14", "cpp20", "sse2", "ssse3", "avx2", "avx512", "openmp", "native")
try:
return os.environ[f"HDF5PLUGIN_{feature.upper()}"] == "True"
except KeyError:
pass
check = getattr(self.__host_config, f"has_{feature.lower()}")
return check()
use_cpp11 = property(lambda self: self._is_enabled('cpp11'))
use_cpp14 = property(lambda self: self._is_enabled('cpp14'))
use_cpp20 = property(lambda self: self._is_enabled('cpp20'))
use_sse2 = property(lambda self: self._is_enabled('sse2'))
use_openmp = property(lambda self: self._is_enabled('openmp'))
use_native = property(lambda self: self._is_enabled('native'))
@cached_property
def use_ssse3(self) -> bool:
enabled = self._is_enabled('ssse3')
if enabled and not self.use_sse2:
logger.error("use_ssse3=True disabled: incompatible with use_sse2=False")
return False
return enabled
@cached_property
def use_avx2(self) -> bool:
enabled = self._is_enabled('avx2')
if enabled and not (self.use_sse2 and self.use_ssse3):
logger.error(
"use_avx2=True disabled: incompatible with use_sse2=False and use_ssse3=False")
return False
return enabled
@cached_property
def use_avx512(self) -> bool:
enabled = self._is_enabled('avx512')
if enabled and not (self.use_sse2 and self.use_ssse3 and self.use_avx2):
logger.error(
"use_avx512=True disabled: incompatible with use_sse2=False, use_ssse3=False and use_avx2=False")
return False
return enabled
USE_BMI2 = bool(
os.environ.get("HDF5PLUGIN_BMI2", 'True') == 'True'
and sys.platform in ('linux', 'darwin')
)
"""Whether to build with BMI2 instruction set or not (bool)"""
INTEL_IPP_DIR = os.environ.get("HDF5PLUGIN_INTEL_IPP_DIR", None)
"""Root directory of Intel IPP or None to disable"""
def get_config_string(self) -> str:
build_config = {
'openmp': self.use_openmp,
'native': self.use_native,
'bmi2': self.USE_BMI2,
'sse2': self.use_sse2,
'ssse3': self.use_ssse3,
'avx2': self.use_avx2,
'avx512': self.use_avx512,
'cpp11': self.use_cpp11,
'cpp14': self.use_cpp14,
'cpp20': self.use_cpp20,
'ipp': self.INTEL_IPP_DIR is not None,
'filter_file_extension': self.filter_file_extension,
'embedded_filters': tuple(sorted(set(self.embedded_filters))),
}
return f"""from collections import namedtuple
HDF5PluginBuildConfig = namedtuple('HDF5PluginBuildConfig', {tuple(build_config.keys())})
build_config = HDF5PluginBuildConfig(**{build_config})
"""
def has_config_changed(self) -> bool:
"""Returns whether config file needs to be changed or not."""
try:
return self.__config_file.read_text() != self.get_config_string()
except: # noqa
pass
return True
def save_config(self) -> None:
"""Save config as a dict in a python file"""
self.__config_file.write_text(self.get_config_string())
# Plugins
class Build(build):
"""Build command with extra options used by PluginBuildExt"""
def finalize_options(self):
build.finalize_options(self)
self.hdf5plugin_config = BuildConfig(
config_file=Path(self.build_lib) / "hdf5plugin" / "_config.py",
compiler=self.compiler,
)
if not self.hdf5plugin_config.use_cpp11:
# Filter out C++11 libraries
self.distribution.libraries = [
(name, info) for name, info in self.distribution.libraries
if '-std=c++11' not in info.get('cflags', [])]
# Filter out C++11-only extensions
self.distribution.ext_modules = [
ext for ext in self.distribution.ext_modules
if not (isinstance(ext, HDF5PluginExtension) and ext.cpp11_required)]
if not self.hdf5plugin_config.use_cpp14:
# Filter out C++14 libraries
self.distribution.libraries = [
(name, info) for name, info in self.distribution.libraries
if '-std=c++14' not in info.get('cflags', [])
]
# Filter out C++14-only extensions
self.distribution.ext_modules = [
ext for ext in self.distribution.ext_modules
if '-std=c++14' not in ext.extra_compile_args
and not (isinstance(ext, HDF5PluginExtension) and ext.cpp14_required)
]
if not self.hdf5plugin_config.use_cpp20:
cpp20_flags = set(["/std:c++20", "-std=c++20"])
# Filter out C++20 libraries
self.distribution.libraries = [
(name, info) for name, info in self.distribution.libraries
if cpp20_flags.isdisjoint(info.get('cflags', []))
]
# Filter out C++20-only extensions
self.distribution.ext_modules = [
ext for ext in self.distribution.ext_modules
if not (isinstance(ext, HDF5PluginExtension) and ext.cpp20_required)]
self.hdf5plugin_config.embedded_filters = [
ext.hdf5_plugin_name for ext in self.distribution.ext_modules
if isinstance(ext, HDF5PluginExtension)
]
logger.info("Build configuration: %s", self.hdf5plugin_config.get_config_string())
def has_config_changed(self):
"""Check if configuration file has changed"""
if not self.hdf5plugin_config.has_config_changed():
logger.info("Build configuration didn't changed")
return False
logger.info('Build configuration has changed')
clean_cmd = self.distribution.get_command_obj('clean')
clean_cmd.all = True
return True
# Add clean to sub-commands
sub_commands = [('clean', has_config_changed)] + build.sub_commands
class BuildPy(build_py):
"""build_py command also writing hdf5plugin._config"""
def run(self):
super().run()
build_cmd = self.distribution.get_command_obj("build")
build_cmd.hdf5plugin_config.save_config()
class BuildCLib(build_clib):
"""build_clib command adding/patching compile args"""
def build_libraries(self, libraries):
updated_libraries = []
for (lib_name, build_info) in libraries:
config = self.distribution.get_command_obj("build").hdf5plugin_config
cflags = list(build_info.get('cflags', []))
# Add flags from build config
cflags.extend(config.compile_args)
if not config.use_openmp: # Remove OpenMP flags
cflags = [f for f in cflags if not f.endswith('openmp')]
prefix = '/' if self.compiler.compiler_type == 'msvc' else '-'
cflags = [
f for f in cflags if f.startswith(prefix)
]
# Patch C++20 flag for older gcc/clang support
if '-std=c++20' in cflags:
if config.cpp20_compile_arg is None:
raise RuntimeError("Cannot compile C++20 code with current compiler")
cflags = [f if f != '-std=c++20' else config.cpp20_compile_arg for f in cflags]
build_info['cflags'] = cflags
updated_libraries.append((lib_name, build_info))
super().build_libraries(updated_libraries)
class PluginBuildExt(build_ext):
"""Build extension command for DLLs that are not Python modules
It also handles extra compile arguments depending on the build options.
"""
def get_export_symbols(self, ext):
"""Overridden to remove PyInit_* export"""
return ext.export_symbols
def get_ext_filename(self, ext_name):
"""Overridden to use .dll as file extension"""
config = self.distribution.get_command_obj("build").hdf5plugin_config
return os.path.join(*ext_name.split('.')) + config.filter_file_extension
def build_extensions(self):
"""Overridden to tune extensions.
- check for OpenMP, SSE2, AVX2 availability
- select compile args for MSVC and others
- Set hdf5 directory
"""
config = self.distribution.get_command_obj("build").hdf5plugin_config
for e in self.extensions:
if isinstance(e, HDF5PluginExtension):
e.extra_compile_args.extend(config.compile_args)
if config.use_cpp11:
for name, value in e.cpp11.items():
attribute = getattr(e, name)
attribute += value
if config.use_sse2:
for name, value in e.sse2.items():
attribute = getattr(e, name)
attribute += value
if config.use_avx2:
for name, value in e.avx2.items():
attribute = getattr(e, name)
attribute += value
if not config.use_openmp: # Remove OpenMP flags
e.extra_compile_args = [
arg for arg in e.extra_compile_args if not arg.endswith('openmp')]
e.extra_link_args = [
arg for arg in e.extra_link_args if not arg.endswith('openmp')]
# Remove flags that do not correspond to compiler
prefix = '/' if self.compiler.compiler_type == 'msvc' else '-'
e.extra_compile_args = [
arg for arg in e.extra_compile_args if arg.startswith(prefix)]
e.extra_link_args = [
arg for arg in e.extra_link_args if arg.startswith(prefix)]
build_ext.build_extensions(self)
class HDF5PluginExtension(Extension):
"""Extension adding specific things to build a HDF5 plugin"""
def __init__(
self,
name,
sse2=None,
avx2=None,
cpp11=None,
cpp11_required=False,
cpp14_required=False,
cpp20_required=False,
**kwargs
):
Extension.__init__(self, name, **kwargs)
if not self.depends:
self.depends = list(itertools.chain.from_iterable(
glob(f"{self.include_dirs}/*.h")))
self.depends += list(itertools.chain.from_iterable(
glob(f"{self.include_dirs}/*.hpp")))
self.export_symbols.append('H5PLget_plugin_info')
if not sys.platform == 'win32':
self.export_symbols.append('init_filter')
if sys.platform == 'win32':
self.define_macros.append(('H5_BUILT_AS_DYNAMIC_LIB', None))
self.libraries.append('hdf5')
self.define_macros.append(('H5_USE_18_API', None))
# Add HDF5 library directories
self.include_dirs = BuildConfig.get_hdf5_include_dirs() + self.include_dirs
self.library_dirs = BuildConfig.get_hdf5_library_dirs() + self.library_dirs
self.sse2 = sse2 if sse2 is not None else {}
self.avx2 = avx2 if avx2 is not None else {}
self.cpp11 = cpp11 if cpp11 is not None else {}
self.cpp11_required = cpp11_required
self.cpp14_required = cpp14_required
self.cpp20_required = cpp20_required
@property
def hdf5_plugin_name(self):
"""Return HDF5 plugin short name"""
module_name = self.name.split('.')[-1]
assert module_name.startswith('libh5')
return module_name[5:] # Strip libh5
def prefix(directory, files):
"""Add a directory as prefix to a list of files.
:param str directory: Directory to add as prefix
:param List[str] files: List of relative file path
:rtype: List[str]
"""
return ['/'.join((directory, f)) for f in files]
PLUGIN_LIB_DEPENDENCIES = dict()
"""Mapping plugin name to library name they depend on"""
# compression libs
def get_charls_clib(field=None):
"""CharLS static lib build config"""
charls_dir = "src/charls/src"
config = dict(
sources=glob(f'{charls_dir}/*.cpp'),
include_dirs=["src/charls/include", "src/charls/include/charls", charls_dir],
macros=[("CHARLS_STATIC", 1), ("CHARLS_LIBRARY_BUILD", 1)],
cflags=['-std=c++14'],
)
if field is None:
return 'charls', config
return config[field]
# Define compilation arguments for Intel IPP
INTEL_IPP_INCLUDE_DIRS = [] # Intel IPP include directories
INTEL_IPP_EXTRA_LINK_ARGS = [] # Intel IPP extra link arguments
INTEL_IPP_LIBRARIES = [] # Intel IPP libraries to link
if BuildConfig.INTEL_IPP_DIR is not None:
INTEL_IPP_INCLUDE_DIRS.append(f'{BuildConfig.INTEL_IPP_DIR}/include')
arch = 'ia32' if HostConfig.ARCH == 'X86_32' else 'intel64'
ipp_lib_dir = f'{BuildConfig.INTEL_IPP_DIR}/lib/{arch}'
if not os.path.isdir(ipp_lib_dir): # Happens on macos as only intel64 is available
ipp_lib_dir = f'{BuildConfig.INTEL_IPP_DIR}/lib'
INTEL_IPP_EXTRA_LINK_ARGS.append(f'-L{ipp_lib_dir}')
INTEL_IPP_EXTRA_LINK_ARGS.append(f'/LIBPATH:{ipp_lib_dir}')
INTEL_IPP_LIBRARIES.extend(['ippdc', 'ipps', 'ippvm', 'ippcore'])
def _get_lz4_ipp_clib(field=None):
"""LZ4 static lib using Intel IPP build config"""
assert BuildConfig.INTEL_IPP_DIR is not None
cflags = ['-O3', '-ffast-math', '-std=gnu99']
cflags += ['/Ox', '/fp:fast']
lz4_dir = 'src/lz4_ipp'
config = dict(
sources=glob(f'{lz4_dir}/*.c'),
include_dirs=[lz4_dir] + INTEL_IPP_INCLUDE_DIRS,
macros=[('WITH_IPP', 1)],
cflags=cflags,
)
if field is None:
return 'lz4', config
if field == 'extra_link_args':
return INTEL_IPP_EXTRA_LINK_ARGS
if field == 'libraries':
# Adding LZ4 here for it to be placed before IPP libs for gcc
return ['lz4'] + INTEL_IPP_LIBRARIES
return config[field]
def get_lz4_clib(field=None):
"""LZ4 static lib build config
If HDFPLUGIN_IPP_DIR is set, it will use a patched LZ4 library to use Intel IPP.
"""
if BuildConfig.INTEL_IPP_DIR is not None:
return _get_lz4_ipp_clib(field)
cflags = ['-O3', '-ffast-math', '-std=gnu99']
cflags += ['/Ox', '/fp:fast']
lz4_dir = glob('src/c-blosc2/internal-complibs/lz4*')[0]
config = dict(
sources=glob(f'{lz4_dir}/*.c'),
include_dirs=[lz4_dir],
cflags=cflags,
)
if field is None:
return 'lz4', config
if field == 'extra_link_args':
return []
if field == 'libraries':
return []
return config[field]
def get_snappy_clib(field=None):
"""snappy static lib build config"""
snappy_dir = 'src/snappy'
config = dict(
sources=prefix(snappy_dir, [
'snappy-c.cc',
'snappy-sinksource.cc',
'snappy-stubs-internal.cc',
'snappy.cc',
]),
include_dirs=[snappy_dir],
cflags=['-std=c++11'],
)
if field is None:
return 'snappy', config
return config[field]
def get_sperr_clib(field=None):
"""sperr static lib build config"""
sperr_dir = "src/SPERR"
config = dict(
sources=glob(f"{sperr_dir}/src/*.cpp"),
include_dirs=[f"{sperr_dir}/include"],
macros=[
("SPERR_VERSION_MAJOR", 0), # Check project(SPERR VERSION ... in src/SPERR/CMakeLists.txt
("USE_VANILLA_CONFIG", 1),
],
cflags=["-std=c++20", "/std:c++20"],
)
if field is None:
return 'sperr', config
return config[field]
def get_zfp_clib(field=None):
"""ZFP static lib build config"""
cflags = ['-O3', '-ffast-math', '-std=c99', '-fopenmp']
# Use /Ob1 to fix ZFP test failing with Visual Studio 2022: MSVC 14.40.33807
# See https://github.com/LLNL/zfp/issues/222
cflags += ['/Ob1', '/fp:fast', '/openmp']
zfp_dir = "src/zfp"
config = dict(
sources=glob(f'{zfp_dir}/src/*.c'),
include_dirs=[f'{zfp_dir}/include'],
macros=[('BIT_STREAM_WORD_TYPE', 'uint8')],
cflags=cflags,
)
if field is None:
return 'zfp', config
return config[field]
def get_zlib_clib(field=None):
"""ZLib static lib build config"""
cflags = ['-O3', '-ffast-math', '-std=gnu99']
cflags += ['/Ox', '/fp:fast']
zlib_dir = glob('src/c-blosc/internal-complibs/zlib*')[0]
config = dict(
sources=glob(f'{zlib_dir}/*.c'),
include_dirs=[zlib_dir],
cflags=cflags,
)
if field is None:
return 'zlib', config
return config[field]
def get_zstd_clib(field=None):
"""zstd static lib build config"""
cflags = ['-O3', '-ffast-math', '-std=gnu99']
cflags += ['/Ox', '/fp:fast']
zstd_dir = glob('src/c-blosc2/internal-complibs/zstd*')[0]
config = dict(
sources=glob(f'{zstd_dir}/*/*.c'),
include_dirs=[zstd_dir, f'{zstd_dir}/common'],
macros=[] if BuildConfig.USE_BMI2 else [('ZSTD_DISABLE_ASM', 1)],
cflags=cflags,
)
if field is None:
return 'zstd', config
if field == 'extra_objects':
return glob(f'{zstd_dir}/*/*.S') if BuildConfig.USE_BMI2 else []
return config[field]
# compression filter plugins
def get_blosc_plugin():
"""blosc plugin build config
Plugin from https://github.com/Blosc/hdf5-blosc
c-blosc from https://github.com/Blosc/c-blosc
"""
blosc_dir = 'src/c-blosc/blosc'
hdf5_blosc_dir = 'src/hdf5-blosc/src'
# blosc sources
sources = glob(f'{blosc_dir}/*.c')
include_dirs = ['src/c-blosc', blosc_dir]
define_macros = []
extra_link_args = []
libraries = []
# compression libs
# lz4
include_dirs += get_lz4_clib('include_dirs')
extra_link_args += get_lz4_clib('extra_link_args')
libraries += get_lz4_clib('libraries')
define_macros.append(('HAVE_LZ4', 1))
# snappy
cpp11_kwargs = {
'include_dirs': get_snappy_clib('include_dirs'),
'extra_link_args': ['-lstdc++'],
'define_macros': [('HAVE_SNAPPY', 1)],
}
# zlib
include_dirs += get_zlib_clib('include_dirs')
define_macros.append(('HAVE_ZLIB', 1))
# zstd
include_dirs += get_zstd_clib('include_dirs')
define_macros.append(('HAVE_ZSTD', 1))
extra_compile_args = ['-std=gnu99'] # Needed to build manylinux1 wheels
extra_compile_args += ['-O3', '-ffast-math']
extra_compile_args += ['/Ox', '/fp:fast']
extra_compile_args += ['-pthread']
extra_link_args += ['-pthread']
return HDF5PluginExtension(
"hdf5plugin.plugins.libh5blosc",
sources=sources + prefix(
hdf5_blosc_dir, ['blosc_filter.c', 'blosc_plugin.c']),
extra_objects=get_zstd_clib('extra_objects'),
include_dirs=include_dirs + [hdf5_blosc_dir],
define_macros=define_macros,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
libraries=libraries,
sse2={'define_macros': [('SHUFFLE_SSE2_ENABLED', 1)]},
avx2={'define_macros': [('SHUFFLE_AVX2_ENABLED', 1)]},
cpp11=cpp11_kwargs,
)
PLUGIN_LIB_DEPENDENCIES['blosc'] = 'snappy', 'lz4', 'zlib', 'zstd'
def get_blosc2_plugin():
"""blosc2 plugin build config
Source from PyTables and c-blosc2
"""
hdf5_blosc2_dir = 'src/PyTables/hdf5-blosc2/src'
blosc2_dir = 'src/c-blosc2'
plugins_dir = f'{blosc2_dir}/plugins'
# blosc sources
sources = glob(f'{blosc2_dir}/blosc/*.c')
sources += [ # Add embedded codecs, filters and tuners
src_file
for src_file in glob(f'{plugins_dir}/*.c') + glob(f'{plugins_dir}/*/*.c') + glob(f'{plugins_dir}/*/*/*.c')
if not os.path.basename(src_file).startswith("test")
]
sources += glob(f'{plugins_dir}/codecs/zfp/src/*.c') # Add ZFP embedded sources
include_dirs = [
blosc2_dir,
f'{blosc2_dir}/blosc',
f'{blosc2_dir}/include',
f'{blosc2_dir}/plugins/codecs/zfp/include',
]
define_macros = [('HAVE_PLUGINS', 1)]
if platform.machine() == 'ppc64le':
define_macros.append(('SHUFFLE_ALTIVEC_ENABLED', 1))
define_macros.append(('NO_WARN_X86_INTRINSICS', None))
else:
define_macros.append(('SHUFFLE_SSE2_ENABLED', 1))
define_macros.append(('SHUFFLE_AVX2_ENABLED', 1))
define_macros.append(('SHUFFLE_AVX512_ENABLED', 1))
define_macros.append(('SHUFFLE_NEON_ENABLED', 1))
extra_compile_args = []
extra_link_args = []
libraries = []
if HostConfig.ARCH == 'ARM_8':
extra_compile_args += ['-flax-vector-conversions']
if HostConfig.ARCH == 'ARM_7':
extra_compile_args += ['-mfpu=neon', '-flax-vector-conversions']
# compression libs
# lz4
include_dirs += get_lz4_clib('include_dirs')
if BuildConfig.INTEL_IPP_DIR is None:
extra_link_args += get_lz4_clib('extra_link_args')
libraries += get_lz4_clib('libraries')
else:
include_dirs += INTEL_IPP_INCLUDE_DIRS
extra_link_args += INTEL_IPP_EXTRA_LINK_ARGS
libraries += INTEL_IPP_LIBRARIES
define_macros.append(('HAVE_IPP', 1))
# zlib
include_dirs += get_zlib_clib('include_dirs')
define_macros.append(('HAVE_ZLIB', 1))
# zstd
include_dirs += get_zstd_clib('include_dirs')
define_macros.append(('HAVE_ZSTD', 1))
extra_compile_args += ['-O3', '-std=gnu99']
extra_compile_args += ['/Ox']
extra_compile_args += ['-pthread']
extra_link_args += ['-pthread']
return HDF5PluginExtension(
"hdf5plugin.plugins.libh5blosc2",
sources=sources + prefix(hdf5_blosc2_dir, ['blosc2_filter.c', 'blosc2_plugin.c']),
extra_objects=get_zstd_clib('extra_objects'),
include_dirs=include_dirs + [hdf5_blosc2_dir],
define_macros=define_macros,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
libraries=libraries,
sse2={'define_macros': [('SHUFFLE_SSE2_ENABLED', 1)]},
avx2={'define_macros': [('SHUFFLE_AVX2_ENABLED', 1)]},
)