|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import functools |
|
|
import os |
|
|
import shutil |
|
|
import subprocess |
|
|
import sys |
|
|
import tempfile |
|
|
from pathlib import Path |
|
|
|
|
|
from setuptools import Extension, find_packages, setup |
|
|
from setuptools.command.build_ext import build_ext |
|
|
|
|
|
import versioningit |
|
|
|
|
|
ZLIB_NG_SOURCE = os.path.join("src", "zlib_ng", "zlib-ng") |
|
|
|
|
|
SYSTEM_IS_UNIX = (sys.platform.startswith("linux") or |
|
|
sys.platform.startswith("darwin") or |
|
|
'bsd' in sys.platform) |
|
|
SYSTEM_IS_WINDOWS = sys.platform.startswith("win") |
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_CACHE_FILE = Path(tempfile.gettempdir() |
|
|
).absolute() / ".zlib_ng_build_cache" |
|
|
BUILD_CACHE = os.environ.get("PYTHON_ZLIB_NG_BUILD_CACHE") |
|
|
BUILD_CACHE_FILE = Path(os.environ.get("PYTHON_ZLIB_NG_BUILD_CACHE_FILE", |
|
|
DEFAULT_CACHE_FILE)) |
|
|
|
|
|
EXTENSIONS = [ |
|
|
Extension("zlib_ng.zlib_ng", ["src/zlib_ng/zlib_ngmodule.c"]), |
|
|
] |
|
|
|
|
|
|
|
|
class BuildZlibNGExt(build_ext): |
|
|
def build_extension(self, ext): |
|
|
|
|
|
if os.getenv("PYTHON_ZLIB_NG_LINK_DYNAMIC") is not None: |
|
|
|
|
|
|
|
|
possible_prefixes = [sys.exec_prefix, sys.base_exec_prefix] |
|
|
for prefix in possible_prefixes: |
|
|
if Path(prefix, "include", "zlib-ng.h").exists(): |
|
|
ext.include_dirs = [os.path.join(prefix, "include")] |
|
|
ext.library_dirs = [os.path.join(prefix, "lib")] |
|
|
break |
|
|
|
|
|
elif Path(prefix, "Library", "include", "zlib-ng.h").exists(): |
|
|
ext.include_dirs = [os.path.join(prefix, "Library", |
|
|
"include")] |
|
|
ext.library_dirs = [os.path.join(prefix, "Library", "lib")] |
|
|
break |
|
|
if SYSTEM_IS_UNIX: |
|
|
ext.libraries = ["z-ng"] |
|
|
elif SYSTEM_IS_WINDOWS: |
|
|
ext.libraries = ["zlib-ng"] |
|
|
else: |
|
|
raise NotImplementedError( |
|
|
f"Unsupported platform: {sys.platform}") |
|
|
else: |
|
|
build_dir = build_zlib_ng() |
|
|
if SYSTEM_IS_UNIX: |
|
|
ext.extra_objects = [ |
|
|
os.path.join(build_dir, "libz-ng.a")] |
|
|
elif SYSTEM_IS_WINDOWS: |
|
|
ext.extra_objects = [ |
|
|
os.path.join(build_dir, "Release", "zlibstatic-ng.lib")] |
|
|
else: |
|
|
raise NotImplementedError( |
|
|
f"Unsupported platform: {sys.platform}") |
|
|
ext.include_dirs = [build_dir] |
|
|
|
|
|
|
|
|
pass |
|
|
super().build_extension(ext) |
|
|
|
|
|
|
|
|
|
|
|
@functools.lru_cache(maxsize=None) |
|
|
def build_zlib_ng(): |
|
|
|
|
|
if BUILD_CACHE: |
|
|
if BUILD_CACHE_FILE.exists(): |
|
|
cache_path = Path(BUILD_CACHE_FILE.read_text()) |
|
|
if (cache_path / "zlib-ng.h").exists(): |
|
|
return str(cache_path) |
|
|
|
|
|
|
|
|
build_dir = tempfile.mktemp() |
|
|
shutil.copytree(ZLIB_NG_SOURCE, build_dir) |
|
|
|
|
|
if hasattr(os, "sched_getaffinity"): |
|
|
cpu_count = len(os.sched_getaffinity(0)) |
|
|
else: |
|
|
cpu_count = os.cpu_count() or 1 |
|
|
|
|
|
|
|
|
build_env = os.environ.copy() |
|
|
build_env["CFLAGS"] = build_env.get("CFLAGS", "") + " -fPIC" |
|
|
|
|
|
run_args = dict(cwd=build_dir, env=build_env) |
|
|
if sys.platform == "darwin": |
|
|
subprocess.run([os.path.join(build_dir, "configure")], **run_args) |
|
|
make_program = "gmake" if shutil.which("gmake") else "make" |
|
|
subprocess.run([make_program, "libz-ng.a"], **run_args) |
|
|
elif sys.platform == "linux": |
|
|
subprocess.run([os.path.join(build_dir, "configure")], **run_args) |
|
|
subprocess.run(["make", "libz-ng.a", "-j", str(cpu_count)], **run_args) |
|
|
else: |
|
|
subprocess.run(["cmake", build_dir], **run_args) |
|
|
|
|
|
|
|
|
subprocess.run(["cmake", "--build", build_dir, "--config", "Release", |
|
|
"--target", "zlibstatic", |
|
|
"-j", str(cpu_count)], **run_args) |
|
|
if BUILD_CACHE: |
|
|
BUILD_CACHE_FILE.write_text(build_dir) |
|
|
return build_dir |
|
|
|
|
|
|
|
|
setup( |
|
|
name="zlib-ng", |
|
|
version=versioningit.get_version(), |
|
|
description="Drop-in replacement for zlib and gzip modules using zlib-ng", |
|
|
author="Leiden University Medical Center", |
|
|
author_email="r.h.p.vorderman@lumc.nl", |
|
|
long_description=Path("README.rst").read_text(), |
|
|
long_description_content_type="text/x-rst", |
|
|
cmdclass={"build_ext": BuildZlibNGExt}, |
|
|
license="PSF-2.0", |
|
|
keywords="zlib-ng zlib compression deflate gzip", |
|
|
zip_safe=False, |
|
|
packages=find_packages('src'), |
|
|
package_dir={'': 'src'}, |
|
|
package_data={'zlib_ng': [ |
|
|
'*.pyi', 'py.typed', |
|
|
|
|
|
'zlib-ng/LICENSE.md', 'zlib-ng/README.md']}, |
|
|
url="https://github.com/pycompression/python-zlib-ng", |
|
|
classifiers=[ |
|
|
"Programming Language :: Python :: 3 :: Only", |
|
|
"Programming Language :: Python :: 3", |
|
|
"Programming Language :: Python :: 3.8", |
|
|
"Programming Language :: Python :: 3.9", |
|
|
"Programming Language :: Python :: 3.10", |
|
|
"Programming Language :: Python :: 3.11", |
|
|
"Programming Language :: Python :: 3.12", |
|
|
"Programming Language :: Python :: 3.13", |
|
|
"Programming Language :: Python :: Implementation :: CPython", |
|
|
"Programming Language :: Python :: Implementation :: PyPy", |
|
|
"Programming Language :: C", |
|
|
"Development Status :: 4 - Beta", |
|
|
"Topic :: System :: Archiving :: Compression", |
|
|
"License :: OSI Approved :: Python Software Foundation License", |
|
|
"Operating System :: POSIX :: Linux", |
|
|
"Operating System :: MacOS", |
|
|
"Operating System :: Microsoft :: Windows", |
|
|
], |
|
|
python_requires=">=3.8", |
|
|
ext_modules=EXTENSIONS |
|
|
) |
|
|
|