setup.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # borgbackup - main setup code (see also other setup_*.py files)
  2. import os
  3. import sys
  4. from collections import defaultdict
  5. from glob import glob
  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, find_packages, Extension
  12. from setuptools.command.sdist import sdist
  13. from distutils.command.clean import clean
  14. try:
  15. from Cython.Build import cythonize
  16. except ImportError:
  17. cythonize = None
  18. import setup_checksums
  19. import setup_compress
  20. import setup_crypto
  21. import setup_docs
  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: blake2 (>= 0.98.1)
  42. prefer_system_libb2 = not bool(os.environ.get('BORG_USE_BUNDLED_B2'))
  43. system_prefix_libb2 = os.environ.get('BORG_LIBB2_PREFIX')
  44. # needed: lz4 (>= 1.7.0 / r129)
  45. prefer_system_liblz4 = not bool(os.environ.get('BORG_USE_BUNDLED_LZ4'))
  46. system_prefix_liblz4 = os.environ.get('BORG_LIBLZ4_PREFIX')
  47. # needed: zstd (>= 1.3.0)
  48. prefer_system_libzstd = not bool(os.environ.get('BORG_USE_BUNDLED_ZSTD'))
  49. system_prefix_libzstd = os.environ.get('BORG_LIBZSTD_PREFIX')
  50. prefer_system_libxxhash = not bool(os.environ.get('BORG_USE_BUNDLED_XXHASH'))
  51. system_prefix_libxxhash = os.environ.get('BORG_LIBXXHASH_PREFIX')
  52. # Number of threads to use for cythonize, not used on windows
  53. cpu_threads = multiprocessing.cpu_count() if multiprocessing else 1
  54. # Are we building on ReadTheDocs?
  55. on_rtd = os.environ.get('READTHEDOCS')
  56. install_requires = [
  57. # we are rather picky about msgpack versions, because a good working msgpack is
  58. # very important for borg, see: https://github.com/borgbackup/borg/issues/3753
  59. 'msgpack >=0.5.6, <=1.0.0',
  60. # Please note:
  61. # using any other version is not supported by borg development and
  62. # any feedback related to issues caused by this will be ignored.
  63. ]
  64. # note for package maintainers: if you package borgbackup for distribution,
  65. # please (if available) add pyfuse3 (preferably) or llfuse (not maintained any more)
  66. # as a *requirement*. "borg mount" needs one of them to work.
  67. # if neither is available, do not require it, most of borgbackup will work.
  68. extras_require = {
  69. 'llfuse': [
  70. 'llfuse >= 1.3.8',
  71. ],
  72. 'pyfuse3': [
  73. 'pyfuse3 >= 3.1.1',
  74. ],
  75. 'nofuse': [],
  76. }
  77. compress_source = 'src/borg/compress.pyx'
  78. crypto_ll_source = 'src/borg/crypto/low_level.pyx'
  79. crypto_helpers = 'src/borg/crypto/_crypto_helpers.c'
  80. chunker_source = 'src/borg/chunker.pyx'
  81. hashindex_source = 'src/borg/hashindex.pyx'
  82. item_source = 'src/borg/item.pyx'
  83. checksums_source = 'src/borg/algorithms/checksums.pyx'
  84. platform_posix_source = 'src/borg/platform/posix.pyx'
  85. platform_linux_source = 'src/borg/platform/linux.pyx'
  86. platform_syncfilerange_source = 'src/borg/platform/syncfilerange.pyx'
  87. platform_darwin_source = 'src/borg/platform/darwin.pyx'
  88. platform_freebsd_source = 'src/borg/platform/freebsd.pyx'
  89. platform_windows_source = 'src/borg/platform/windows.pyx'
  90. cython_sources = [
  91. compress_source,
  92. crypto_ll_source,
  93. chunker_source,
  94. hashindex_source,
  95. item_source,
  96. checksums_source,
  97. platform_posix_source,
  98. platform_linux_source,
  99. platform_syncfilerange_source,
  100. platform_freebsd_source,
  101. platform_darwin_source,
  102. platform_windows_source,
  103. ]
  104. if cythonize:
  105. Sdist = sdist
  106. else:
  107. class Sdist(sdist):
  108. def __init__(self, *args, **kwargs):
  109. raise Exception('Cython is required to run sdist')
  110. cython_c_files = [fn.replace('.pyx', '.c') for fn in cython_sources]
  111. if not on_rtd and not all(os.path.exists(path) for path in cython_c_files):
  112. raise ImportError('The GIT version of Borg needs Cython. Install Cython or use a released version.')
  113. def rm(file):
  114. try:
  115. os.unlink(file)
  116. print('rm', file)
  117. except FileNotFoundError:
  118. pass
  119. class Clean(clean):
  120. def run(self):
  121. super().run()
  122. for source in cython_sources:
  123. genc = source.replace('.pyx', '.c')
  124. rm(genc)
  125. compiled_glob = source.replace('.pyx', '.cpython*')
  126. for compiled in sorted(glob(compiled_glob)):
  127. rm(compiled)
  128. cmdclass = {
  129. 'build_ext': build_ext,
  130. 'build_usage': setup_docs.build_usage,
  131. 'build_man': setup_docs.build_man,
  132. 'sdist': Sdist,
  133. 'clean': Clean,
  134. }
  135. ext_modules = []
  136. if not on_rtd:
  137. def members_appended(*ds):
  138. result = defaultdict(list)
  139. for d in ds:
  140. for k, v in d.items():
  141. assert isinstance(v, list)
  142. result[k].extend(v)
  143. return result
  144. try:
  145. import pkgconfig as pc
  146. except ImportError:
  147. print('Warning: can not import pkgconfig python package.')
  148. pc = None
  149. crypto_ext_kwargs = members_appended(
  150. dict(sources=[crypto_ll_source, crypto_helpers]),
  151. setup_crypto.crypto_ext_kwargs(pc, system_prefix_openssl),
  152. setup_crypto.b2_ext_kwargs(pc, prefer_system_libb2, system_prefix_libb2),
  153. )
  154. compress_ext_kwargs = members_appended(
  155. dict(sources=[compress_source]),
  156. setup_compress.lz4_ext_kwargs(pc, prefer_system_liblz4, system_prefix_liblz4),
  157. setup_compress.zstd_ext_kwargs(pc, prefer_system_libzstd, system_prefix_libzstd,
  158. multithreaded=False, legacy=False),
  159. )
  160. checksums_ext_kwargs = members_appended(
  161. dict(sources=[checksums_source]),
  162. setup_checksums.xxhash_ext_kwargs(pc, prefer_system_libxxhash, system_prefix_libxxhash),
  163. )
  164. ext_modules += [
  165. Extension('borg.crypto.low_level', **crypto_ext_kwargs),
  166. Extension('borg.compress', **compress_ext_kwargs),
  167. Extension('borg.hashindex', [hashindex_source]),
  168. Extension('borg.item', [item_source]),
  169. Extension('borg.chunker', [chunker_source]),
  170. Extension('borg.algorithms.checksums', **checksums_ext_kwargs),
  171. ]
  172. posix_ext = Extension('borg.platform.posix', [platform_posix_source])
  173. linux_ext = Extension('borg.platform.linux', [platform_linux_source], libraries=['acl'])
  174. syncfilerange_ext = Extension('borg.platform.syncfilerange', [platform_syncfilerange_source])
  175. freebsd_ext = Extension('borg.platform.freebsd', [platform_freebsd_source])
  176. darwin_ext = Extension('borg.platform.darwin', [platform_darwin_source])
  177. windows_ext = Extension('borg.platform.windows', [platform_windows_source])
  178. if not is_win32:
  179. ext_modules.append(posix_ext)
  180. else:
  181. ext_modules.append(windows_ext)
  182. if sys.platform == 'linux':
  183. ext_modules.append(linux_ext)
  184. ext_modules.append(syncfilerange_ext)
  185. elif sys.platform.startswith('freebsd'):
  186. ext_modules.append(freebsd_ext)
  187. elif sys.platform == 'darwin':
  188. ext_modules.append(darwin_ext)
  189. # sometimes there's no need to cythonize
  190. # this breaks chained commands like 'clean sdist'
  191. cythonizing = len(sys.argv) > 1 and sys.argv[1] not in ('clean', 'egg_info', '--help-commands', '--version') \
  192. and '--help' not in sys.argv[1:]
  193. if cythonize and cythonizing:
  194. cython_opts = dict(
  195. # default language_level will be '3str' starting from Cython 3.0.0,
  196. # but old cython versions (< 0.29) do not know that, thus we use 3 for now.
  197. compiler_directives={'language_level': 3},
  198. )
  199. if not is_win32:
  200. # compile .pyx extensions to .c in parallel, does not work on windows
  201. cython_opts['nthreads'] = cpu_threads + 1
  202. # generate C code from Cython for ALL supported platforms, so we have them in the sdist.
  203. # the sdist does not require Cython at install time, so we need all as C.
  204. cythonize([posix_ext, linux_ext, syncfilerange_ext, freebsd_ext, darwin_ext, windows_ext], **cython_opts)
  205. # generate C code from Cython for THIS platform (and for all platform-independent Cython parts).
  206. ext_modules = cythonize(ext_modules, **cython_opts)
  207. setup(
  208. name='borgbackup',
  209. use_scm_version={
  210. 'write_to': 'src/borg/_version.py',
  211. },
  212. author='The Borg Collective (see AUTHORS file)',
  213. author_email='borgbackup@python.org',
  214. url='https://borgbackup.readthedocs.io/',
  215. description='Deduplicated, encrypted, authenticated and compressed backups',
  216. long_description=setup_docs.long_desc_from_readme(),
  217. license='BSD',
  218. platforms=['Linux', 'MacOS X', 'FreeBSD', 'OpenBSD', 'NetBSD', ],
  219. classifiers=[
  220. 'Development Status :: 3 - Alpha',
  221. 'Environment :: Console',
  222. 'Intended Audience :: System Administrators',
  223. 'License :: OSI Approved :: BSD License',
  224. 'Operating System :: POSIX :: BSD :: FreeBSD',
  225. 'Operating System :: POSIX :: BSD :: OpenBSD',
  226. 'Operating System :: POSIX :: BSD :: NetBSD',
  227. 'Operating System :: MacOS :: MacOS X',
  228. 'Operating System :: POSIX :: Linux',
  229. 'Programming Language :: Python',
  230. 'Programming Language :: Python :: 3',
  231. 'Programming Language :: Python :: 3.6',
  232. 'Programming Language :: Python :: 3.7',
  233. 'Programming Language :: Python :: 3.8',
  234. 'Programming Language :: Python :: 3.9',
  235. 'Topic :: Security :: Cryptography',
  236. 'Topic :: System :: Archiving :: Backup',
  237. ],
  238. packages=find_packages('src'),
  239. package_dir={'': 'src'},
  240. zip_safe=False,
  241. entry_points={
  242. 'console_scripts': [
  243. 'borg = borg.archiver:main',
  244. 'borgfs = borg.archiver:main',
  245. ]
  246. },
  247. # See also the MANIFEST.in file.
  248. # We want to install all the files in the package directories...
  249. include_package_data=True,
  250. # ...except the source files which have been compiled (C extensions):
  251. exclude_package_data={
  252. '': ['*.c', '*.h', '*.pyx', ],
  253. },
  254. cmdclass=cmdclass,
  255. ext_modules=ext_modules,
  256. setup_requires=['setuptools_scm>=1.7'],
  257. install_requires=install_requires,
  258. extras_require=extras_require,
  259. python_requires='>=3.6',
  260. )