setup_crypto.py 1.9 KB

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