|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 3 | +"""Build cuda_bindings Cython test extensions in-place. |
| 4 | +
|
| 5 | +pixi-build's editable install exposes the `cuda` namespace package via a |
| 6 | +PEP 660 finder hook. Python's import machinery honors the hook, but |
| 7 | +Cython's filesystem .pxd resolver only walks real directories on sys.path, |
| 8 | +so `cimport cuda.bindings.*` fails to locate the .pxd files. We resolve |
| 9 | +the namespace package's source root from `cuda.bindings.__file__` and pass |
| 10 | +it via `include_path=` so cythonize finds the .pxd tree on every platform. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import cuda.bindings |
| 19 | +from Cython.Build import cythonize |
| 20 | +from setuptools import setup |
| 21 | + |
| 22 | + |
| 23 | +def _bindings_source_root() -> Path: |
| 24 | + # cuda.bindings.__file__ -> .../<root>/cuda/bindings/__init__.py |
| 25 | + root = Path(cuda.bindings.__file__).resolve().parents[2] |
| 26 | + if not (root / "cuda" / "bindings").is_dir(): |
| 27 | + raise RuntimeError( |
| 28 | + f"cuda.bindings source tree not found at {root}; " |
| 29 | + "pixi-build editable install layout may have changed." |
| 30 | + ) |
| 31 | + return root |
| 32 | + |
| 33 | + |
| 34 | +def main() -> None: |
| 35 | + script_dir = Path(__file__).resolve().parent |
| 36 | + pyx_files = sorted(str(p) for p in script_dir.glob("test_*.pyx")) |
| 37 | + if not pyx_files: |
| 38 | + raise SystemExit(f"no test_*.pyx files under {script_dir}") |
| 39 | + |
| 40 | + ext_modules = cythonize( |
| 41 | + pyx_files, |
| 42 | + language_level=3, |
| 43 | + nthreads=1, |
| 44 | + include_path=[str(_bindings_source_root())], |
| 45 | + compiler_directives={"freethreading_compatible": True}, |
| 46 | + ) |
| 47 | + |
| 48 | + sys.argv = [sys.argv[0], "build_ext", "--inplace"] |
| 49 | + setup(name="cuda_bindings_cython_tests", ext_modules=ext_modules) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments