setup_lz4.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. # bundled_path: relative (to this file) path to the bundled library source code files
  12. bundled_path = 'src/borg/algorithms/lz4'
  13. lz4_sources = [
  14. 'lib/lz4.c',
  15. ]
  16. lz4_includes = [
  17. 'lib',
  18. ]
  19. def lz4_ext_kwargs(prefer_system):
  20. """return kwargs with lz4 stuff for a distutils.extension.Extension initialization.
  21. prefer_system: prefer the system-installed library (if found) over the bundled C code
  22. returns: kwargs for this lib
  23. """
  24. def multi_join(paths, *path_segments):
  25. """apply os.path.join on a list of paths"""
  26. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  27. define_macros = []
  28. system_prefix = os.environ.get('BORG_LIBLZ4_PREFIX')
  29. if prefer_system and system_prefix:
  30. print('Detected and preferring liblz4 over bundled LZ4')
  31. define_macros.append(('BORG_USE_LIBLZ4', 'YES'))
  32. system = True
  33. else:
  34. print('Using bundled LZ4')
  35. system = False
  36. use_system = system and system_prefix is not None
  37. if use_system:
  38. sources = []
  39. include_dirs = multi_join(['include'], system_prefix)
  40. library_dirs = multi_join(['lib'], system_prefix)
  41. libraries = ['lz4', ]
  42. else:
  43. sources = multi_join(lz4_sources, bundled_path)
  44. include_dirs = multi_join(lz4_includes, bundled_path)
  45. library_dirs = []
  46. libraries = []
  47. extra_compile_args = []
  48. return dict(sources=sources, define_macros=define_macros, extra_compile_args=extra_compile_args,
  49. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries)