setup_zstd.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_ext_kwargs(bundled_path, prefer_system, multithreaded=False, legacy=False, **kwargs):
  63. """amend kwargs with zstd suff for a distutils.extension.Extension initialization.
  64. bundled_path: relative (to this file) path to the bundled library source code files
  65. prefer_system: prefer the system-installed library (if found) over the bundled C code
  66. multithreaded: True: define ZSTD_MULTITHREAD
  67. legacy: include legacy API support
  68. kwargs: distutils.extension.Extension kwargs that should be amended
  69. returns: amended kwargs
  70. """
  71. def multi_join(paths, *path_segments):
  72. """apply os.path.join on a list of paths"""
  73. return [os.path.join(*(path_segments + (path, ))) for path in paths]
  74. define_macros = kwargs.get('define_macros', [])
  75. system_prefix = os.environ.get('BORG_LIBZSTD_PREFIX')
  76. if prefer_system and system_prefix:
  77. print('Detected and preferring libzstd over bundled ZSTD')
  78. define_macros.append(('BORG_USE_LIBZSTD', 'YES'))
  79. system = True
  80. else:
  81. print('Using bundled ZSTD')
  82. system = False
  83. use_system = system and system_prefix is not None
  84. sources = kwargs.get('sources', [])
  85. if not use_system:
  86. sources += multi_join(zstd_sources, bundled_path)
  87. if legacy:
  88. sources += multi_join(zstd_sources_legacy, bundled_path)
  89. include_dirs = kwargs.get('include_dirs', [])
  90. if use_system:
  91. include_dirs += multi_join(['include'], system_prefix)
  92. else:
  93. include_dirs += multi_join(zstd_includes, bundled_path)
  94. if legacy:
  95. include_dirs += multi_join(zstd_includes_legacy, bundled_path)
  96. library_dirs = kwargs.get('library_dirs', [])
  97. if use_system:
  98. library_dirs += multi_join(['lib'], system_prefix)
  99. libraries = kwargs.get('libraries', [])
  100. if use_system:
  101. libraries += ['zstd', ]
  102. extra_compile_args = kwargs.get('extra_compile_args', [])
  103. if multithreaded:
  104. extra_compile_args += ['-DZSTD_MULTITHREAD', ]
  105. if not use_system:
  106. extra_compile_args += ['-DZSTDLIB_VISIBILITY=', '-DZDICTLIB_VISIBILITY=', '-DZSTDERRORLIB_VISIBILITY=', ]
  107. # '-fvisibility=hidden' does not work, doesn't find PyInit_compress then
  108. if legacy:
  109. extra_compile_args += ['-DZSTD_LEGACY_SUPPORT=1', ]
  110. ret = dict(**kwargs)
  111. ret.update(dict(sources=sources, extra_compile_args=extra_compile_args,
  112. include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries))
  113. return ret