o
    §Ù·iì  ã                	   @   sÚ  U d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ dd	lmZ eG d
d„ dƒƒZeG dd„ dƒƒZeegedB f ZdededededB fdd„ZdededB dedB fdd„ZdededB fdd„ZdededB fdd„ZdededB fdd„ZdedededB fd d!„ZdededB fd"d#„ZdededB fd$d%„ZdededB fd&d'„Z eefZ!e"ed(f e#d)< e fZ$e"ed(f e#d*< ded+e"ed(f dedB fd,d-„Z%dS ).a¬  Composable search steps for locating NVIDIA libraries.

Each find step is a callable with signature::

    (SearchContext) -> FindResult | None

Find steps locate a library file on disk without loading it.  The
orchestrator in :mod:`load_nvidia_dynamic_lib` handles loading, the
already-loaded check, and dependency resolution.

Step sequences are defined per search strategy so that adding a new
step or strategy only requires adding a function and a tuple entry.

This module is intentionally platform-agnostic: it does not branch on the
current operating system. Platform differences are routed through the
:data:`~cuda.pathfinder._dynamic_libs.search_platform.PLATFORM` instance.
é    N)ÚCallable)Ú	dataclassÚfield)ÚNoReturnÚcast)ÚLibDescriptor)ÚDynamicLibNotFoundError)ÚPLATFORMÚSearchPlatform)Úget_cuda_home_or_pathc                   @   s"   e Zd ZU dZeed< eed< dS )Ú
FindResultz0A library file located on disk (not yet loaded).Úabs_pathÚ	found_viaN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__© r   r   ú^/home/ubuntu/.local/lib/python3.10/site-packages/cuda/pathfinder/_dynamic_libs/search_steps.pyr   &   s   
 r   c                   @   s„   e Zd ZU dZeed< eZeed< e	e
dZe
e ed< e	e
dZe
e ed< edefdd	„ƒZedefd
d„ƒZdefdd„ZdS )ÚSearchContextz4Mutable state accumulated during the search cascade.ÚdescÚplatform)Údefault_factoryÚerror_messagesÚattachmentsÚreturnc                 C   s   | j jS ©N)r   Úname©Úselfr   r   r   Úlibname7   s   zSearchContext.libnamec                 C   s   t t| j | j¡ƒS r   )r   r   r   Úlib_searched_forr"   r    r   r   r   r#   ;   s   zSearchContext.lib_searched_forc                 C   s4   d  | j¡}d  | j¡}td| j› d|› d|› ƒ‚)Nz, Ú
zFailure finding "z": )Újoinr   r   r   r#   )r!   ÚerrÚattr   r   r   Úraise_not_found?   s   zSearchContext.raise_not_foundN)r   r   r   r   r   r   r	   r   r
   r   Úlistr   r   r   Úpropertyr"   r#   r   r(   r   r   r   r   r   .   s   
 r   r   r   Úanchor_pointr   c                 C   sV   |  | ¡}|D ]!}tt tj ||¡¡ƒD ]}tj |¡r'tj |¡    S qqdS )zVFind the library directory under *anchor_point* using the descriptor's relative paths.N)Úanchor_rel_dirsÚsortedÚglobÚosÚpathr%   ÚisdirÚnormpath)r   r   r+   Úrel_dirsÚrel_pathÚdirnamer   r   r   Ú_find_lib_dir_using_anchorI   s   
ÿÿr6   ÚctxÚlib_dirc              	   C   s2   |du rdS t tdB | j || j| j| j| j¡ƒS )z0Find a library file in a resolved lib directory.N)r   r   r   Úfind_in_lib_dirr"   r#   r   r   )r7   r8   r   r   r   Ú_find_using_lib_dirS   s   ûþr:   Úresolved_lib_pathc                 C   s\   t j | ¡}t j |¡}|dv r,t j |¡}t j |¡}t j |¡dkr*t j |¡S |S dS )zñDerive CTK root from Linux canary path.

    Supports:
    - ``$CTK_ROOT/lib64/libfoo.so.*``
    - ``$CTK_ROOT/lib/libfoo.so.*``
    - ``$CTK_ROOT/targets/<triple>/lib64/libfoo.so.*``
    - ``$CTK_ROOT/targets/<triple>/lib/libfoo.so.*``
    )Úlib64ÚlibÚtargetsN)r/   r0   r5   Úbasename)r;   r8   r?   ÚparentÚgrandparentr   r   r   Ú_derive_ctk_root_linuxc   s   	rB   c                 C   sh   ddl }| | ¡}| |¡ ¡ }|dkr)| |¡}| |¡ ¡ dkr'| |¡S dS |dkr2| |¡S dS )zŸDerive CTK root from Windows canary path.

    Supports:
    - ``$CTK_ROOT/bin/x64/foo.dll`` (CTK 13 style)
    - ``$CTK_ROOT/bin/foo.dll`` (CTK 12 style)
    r   NÚx64Úbin)Úntpathr5   r?   Úlower)r;   rE   r8   r?   r@   r   r   r   Ú_derive_ctk_root_windowsw   s   


þ
rG   c                 C   s   t | ƒ}|dur
|S t| ƒS )z4Derive CTK root from a resolved canary library path.N)rB   rG   )r;   Úctk_rootr   r   r   Úderive_ctk_root‹   s   rI   rH   c                 C   s0   t | j| j|ƒ}t| |ƒ}|du rdS t|dƒS )z3Find a library under a previously derived CTK root.Nzsystem-ctk-root)r6   r   r   r:   r   )r7   rH   r8   r   r   r   r   Úfind_via_ctk_root“   s
   

rJ   c                 C   sD   | j  | j¡}|sdS | j  || j| j| j¡}|dur t|dƒS dS )z#Search pip wheel install locations.Nzsite-packages)r   Úsite_packages_rel_dirsr   Úfind_in_site_packagesr#   r   r   r   )r7   r3   r   r   r   r   rL   ¡   s   
rL   c                 C   sP   t j d¡}|s
dS | j |¡}t| j| j|ƒ}t| |ƒ}|dur&t|dƒS dS )zSearch ``$CONDA_PREFIX``.ÚCONDA_PREFIXNÚconda)	r/   ÚenvironÚgetr   Úconda_anchor_pointr6   r   r:   r   )r7   Úconda_prefixÚanchorr8   r   r   r   r   Úfind_in_conda¬   s   

rT   c                 C   sB   t ƒ }|du r	dS t| j| j|ƒ}t| |ƒ}|durt|dƒS dS )z'Search ``$CUDA_HOME`` / ``$CUDA_PATH``.NÚ	CUDA_HOME)r   r6   r   r   r:   r   )r7   Ú	cuda_homer8   r   r   r   r   Úfind_in_cuda_home¹   s   

rW   .ÚEARLY_FIND_STEPSÚLATE_FIND_STEPSÚstepsc                 C   s&   |D ]}|| ƒ}|dur|  S qdS )z1Run find steps in order, returning the first hit.Nr   )r7   rZ   ÚstepÚresultr   r   r   Úrun_find_stepsÕ   s   ÿr]   )&r   r.   r/   Úcollections.abcr   Údataclassesr   r   Útypingr   r   Ú,cuda.pathfinder._dynamic_libs.lib_descriptorr   Ú,cuda.pathfinder._dynamic_libs.load_dl_commonr   Ú-cuda.pathfinder._dynamic_libs.search_platformr	   r
   Úcuda.pathfinder._utils.env_varsr   r   r   ÚFindStepr   r6   r:   rB   rG   rI   rJ   rL   rT   rW   rX   Útupler   rY   r]   r   r   r   r   Ú<module>   s6   
&