setup_b2.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Support code for building a C extension with blake2
  2. import os
  3. # b2 files, structure as seen in BLAKE2 (reference implementation) project repository:
  4. # bundled_path: relative (to this file) path to the bundled library source code files
  5. bundled_path = 'src/borg/algorithms/blake2'
  6. b2_sources = [
  7. 'ref/blake2b-ref.c',
  8. ]
  9. b2_includes = [
  10. 'ref',
  11. ]
  12. def multi_join(paths, *path_segments):
  13. """apply os.path.join on a list of paths"""
  14. return [os.path.join(*(path_segments + (path,))) for path in paths]
  15. def b2_ext_kwargs(prefer_system):
  16. if prefer_system:
  17. system_prefix = os.environ.get('BORG_LIBB2_PREFIX')
  18. if system_prefix:
  19. print('Detected and preferring libb2 [via BORG_LIBB2_PREFIX]')
  20. return dict(include_dirs=[os.path.join(system_prefix, 'include')],
  21. library_dirs=[os.path.join(system_prefix, 'lib')],
  22. libraries=['b2'])
  23. import pkgconfig
  24. if pkgconfig.installed('libb2', '>= 0.98.1'):
  25. print('Detected and preferring libb2 [via pkg-config]')
  26. return pkgconfig.parse('libb2')
  27. print('Using bundled BLAKE2')
  28. sources = multi_join(b2_sources, bundled_path)
  29. include_dirs = multi_join(b2_includes, bundled_path)
  30. define_macros = [('BORG_USE_BUNDLED_B2', 'YES')]
  31. return dict(sources=sources, include_dirs=include_dirs, define_macros=define_macros)