setup_crypto.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Support code for building a C extension with crypto from OpenSSL
  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 openssl)
  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. def crypto_ext_kwargs(**kwargs):
  11. """amend kwargs with crypto stuff for a distutils.extension.Extension initialization.
  12. kwargs: distutils.extension.Extension kwargs that should be amended
  13. returns: amended kwargs
  14. """
  15. def multi_join(paths, *path_segments):
  16. """apply os.path.join on a list of paths"""
  17. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  18. system_prefix = os.environ.get('BORG_OPENSSL_PREFIX')
  19. if system_prefix:
  20. print('Detected system OpenSSL')
  21. else:
  22. raise Exception('Could not find OpenSSL lib/headers, please set BORG_OPENSSL_PREFIX')
  23. include_dirs = kwargs.get('include_dirs', [])
  24. include_dirs += multi_join(['include'], system_prefix)
  25. library_dirs = kwargs.get('library_dirs', [])
  26. library_dirs += multi_join(['lib'], system_prefix)
  27. libraries = kwargs.get('libraries', [])
  28. libraries += ['crypto', ]
  29. ret = dict(**kwargs)
  30. ret.update(dict(include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  31. return ret