setup_zstd.py 4.4 KB

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