setup.py 9.1 KB

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