setup_zstd.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Support code for building a C extension with zstd files
  2. #
  3. # Copyright (c) 2016-present, Gregory Szorc
  4. # 2017-present, Thomas Waldmann (mods to make it more generic)
  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. # zstd files, structure as seen in zstd project repository:
  11. zstd_sources = [
  12. 'lib/common/debug.c',
  13. 'lib/common/entropy_common.c',
  14. 'lib/common/error_private.c',
  15. 'lib/common/fse_decompress.c',
  16. 'lib/common/pool.c',
  17. 'lib/common/threading.c',
  18. 'lib/common/xxhash.c',
  19. 'lib/common/zstd_common.c',
  20. 'lib/compress/fse_compress.c',
  21. 'lib/compress/hist.c',
  22. 'lib/compress/huf_compress.c',
  23. 'lib/compress/zstd_compress.c',
  24. 'lib/compress/zstd_double_fast.c',
  25. 'lib/compress/zstd_fast.c',
  26. 'lib/compress/zstd_lazy.c',
  27. 'lib/compress/zstd_ldm.c',
  28. 'lib/compress/zstd_opt.c',
  29. 'lib/compress/zstdmt_compress.c',
  30. 'lib/decompress/huf_decompress.c',
  31. 'lib/decompress/zstd_ddict.c',
  32. 'lib/decompress/zstd_decompress.c',
  33. 'lib/decompress/zstd_decompress_block.c',
  34. 'lib/dictBuilder/cover.c',
  35. 'lib/dictBuilder/divsufsort.c',
  36. 'lib/dictBuilder/fastcover.c',
  37. 'lib/dictBuilder/zdict.c',
  38. ]
  39. zstd_sources_legacy = [
  40. 'lib/deprecated/zbuff_common.c',
  41. 'lib/deprecated/zbuff_compress.c',
  42. 'lib/deprecated/zbuff_decompress.c',
  43. 'lib/legacy/zstd_v01.c',
  44. 'lib/legacy/zstd_v02.c',
  45. 'lib/legacy/zstd_v03.c',
  46. 'lib/legacy/zstd_v04.c',
  47. 'lib/legacy/zstd_v05.c',
  48. 'lib/legacy/zstd_v06.c',
  49. 'lib/legacy/zstd_v07.c',
  50. ]
  51. zstd_includes = [
  52. 'lib',
  53. 'lib/common',
  54. 'lib/compress',
  55. 'lib/decompress',
  56. 'lib/dictBuilder',
  57. ]
  58. zstd_includes_legacy = [
  59. 'lib/deprecated',
  60. 'lib/legacy',
  61. ]
  62. def zstd_system_prefix(prefixes):
  63. for prefix in prefixes:
  64. filename = os.path.join(prefix, 'include', 'zstd.h')
  65. if os.path.exists(filename):
  66. with open(filename, 'rb') as fd:
  67. if b'ZSTD_getFrameContentSize' in fd.read(): # checks for zstd >= 1.3.0
  68. return prefix
  69. def zstd_ext_kwargs(bundled_path, system_prefix=None, system=False, multithreaded=False, legacy=False, **kwargs):
  70. """amend kwargs with zstd suff for a distutils.extension.Extension initialization.
  71. bundled_path: relative (to this file) path to the bundled library source code files
  72. system_prefix: where the system-installed library can be found
  73. system: True: use the system-installed shared library, False: use the bundled library code
  74. multithreaded: True: define ZSTD_MULTITHREAD
  75. legacy: include legacy API support
  76. kwargs: distutils.extension.Extension kwargs that should be amended
  77. returns: amended kwargs
  78. """
  79. def multi_join(paths, *path_segments):
  80. """apply os.path.join on a list of paths"""
  81. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  82. use_system = system and system_prefix is not None
  83. sources = kwargs.get('sources', [])
  84. if not use_system:
  85. sources += multi_join(zstd_sources, bundled_path)
  86. if legacy:
  87. sources += multi_join(zstd_sources_legacy, bundled_path)
  88. include_dirs = kwargs.get('include_dirs', [])
  89. if use_system:
  90. include_dirs += multi_join(['include'], system_prefix)
  91. else:
  92. include_dirs += multi_join(zstd_includes, bundled_path)
  93. if legacy:
  94. include_dirs += multi_join(zstd_includes_legacy, bundled_path)
  95. library_dirs = kwargs.get('library_dirs', [])
  96. if use_system:
  97. library_dirs += multi_join(['lib'], system_prefix)
  98. libraries = kwargs.get('libraries', [])
  99. if use_system:
  100. libraries += ['zstd', ]
  101. extra_compile_args = kwargs.get('extra_compile_args', [])
  102. if multithreaded:
  103. extra_compile_args += ['-DZSTD_MULTITHREAD', ]
  104. if not use_system:
  105. extra_compile_args += ['-DZSTDLIB_VISIBILITY=', '-DZDICTLIB_VISIBILITY=', '-DZSTDERRORLIB_VISIBILITY=', ]
  106. # '-fvisibility=hidden' does not work, doesn't find PyInit_compress then
  107. if legacy:
  108. extra_compile_args += ['-DZSTD_LEGACY_SUPPORT=1', ]
  109. ret = dict(**kwargs)
  110. ret.update(dict(sources=sources, extra_compile_args=extra_compile_args,
  111. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  112. return ret