setup.py 9.8 KB

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