|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright 2025 NXP |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: BSD-3-Clause |
| 7 | +"""Custom build backend that wraps setuptools and forces platform wheels.""" |
| 8 | + |
| 9 | +import os |
| 10 | +from typing import Any, Tuple |
| 11 | + |
| 12 | +from setuptools.build_meta import build_editable as _build_editable |
| 13 | +from setuptools.build_meta import build_sdist as _build_sdist |
| 14 | +from setuptools.build_meta import build_wheel as _build_wheel |
| 15 | +from setuptools.dist import Distribution |
| 16 | +from wheel.bdist_wheel import bdist_wheel |
| 17 | + |
| 18 | + |
| 19 | +def patch() -> None: |
| 20 | + """Patch the bdist wheel build.""" |
| 21 | + # Apply the monkey patch when the module is imported |
| 22 | + original_has_ext_modules = getattr(Distribution, "has_ext_modules", None) |
| 23 | + if original_has_ext_modules is not None: |
| 24 | + Distribution.has_ext_modules = lambda self: True |
| 25 | + |
| 26 | + # Also patch the is_pure method to return False |
| 27 | + original_is_pure = getattr(Distribution, "is_pure", None) |
| 28 | + if original_is_pure is not None: |
| 29 | + Distribution.is_pure = lambda self: False |
| 30 | + |
| 31 | + # Override the wheel tag generation |
| 32 | + |
| 33 | + original_get_tag = getattr(bdist_wheel, "get_tag", None) |
| 34 | + if original_get_tag is not None: |
| 35 | + |
| 36 | + def tag(self: Any) -> Tuple[str, str, str]: |
| 37 | + _, _, platform_tag = original_get_tag(self) |
| 38 | + return ("py3", "none", platform_tag) |
| 39 | + |
| 40 | + bdist_wheel.get_tag = tag |
| 41 | + |
| 42 | + print("Custom build backend loaded: Platform-specific wheels enabled") |
| 43 | + |
| 44 | + |
| 45 | +def validate_native_libraries() -> None: |
| 46 | + """Validate that native libraries exist in the expected locations.""" |
| 47 | + lib_dir = os.path.join(os.path.dirname(__file__), "libuuu", "lib") |
| 48 | + |
| 49 | + if os.path.exists(lib_dir): |
| 50 | + print(f"Native libraries found in: {lib_dir}") |
| 51 | + # List found libraries |
| 52 | + for root, _, files in os.walk(lib_dir): |
| 53 | + for file in files: |
| 54 | + if file.endswith((".dll", ".so", ".dylib")): |
| 55 | + print(f" - {os.path.relpath(os.path.join(root, file), lib_dir)}") |
| 56 | + else: |
| 57 | + print(f"Warning: No native libraries found in {lib_dir}") |
| 58 | + |
| 59 | + |
| 60 | +def build_wheel(wheel_directory, config_settings=None, metadata_directory=None) -> str: # type: ignore |
| 61 | + """Build wheel.""" |
| 62 | + patch() |
| 63 | + validate_native_libraries() |
| 64 | + return _build_wheel(wheel_directory, config_settings, metadata_directory) |
| 65 | + |
| 66 | + |
| 67 | +def build_sdist(sdist_directory, config_settings=None) -> str: # type: ignore |
| 68 | + """Build source.""" |
| 69 | + patch() |
| 70 | + validate_native_libraries() |
| 71 | + return _build_sdist(sdist_directory, config_settings) |
| 72 | + |
| 73 | + |
| 74 | +def build_editable(wheel_directory, config_settings=None, metadata_directory=None) -> str: # type: ignore |
| 75 | + """Build editable.""" |
| 76 | + patch() |
| 77 | + validate_native_libraries() |
| 78 | + return _build_editable(wheel_directory, config_settings, metadata_directory) |
0 commit comments