setup_zstd.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Support code for building a C extension with zstd
  2. import os
  3. # zstd files, structure as seen in zstd project repository:
  4. # bundled_path: relative (to this file) path to the bundled library source code files
  5. bundled_path = 'src/borg/algorithms/zstd'
  6. zstd_sources = [
  7. 'lib/common/debug.c',
  8. 'lib/common/entropy_common.c',
  9. 'lib/common/error_private.c',
  10. 'lib/common/fse_decompress.c',
  11. 'lib/common/pool.c',
  12. 'lib/common/threading.c',
  13. 'lib/common/xxhash.c',
  14. 'lib/common/zstd_common.c',
  15. 'lib/compress/fse_compress.c',
  16. 'lib/compress/hist.c',
  17. 'lib/compress/huf_compress.c',
  18. 'lib/compress/zstd_compress.c',
  19. 'lib/compress/zstd_double_fast.c',
  20. 'lib/compress/zstd_fast.c',
  21. 'lib/compress/zstd_lazy.c',
  22. 'lib/compress/zstd_ldm.c',
  23. 'lib/compress/zstd_opt.c',
  24. 'lib/compress/zstdmt_compress.c',
  25. 'lib/decompress/huf_decompress.c',
  26. 'lib/decompress/zstd_ddict.c',
  27. 'lib/decompress/zstd_decompress.c',
  28. 'lib/decompress/zstd_decompress_block.c',
  29. 'lib/dictBuilder/cover.c',
  30. 'lib/dictBuilder/divsufsort.c',
  31. 'lib/dictBuilder/fastcover.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 multi_join(paths, *path_segments):
  58. """apply os.path.join on a list of paths"""
  59. return [os.path.join(*(path_segments + (path,))) for path in paths]
  60. def zstd_ext_kwargs(prefer_system, multithreaded=False, legacy=False):
  61. if prefer_system:
  62. system_prefix = os.environ.get('BORG_LIBZSTD_PREFIX')
  63. if system_prefix:
  64. print('Detected and preferring libzstd [via BORG_LIBZSTD_PREFIX]')
  65. return dict(include_dirs=[os.path.join(system_prefix, 'include')],
  66. library_dirs=[os.path.join(system_prefix, 'lib')],
  67. libraries=['zstd'])
  68. import pkgconfig
  69. if pkgconfig.installed('libzstd', '>= 1.3.0'):
  70. print('Detected and preferring libzstd [via pkg-config]')
  71. return pkgconfig.parse('libzstd')
  72. print('Using bundled ZSTD')
  73. sources = multi_join(zstd_sources, bundled_path)
  74. if legacy:
  75. sources += multi_join(zstd_sources_legacy, bundled_path)
  76. include_dirs = multi_join(zstd_includes, bundled_path)
  77. if legacy:
  78. include_dirs += multi_join(zstd_includes_legacy, bundled_path)
  79. extra_compile_args = ['-DZSTDLIB_VISIBILITY=', '-DZDICTLIB_VISIBILITY=', '-DZSTDERRORLIB_VISIBILITY=', ]
  80. # '-fvisibility=hidden' does not work, doesn't find PyInit_compress then
  81. if legacy:
  82. extra_compile_args += ['-DZSTD_LEGACY_SUPPORT=1', ]
  83. if multithreaded:
  84. extra_compile_args += ['-DZSTD_MULTITHREAD', ]
  85. define_macros = [('BORG_USE_BUNDLED_ZSTD', 'YES')]
  86. return dict(sources=sources, include_dirs=include_dirs,
  87. extra_compile_args=extra_compile_args, define_macros=define_macros)