setup_xxhash.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Support code for building a C extension with xxhash 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. # 2020-present, Gianfranco Costamagna (code for xxhash)
  6. # All rights reserved.
  7. #
  8. # This software may be modified and distributed under the terms
  9. # of the BSD license. See the LICENSE file for details.
  10. import os
  11. # xxhash files, structure as seen in XXHASH (reference implementation) project repository:
  12. xxhash_sources = [
  13. # 'xxhash.c', # do not compile xxhash.c (it is included by xxhash-libselect.h)
  14. ]
  15. xxhash_includes = [
  16. '',
  17. ]
  18. def xxhash_system_prefix(prefixes):
  19. for prefix in prefixes:
  20. filename = os.path.join(prefix, 'include', 'xxhash.h')
  21. if os.path.exists(filename):
  22. with open(filename, 'rb') as fd:
  23. if b'XXH64_digest' in fd.read():
  24. return prefix
  25. def xxhash_ext_kwargs(bundled_path, system_prefix=None, system=False, **kwargs):
  26. """amend kwargs with xxhash stuff for a distutils.extension.Extension initialization.
  27. bundled_path: relative (to this file) path to the bundled library source code files
  28. system_prefix: where the system-installed library can be found
  29. system: True: use the system-installed shared library, False: use the bundled library code
  30. kwargs: distutils.extension.Extension kwargs that should be amended
  31. returns: amended kwargs
  32. """
  33. def multi_join(paths, *path_segments):
  34. """apply os.path.join on a list of paths"""
  35. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  36. use_system = system and system_prefix is not None
  37. sources = kwargs.get('sources', [])
  38. if not use_system:
  39. sources += multi_join(xxhash_sources, bundled_path)
  40. include_dirs = kwargs.get('include_dirs', [])
  41. if use_system:
  42. include_dirs += multi_join(['include'], system_prefix)
  43. else:
  44. include_dirs += multi_join(xxhash_includes, bundled_path)
  45. library_dirs = kwargs.get('library_dirs', [])
  46. if use_system:
  47. library_dirs += multi_join(['lib'], system_prefix)
  48. libraries = kwargs.get('libraries', [])
  49. if use_system:
  50. libraries += ['xxhash', ]
  51. extra_compile_args = kwargs.get('extra_compile_args', [])
  52. if not use_system:
  53. extra_compile_args += [] # not used yet
  54. define_macros = kwargs.get('define_macros', [])
  55. ret = dict(**kwargs)
  56. ret.update(dict(sources=sources, extra_compile_args=extra_compile_args, define_macros=define_macros,
  57. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  58. return ret