setup_b2.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. b2_sources = [
  12. 'ref/blake2b-ref.c',
  13. ]
  14. b2_includes = [
  15. 'ref',
  16. ]
  17. def b2_system_prefix(prefixes):
  18. for prefix in prefixes:
  19. filename = os.path.join(prefix, 'include', 'blake2.h')
  20. if os.path.exists(filename):
  21. with open(filename, 'rb') as fd:
  22. if b'blake2b_init' in fd.read():
  23. return prefix
  24. def b2_ext_kwargs(bundled_path, system_prefix=None, system=False, **kwargs):
  25. """amend kwargs with b2 stuff for a distutils.extension.Extension initialization.
  26. bundled_path: relative (to this file) path to the bundled library source code files
  27. system_prefix: where the system-installed library can be found
  28. system: True: use the system-installed shared library, False: use the bundled library code
  29. kwargs: distutils.extension.Extension kwargs that should be amended
  30. returns: amended kwargs
  31. """
  32. def multi_join(paths, *path_segments):
  33. """apply os.path.join on a list of paths"""
  34. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  35. use_system = system and system_prefix is not None
  36. sources = kwargs.get('sources', [])
  37. if not use_system:
  38. sources += multi_join(b2_sources, bundled_path)
  39. include_dirs = kwargs.get('include_dirs', [])
  40. if use_system:
  41. include_dirs += multi_join(['include'], system_prefix)
  42. else:
  43. include_dirs += multi_join(b2_includes, bundled_path)
  44. library_dirs = kwargs.get('library_dirs', [])
  45. if use_system:
  46. library_dirs += multi_join(['lib'], system_prefix)
  47. libraries = kwargs.get('libraries', [])
  48. if use_system:
  49. libraries += ['b2', ]
  50. extra_compile_args = kwargs.get('extra_compile_args', [])
  51. if not use_system:
  52. extra_compile_args += [] # not used yet
  53. ret = dict(**kwargs)
  54. ret.update(dict(sources=sources, extra_compile_args=extra_compile_args,
  55. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  56. return ret