Coverage for cuda/pathfinder/_headers/header_descriptor.py: 73.53%

34 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-03 02:41 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# SPDX-License-Identifier: Apache-2.0 

3 

4"""Per-header descriptor, registry, and platform-aware accessors. 

5 

6The canonical authored data lives in :mod:`header_descriptor_catalog`. This 

7module provides a name-keyed registry and platform-dispatch helpers consumed 

8by the runtime search path — keeping the search code itself platform-agnostic. 

9""" 

10 

11from __future__ import annotations 

12 

13import glob 

14import os 

15import sysconfig 

16from typing import TypeAlias, cast 

17 

18from cuda.pathfinder._headers.header_descriptor_catalog import ( 

19 HEADER_DESCRIPTOR_CATALOG, 

20 HeaderDescriptorSpec, 

21) 

22from cuda.pathfinder._utils.platform_aware import IS_WINDOWS 

23 

24HeaderDescriptor: TypeAlias = HeaderDescriptorSpec 

25 

26#: Canonical registry of all known header libraries. 

27HEADER_DESCRIPTORS: dict[str, HeaderDescriptor] = {desc.name: desc for desc in HEADER_DESCRIPTOR_CATALOG} 

28 

29 

30def platform_include_subdirs(desc: HeaderDescriptor) -> tuple[str, ...]: 

31 """Return the effective include subdirectory search list for the current platform. 

32 

33 On Windows, Windows-specific subdirs are checked first, followed by the 

34 common subdirs. On Linux, only the common subdirs are returned. 

35 """ 

36 if IS_WINDOWS: 1EcFrdQefghGHIiJKLMNsbtujvwklxmnyzOP

37 return cast(tuple[str, ...], desc.include_subdirs_windows + desc.include_subdirs) 1EcFdefghGHIiJKLMNbjklmnOP

38 return cast(tuple[str, ...], desc.include_subdirs) 1EcFrdQefghGHIiJKLMNsbtujvwklxmnyzOP

39 

40 

41def system_install_dir_patterns(desc: HeaderDescriptor) -> tuple[str, ...]: 

42 """Return platform-aware, expanded system include-directory patterns.""" 

43 patterns: list[str] = [] 1aDopBCqcrdefghiAsbtujvwklxmnyz

44 if IS_WINDOWS: 1aDopBCqcrdefghiAsbtujvwklxmnyz

45 patterns.extend(os.path.expandvars(pattern) for pattern in desc.system_install_dirs_windows) 1aDcdefghiAbjklmn

46 return tuple(patterns) 1aDcdefghiAbjklmn

47 if desc.use_linux_multiarch_include_dir: 1aopBCqcrdefghiAsbtujvwklxmnyz

48 multiarch = sysconfig.get_config_var("MULTIARCH") 1aopqb

49 if isinstance(multiarch, str) and multiarch: 1aopqb

50 patterns.append(os.path.join("/usr/include", multiarch)) 1aopqb

51 patterns.extend(os.path.expandvars(pattern) for pattern in desc.system_install_dirs) 1aopBCqcrdefghiAsbtujvwklxmnyz

52 return tuple(patterns) 1aopBCqcrdefghiAsbtujvwklxmnyz

53 

54 

55def resolve_conda_anchor(desc: HeaderDescriptor, conda_prefix: str) -> str | None: 

56 """Resolve the conda anchor point for header search on the current platform. 

57 

58 Returns the directory that ``_locate_in_anchor_layout`` should use as 

59 *anchor_point*, or ``None`` if the conda layout is not usable. 

60 """ 

61 if IS_WINDOWS: 

62 anchor = os.path.join(conda_prefix, "Library") 

63 return anchor if os.path.isdir(anchor) else None 

64 if desc.conda_targets_layout: 

65 targets_include_path = glob.glob(os.path.join(conda_prefix, "targets", "*", "include")) 

66 if not targets_include_path or len(targets_include_path) != 1: 

67 return None 

68 return os.path.dirname(targets_include_path[0]) 

69 return conda_prefix