2024-02-20 16:04:16 +00:00
|
|
|
# borgbackup - main setup code (extension building here, rest see pyproject.toml)
|
2019-03-13 23:28:59 +00:00
|
|
|
|
2011-08-20 16:11:23 +00:00
|
|
|
import os
|
2024-01-29 18:50:14 +00:00
|
|
|
import re
|
2010-10-30 11:24:23 +00:00
|
|
|
import sys
|
2019-03-13 02:01:00 +00:00
|
|
|
from collections import defaultdict
|
2011-11-06 20:40:29 +00:00
|
|
|
|
2018-04-28 16:06:10 +00:00
|
|
|
try:
|
|
|
|
import multiprocessing
|
|
|
|
except ImportError:
|
|
|
|
multiprocessing = None
|
|
|
|
|
|
|
|
from setuptools.command.build_ext import build_ext
|
2024-01-30 22:55:18 +00:00
|
|
|
from setuptools import setup, Extension
|
2018-04-28 16:06:10 +00:00
|
|
|
from setuptools.command.sdist import sdist
|
|
|
|
|
|
|
|
try:
|
|
|
|
from Cython.Build import cythonize
|
2024-05-12 13:31:35 +00:00
|
|
|
|
2024-05-12 11:39:57 +00:00
|
|
|
cythonize_import_error_msg = None
|
|
|
|
except ImportError as exc:
|
|
|
|
# either there is no Cython installed or there is some issue with it.
|
2018-04-28 16:06:10 +00:00
|
|
|
cythonize = None
|
2024-05-12 11:39:57 +00:00
|
|
|
cythonize_import_error_msg = "ImportError: " + str(exc)
|
2024-05-12 12:36:51 +00:00
|
|
|
if "failed to map segment from shared object" in cythonize_import_error_msg:
|
|
|
|
cythonize_import_error_msg += " Check if the borg build uses a +exec filesystem."
|
2015-10-07 23:55:56 +00:00
|
|
|
|
2022-02-25 04:52:50 +00:00
|
|
|
sys.path += [os.path.dirname(__file__)]
|
2017-12-05 03:16:25 +00:00
|
|
|
|
2019-08-08 22:01:38 +00:00
|
|
|
is_win32 = sys.platform.startswith("win32")
|
2022-04-14 10:14:15 +00:00
|
|
|
is_openbsd = sys.platform.startswith("openbsd")
|
2019-08-08 22:01:38 +00:00
|
|
|
|
|
|
|
# Number of threads to use for cythonize, not used on windows
|
2020-12-28 18:56:08 +00:00
|
|
|
cpu_threads = multiprocessing.cpu_count() if multiprocessing and multiprocessing.get_start_method() != "spawn" else None
|
2018-04-28 16:06:10 +00:00
|
|
|
|
2022-03-24 04:41:34 +00:00
|
|
|
# How the build process finds the system libs:
|
|
|
|
#
|
|
|
|
# 1. if BORG_{LIBXXX,OPENSSL}_PREFIX is set, it will use headers and libs from there.
|
|
|
|
# 2. if not and pkg-config can locate the lib, the lib located by
|
|
|
|
# pkg-config will be used. We use the pkg-config tool via the pkgconfig
|
|
|
|
# python package, which must be installed before invoking setup.py.
|
|
|
|
# if pkgconfig is not installed, this step is skipped.
|
|
|
|
# 3. otherwise raise a fatal error.
|
|
|
|
|
2015-10-07 23:16:49 +00:00
|
|
|
# Are we building on ReadTheDocs?
|
2015-10-08 12:23:37 +00:00
|
|
|
on_rtd = os.environ.get("READTHEDOCS")
|
2015-10-07 23:16:49 +00:00
|
|
|
|
2022-12-29 22:26:54 +00:00
|
|
|
# Extra cflags for all extensions, usually just warnings we want to enable explicitly
|
2022-03-09 11:18:52 +00:00
|
|
|
cflags = ["-Wall", "-Wextra", "-Wpointer-arith"]
|
2022-07-06 13:37:27 +00:00
|
|
|
|
2016-05-05 18:59:53 +00:00
|
|
|
compress_source = "src/borg/compress.pyx"
|
2017-05-02 17:05:27 +00:00
|
|
|
crypto_ll_source = "src/borg/crypto/low_level.pyx"
|
2017-05-02 16:52:36 +00:00
|
|
|
chunker_source = "src/borg/chunker.pyx"
|
2016-05-05 18:59:53 +00:00
|
|
|
hashindex_source = "src/borg/hashindex.pyx"
|
2016-11-30 11:43:28 +00:00
|
|
|
item_source = "src/borg/item.pyx"
|
2022-03-16 23:24:49 +00:00
|
|
|
checksums_source = "src/borg/checksums.pyx"
|
2016-05-31 00:35:54 +00:00
|
|
|
platform_posix_source = "src/borg/platform/posix.pyx"
|
|
|
|
platform_linux_source = "src/borg/platform/linux.pyx"
|
2020-01-02 17:54:00 +00:00
|
|
|
platform_syncfilerange_source = "src/borg/platform/syncfilerange.pyx"
|
2016-05-31 00:35:54 +00:00
|
|
|
platform_darwin_source = "src/borg/platform/darwin.pyx"
|
|
|
|
platform_freebsd_source = "src/borg/platform/freebsd.pyx"
|
2019-08-08 22:01:38 +00:00
|
|
|
platform_windows_source = "src/borg/platform/windows.pyx"
|
2016-05-31 00:35:54 +00:00
|
|
|
|
|
|
|
cython_sources = [
|
|
|
|
compress_source,
|
2017-05-02 17:05:27 +00:00
|
|
|
crypto_ll_source,
|
2016-05-31 00:35:54 +00:00
|
|
|
chunker_source,
|
|
|
|
hashindex_source,
|
2016-11-30 11:43:28 +00:00
|
|
|
item_source,
|
2017-05-31 21:17:27 +00:00
|
|
|
checksums_source,
|
2016-05-31 00:35:54 +00:00
|
|
|
platform_posix_source,
|
|
|
|
platform_linux_source,
|
2020-01-02 17:54:00 +00:00
|
|
|
platform_syncfilerange_source,
|
2016-05-31 00:35:54 +00:00
|
|
|
platform_freebsd_source,
|
|
|
|
platform_darwin_source,
|
2019-08-08 22:01:38 +00:00
|
|
|
platform_windows_source,
|
2016-05-31 00:35:54 +00:00
|
|
|
]
|
2013-06-18 19:34:22 +00:00
|
|
|
|
2018-04-28 16:06:10 +00:00
|
|
|
if cythonize:
|
|
|
|
Sdist = sdist
|
|
|
|
else:
|
2022-07-06 13:37:27 +00:00
|
|
|
|
2015-08-22 12:58:06 +00:00
|
|
|
class Sdist(sdist):
|
2013-06-24 11:53:02 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
raise Exception("Cython is required to run sdist")
|
|
|
|
|
2019-06-04 15:50:00 +00:00
|
|
|
cython_c_files = [fn.replace(".pyx", ".c") for fn in cython_sources]
|
|
|
|
if not on_rtd and not all(os.path.exists(path) for path in cython_c_files):
|
2024-05-12 13:31:35 +00:00
|
|
|
raise ImportError(
|
|
|
|
"The GIT version of Borg needs a working Cython. "
|
|
|
|
+ "Install or fix Cython or use a released borg version. "
|
|
|
|
+ "Importing cythonize failed with: "
|
|
|
|
+ cythonize_import_error_msg
|
|
|
|
)
|
2014-03-10 21:50:38 +00:00
|
|
|
|
|
|
|
|
2024-01-30 22:55:18 +00:00
|
|
|
cmdclass = {"build_ext": build_ext, "sdist": Sdist}
|
2013-08-05 20:45:24 +00:00
|
|
|
|
2022-03-23 11:19:18 +00:00
|
|
|
|
2015-10-07 23:16:49 +00:00
|
|
|
ext_modules = []
|
|
|
|
if not on_rtd:
|
2019-03-13 02:01:00 +00:00
|
|
|
|
|
|
|
def members_appended(*ds):
|
|
|
|
result = defaultdict(list)
|
|
|
|
for d in ds:
|
|
|
|
for k, v in d.items():
|
|
|
|
assert isinstance(v, list)
|
|
|
|
result[k].extend(v)
|
|
|
|
return result
|
|
|
|
|
2019-03-13 22:59:04 +00:00
|
|
|
try:
|
|
|
|
import pkgconfig as pc
|
|
|
|
except ImportError:
|
|
|
|
print("Warning: can not import pkgconfig python package.")
|
|
|
|
pc = None
|
|
|
|
|
2022-03-23 13:19:43 +00:00
|
|
|
def lib_ext_kwargs(pc, prefix_env_var, lib_name, lib_pkg_name, pc_version, lib_subdir="lib"):
|
2022-03-23 11:19:18 +00:00
|
|
|
system_prefix = os.environ.get(prefix_env_var)
|
|
|
|
if system_prefix:
|
2022-03-24 04:41:34 +00:00
|
|
|
print(f"Detected and preferring {lib_pkg_name} [via {prefix_env_var}]")
|
2022-03-23 11:19:18 +00:00
|
|
|
return dict(
|
|
|
|
include_dirs=[os.path.join(system_prefix, "include")],
|
2022-03-23 13:19:43 +00:00
|
|
|
library_dirs=[os.path.join(system_prefix, lib_subdir)],
|
2022-03-23 11:19:18 +00:00
|
|
|
libraries=[lib_name],
|
|
|
|
)
|
|
|
|
|
|
|
|
if pc and pc.installed(lib_pkg_name, pc_version):
|
2022-03-24 04:41:34 +00:00
|
|
|
print(f"Detected and preferring {lib_pkg_name} [via pkg-config]")
|
2022-03-23 11:19:18 +00:00
|
|
|
return pc.parse(lib_pkg_name)
|
2022-03-24 04:41:34 +00:00
|
|
|
raise Exception(
|
|
|
|
f"Could not find {lib_name} lib/headers, please set {prefix_env_var} "
|
|
|
|
f"or ensure {lib_pkg_name}.pc is in PKG_CONFIG_PATH."
|
|
|
|
)
|
2022-03-23 11:19:18 +00:00
|
|
|
|
2022-03-23 13:28:27 +00:00
|
|
|
if is_win32:
|
|
|
|
crypto_ext_lib = lib_ext_kwargs(pc, "BORG_OPENSSL_PREFIX", "libcrypto", "libcrypto", ">=1.1.1", lib_subdir="")
|
2022-04-14 10:14:15 +00:00
|
|
|
elif is_openbsd:
|
2022-09-10 15:23:56 +00:00
|
|
|
# Use openssl (not libressl) because we need AES-OCB via EVP api. Link
|
|
|
|
# it statically to avoid conflicting with shared libcrypto from the base
|
|
|
|
# OS pulled in via dependencies.
|
2024-11-23 06:48:26 +00:00
|
|
|
openssl_prefix = os.environ.get("BORG_OPENSSL_PREFIX", "/usr/local")
|
|
|
|
openssl_name = os.environ.get("BORG_OPENSSL_NAME", "eopenssl33")
|
|
|
|
crypto_ext_lib = dict(
|
|
|
|
include_dirs=[os.path.join(openssl_prefix, "include", openssl_name)],
|
|
|
|
extra_objects=[os.path.join(openssl_prefix, "lib", openssl_name, "libcrypto.a")],
|
|
|
|
)
|
2022-03-23 13:28:27 +00:00
|
|
|
else:
|
2022-03-24 04:41:34 +00:00
|
|
|
crypto_ext_lib = lib_ext_kwargs(pc, "BORG_OPENSSL_PREFIX", "crypto", "libcrypto", ">=1.1.1")
|
2022-03-23 11:19:18 +00:00
|
|
|
|
2019-03-13 02:01:00 +00:00
|
|
|
crypto_ext_kwargs = members_appended(
|
2024-11-23 06:48:26 +00:00
|
|
|
dict(sources=[crypto_ll_source]), crypto_ext_lib, dict(extra_compile_args=cflags)
|
2019-03-13 02:01:00 +00:00
|
|
|
)
|
|
|
|
|
2019-03-13 13:53:18 +00:00
|
|
|
compress_ext_kwargs = members_appended(
|
|
|
|
dict(sources=[compress_source]),
|
2022-03-23 11:19:18 +00:00
|
|
|
lib_ext_kwargs(pc, "BORG_LIBLZ4_PREFIX", "lz4", "liblz4", ">= 1.7.0"),
|
|
|
|
lib_ext_kwargs(pc, "BORG_LIBZSTD_PREFIX", "zstd", "libzstd", ">= 1.3.0"),
|
2022-03-09 11:18:52 +00:00
|
|
|
dict(extra_compile_args=cflags),
|
2019-03-13 13:53:18 +00:00
|
|
|
)
|
|
|
|
|
2019-06-03 21:37:46 +00:00
|
|
|
checksums_ext_kwargs = members_appended(
|
|
|
|
dict(sources=[checksums_source]),
|
2022-03-23 13:40:33 +00:00
|
|
|
lib_ext_kwargs(pc, "BORG_LIBXXHASH_PREFIX", "xxhash", "libxxhash", ">= 0.7.3"),
|
2022-03-09 11:18:52 +00:00
|
|
|
dict(extra_compile_args=cflags),
|
2019-06-03 21:37:46 +00:00
|
|
|
)
|
|
|
|
|
2023-10-15 14:53:02 +00:00
|
|
|
if sys.platform == "linux":
|
|
|
|
linux_ext_kwargs = members_appended(
|
|
|
|
dict(sources=[platform_linux_source]),
|
2024-02-20 20:40:43 +00:00
|
|
|
lib_ext_kwargs(pc, "BORG_LIBACL_PREFIX", "acl", "libacl", ">= 2.2.47"),
|
2023-10-15 14:53:02 +00:00
|
|
|
dict(extra_compile_args=cflags),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
linux_ext_kwargs = members_appended(
|
2023-11-05 12:33:25 +00:00
|
|
|
dict(sources=[platform_linux_source], libraries=["acl"], extra_compile_args=cflags)
|
2023-10-15 14:53:02 +00:00
|
|
|
)
|
|
|
|
|
2024-10-25 17:03:38 +00:00
|
|
|
# note: _chunker.c is a relatively complex/large piece of handwritten C code,
|
|
|
|
# thus we undef NDEBUG for it, so the compiled code will contain and execute assert().
|
2015-10-07 23:16:49 +00:00
|
|
|
ext_modules += [
|
2017-12-17 02:06:12 +00:00
|
|
|
Extension("borg.crypto.low_level", **crypto_ext_kwargs),
|
2019-03-13 13:53:18 +00:00
|
|
|
Extension("borg.compress", **compress_ext_kwargs),
|
2024-10-25 17:03:38 +00:00
|
|
|
Extension("borg.hashindex", [hashindex_source], extra_compile_args=cflags),
|
2022-03-09 11:18:52 +00:00
|
|
|
Extension("borg.item", [item_source], extra_compile_args=cflags),
|
2023-02-09 19:59:14 +00:00
|
|
|
Extension("borg.chunker", [chunker_source], extra_compile_args=cflags, undef_macros=["NDEBUG"]),
|
2022-03-16 23:24:49 +00:00
|
|
|
Extension("borg.checksums", **checksums_ext_kwargs),
|
2017-12-05 03:16:25 +00:00
|
|
|
]
|
2018-04-28 16:06:10 +00:00
|
|
|
|
2022-03-09 11:18:52 +00:00
|
|
|
posix_ext = Extension("borg.platform.posix", [platform_posix_source], extra_compile_args=cflags)
|
2023-10-15 14:53:02 +00:00
|
|
|
linux_ext = Extension("borg.platform.linux", **linux_ext_kwargs)
|
|
|
|
|
2022-03-09 11:18:52 +00:00
|
|
|
syncfilerange_ext = Extension(
|
|
|
|
"borg.platform.syncfilerange", [platform_syncfilerange_source], extra_compile_args=cflags
|
|
|
|
)
|
|
|
|
freebsd_ext = Extension("borg.platform.freebsd", [platform_freebsd_source], extra_compile_args=cflags)
|
|
|
|
darwin_ext = Extension("borg.platform.darwin", [platform_darwin_source], extra_compile_args=cflags)
|
|
|
|
windows_ext = Extension("borg.platform.windows", [platform_windows_source], extra_compile_args=cflags)
|
2018-04-28 16:06:10 +00:00
|
|
|
|
2019-08-08 22:01:38 +00:00
|
|
|
if not is_win32:
|
2018-04-28 16:06:10 +00:00
|
|
|
ext_modules.append(posix_ext)
|
2019-08-08 22:01:38 +00:00
|
|
|
else:
|
|
|
|
ext_modules.append(windows_ext)
|
2015-12-15 00:08:35 +00:00
|
|
|
if sys.platform == "linux":
|
2018-04-28 16:06:10 +00:00
|
|
|
ext_modules.append(linux_ext)
|
2020-01-02 17:54:00 +00:00
|
|
|
ext_modules.append(syncfilerange_ext)
|
2015-10-07 23:16:49 +00:00
|
|
|
elif sys.platform.startswith("freebsd"):
|
2018-04-28 16:06:10 +00:00
|
|
|
ext_modules.append(freebsd_ext)
|
2015-10-07 23:16:49 +00:00
|
|
|
elif sys.platform == "darwin":
|
2018-04-28 16:06:10 +00:00
|
|
|
ext_modules.append(darwin_ext)
|
|
|
|
|
|
|
|
# sometimes there's no need to cythonize
|
|
|
|
# this breaks chained commands like 'clean sdist'
|
2021-03-13 00:38:09 +00:00
|
|
|
cythonizing = (
|
|
|
|
len(sys.argv) > 1
|
2024-01-30 22:55:18 +00:00
|
|
|
and sys.argv[1] not in (("clean", "egg_info", "--help-commands", "--version"))
|
2021-03-13 00:38:09 +00:00
|
|
|
and "--help" not in sys.argv[1:]
|
2022-07-06 13:37:27 +00:00
|
|
|
)
|
2018-04-28 16:06:10 +00:00
|
|
|
|
|
|
|
if cythonize and cythonizing:
|
2024-01-02 18:30:14 +00:00
|
|
|
# 3str is the default in Cython3 and we do not support older Cython releases.
|
|
|
|
# we only set this to avoid the related FutureWarning from Cython3.
|
2022-07-04 22:16:48 +00:00
|
|
|
cython_opts = dict(compiler_directives={"language_level": "3str"})
|
2019-08-08 22:01:38 +00:00
|
|
|
if not is_win32:
|
|
|
|
# compile .pyx extensions to .c in parallel, does not work on windows
|
2020-12-28 18:56:08 +00:00
|
|
|
cython_opts["nthreads"] = cpu_threads
|
2019-08-08 22:01:38 +00:00
|
|
|
|
2020-03-08 22:20:11 +00:00
|
|
|
# generate C code from Cython for ALL supported platforms, so we have them in the sdist.
|
|
|
|
# the sdist does not require Cython at install time, so we need all as C.
|
|
|
|
cythonize([posix_ext, linux_ext, syncfilerange_ext, freebsd_ext, darwin_ext, windows_ext], **cython_opts)
|
|
|
|
# generate C code from Cython for THIS platform (and for all platform-independent Cython parts).
|
2018-12-15 17:12:10 +00:00
|
|
|
ext_modules = cythonize(ext_modules, **cython_opts)
|
2014-04-13 18:26:46 +00:00
|
|
|
|
2019-03-13 23:40:30 +00:00
|
|
|
|
2024-01-29 18:50:14 +00:00
|
|
|
def long_desc_from_readme():
|
|
|
|
with open("README.rst") as fd:
|
|
|
|
long_description = fd.read()
|
|
|
|
# remove header, but have one \n before first headline
|
|
|
|
start = long_description.find("What is BorgBackup?")
|
|
|
|
assert start >= 0
|
|
|
|
long_description = "\n" + long_description[start:]
|
|
|
|
# remove badges
|
|
|
|
long_description = re.compile(r"^\.\. start-badges.*^\.\. end-badges", re.M | re.S).sub("", long_description)
|
|
|
|
# remove unknown directives
|
|
|
|
long_description = re.compile(r"^\.\. highlight:: \w+$", re.M).sub("", long_description)
|
|
|
|
return long_description
|
|
|
|
|
|
|
|
|
|
|
|
setup(cmdclass=cmdclass, ext_modules=ext_modules, long_description=long_desc_from_readme())
|