setup.py 10 KB

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