setup_crypto.py 2.0 KB

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