
    ^j                     d   U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ e
rd dlmZ e G d d             Zedgedz  f   ZddZddZddZddZddZddZddZ ddZ!eeee e!fZ"de#d<   d dZ$ejJ                  d!d       Z&d"dZ'y)#    )annotationsN)Callable)	dataclass)TYPE_CHECKING)-_resolve_system_loaded_abs_path_in_subprocess)derive_ctk_root)HEADER_DESCRIPTORSplatform_include_subdirsresolve_conda_anchor)get_cuda_path_or_home)find_sub_dirs_all_sitepackages)HeaderDescriptorc                  *    e Zd ZU ded<   ded<   ddZy)LocatedHeaderDir
str | Noneabs_pathstr	found_viac                8    t        | j                        | _        y N)	_abs_normr   )selfs    w/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/cuda/pathfinder/_headers/find_nvidia_headers.py__post_init__zLocatedHeaderDir.__post_init__&   s    !$--0    N)returnNone)__name__
__module____qualname____annotations__r    r   r   r   r   !   s    N1r   r   r   c                    | r<t         j                  j                  t         j                  j                  |             S y r   )ospathnormpathabspath)r%   s    r   r   r   2   s*    ww 566r   c                |    t         j                  j                  t         j                  j                  | |            S r   )r$   r%   isfilejoin)dirpathbasenames     r   _joined_isfiler-   8   s$    77>>"'',,w9::r   c                "   | j                   }| j                  D ]t  }t        j                  j	                  ||      }t        |       D ]4  }t        j                  j	                  ||      }t        ||      s0|c c S  t        ||      sr|c S  y)zNSearch for a header under *anchor_point* using the descriptor's layout fields.N)header_basenameanchor_include_rel_dirsr$   r%   r*   r
   r-   )descanchor_point
h_basenamerel_diridirsubdircdirs          r   _locate_in_anchor_layoutr8   <   s    %%J// ww||L'2.t4 	F77<<f-DdJ/	 $
+K r   c                    | j                   D ]Q  }t        t        |j                  d                  D ])  }t	        || j
                        st        |d      c c S  S y)z#Search pip wheel install locations./zsite-packagesr   r   N)site_packages_dirsr   tuplesplitr-   r/   r   )r1   sub_dirhdr_dirs      r   find_in_site_packagesrA   O   s_    ** U5eGMM#<N6OP 	UGgt';';<'OTT	UU
 r   c                    t         j                  j                  d      }|syt        | |      }|yt	        | |      }|rt        |d      S y)zSearch ``$CONDA_PREFIX``.CONDA_PREFIXNcondar;   )r$   environgetr   r8   r   )r1   conda_prefixr2   found_header_paths       r   find_in_condarI   Y   sP    ::>>.1L'l;L0|D):gNNr   c                T    t               }|yt        | |      }|t        |d      S y)z'Search ``$CUDA_PATH`` / ``$CUDA_HOME``.N	CUDA_PATHr;   )r   r8   r   )r1   	cuda_homeresults      r   find_in_cuda_pathrN   g   s5    %'I%dI6F;GGr   c                    | j                   syt        d      }|yt        |      }|yt        | |      }|t	        |d      S y)aL  Try CTK header lookup via CTK-root canary probing.

    Skips immediately if the descriptor does not opt in (``use_ctk_root_canary``).
    Otherwise, system-loads ``cudart`` in a fully isolated Python subprocess, derives
    CTK root from the resolved library path, and searches the expected include
    layout under that root.
    Ncudartzsystem-ctk-rootr;   )use_ctk_root_canaryr   r   r8   r   )r1   canary_abs_pathctk_rootrM   s       r   find_via_ctk_root_canaryrT   r   sY     ##CHMO/H%dH5F;LMMr   c                    | j                   D ]N  }t        t        j                  |      d      D ])  }t        || j                        st        |d      c c S  P y)z2Search system install directories (glob patterns).T)reversesupported_install_dirr;   N)system_install_dirssortedglobr-   r/   r   )r1   patternr@   s      r   find_in_system_install_dirsr\      s]    ++ ]dii0$? 	]Ggt';';<'D[\\	]] r   tuple[HeaderFindStep, ...]
FIND_STEPSc                .    |D ]  } ||       }||c S  y)z1Run find steps in order, returning the first hit.Nr"   )r1   stepssteprM   s       r   run_find_stepsrb      s*     dM r   c                l    t        j                  |       }|t        d|       t        |t              S )u  Locate the header directory for a supported NVIDIA library.

    Args:
        libname (str): The short name of the library whose headers are needed
            (e.g., ``"nvrtc"``, ``"cusolver"``, ``"nvshmem"``).

    Returns:
        LocatedHeaderDir or None: A LocatedHeaderDir object containing the absolute path
        to the discovered header directory and information about where it was found,
        or ``None`` if the headers cannot be found.

    Raises:
        RuntimeError: If ``libname`` is not in the supported set.

    Search order:
        1. **NVIDIA Python wheels** — site-packages directories from the descriptor.
        2. **Conda environments** — platform-specific conda include layouts.
        3. **CUDA Toolkit environment variables** — ``CUDA_PATH`` / ``CUDA_HOME``.
        4. **CTK root canary probe** — subprocess canary (descriptors with
           ``use_ctk_root_canary=True`` only).
        5. **System install directories** — glob patterns from the descriptor.
    zUNKNOWN libname=)r	   rF   RuntimeErrorrb   r^   )libnamer1   s     r   locate_nvidia_header_directoryrf      s9    0 !!'*D|.gZ011$
++r   c                8    t        |       }|r|j                  S dS )u  Locate the header directory for a supported NVIDIA library.

    Args:
        libname (str): The short name of the library whose headers are needed
            (e.g., ``"nvrtc"``, ``"cusolver"``, ``"nvshmem"``).

    Returns:
        str or None: Absolute path to the discovered header directory, or ``None``
        if the headers cannot be found.

    Raises:
        RuntimeError: If ``libname`` is not in the supported set.

    Search order:
        1. **NVIDIA Python wheels** — site-packages directories from the descriptor.
        2. **Conda environments** — platform-specific conda include layouts.
        3. **CUDA Toolkit environment variables** — ``CUDA_PATH`` / ``CUDA_HOME``.
        4. **CTK root canary probe** — subprocess canary (descriptors with
           ``use_ctk_root_canary=True`` only).
        5. **System install directories** — glob patterns from the descriptor.
    N)rf   r   )re   founds     r   find_nvidia_header_directoryri      s    , +73E"5>>,,r   )r%   r   r   r   )r+   r   r,   r   r   bool)r1   r   r2   r   r   r   )r1   r   r   LocatedHeaderDir | None)r1   r   r`   r]   r   rk   )re   r   r   rk   )re   r   r   r   )(
__future__r   	functoolsrZ   r$   collections.abcr   dataclassesr   typingr   5cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_libr   *cuda.pathfinder._dynamic_libs.search_stepsr   *cuda.pathfinder._headers.header_descriptorr	   r
   r   cuda.pathfinder._utils.env_varsr   $cuda.pathfinder._utils.find_sub_dirsr   r   r   HeaderFindStepr   r-   r8   rA   rI   rN   rT   r\   r^   r!   rb   cacherf   ri   r"   r   r   <module>rx      s    #   	 $ !   G 
 B OK 1 1 1 -.0@40GGH;&, *
&  , ,:-r   