setup_zstd.py 4.5 KB

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