setup.py 10 KB

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