From f3e0613de3f0f34ae5d95c9898b1aa07a25466d1 Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Sat, 8 Feb 2025 00:25:17 +0200 Subject: [PATCH] python: expose FEATURES variable --- python/Makefile | 2 +- python/setup.py | 37 +++++++++++++++++++++++++++++++++---- python/src/ustreamer.c | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/python/Makefile b/python/Makefile index 71ab8abf..43631b9e 100644 --- a/python/Makefile +++ b/python/Makefile @@ -8,7 +8,7 @@ PY ?= python3 # ===== all: root -root: $(shell find src -type f,l) +root: $(shell find src -type f,l) setup.py $(info == PY_BUILD ustreamer-*.so) rm -rf root $(ECHO) $(PY) -m build --skip-dependency-check --no-isolation diff --git a/python/setup.py b/python/setup.py index 438395d5..66c2ca6f 100644 --- a/python/setup.py +++ b/python/setup.py @@ -5,16 +5,41 @@ # ===== -def _find_sources(suffix: str) -> list[str]: +def _find_sources() -> list[str]: sources: list[str] = [] for (root_path, _, names) in os.walk("src"): for name in names: - if name.endswith(suffix): + if name.endswith(".c"): sources.append(os.path.join(root_path, name)) return sources +def _find_flags() -> dict[str, bool]: + return { + key: bool(int(value)) + for (key, value) in sorted(os.environ.items()) + if key.startswith("WITH_") + } + + +def _make_d_flags(flags: dict[str, bool]) -> list[str]: + return [ + f"-D{key}" + for (key, value) in flags.items() + if value + ] + + +def _make_d_features(flags: dict[str, bool]) -> str: + features = " ".join([ + f"{key}={int(value)}" + for (key, value) in flags.items() + ]) + return f"-DUS_FEATURES=\"{features}\"" + + if __name__ == "__main__": + flags = _find_flags() setup( name="ustreamer", version="6.29", @@ -26,9 +51,13 @@ def _find_sources(suffix: str) -> list[str]: Extension( "ustreamer", libraries=["rt", "m", "pthread"], - extra_compile_args=["-std=c17", "-D_GNU_SOURCE"], + extra_compile_args=[ + "-std=c17", "-D_GNU_SOURCE", + _make_d_features(flags), + *_make_d_flags(flags), + ], undef_macros=["NDEBUG"], - sources=_find_sources(".c"), + sources=_find_sources(), ), ], ) diff --git a/python/src/ustreamer.c b/python/src/ustreamer.c index f5551a51..c4bb8edb 100644 --- a/python/src/ustreamer.c +++ b/python/src/ustreamer.c @@ -325,6 +325,7 @@ PyMODINIT_FUNC PyInit_ustreamer(void) { ADD(StringConstant, "VERSION", US_VERSION); ADD(IntConstant, "VERSION_MAJOR", US_VERSION_MAJOR); ADD(IntConstant, "VERSION_MINOR", US_VERSION_MINOR); + ADD(StringConstant, "FEATURES", US_FEATURES); // Defined in setup.py ADD(ObjectRef, "Memsink", (PyObject*)&_MemsinkType); # undef ADD return module;