setup_b2.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Support code for building a C extension with blake2 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 blake2)
  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. # b2 files, structure as seen in BLAKE2 (reference implementation) project repository:
  11. # bundled_path: relative (to this file) path to the bundled library source code files
  12. bundled_path = 'src/borg/algorithms/blake2'
  13. b2_sources = [
  14. 'ref/blake2b-ref.c',
  15. ]
  16. b2_includes = [
  17. 'ref',
  18. ]
  19. def b2_ext_kwargs(prefer_system):
  20. """return kwargs with b2 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_LIBB2_PREFIX')
  29. if prefer_system and system_prefix:
  30. print('Detected and preferring libb2 over bundled BLAKE2')
  31. define_macros.append(('BORG_USE_LIBB2', 'YES'))
  32. system = True
  33. else:
  34. print('Using bundled BLAKE2')
  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 = ['b2', ]
  42. else:
  43. sources = multi_join(b2_sources, bundled_path)
  44. include_dirs = multi_join(b2_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)