| 1 |
import platform |
| 2 |
import sys |
| 3 |
import sysconfig |
| 4 |
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 |
# create a LICENSE_zstd.txt file |
| 14 |
# wheels distributions needs to ship the license of the zstd library |
| 15 |
ROOT_PATH = Path(__file__).parent.absolute() |
| 16 |
with (ROOT_PATH / "LICENSE_zstd.txt").open("w") as f: |
| 17 |
f.write( |
| 18 |
"Depending on how it is built, this package may distribute the zstd library,\n" |
| 19 |
"partially or in its integrality, in source or binary form.\n\n" |
| 20 |
"Its license is reproduced below.\n\n" |
| 21 |
"---\n\n" |
| 22 |
) |
| 23 |
f.write((ROOT_PATH / "src" / "c" / "zstd" / "LICENSE").read_text()) |
| 24 |
|
| 25 |
|
| 26 |
UnixCCompiler.src_extensions.append(".S") |
| 27 |
|
| 28 |
|
| 29 |
_PLATFORM_IS_WIN = sysconfig.get_platform().startswith("win") |
| 30 |
_USE_CFFI = platform.python_implementation() == "PyPy" |
| 31 |
try: |
| 32 |
sys.argv.remove("--system-zstd") |
| 33 |
except ValueError: |
| 34 |
_SYSTEM_ZSTD = False |
| 35 |
else: |
| 36 |
_SYSTEM_ZSTD = True |
| 37 |
|
| 38 |
|
| 39 |
def locate_sources(*sub_paths): |
| 40 |
extensions = "cC" if _PLATFORM_IS_WIN else "cCsS" |
| 41 |
yield from map(str, Path(*sub_paths).rglob(f"*.[{extensions}]")) |
| 42 |
|
| 43 |
|
| 44 |
def build_extension(): |
| 45 |
kwargs = dict( |
| 46 |
sources=[], |
| 47 |
include_dirs=[], |
| 48 |
libraries=[], |
| 49 |
extra_compile_args=[], |
| 50 |
extra_link_args=[], |
| 51 |
define_macros=[ |
| 52 |
("ZSTD_MULTITHREAD", None), # enable multithreading support |
| 53 |
], |
| 54 |
) |
| 55 |
|
| 56 |
if _PLATFORM_IS_WIN: |
| 57 |
kwargs["extra_compile_args"] += ["/Ob3", "/GF", "/Gy"] |
| 58 |
else: |
| 59 |
kwargs["extra_compile_args"] += ["-g0", "-flto"] |
| 60 |
kwargs["extra_link_args"] += ["-g0", "-flto"] |
| 61 |
|
| 62 |
if _SYSTEM_ZSTD: |
| 63 |
kwargs["libraries"].append("zstd") |
| 64 |
else: |
| 65 |
kwargs["sources"] += [ |
| 66 |
*locate_sources("src", "c", "zstd", "lib", "common"), |
| 67 |
*locate_sources("src", "c", "zstd", "lib", "compress"), |
| 68 |
*locate_sources("src", "c", "zstd", "lib", "decompress"), |
| 69 |
*locate_sources("src", "c", "zstd", "lib", "dictBuilder"), |
| 70 |
] |
| 71 |
kwargs["include_dirs"] += [ |
| 72 |
"src/c/zstd/lib", |
| 73 |
"src/c/zstd/lib/common", |
| 74 |
"src/c/zstd/lib/dictBuilder", |
| 75 |
] |
| 76 |
|
| 77 |
if _USE_CFFI: |
| 78 |
import cffi |
| 79 |
|
| 80 |
ffibuilder = cffi.FFI() |
| 81 |
ffibuilder.cdef((ROOT_PATH / "src" / "c" / "cffi" / "cdef.h").read_text()) |
| 82 |
ffibuilder.set_source( |
| 83 |
source=(ROOT_PATH / "src" / "c" / "cffi" / "source.c").read_text(), |
| 84 |
module_name="backports.zstd._zstd_cffi", |
| 85 |
**kwargs, |
| 86 |
) |
| 87 |
return ffibuilder.distutils_extension() |
| 88 |
|
| 89 |
else: |
| 90 |
kwargs["sources"] += [ |
| 91 |
*locate_sources("src", "c", "compression_zstd"), |
| 92 |
*locate_sources("src", "c", "compat"), |
| 93 |
] |
| 94 |
kwargs["include_dirs"] += [ |
| 95 |
"src/c/compat", |
| 96 |
"src/c/compression_zstd", |
| 97 |
"src/c/compression_zstd/clinic", |
| 98 |
"src/c/pythoncapi-compat", |
| 99 |
] |
| 100 |
return Extension( |
| 101 |
name="backports.zstd._zstd", |
| 102 |
**kwargs, |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
setup(ext_modules=[build_extension()]) |