setup.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # borgbackup - main setup code (see also pyproject.toml and other setup_*.py files)
  2. import os
  3. import re
  4. import sys
  5. from collections import defaultdict
  6. try:
  7. import multiprocessing
  8. except ImportError:
  9. multiprocessing = None
  10. from setuptools.command.build_ext import build_ext
  11. from setuptools import setup, Extension
  12. from setuptools.command.sdist import sdist
  13. try:
  14. from Cython.Build import cythonize
  15. except ImportError:
  16. cythonize = None
  17. sys.path += [os.path.dirname(__file__)]
  18. import setup_checksums
  19. import setup_compress
  20. import setup_crypto
  21. is_win32 = sys.platform.startswith('win32')
  22. # How the build process finds the system libs / uses the bundled code:
  23. #
  24. # 1. it will try to use (system) libs (see 1.1. and 1.2.),
  25. # except if you use these env vars to force using the bundled code:
  26. # BORG_USE_BUNDLED_XXX undefined --> try using system lib
  27. # BORG_USE_BUNDLED_XXX=YES --> use the bundled code
  28. # Note: do not use =NO, that is not supported!
  29. # 1.1. if BORG_LIBXXX_PREFIX is set, it will use headers and libs from there.
  30. # 1.2. if not and pkg-config can locate the lib, the lib located by
  31. # pkg-config will be used. We use the pkg-config tool via the pkgconfig
  32. # python package, which must be installed before invoking setup.py.
  33. # if pkgconfig is not installed, this step is skipped.
  34. # 2. if no system lib could be located via 1.1. or 1.2., it will fall back
  35. # to using the bundled code.
  36. # OpenSSL is required as a (system) lib in any case as we do not bundle it.
  37. # Thus, only step 1.1. and 1.2. apply to openssl (but not 1. and 2.).
  38. # needed: openssl >=1.0.2 or >=1.1.0 (or compatible)
  39. system_prefix_openssl = os.environ.get('BORG_OPENSSL_PREFIX')
  40. # needed: lz4 (>= 1.7.0 / r129)
  41. prefer_system_liblz4 = not bool(os.environ.get('BORG_USE_BUNDLED_LZ4'))
  42. system_prefix_liblz4 = os.environ.get('BORG_LIBLZ4_PREFIX')
  43. # needed: zstd (>= 1.3.0)
  44. prefer_system_libzstd = not bool(os.environ.get('BORG_USE_BUNDLED_ZSTD'))
  45. system_prefix_libzstd = os.environ.get('BORG_LIBZSTD_PREFIX')
  46. prefer_system_libxxhash = not bool(os.environ.get('BORG_USE_BUNDLED_XXHASH'))
  47. system_prefix_libxxhash = os.environ.get('BORG_LIBXXHASH_PREFIX')
  48. # Number of threads to use for cythonize, not used on windows
  49. cpu_threads = multiprocessing.cpu_count() if multiprocessing and multiprocessing.get_start_method() != 'spawn' else None
  50. # Are we building on ReadTheDocs?
  51. on_rtd = os.environ.get('READTHEDOCS')
  52. # Extra cflags for all extensions, usually just warnings we want to explicitly enable
  53. cflags = [
  54. '-Wall',
  55. '-Wextra',
  56. '-Wpointer-arith',
  57. ]
  58. compress_source = 'src/borg/compress.pyx'
  59. crypto_ll_source = 'src/borg/crypto/low_level.pyx'
  60. crypto_helpers = 'src/borg/crypto/_crypto_helpers.c'
  61. chunker_source = 'src/borg/chunker.pyx'
  62. hashindex_source = 'src/borg/hashindex.pyx'
  63. item_source = 'src/borg/item.pyx'
  64. checksums_source = 'src/borg/algorithms/checksums.pyx'
  65. platform_posix_source = 'src/borg/platform/posix.pyx'
  66. platform_linux_source = 'src/borg/platform/linux.pyx'
  67. platform_syncfilerange_source = 'src/borg/platform/syncfilerange.pyx'
  68. platform_darwin_source = 'src/borg/platform/darwin.pyx'
  69. platform_freebsd_source = 'src/borg/platform/freebsd.pyx'
  70. platform_windows_source = 'src/borg/platform/windows.pyx'
  71. cython_sources = [
  72. compress_source,
  73. crypto_ll_source,
  74. chunker_source,
  75. hashindex_source,
  76. item_source,
  77. checksums_source,
  78. platform_posix_source,
  79. platform_linux_source,
  80. platform_syncfilerange_source,
  81. platform_freebsd_source,
  82. platform_darwin_source,
  83. platform_windows_source,
  84. ]
  85. if cythonize:
  86. Sdist = sdist
  87. else:
  88. class Sdist(sdist):
  89. def __init__(self, *args, **kwargs):
  90. raise Exception('Cython is required to run sdist')
  91. cython_c_files = [fn.replace('.pyx', '.c') for fn in cython_sources]
  92. if not on_rtd and not all(os.path.exists(path) for path in cython_c_files):
  93. raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.')
  94. cmdclass = {
  95. 'build_ext': build_ext,
  96. 'sdist': Sdist,
  97. }
  98. ext_modules = []
  99. if not on_rtd:
  100. def members_appended(*ds):
  101. result = defaultdict(list)
  102. for d in ds:
  103. for k, v in d.items():
  104. assert isinstance(v, list)
  105. result[k].extend(v)
  106. return result
  107. try:
  108. import pkgconfig as pc
  109. except ImportError:
  110. print('Warning: can not import pkgconfig python package.')
  111. pc = None
  112. crypto_ext_kwargs = members_appended(
  113. dict(sources=[crypto_ll_source, crypto_helpers]),
  114. setup_crypto.crypto_ext_kwargs(pc, system_prefix_openssl),
  115. dict(extra_compile_args=cflags),
  116. )
  117. compress_ext_kwargs = members_appended(
  118. dict(sources=[compress_source]),
  119. setup_compress.lz4_ext_kwargs(pc, prefer_system_liblz4, system_prefix_liblz4),
  120. setup_compress.zstd_ext_kwargs(pc, prefer_system_libzstd, system_prefix_libzstd,
  121. multithreaded=False, legacy=False),
  122. dict(extra_compile_args=cflags),
  123. )
  124. checksums_ext_kwargs = members_appended(
  125. dict(sources=[checksums_source]),
  126. setup_checksums.xxhash_ext_kwargs(pc, prefer_system_libxxhash, system_prefix_libxxhash),
  127. dict(extra_compile_args=cflags),
  128. )
  129. ext_modules += [
  130. Extension('borg.crypto.low_level', **crypto_ext_kwargs),
  131. Extension('borg.compress', **compress_ext_kwargs),
  132. Extension('borg.hashindex', [hashindex_source], extra_compile_args=cflags),
  133. Extension('borg.item', [item_source], extra_compile_args=cflags),
  134. Extension('borg.chunker', [chunker_source], extra_compile_args=cflags),
  135. Extension('borg.algorithms.checksums', **checksums_ext_kwargs),
  136. ]
  137. posix_ext = Extension('borg.platform.posix', [platform_posix_source], extra_compile_args=cflags)
  138. linux_ext = Extension('borg.platform.linux', [platform_linux_source], libraries=['acl'], extra_compile_args=cflags)
  139. syncfilerange_ext = Extension('borg.platform.syncfilerange', [platform_syncfilerange_source], extra_compile_args=cflags)
  140. freebsd_ext = Extension('borg.platform.freebsd', [platform_freebsd_source], extra_compile_args=cflags)
  141. darwin_ext = Extension('borg.platform.darwin', [platform_darwin_source], extra_compile_args=cflags)
  142. windows_ext = Extension('borg.platform.windows', [platform_windows_source], extra_compile_args=cflags)
  143. if not is_win32:
  144. ext_modules.append(posix_ext)
  145. else:
  146. ext_modules.append(windows_ext)
  147. if sys.platform == 'linux':
  148. ext_modules.append(linux_ext)
  149. ext_modules.append(syncfilerange_ext)
  150. elif sys.platform.startswith('freebsd'):
  151. ext_modules.append(freebsd_ext)
  152. elif sys.platform == 'darwin':
  153. ext_modules.append(darwin_ext)
  154. # sometimes there's no need to cythonize
  155. # this breaks chained commands like 'clean sdist'
  156. cythonizing = len(sys.argv) > 1 and sys.argv[1] not in (
  157. ('clean', 'egg_info', '--help-commands', '--version')) and '--help' not in sys.argv[1:]
  158. if cythonize and cythonizing:
  159. cython_opts = dict(
  160. # 3str is the default in Cython3 and we do not support older Cython releases.
  161. # we only set this to avoid the related FutureWarning from Cython3.
  162. compiler_directives={'language_level': '3str'}
  163. )
  164. if not is_win32:
  165. # compile .pyx extensions to .c in parallel, does not work on windows
  166. cython_opts['nthreads'] = cpu_threads
  167. # generate C code from Cython for ALL supported platforms, so we have them in the sdist.
  168. # the sdist does not require Cython at install time, so we need all as C.
  169. cythonize([posix_ext, linux_ext, syncfilerange_ext, freebsd_ext, darwin_ext, windows_ext], **cython_opts)
  170. # generate C code from Cython for THIS platform (and for all platform-independent Cython parts).
  171. ext_modules = cythonize(ext_modules, **cython_opts)
  172. def long_desc_from_readme():
  173. with open('README.rst') as fd:
  174. long_description = fd.read()
  175. # remove header, but have one \n before first headline
  176. start = long_description.find('What is BorgBackup?')
  177. assert start >= 0
  178. long_description = '\n' + long_description[start:]
  179. # remove badges
  180. long_description = re.compile(r'^\.\. start-badges.*^\.\. end-badges', re.M | re.S).sub('', long_description)
  181. # remove unknown directives
  182. long_description = re.compile(r'^\.\. highlight:: \w+$', re.M).sub('', long_description)
  183. return long_description
  184. setup(cmdclass=cmdclass, ext_modules=ext_modules, long_description=long_desc_from_readme())