From 6e60687af52cb3e5831747f65ce710bb26be5b5f Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Wed, 25 Nov 2026 12:58:56 -0700 Subject: [PATCH] refactor(pathfinder): add a search context resolved per platform Most of the `SearchPlatform` protocol only selects descriptor fields for one platform. This adds that selection as plain data: a frozen `SearchContext` record, built by `linux_search_context()` or `windows_search_context()`. Each platform's rules now read as one table. Nothing uses the record yet. A new test checks that it equals the existing platform classes for every catalog entry on Linux, Windows x64 and Windows arm64 (150 cases). The next change switches the search steps over and removes the classes. No behavior change. 1795 passed, 6 skipped. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- .../_dynamic_libs/search_platform.py | 72 ++++++++++++++++++- cuda_pathfinder/tests/test_search_steps.py | 28 ++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py index c7f598c96e8b..4a7a6b2db140 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py @@ -12,7 +12,7 @@ import os from collections.abc import Sequence -from dataclasses import dataclass +from dataclasses import dataclass, field from pathlib import PurePath from typing import Protocol, cast @@ -23,6 +23,76 @@ from cuda.pathfinder._utils.windows_arch import windows_pe_matches_arch, windows_python_arch +@dataclass(frozen=True, slots=True) +class SearchContext: + """One library's search inputs, resolved for one target platform. + + Build one with :func:`linux_search_context`, :func:`windows_search_context`, + or :func:`search_context` (the platform this process runs on). + """ + + desc: LibDescriptor + #: ``None`` on Linux; ``"x64"`` or ``"arm64"`` on Windows. + windows_arch: str | None + #: Wording that names the library in error messages. + lib_searched_for: str + site_packages_rel_dirs: tuple[str, ...] + #: Path parts between ``$CONDA_PREFIX`` and the directory the anchors hang off. + conda_anchor_parts: tuple[str, ...] + anchor_rel_dirs: tuple[str, ...] + install_root_env_vars: tuple[str, ...] + install_root_env_rel_dirs: tuple[str, ...] + #: Globs relative to the Windows Program Files directory. + program_files_rel_globs: tuple[str, ...] + #: Windows architecture that a candidate DLL must match; ``None`` means no check. + pe_arch_to_check: str | None + error_messages: list[str] = field(default_factory=list) + attachments: list[str] = field(default_factory=list) + + def conda_anchor_point(self, conda_prefix: str) -> str: + return os.path.join(conda_prefix, *self.conda_anchor_parts) + + +def linux_search_context(desc: LibDescriptor) -> SearchContext: + return SearchContext( + desc=desc, + windows_arch=None, + lib_searched_for=" or ".join(desc.linux_sonames), + site_packages_rel_dirs=desc.site_packages_linux, + conda_anchor_parts=(), + anchor_rel_dirs=desc.anchor_rel_dirs_linux, + install_root_env_vars=desc.install_root_env_vars_linux, + install_root_env_rel_dirs=desc.install_root_env_rel_dirs_linux, + program_files_rel_globs=(), + pe_arch_to_check=None, + ) + + +def windows_search_context(desc: LibDescriptor, target_arch: str) -> SearchContext: + arch_is_supported = target_arch in desc.supported_windows_arch + return SearchContext( + desc=desc, + windows_arch=target_arch, + lib_searched_for=f"known {desc.name} DLL", + site_packages_rel_dirs=desc.site_packages_windows.for_arch(target_arch), + conda_anchor_parts=("Library",), + anchor_rel_dirs=desc.anchor_rel_dirs_windows.for_arch(target_arch), + install_root_env_vars=desc.install_root_env_vars_windows if arch_is_supported else (), + install_root_env_rel_dirs=( + desc.install_root_env_rel_dirs_windows.for_arch(target_arch) if arch_is_supported else () + ), + program_files_rel_globs=desc.program_files_root_globs_windows.for_arch(target_arch), + pe_arch_to_check=target_arch if desc.requires_windows_binary_arch_check else None, + ) + + +def search_context(desc: LibDescriptor) -> SearchContext: + """Resolve *desc* for the platform this process runs on.""" + if IS_WINDOWS: + return windows_search_context(desc, windows_python_arch()) + return linux_search_context(desc) + + def _append_dir_listing(dirpath: str, attachments: list[str]) -> None: # The listing only illustrates a miss. A directory that cannot be listed is # reported in the not-found message so the search can move on to the next location. diff --git a/cuda_pathfinder/tests/test_search_steps.py b/cuda_pathfinder/tests/test_search_steps.py index a58e9c670c30..fe90c61d48f2 100644 --- a/cuda_pathfinder/tests/test_search_steps.py +++ b/cuda_pathfinder/tests/test_search_steps.py @@ -20,6 +20,8 @@ LinuxSearchPlatform, WindowsSearchPlatform, _find_descriptor_dll_under_dir, + linux_search_context, + windows_search_context, ) from cuda.pathfinder._dynamic_libs.search_steps import ( EARLY_FIND_STEPS, @@ -1074,3 +1076,29 @@ def test_nvvm_cuda_home_linux(self, mocker, tmp_path): assert result is not None assert result.abs_path == str(so_file) assert result.found_via == "CUDA_PATH" + + +# --------------------------------------------------------------------------- +# Resolved search context vs. the platform classes it replaces +# --------------------------------------------------------------------------- + + +@pytest.mark.parametrize("windows_arch", (None, "x64", "arm64"), ids=("linux", "x64", "arm64")) +@pytest.mark.parametrize("desc", LIB_DESCRIPTORS.values(), ids=lambda desc: desc.name) +@pytest.mark.agent_authored(model="claude-fable-5-2") +def test_search_context_matches_search_platform(desc, windows_arch, monkeypatch): + monkeypatch.delenv("PROGRAMW6432", raising=False) + monkeypatch.setenv("PROGRAMFILES", "ProgramFilesRoot") + if windows_arch is None: + platform, ctx = LinuxSearchPlatform(), linux_search_context(desc) + else: + platform, ctx = WindowsSearchPlatform(target_arch=windows_arch), windows_search_context(desc, windows_arch) + + assert ctx.lib_searched_for == platform.lib_searched_for(desc) + assert ctx.site_packages_rel_dirs == platform.site_packages_rel_dirs(desc) + assert ctx.conda_anchor_point("conda-prefix") == platform.conda_anchor_point("conda-prefix") + assert ctx.anchor_rel_dirs == platform.anchor_rel_dirs(desc) + assert ctx.install_root_env_vars == platform.install_root_env_vars(desc) + assert ctx.install_root_env_rel_dirs == platform.install_root_env_rel_dirs(desc) + program_files_root_globs = tuple(os.path.join("ProgramFilesRoot", glob) for glob in ctx.program_files_rel_globs) + assert program_files_root_globs == platform.program_files_root_globs(desc)