o
    ٷij"                     @  s   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 d d	lmZ eG d
d dZd+ddZd,ddZd-ddZd.ddZd/d!d"Zd0d#d$Zd1d%d&Zejd1d'd(Zd2d)d*ZdS )3    )annotationsN)	dataclass)-_resolve_system_loaded_abs_path_in_subprocess)derive_ctk_root)supported_nvidia_headers)get_cuda_home_or_path)find_sub_dirs_all_sitepackages)
IS_WINDOWSc                   @  s(   e Zd ZU ded< ded< d
ddZd	S )LocatedHeaderDir
str | Noneabs_pathstr	found_viareturnNonec                 C  s   t | j| _d S N)	_abs_normr   )self r   `/home/ubuntu/.local/lib/python3.10/site-packages/cuda/pathfinder/_headers/find_nvidia_headers.py__post_init__   s   zLocatedHeaderDir.__post_init__N)r   r   )__name__
__module____qualname____annotations__r   r   r   r   r   r
      s   
 r
   pathr   r   c                 C  s   | rt jt j| S d S r   )osr   normpathabspath)r   r   r   r   r      s   r   dirpathr   basenameboolc                 C  s   t jt j| |S r   )r   r   isfilejoin)r   r    r   r   r   _joined_isfile$   s   r$   sub_dir
h_basenameLocatedHeaderDir | Nonec                 C  s6   t t| dD ]}t||rt|dd  S q	d S )N/zsite-packagesr   r   )r   tuplesplitr$   r
   )r%   r&   hdr_dirr   r   r   _locate_under_site_packages(   s
   
r-   libnameanchor_pointc                 C  s   |g}| dkr| |  | d tjj| }| dkrHtr:tj|dd}tj|d}t||r3|S t||r:|S tj|d}t||rH|S t||rO|S d S )Nnvvmincludecccltargetsx64)appendr   r   r#   r	   r$   )r.   r&   r/   partsidir
cdir_ctk12
cdir_ctk13cdirr   r   r   _locate_based_on_ctk_layout1   s&   





r;   
ctk_layoutc                 C  s   t jd}|s
d S trt j|d}t j|sd S n,|r;tt j|ddd}|s.d S t|dkr6d S |d }nt j|d}t j	|}t
| ||}|rVt|dd	S d S )
NCONDA_PREFIXLibraryr3   *r1      r   condar)   )r   environgetr	   r   r#   isdirgloblendirnamer;   r
   )r.   r&   r<   conda_prefixr/   targets_include_pathinclude_pathfound_header_pathr   r   r   _find_based_on_conda_layoutG   s*   
rL   c                 C  s4   t d}|du r
dS t|}|du rdS t| ||S )a-  Try CTK header lookup via CTK-root canary probing.

    Uses the same canary as dynamic-library CTK-root discovery: system-load
    ``cudart`` in a spawned child process, derive CTK root from the resolved
    absolute library path, then search the expected CTK include layout under
    that root.
    cudartN)r   r   r;   )r.   r&   canary_abs_pathctk_rootr   r   r   %_find_ctk_header_directory_via_canaryb   s   rP   c                 C  s   t j|  }t j|  }|D ]}t|| }r|  S qt| |d }r$|S t }|r7t| || }r7t|ddS t| | }rDt|ddS d S )NT	CUDA_HOMEr)   zsystem-ctk-root)	r   SUPPORTED_HEADERS_CTK&SUPPORTED_SITE_PACKAGE_HEADER_DIRS_CTKr-   rL   r   r;   r
   rP   )r.   r&   candidate_dirsr:   r,   	cuda_homeresultr   r   r   _find_ctk_header_directorys   s   

rW   c                 C  s   | t jv r	t| S t j| }|du rtd| t j| g }|D ]}t|| }r0|  S q#t| |d }r;|S t j	| g }|D ]}t
t|ddD ]}t||r`t|dd    S qOqDdS )a  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**

           - Scan installed distributions (``site-packages``) for header layouts
             shipped in NVIDIA wheels (e.g., ``cuda-toolkit[nvrtc]``).

        2. **Conda environments**

           - Check Conda-style installation prefixes, which use platform-specific
             include directory layouts.

        3. **CUDA Toolkit environment variables**

           - Use ``CUDA_HOME`` or ``CUDA_PATH`` (in that order).

        4. **CTK root canary probe**

           - Probe a system-loaded ``cudart`` in a spawned child process,
             derive the CTK root from the resolved library path, then search
             CTK include layout under that root.
    NzUNKNOWN libname=FT)reversesupported_install_dirr)   )r   rR   rW   SUPPORTED_HEADERS_NON_CTKrC   RuntimeError*SUPPORTED_SITE_PACKAGE_HEADER_DIRS_NON_CTKr-   rL   SUPPORTED_INSTALL_DIRS_NON_CTKsortedrE   r$   r
   )r.   r&   rT   r:   	found_hdrr,   r   r   r   locate_nvidia_header_directory   s(   
&
r`   c                 C  s   t | }|r	|jS dS )a  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**

           - Scan installed distributions (``site-packages``) for header layouts
             shipped in NVIDIA wheels (e.g., ``cuda-toolkit[nvrtc]``).

        2. **Conda environments**

           - Check Conda-style installation prefixes, which use platform-specific
             include directory layouts.

        3. **CUDA Toolkit environment variables**

           - Use ``CUDA_HOME`` or ``CUDA_PATH`` (in that order).

        4. **CTK root canary probe**

           - Probe a system-loaded ``cudart`` in a spawned child process,
             derive the CTK root from the resolved library path, then search
             CTK include layout under that root.
    N)r`   r   )r.   foundr   r   r   find_nvidia_header_directory   s   #rb   )r   r   r   r   )r   r   r    r   r   r!   )r%   r   r&   r   r   r'   )r.   r   r&   r   r/   r   r   r   )r.   r   r&   r   r<   r!   r   r'   )r.   r   r&   r   r   r   )r.   r   r   r'   )r.   r   r   r   )
__future__r   	functoolsrE   r   dataclassesr   5cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_libr   *cuda.pathfinder._dynamic_libs.search_stepsr   cuda.pathfinder._headersr   cuda.pathfinder._utils.env_varsr   $cuda.pathfinder._utils.find_sub_dirsr   %cuda.pathfinder._utils.platform_awarer	   r
   r   r$   r-   r;   rL   rP   rW   cacher`   rb   r   r   r   r   <module>   s.   



	


?