setup_lz4.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Support code for building a C extension with lz4 files
  2. #
  3. # Copyright (c) 2016-present, Gregory Szorc (original code for zstd)
  4. # 2017-present, Thomas Waldmann (mods to make it more generic, code for lz4)
  5. # All rights reserved.
  6. #
  7. # This software may be modified and distributed under the terms
  8. # of the BSD license. See the LICENSE file for details.
  9. import os
  10. # lz4 files, structure as seen in lz4 project repository:
  11. lz4_sources = [
  12. 'lib/lz4.c',
  13. ]
  14. lz4_includes = [
  15. 'lib',
  16. ]
  17. def lz4_system_prefix(prefixes):
  18. for prefix in prefixes:
  19. filename = os.path.join(prefix, 'include', 'lz4.h')
  20. if os.path.exists(filename):
  21. with open(filename, 'rb') as fd:
  22. if b'LZ4_compress_default' in fd.read(): # requires lz4 >= 1.7.0 (r129)
  23. return prefix
  24. def lz4_ext_kwargs(bundled_path, system_prefix=None, system=False, **kwargs):
  25. """amend kwargs with lz4 stuff for a distutils.extension.Extension initialization.
  26. bundled_path: relative (to this file) path to the bundled library source code files
  27. system_prefix: where the system-installed library can be found
  28. system: True: use the system-installed shared library, False: use the bundled library code
  29. kwargs: distutils.extension.Extension kwargs that should be amended
  30. returns: amended kwargs
  31. """
  32. def multi_join(paths, *path_segments):
  33. """apply os.path.join on a list of paths"""
  34. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  35. use_system = system and system_prefix is not None
  36. sources = kwargs.get('sources', [])
  37. if not use_system:
  38. sources += multi_join(lz4_sources, bundled_path)
  39. include_dirs = kwargs.get('include_dirs', [])
  40. if use_system:
  41. include_dirs += multi_join(['include'], system_prefix)
  42. else:
  43. include_dirs += multi_join(lz4_includes, bundled_path)
  44. library_dirs = kwargs.get('library_dirs', [])
  45. if use_system:
  46. library_dirs += multi_join(['lib'], system_prefix)
  47. libraries = kwargs.get('libraries', [])
  48. if use_system:
  49. libraries += ['lz4', ]
  50. extra_compile_args = kwargs.get('extra_compile_args', [])
  51. if not use_system:
  52. extra_compile_args += [] # not used yet
  53. ret = dict(**kwargs)
  54. ret.update(dict(sources=sources, extra_compile_args=extra_compile_args,
  55. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  56. return ret