Debian Codegraph @

setup.py

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 import platform
2 import sys
3 import sysconfig
•4 import setuptools; from distutils.unixccompiler import UnixCCompiler
5 from pathlib import Path
6
7 from setuptools import Extension, setup
8
9 if not ((3, 10) <= sys.version_info < (3, 14)):
10 raise RuntimeError(f"Unsupported Python version: {sys.version}")
11
12
13 ROOT_PATH = Path(__file__).parent.absolute()
14
15
16 UnixCCompiler.src_extensions.append(".S")
17
18
19 _PLATFORM_IS_WIN = sysconfig.get_platform().startswith("win")
20 _USE_CFFI = platform.python_implementation() == "PyPy"
21 try:
22 sys.argv.remove("--system-zstd")
23 except ValueError:
24 _SYSTEM_ZSTD = False
25 else:
26 _SYSTEM_ZSTD = True
27
28
29 def locate_sources(*sub_paths):
30 extensions = "cC" if _PLATFORM_IS_WIN else "cCsS"
31 yield from map(str, Path(*sub_paths).rglob(f"*.[{extensions}]"))
32
33
34 def build_extension():
35 kwargs = dict(
36 sources=[],
37 include_dirs=[],
38 libraries=[],
39 extra_compile_args=[],
40 extra_link_args=[],
41 define_macros=[
42 ("ZSTD_MULTITHREAD", None), # enable multithreading support
43 ],
44 )
45
46 if _SYSTEM_ZSTD:
47 kwargs["libraries"].append("zstd")
48 else:
49 kwargs["sources"] += [
50 *locate_sources("src", "c", "zstd", "lib", "common"),
51 *locate_sources("src", "c", "zstd", "lib", "compress"),
52 *locate_sources("src", "c", "zstd", "lib", "decompress"),
53 *locate_sources("src", "c", "zstd", "lib", "dictBuilder"),
54 ]
55 kwargs["include_dirs"] += [
56 "src/c/zstd/lib",
57 "src/c/zstd/lib/common",
58 "src/c/zstd/lib/dictBuilder",
59 ]
60
61 if _USE_CFFI:
62 import cffi
63
64 ffibuilder = cffi.FFI()
65 ffibuilder.cdef((ROOT_PATH / "src" / "c" / "cffi" / "cdef.h").read_text())
66 ffibuilder.set_source(
67 source=(ROOT_PATH / "src" / "c" / "cffi" / "source.c").read_text(),
68 module_name="backports.zstd._zstd_cffi",
69 **kwargs,
70 )
71 return ffibuilder.distutils_extension()
72
73 else:
74 kwargs["sources"] += [
75 *locate_sources("src", "c", "compression_zstd"),
76 *locate_sources("src", "c", "compat"),
77 ]
78 kwargs["include_dirs"] += [
79 "src/c/compat",
80 "src/c/compression_zstd",
81 "src/c/compression_zstd/clinic",
82 "src/c/pythoncapi-compat",
83 ]
84 return Extension(
85 name="backports.zstd._zstd",
86 **kwargs,
87 )
88
89
90 setup(ext_modules=[build_extension()])