setup_crypto.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, system_prefix):
  7. if system_prefix:
  8. print('Detected OpenSSL [via BORG_OPENSSL_PREFIX]')
  9. return dict(include_dirs=[os.path.join(system_prefix, 'include')],
  10. library_dirs=[os.path.join(system_prefix, 'lib')],
  11. libraries=['crypto'])
  12. if pc and pc.exists('libcrypto'):
  13. print('Detected OpenSSL [via pkg-config]')
  14. return pc.parse('libcrypto')
  15. raise Exception('Could not find OpenSSL lib/headers, please set BORG_OPENSSL_PREFIX')
  16. # b2 files, structure as seen in BLAKE2 (reference implementation) project repository:
  17. # path relative (to this file) to the bundled library source code files
  18. b2_bundled_path = 'src/borg/algorithms/blake2'
  19. b2_sources = [
  20. 'ref/blake2b-ref.c',
  21. ]
  22. b2_includes = [
  23. 'ref',
  24. ]
  25. def b2_ext_kwargs(pc, prefer_system, system_prefix):
  26. if prefer_system:
  27. if system_prefix:
  28. print('Detected and preferring libb2 [via BORG_LIBB2_PREFIX]')
  29. return dict(include_dirs=[os.path.join(system_prefix, 'include')],
  30. library_dirs=[os.path.join(system_prefix, 'lib')],
  31. libraries=['b2'])
  32. if pc and pc.installed('libb2', '>= 0.98.1'):
  33. print('Detected and preferring libb2 [via pkg-config]')
  34. return pc.parse('libb2')
  35. print('Using bundled BLAKE2')
  36. sources = multi_join(b2_sources, b2_bundled_path)
  37. include_dirs = multi_join(b2_includes, b2_bundled_path)
  38. define_macros = [('BORG_USE_BUNDLED_B2', 'YES')]
  39. return dict(sources=sources, include_dirs=include_dirs, define_macros=define_macros)