setup.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # borgbackup - main setup code (see also setup.cfg and 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, 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_docs
  19. is_win32 = sys.platform.startswith("win32")
  20. is_openbsd = sys.platform.startswith("openbsd")
  21. # Number of threads to use for cythonize, not used on windows
  22. cpu_threads = multiprocessing.cpu_count() if multiprocessing and multiprocessing.get_start_method() != "spawn" else None
  23. # How the build process finds the system libs:
  24. #
  25. # 1. if BORG_{LIBXXX,OPENSSL}_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. # Are we building on ReadTheDocs?
  32. on_rtd = os.environ.get("READTHEDOCS")
  33. # Extra cflags for all extensions, usually just warnings we want to explicitly enable
  34. cflags = ["-Wall", "-Wextra", "-Wpointer-arith"]
  35. compress_source = "src/borg/compress.pyx"
  36. crypto_ll_source = "src/borg/crypto/low_level.pyx"
  37. chunker_source = "src/borg/chunker.pyx"
  38. hashindex_source = "src/borg/hashindex.pyx"
  39. item_source = "src/borg/item.pyx"
  40. checksums_source = "src/borg/checksums.pyx"
  41. platform_posix_source = "src/borg/platform/posix.pyx"
  42. platform_linux_source = "src/borg/platform/linux.pyx"
  43. platform_syncfilerange_source = "src/borg/platform/syncfilerange.pyx"
  44. platform_darwin_source = "src/borg/platform/darwin.pyx"
  45. platform_freebsd_source = "src/borg/platform/freebsd.pyx"
  46. platform_windows_source = "src/borg/platform/windows.pyx"
  47. cython_sources = [
  48. compress_source,
  49. crypto_ll_source,
  50. chunker_source,
  51. hashindex_source,
  52. item_source,
  53. checksums_source,
  54. platform_posix_source,
  55. platform_linux_source,
  56. platform_syncfilerange_source,
  57. platform_freebsd_source,
  58. platform_darwin_source,
  59. platform_windows_source,
  60. ]
  61. if cythonize:
  62. Sdist = sdist
  63. else:
  64. class Sdist(sdist):
  65. def __init__(self, *args, **kwargs):
  66. raise Exception("Cython is required to run sdist")
  67. cython_c_files = [fn.replace(".pyx", ".c") for fn in cython_sources]
  68. if not on_rtd and not all(os.path.exists(path) for path in cython_c_files):
  69. raise ImportError("The GIT version of Borg needs Cython. Install Cython or use a released version.")
  70. def rm(file):
  71. try:
  72. os.unlink(file)
  73. print("rm", file)
  74. except FileNotFoundError:
  75. pass
  76. class Clean(Command):
  77. user_options = []
  78. def initialize_options(self):
  79. pass
  80. def finalize_options(self):
  81. pass
  82. def run(self):
  83. for source in cython_sources:
  84. genc = source.replace(".pyx", ".c")
  85. rm(genc)
  86. compiled_glob = source.replace(".pyx", ".cpython*")
  87. for compiled in sorted(glob(compiled_glob)):
  88. rm(compiled)
  89. cmdclass = {
  90. "build_ext": build_ext,
  91. "build_usage": setup_docs.build_usage,
  92. "build_man": setup_docs.build_man,
  93. "sdist": Sdist,
  94. "clean2": Clean,
  95. }
  96. ext_modules = []
  97. if not on_rtd:
  98. def members_appended(*ds):
  99. result = defaultdict(list)
  100. for d in ds:
  101. for k, v in d.items():
  102. assert isinstance(v, list)
  103. result[k].extend(v)
  104. return result
  105. try:
  106. import pkgconfig as pc
  107. except ImportError:
  108. print("Warning: can not import pkgconfig python package.")
  109. pc = None
  110. def lib_ext_kwargs(pc, prefix_env_var, lib_name, lib_pkg_name, pc_version, lib_subdir="lib"):
  111. system_prefix = os.environ.get(prefix_env_var)
  112. if system_prefix:
  113. print(f"Detected and preferring {lib_pkg_name} [via {prefix_env_var}]")
  114. return dict(
  115. include_dirs=[os.path.join(system_prefix, "include")],
  116. library_dirs=[os.path.join(system_prefix, lib_subdir)],
  117. libraries=[lib_name],
  118. )
  119. if pc and pc.installed(lib_pkg_name, pc_version):
  120. print(f"Detected and preferring {lib_pkg_name} [via pkg-config]")
  121. return pc.parse(lib_pkg_name)
  122. raise Exception(
  123. f"Could not find {lib_name} lib/headers, please set {prefix_env_var} "
  124. f"or ensure {lib_pkg_name}.pc is in PKG_CONFIG_PATH."
  125. )
  126. crypto_ldflags = []
  127. if is_win32:
  128. crypto_ext_lib = lib_ext_kwargs(pc, "BORG_OPENSSL_PREFIX", "libcrypto", "libcrypto", ">=1.1.1", lib_subdir="")
  129. elif is_openbsd:
  130. # use openssl (not libressl) because we need AES-OCB and CHACHA20-POLY1305 via EVP api
  131. crypto_ext_lib = lib_ext_kwargs(pc, "BORG_OPENSSL_PREFIX", "crypto", "libecrypto11", ">=1.1.1")
  132. crypto_ldflags += ["-Wl,-rpath=/usr/local/lib/eopenssl11"]
  133. else:
  134. crypto_ext_lib = lib_ext_kwargs(pc, "BORG_OPENSSL_PREFIX", "crypto", "libcrypto", ">=1.1.1")
  135. crypto_ext_kwargs = members_appended(
  136. dict(sources=[crypto_ll_source]),
  137. crypto_ext_lib,
  138. dict(extra_compile_args=cflags),
  139. dict(extra_link_args=crypto_ldflags),
  140. )
  141. compress_ext_kwargs = members_appended(
  142. dict(sources=[compress_source]),
  143. lib_ext_kwargs(pc, "BORG_LIBLZ4_PREFIX", "lz4", "liblz4", ">= 1.7.0"),
  144. lib_ext_kwargs(pc, "BORG_LIBZSTD_PREFIX", "zstd", "libzstd", ">= 1.3.0"),
  145. dict(extra_compile_args=cflags),
  146. )
  147. checksums_ext_kwargs = members_appended(
  148. dict(sources=[checksums_source]),
  149. lib_ext_kwargs(pc, "BORG_LIBXXHASH_PREFIX", "xxhash", "libxxhash", ">= 0.7.3"),
  150. dict(extra_compile_args=cflags),
  151. )
  152. ext_modules += [
  153. Extension("borg.crypto.low_level", **crypto_ext_kwargs),
  154. Extension("borg.compress", **compress_ext_kwargs),
  155. Extension("borg.hashindex", [hashindex_source], extra_compile_args=cflags),
  156. Extension("borg.item", [item_source], extra_compile_args=cflags),
  157. Extension("borg.chunker", [chunker_source], extra_compile_args=cflags),
  158. Extension("borg.checksums", **checksums_ext_kwargs),
  159. ]
  160. posix_ext = Extension("borg.platform.posix", [platform_posix_source], extra_compile_args=cflags)
  161. linux_ext = Extension("borg.platform.linux", [platform_linux_source], libraries=["acl"], extra_compile_args=cflags)
  162. syncfilerange_ext = Extension(
  163. "borg.platform.syncfilerange", [platform_syncfilerange_source], extra_compile_args=cflags
  164. )
  165. freebsd_ext = Extension("borg.platform.freebsd", [platform_freebsd_source], extra_compile_args=cflags)
  166. darwin_ext = Extension("borg.platform.darwin", [platform_darwin_source], extra_compile_args=cflags)
  167. windows_ext = Extension("borg.platform.windows", [platform_windows_source], extra_compile_args=cflags)
  168. if not is_win32:
  169. ext_modules.append(posix_ext)
  170. else:
  171. ext_modules.append(windows_ext)
  172. if sys.platform == "linux":
  173. ext_modules.append(linux_ext)
  174. ext_modules.append(syncfilerange_ext)
  175. elif sys.platform.startswith("freebsd"):
  176. ext_modules.append(freebsd_ext)
  177. elif sys.platform == "darwin":
  178. ext_modules.append(darwin_ext)
  179. # sometimes there's no need to cythonize
  180. # this breaks chained commands like 'clean sdist'
  181. cythonizing = (
  182. len(sys.argv) > 1
  183. and sys.argv[1] not in (("clean", "clean2", "egg_info", "--help-commands", "--version"))
  184. and "--help" not in sys.argv[1:]
  185. )
  186. if cythonize and cythonizing:
  187. cython_opts = dict(compiler_directives={"language_level": "3str"})
  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(cmdclass=cmdclass, ext_modules=ext_modules, long_description=setup_docs.long_desc_from_readme())