setup_zstd.py 4.2 KB

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