
    ^j              	          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             Ze G d d             Zeegedz  f   Zdededededz  fdZdededz  dedz  fdZdededz  fdZdededz  fdZdededz  fdZdedededz  fdZdededz  fdZdededz  fdZdededz  f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z  fd#Z%y)$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_path_or_homec                   &    e Zd ZU dZeed<   eed<   y)
FindResultz0A library file located on disk (not yet loaded).abs_path	found_viaN)__name__
__module____qualname____doc__str__annotations__     u/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/cuda/pathfinder/_dynamic_libs/search_steps.pyr   r   &   s    :MNr   r   c                       e Zd ZU dZeed<   eZeed<    e	e
      Ze
e   ed<    e	e
      Ze
e   ed<   edefd       Zedefd	       Zdefd
Zy)SearchContextz4Mutable state accumulated during the search cascade.descplatform)default_factoryerror_messagesattachmentsreturnc                 .    | j                   j                  S N)r   nameselfs    r   libnamezSearchContext.libname7   s    yy~~r   c                 h    t        t        | j                  j                  | j                              S r#   )r   r   r   lib_searched_forr'   r%   s    r   r)   zSearchContext.lib_searched_for;   s"    C77EFFr   c                     dj                  | j                        }dj                  | j                        }t        d| j                   d| d|       )Nz, 
zFailure finding "z": )joinr   r    r	   r)   )r&   erratts      r   raise_not_foundzSearchContext.raise_not_found?   sV    ii++,ii(()%(9$:O:O9PPSTWSXXZ[^Z_&`aar   N)r   r   r   r   r   r   r
   r   r   r   listr   r   r    propertyr'   r)   r   r/   r   r   r   r   r   .   s    >
'Hn' %d ;NDI;"48Kc8   G# G Gb br   r   r   r   anchor_pointr!   c           	      :   |j                  |       }|D ]  }t        t        j                  t        j                  j                  ||                  D ]D  }t        j                  j                  |      s#t        j                  j                  |      c c S   y)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   r2   rel_dirsrel_pathdirnames         r   _find_lib_dir_using_anchorr>   I   sw    ''-H 1dii\8(LMN 	1Gww}}W%ww''00	11 r   ctxlib_dirc           
          |yt        t        dz  | j                  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    )r?   r@   s     r   _find_using_lib_dirrC   S   sS    d
$$KK  OO	
	 	r   resolved_lib_pathc                    t         j                  j                  |       }t         j                  j                  |      }|dv rt         j                  j                  |      }t         j                  j                  |      }t         j                  j                  |      dk(  rt         j                  j                  |      S |S y)zDerive 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)r7   r8   r=   basename)rD   r@   rI   parentgrandparents        r   _derive_ctk_root_linuxrL   c   s     ggoo/0Gww(H##)ggoof-77K(I577??;//r   c                 ,   ddl }|j                  |       }|j                  |      j                         }|dk(  rE|j                  |      }|j                  |      j                         dk(  r|j                  |      S y|dk(  r|j                  |      S y)zDerive 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)ntpathr=   rI   lower)rD   rP   r@   rI   rJ   s        r   _derive_ctk_root_windowsrR   w   s     nn./Gw'--/H5(??6"((*e3>>&))  
U	~~g&&r   c                 6    t        |       }||S t        |       S )z4Derive CTK root from a resolved canary library path.)rL   rR   )rD   ctk_roots     r   derive_ctk_rootrU      s$    %&78H#$566r   rT   c                 z    t        | j                  | j                  |      }t        | |      }|yt	        |d      S )z3Find a library under a previously derived CTK root.Nzsystem-ctk-root)r>   r   r   rC   r   )r?   rT   r@   r   s       r   find_via_ctk_rootrW      s;    (3<<JG"30Hh 122r   c                     | j                   j                  | j                        }|sy| j                   j                  || j                  | j
                  | j                        }|t        |d      S y)z#Search pip wheel install locations.Nzsite-packages)r   site_packages_rel_dirsr   find_in_site_packagesr)   r   r    r   )r?   r;   r   s      r   rZ   rZ      sd    ||22388<H||11(C<P<PRURdRdfifufuvH(O44r   c                     t         j                  j                  d      }|sy| j                  j	                  |      }t        | j                  | j                  |      }t        | |      }|t        |d      S y)zSearch ``$CONDA_PREFIX``.CONDA_PREFIXNconda)	r7   environgetr   conda_anchor_pointr>   r   rC   r   )r?   conda_prefixanchorr@   r   s        r   find_in_condarc      sf    ::>>.1L\\,,\:F(3<<HG"30H(G,,r   c                     t               }|yt        | j                  | j                  |      }t	        | |      }|t        |d      S y)a  Search ``$CUDA_PATH`` / ``$CUDA_HOME``.

    On Windows, this is the normal fallback for system-installed CTK DLLs when
    they are not already discoverable via the native ``LoadLibraryExW(..., 0)``
    path used by :func:`cuda.pathfinder._dynamic_libs.load_dl_windows.load_with_system_search`.
    Python 3.8+ does not include ``PATH`` in that native DLL search.

    The returned ``found_via`` is always ``"CUDA_PATH"`` regardless of which
    environment variable actually provided the value.
    N	CUDA_PATH)r   r>   r   r   rC   r   )r?   	cuda_homer@   r   s       r   find_in_cuda_pathrg      sM     &'I(3<<KG"30H(K00r   .EARLY_FIND_STEPSLATE_FIND_STEPSstepsc                 .    |D ]  } ||       }||c S  y)z1Run find steps in order, returning the first hit.Nr   )r?   rj   stepresults       r   run_find_stepsrn      s*     cM r   )&r   r6   r7   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   r>   rC   rL   rR   rU   rW   rZ   rc   rg   rh   tupler   ri   rn   r   r   r   <module>rx      s  $  	 $ ( ! F P R A    b b b. ]OZ$%667] n \_ dgjndn ] S4Z C$J  c cDj ( d
 (7s 7sTz 73= 3C 3J<M 3} d1B 
} 
d): 
= Z$-> 4 +@)O %#& O *;(<x}% < eHcM.B zTXGX r   