From 8f3cf1673c1ea289d3bde274ae45f30fa6105e82 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Dec 2023 15:41:09 -1000 Subject: [PATCH] feat: add cython pxd for base_scanner --- build_ext.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 6 +++++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 build_ext.py diff --git a/build_ext.py b/build_ext.py new file mode 100644 index 0000000..3bd266d --- /dev/null +++ b/build_ext.py @@ -0,0 +1,45 @@ +"""Build optional cython modules.""" + +import logging +import os +from distutils.command.build_ext import build_ext +from typing import Any + +_LOGGER = logging.getLogger(__name__) + + +class BuildExt(build_ext): + """Build extension.""" + + def build_extensions(self) -> None: + """Build extensions.""" + try: + super().build_extensions() + except Exception as ex: # nosec + _LOGGER.debug("Failed to build extensions: %s", ex, exc_info=True) + pass + + +def build(setup_kwargs: Any) -> None: + """Build optional cython modules.""" + if os.environ.get("SKIP_CYTHON", False): + return + try: + from Cython.Build import cythonize + + setup_kwargs.update( + { + "ext_modules": cythonize( + ["src/habluetooth/base_scanner.py"], + compiler_directives={"language_level": "3"}, # Python 3 + ), + "cmdclass": {"build_ext": BuildExt}, + } + ) + setup_kwargs["exclude_package_data"] = { + pkg: ["*.c"] for pkg in setup_kwargs["packages"] + } + except Exception: + if os.environ.get("REQUIRE_CYTHON"): + raise + pass diff --git a/pyproject.toml b/pyproject.toml index 6dd11be..effc5d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,10 @@ packages = [ { include = "habluetooth", from = "src" }, ] +[tool.poetry.build] +generate-setup-file = true +script = "build_ext.py" + [tool.poetry.urls] "Bug Tracker" = "https://github.com/bluetooth-devices/habluetooth/issues" "Changelog" = "https://github.com/bluetooth-devices/habluetooth/blob/main/CHANGELOG.md" @@ -147,5 +151,5 @@ module = "docs.*" ignore_errors = true [build-system] -requires = ["poetry-core>=1.0.0"] +requires = ['setuptools>=65.4.1', 'Cython', "poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"