88so `cimport cuda.bindings.*` fails to locate the .pxd files. We resolve
99the namespace package's source root from `cuda.bindings.__file__` and pass
1010it via `include_path=` so cythonize finds the .pxd tree on every platform.
11+
12+ When CUDA_PYTHON_CYTHON_CACHE_DIR is set, cythonize uses the same cache
13+ namespacing and include-path aliasing as cuda_core/build_hooks.py.
1114"""
1215
1316from __future__ import annotations
1417
18+ import importlib .util
1519import os
1620import sys
1721from pathlib import Path
1822
23+ import Cython
1924from Cython .Build import cythonize
2025from setuptools import setup
2126
2227import cuda .bindings
2328
29+ _COMPILER_DIRECTIVES = {"freethreading_compatible" : True }
30+
31+
32+ def _load_build_hooks ():
33+ # PEP 517 backend, not an installed module. Load by path so we do not put
34+ # cuda_core/ on sys.path (that would shadow the installed package).
35+ build_hooks_path = Path (__file__ ).resolve ().parents [2 ] / "build_hooks.py"
36+ spec = importlib .util .spec_from_file_location ("cuda_core_build_hooks" , build_hooks_path )
37+ module = importlib .util .module_from_spec (spec )
38+ spec .loader .exec_module (module )
39+ return module
40+
41+
42+ build_hooks = _load_build_hooks ()
43+
2444
2545def _bindings_source_root () -> Path :
2646 # cuda.bindings.__file__ -> .../<root>/cuda/bindings/__init__.py
@@ -32,26 +52,55 @@ def _bindings_source_root() -> Path:
3252 return root
3353
3454
55+ def _cythonize_tests (pyx_files ):
56+ cache_path = build_hooks ._cython_cache_path (
57+ "cuda-core-cython-tests" ,
58+ compiler_directives = _COMPILER_DIRECTIVES ,
59+ language_level = 3 ,
60+ cplus = True ,
61+ )
62+ cythonize_kwargs = {
63+ "language_level" : 3 ,
64+ "compiler_directives" : _COMPILER_DIRECTIVES ,
65+ "cache" : cache_path ,
66+ }
67+ if cache_path is None :
68+ return cythonize (
69+ pyx_files ,
70+ include_path = [str (_bindings_source_root ())],
71+ ** cythonize_kwargs ,
72+ )
73+
74+ # Distinct alias names so a concurrent package build's .cython-stdlib /
75+ # .cython-bindings symlinks are not replaced. Relative aliases resolve
76+ # next to build_hooks.py (package root).
77+ stdlib_target = Path (Cython .__file__ ).parent / "Includes"
78+ with (
79+ build_hooks ._stable_cython_alias (stdlib_target , Path (".cython-stdlib-tests" )) as rel_stdlib ,
80+ build_hooks ._stable_cython_alias (_bindings_source_root (), Path (".cython-bindings-tests" )) as rel_bindings ,
81+ ):
82+ return cythonize (
83+ pyx_files ,
84+ include_path = ["." , rel_bindings , rel_stdlib ],
85+ ** cythonize_kwargs ,
86+ )
87+
88+
3589def main () -> None :
3690 script_dir = Path (__file__ ).resolve ().parent
37- pyx_files = sorted (str ( p ) for p in script_dir .glob ("test_*.pyx" ))
91+ pyx_files = sorted (p . name for p in script_dir .glob ("test_*.pyx" ))
3892 if not pyx_files :
3993 raise SystemExit (f"no test_*.pyx files under { script_dir } " )
4094
41- ext_modules = cythonize (
42- pyx_files ,
43- language_level = 3 ,
44- include_path = [str (_bindings_source_root ())],
45- compiler_directives = {"freethreading_compatible" : True },
46- )
47-
4895 # `build_ext --inplace` places the compiled .so relative to the current
4996 # working directory, but pixi runs this task from the project root. pytest
5097 # imports each extension by bare module name (see test_cython.py), which
5198 # only resolves when the .so sits in tests/cython (the dir pytest puts on
52- # sys.path). chdir here so the . so lands next to its .pyx regardless of the
53- # invoking cwd.
99+ # sys.path). chdir before cythonize so alias relpaths and the .so location
100+ # are stable regardless of the invoking cwd.
54101 os .chdir (script_dir )
102+ ext_modules = _cythonize_tests (pyx_files )
103+
55104 sys .argv = [sys .argv [0 ], "build_ext" , "--inplace" ]
56105 setup (name = "cuda_core_cython_tests" , ext_modules = ext_modules )
57106
0 commit comments