Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 _no_such_file_in_sub_dirs(
sub_dirs: Sequence[str], file_description: str, error_messages: list[str], attachments: list[str]
) -> None:
Expand Down
28 changes: 28 additions & 0 deletions cuda_pathfinder/tests/test_search_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Loading