setup_compress.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Support code for building a C extension with compression code
  2. import os
  3. def multi_join(paths, *path_segments):
  4. """apply os.path.join on a list of paths"""
  5. return [os.path.join(*(path_segments + (path,))) for path in paths]
  6. # zstd files, structure as seen in zstd project repository:
  7. # path relative (to this file) to the bundled library source code files
  8. zstd_bundled_path = 'src/borg/algorithms/zstd'
  9. zstd_sources = [
  10. 'lib/common/debug.c',
  11. 'lib/common/entropy_common.c',
  12. 'lib/common/error_private.c',
  13. 'lib/common/fse_decompress.c',
  14. 'lib/common/pool.c',
  15. 'lib/common/threading.c',
  16. 'lib/common/xxhash.c',
  17. 'lib/common/zstd_common.c',
  18. 'lib/compress/fse_compress.c',
  19. 'lib/compress/hist.c',
  20. 'lib/compress/huf_compress.c',
  21. 'lib/compress/zstd_compress.c',
  22. 'lib/compress/zstd_compress_literals.c',
  23. 'lib/compress/zstd_compress_sequences.c',
  24. 'lib/compress/zstd_compress_superblock.c',
  25. 'lib/compress/zstd_double_fast.c',
  26. 'lib/compress/zstd_fast.c',
  27. 'lib/compress/zstd_lazy.c',
  28. 'lib/compress/zstd_ldm.c',
  29. 'lib/compress/zstd_opt.c',
  30. 'lib/compress/zstdmt_compress.c',
  31. 'lib/decompress/huf_decompress.c',
  32. 'lib/decompress/zstd_ddict.c',
  33. 'lib/decompress/zstd_decompress.c',
  34. 'lib/decompress/zstd_decompress_block.c',
  35. 'lib/dictBuilder/cover.c',
  36. 'lib/dictBuilder/divsufsort.c',
  37. 'lib/dictBuilder/fastcover.c',
  38. 'lib/dictBuilder/zdict.c',
  39. ]
  40. zstd_sources_legacy = [
  41. 'lib/deprecated/zbuff_common.c',
  42. 'lib/deprecated/zbuff_compress.c',
  43. 'lib/deprecated/zbuff_decompress.c',
  44. 'lib/legacy/zstd_v01.c',
  45. 'lib/legacy/zstd_v02.c',
  46. 'lib/legacy/zstd_v03.c',
  47. 'lib/legacy/zstd_v04.c',
  48. 'lib/legacy/zstd_v05.c',
  49. 'lib/legacy/zstd_v06.c',
  50. 'lib/legacy/zstd_v07.c',
  51. ]
  52. zstd_includes = [
  53. 'lib',
  54. 'lib/common',
  55. 'lib/compress',
  56. 'lib/decompress',
  57. 'lib/dictBuilder',
  58. ]
  59. zstd_includes_legacy = [
  60. 'lib/deprecated',
  61. 'lib/legacy',
  62. ]
  63. def zstd_ext_kwargs(pc, prefer_system, system_prefix, multithreaded=False, legacy=False):
  64. if prefer_system:
  65. if system_prefix:
  66. print('Detected and preferring libzstd [via BORG_LIBZSTD_PREFIX]')
  67. return dict(include_dirs=[os.path.join(system_prefix, 'include')],
  68. library_dirs=[os.path.join(system_prefix, 'lib')],
  69. libraries=['zstd'])
  70. if pc and pc.installed('libzstd', '>= 1.3.0'):
  71. print('Detected and preferring libzstd [via pkg-config]')
  72. return pc.parse('libzstd')
  73. print('Using bundled ZSTD')
  74. sources = multi_join(zstd_sources, zstd_bundled_path)
  75. if legacy:
  76. sources += multi_join(zstd_sources_legacy, zstd_bundled_path)
  77. include_dirs = multi_join(zstd_includes, zstd_bundled_path)
  78. if legacy:
  79. include_dirs += multi_join(zstd_includes_legacy, zstd_bundled_path)
  80. extra_compile_args = ['-DZSTDLIB_VISIBILITY=', '-DZDICTLIB_VISIBILITY=', '-DZSTDERRORLIB_VISIBILITY=', ]
  81. # '-fvisibility=hidden' does not work, doesn't find PyInit_compress then
  82. if legacy:
  83. extra_compile_args += ['-DZSTD_LEGACY_SUPPORT=1', ]
  84. if multithreaded:
  85. extra_compile_args += ['-DZSTD_MULTITHREAD', ]
  86. define_macros = [('BORG_USE_BUNDLED_ZSTD', 'YES')]
  87. return dict(sources=sources, include_dirs=include_dirs,
  88. extra_compile_args=extra_compile_args, define_macros=define_macros)
  89. # lz4 files, structure as seen in lz4 project repository:
  90. # path relative (to this file) to the bundled library source code files
  91. lz4_bundled_path = 'src/borg/algorithms/lz4'
  92. lz4_sources = [
  93. 'lib/lz4.c',
  94. ]
  95. lz4_includes = [
  96. 'lib',
  97. ]
  98. def lz4_ext_kwargs(pc, prefer_system, system_prefix):
  99. if prefer_system:
  100. if system_prefix:
  101. print('Detected and preferring liblz4 [via BORG_LIBLZ4_PREFIX]')
  102. return dict(include_dirs=[os.path.join(system_prefix, 'include')],
  103. library_dirs=[os.path.join(system_prefix, 'lib')],
  104. libraries=['lz4'])
  105. if pc and pc.installed('liblz4', '>= 1.7.0'):
  106. print('Detected and preferring liblz4 [via pkg-config]')
  107. return pc.parse('liblz4')
  108. print('Using bundled LZ4')
  109. sources = multi_join(lz4_sources, lz4_bundled_path)
  110. include_dirs = multi_join(lz4_includes, lz4_bundled_path)
  111. define_macros = [('BORG_USE_BUNDLED_LZ4', 'YES')]
  112. return dict(sources=sources, include_dirs=include_dirs, define_macros=define_macros)