Use pathlib in cuda.core build hooks, tests and examples (part 5 of #2410) - #2500
Use pathlib in cuda.core build hooks, tests and examples (part 5 of #2410)#2500LeSingh1 wants to merge 1 commit into
Conversation
mdboom
left a comment
There was a problem hiding this comment.
This generally looks good, but we should make ProgramOptions accept either Path or str (and coerce all incoming str to Path) to simplify this further.
| samples_path = os.path.join(os.path.dirname(__file__), "..", "..", "examples") | ||
| sample_files = [os.path.basename(x) for x in glob.glob(samples_path + "**/*.py", recursive=True)] | ||
| samples_path = Path(__file__).parents[2] / "examples" | ||
| sample_files = [Path(x).name for x in glob.glob(f"{samples_path}**/*.py", recursive=True)] |
There was a problem hiding this comment.
Why no samples_path.glob here?
| include_path = [str(cuda_include)] | ||
| cccl_include = cuda_include / "cccl" | ||
| if cccl_include.is_dir(): | ||
| include_path.insert(0, str(cccl_include)) |
There was a problem hiding this comment.
Let's expand what ProgramOptions can take so it also accepts a Path (in addition to str). That means we don't need to convert back to a str here.
Part of NVIDIA#2410. Replaces os.path with pathlib.Path in cuda_core/build_hooks.py, tests/helpers, the example test driver and the two examples that assemble CUDA include paths. ProgramOptions now accepts os.PathLike for every path-valued option (include_path, pre_include, create_pch, use_pch, pch_dir, fdevice_time_trace) and normalizes what it is given to pathlib.Path, so callers no longer have to convert back to str. Non-path values (False, range(...), ...) are left untouched, preserving the existing "silently ignored at compile time" behavior. name stays str (NVRTC uses it as a label and the program cache inspects it for a directory component); time stays str-or-bool because the same field is forwarded to LinkerOptions.time, which is a flag.
49a2238 to
7ea6afe
Compare
|
Done — Two widenings were needed to make that work, and one of them fixes a latent bug: Left alone:
One behaviour change worth your attention rather than burying: an empty-string path value now normalizes to Not run locally: the cuda_core suite and the Cython build need CUDA. I verified with py_compile, ruff, cython-lint, regenerated |
Part 5 of #2410.
Replaces
os.pathwithpathlib.Pathincuda_core/build_hooks.py,tests/helpers/__init__.py, the example test driver, and the two examples that assemble CUDA include paths.ProgramOptions.include_pathis typedstr | list[str] | tuple[str], so the values handed to it staystr; only the path construction moves to pathlib. Same for theExtensionarguments.The one non-mechanical change is extension discovery, which previously sliced glob result strings against an
os.path.sep-built prefix. It now relative-paths againstPath("cuda", "core")and yields POSIX-style module names on every platform — which is what the oldmod.replace(os.path.sep, "/")normalization already did. I checked that by re-implementing the old and new logic side by side and diffing them against the real source tree on Linux: identical module names,Extensionnames, source tuples and arch-specific sources across all 45 modules.glob.globis left as-is throughout; only the pattern construction moved.os.path.isdir→Path.is_dir()was checked per call rather than bulk-replaced — each one guards a "does this include/lib dir exist" decision where both spellings returnFalsefor a broken symlink or a permission error.Verified with
cuda_core/tests/test_build_hooks.pyon Linux CI: 14 passed, unchanged from the baseline. The test modules and examples need a GPU or a builtcuda.bindings, so those changes are by inspection plusruffandpy_compile; they are strictly mechanical.NOTE: developed with the assistance of an AI coding agent. I reviewed and verified the change before submitting.