setup_lz4.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_ext_kwargs(bundled_path, prefer_system, **kwargs):
  18. """amend kwargs with lz4 stuff for a distutils.extension.Extension initialization.
  19. bundled_path: relative (to this file) path to the bundled library source code files
  20. prefer_system: prefer the system-installed library (if found) over the bundled C code
  21. kwargs: distutils.extension.Extension kwargs that should be amended
  22. returns: amended kwargs
  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 = kwargs.get('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. sources = kwargs.get('sources', [])
  38. if not use_system:
  39. sources += multi_join(lz4_sources, bundled_path)
  40. include_dirs = kwargs.get('include_dirs', [])
  41. if use_system:
  42. include_dirs += multi_join(['include'], system_prefix)
  43. else:
  44. include_dirs += multi_join(lz4_includes, bundled_path)
  45. library_dirs = kwargs.get('library_dirs', [])
  46. if use_system:
  47. library_dirs += multi_join(['lib'], system_prefix)
  48. libraries = kwargs.get('libraries', [])
  49. if use_system:
  50. libraries += ['lz4', ]
  51. extra_compile_args = kwargs.get('extra_compile_args', [])
  52. if not use_system:
  53. extra_compile_args += [] # not used yet
  54. ret = dict(**kwargs)
  55. ret.update(dict(sources=sources, extra_compile_args=extra_compile_args,
  56. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  57. return ret