setup.py 12 KB

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