o
    ƒnócB4  ã                   @  sº   d dl mZ d dl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mZ ejrSdd
lmZ ddlmZ ejeje eje ef ZG dd„ deƒZdS )é    )ÚannotationsN)ÚPathé   )Úmesonlib)Úmlog)Údetect_cpu_familyé   )ÚDependencyExceptionÚSystemDependency)ÚEnvironment)ÚCompilerc                      s8  e Zd Zg d¢ZdL‡ fdd	„ZedMdd„ƒZdNdd„ZdOdd„ZdPdd„Z	dQdd„Z
dQdd „ZdQd!d"„Ze d#¡Ze d$¡Ze d%¡Ze d&¡ZdRd(d)„ZdSd+d,„ZdTd-d.„ZedUd0d1„ƒZdVd2d3„ZdWd5d6„ZdWd7d8„ZejdXd:d;„ƒZejdYd=d;„ƒZdZd[d@d;„ZdVdAdB„ZdVdCdD„Zd\dEdF„Zd]d^dJdK„Z ‡  Z!S )_ÚCudaDependency)ÚcudaÚcppÚcÚenvironmentú'Environment'ÚkwargsúT.Dict[str, T.Any]ÚreturnÚNonec                   sH  |j j|  |¡ }|  |¡}|| jvrtd|› d| j› dƒ‚tƒ jd|||d i | _|  	|¡| _
d| j
vr>dg| j
 | _
|  ¡ \| _| _| _| jsMd S tj | j¡s]td| j› dƒ‚| jdksht|ƒd	kr}tj | jd
¡| _|  jd| j› g7  _| jdkrš|  ¡ }tj | j|¡| _t dt | j¡¡ nd | _|  ¡ | _d S )Nz
Language 'z@' is not supported by the CUDA Toolkit. Supported languages are Ú.r   )ÚlanguageZcudartz)CUDA Toolkit path must be absolute, got 'z'.r   Úincludez-IzCUDA library directory is)ÚcoredataÚ	compilersZget_for_machine_from_kwargsÚ_detect_languageÚsupported_languagesr	   ÚsuperÚ__init__Úlib_modulesÚget_requestedÚrequested_modulesÚ_detect_cuda_path_and_versionÚ	cuda_pathÚversionZis_foundÚosÚpathÚisabsr   ÚlenÚjoinÚincdirZcompile_argsÚ_detect_arch_libdirÚlibdirr   ÚdebugÚboldÚ_find_requested_libraries)Úselfr   r   r   r   Zarch_libdir©Ú	__class__© ú{home/ych/rk3568/buildroot/output/rockchip_rk3568_recovery/host/lib/python3.10/site-packages/mesonbuild/dependencies/cuda.pyr   &   s.   



zCudaDependency.__init__r   úT.Dict[str, 'Compiler']Ústrc                 C  s,   | j D ]
}||v r|  S qt| ¡ ƒd S )Nr   )r   ÚlistÚkeys)Úclsr   Úlangr4   r4   r5   r   H   s
   
ÿzCudaDependency._detect_languageÚTV_ResultTuplec           
        s  ˆ   ¡ ˆ _t dt ˆ j¡¡ ˆ j}ˆ jdkrKˆ  ˆ  ¡ j	¡}t dt |¡¡ |rDt
 ||¡\}}}|rDd|› d|› d}ˆ  |d¡S d|› g}nd }‡ fd	d
„ˆ  ¡ D ƒ}|raˆ  |||¡S dd
„ |D ƒ}|rw|d d |d d dfS ˆ  ¡ r}dnd}	d|	› d}ˆ  |d¡S )NzDefault path env var:r   znvcc version:zThe current nvcc version zB does not satisfy the specified CUDA Toolkit version requirements r   ©NNFú=c                   s    g | ]\}}|ˆ   |¡|f‘qS r4   )Ú_cuda_toolkit_version)Ú.0r'   Údefault©r1   r4   r5   Ú
<listcomp>c   ó     z@CudaDependency._detect_cuda_path_and_version.<locals>.<listcomp>c                 S  s   g | ]\}}}|r||f‘qS r4   r4   )r@   r'   r%   rA   r4   r4   r5   rC   g   s    r   r   Tz&set the CUDA_PATH environment variablezQset the CUDA_PATH environment variable/create the '/usr/local/cuda' symbolic linkzaPlease specify the desired CUDA Toolkit version (e.g. dependency('cuda', version : '>=10.1')) or z2 to point to the location of your desired version.)Ú_default_path_env_varÚenv_varr   r.   r/   Úversion_reqsr   Ú_strip_patch_versionZget_compilerr%   r   Úversion_compare_manyÚ_report_dependency_errorÚ_cuda_pathsÚ_find_matching_toolkitÚ_is_windows)
r1   rG   Únvcc_versionÚ
found_someÚ	not_foundÚfoundÚmsgÚpathsÚdefaultsZplatform_msgr4   rB   r5   r#   O   s0   

ÿz,CudaDependency._detect_cuda_path_and_versionrS   úT.List[TV_ResultTuple]rG   úT.List[str]rN   úT.Optional[str]c                 C  sâ   dd„ }t  ||¡\}}t|ƒ}|t|dd„ dd }t d|› ¡ |rC|rC| jr1d| j› dnd	}d
 |tj	 
|d d ¡|¡}	nd }	|D ] \}
}}t  ||¡\}}}|sg|s`|	r`t |	¡ |
|df  S qG|	rot |	¡ dS )Nc                 S  s
   | d  S )Nr   r4   ©Útr4   r4   r5   Ú<lambda>s   s   
 z7CudaDependency._find_matching_toolkit.<locals>.<lambda>c                 S  s   t  | d ¡S )Nr   )r   ÚVersionrX   r4   r4   r5   rZ   v   s    T)ÚkeyÚreversezSearch paths: zthe z environment variablez#the '/usr/local/cuda' symbolic linkzpThe default CUDA Toolkit as designated by {} ({}) doesn't match the current nvcc version {} and will be ignored.r   r=   )r   Ú	partitionr8   Úsortedr   r.   rF   Úformatr&   r'   ÚrealpathrI   Úwarning)r1   rS   rG   rN   Z	part_funcZdefaults_itZrest_itrT   Zdefault_srcZnvcc_warningr'   r%   rA   rO   rP   rQ   r4   r4   r5   rL   p   s&    
ý
z%CudaDependency._find_matching_toolkitc                 C  sl   |   ¡ rdgng d¢}dd„ |D ƒ}dd„ |D ƒ}t|ƒdkr.t d d	 |¡d	 |¡¡¡ |r4|d
 S d S )NÚ	CUDA_PATH)rc   Z	CUDA_HOMEZ	CUDA_ROOTc                 S  s   g | ]	}|t jv r|‘qS r4   ©r&   Úenviron©r@   Úvarr4   r4   r5   rC   Œ   s    z8CudaDependency._default_path_env_var.<locals>.<listcomp>c                 S  s   h | ]}t j| ’qS r4   rd   rf   r4   r4   r5   Ú	<setcomp>   s    z7CudaDependency._default_path_env_var.<locals>.<setcomp>r   zyEnvironment variables {} point to conflicting toolkit locations ({}). Toolkit selection might produce unexpected results.ú, r   )rM   r)   r   rb   r`   r*   )r1   Úenv_varsZuser_defaultsr4   r4   r5   rE   Š   s   z$CudaDependency._default_path_env_varúT.List[T.Tuple[str, bool]]c                 C  s6   | j rtj| j  dfgng |  ¡ r|  ¡  S |  ¡  S )NT)rF   r&   re   rM   Ú_cuda_paths_winÚ_cuda_paths_nixrB   r4   r4   r5   rK   ’   s
   ÿÿzCudaDependency._cuda_pathsc                 C  s   t j ¡ }dd„ |D ƒS )Nc                 S  s$   g | ]}|  d ¡rtj| df‘qS )Z
CUDA_PATH_F)Ú
startswithr&   re   rf   r4   r4   r5   rC   ˜   s   $ z2CudaDependency._cuda_paths_win.<locals>.<listcomp>)r&   re   r9   )r1   rj   r4   r4   r5   rl   –   s   
zCudaDependency._cuda_paths_winc                 C  s"   | j rdnd}dd„ t |¡D ƒS )Nz/usr/local/cuda-*z/usr/local/cuda*c                 S  s    g | ]}|t j |¡d kf‘qS )r   )r&   r'   Úbasename)r@   r'   r4   r4   r5   rC      rD   z2CudaDependency._cuda_paths_nix.<locals>.<listcomp>)rF   ÚglobÚiglob)r1   Úpatternr4   r4   r5   rm   š   s   zCudaDependency._cuda_paths_nixz^CUDA Version\s+(.*)$z^v(.*)$z^cuda-(.*)$z##define\s+CUDART_VERSION\s+([0-9]+)r'   c              
   C  s¸   |   |¡}|r	|S |  |¡}|r|S t d¡ |  ¡ r| jn| j}z| tj	 
|¡¡}|r3| d¡W S t d|› ¡ W dS  ty[ } zt d|› d|›¡ W Y d }~dS d }~ww )Nz,Falling back to extracting version from pathr   z*Could not detect CUDA Toolkit version for ú: z0.0)Ú_read_toolkit_version_txtÚ_read_cuda_runtime_api_versionr   r.   rM   Úpath_version_win_regexÚpath_version_nix_regexÚmatchr&   r'   ro   Úgrouprb   Ú	Exception)r1   r'   r%   Zpath_version_regexÚmÚer4   r4   r5   r?   ¤   s&   


ý €ýz$CudaDependency._cuda_toolkit_versionÚpath_strc           	   	   C  sˆ   t |ƒ}| d¡D ]8}|jdd}| j |¡}|sq	z	t| d¡ƒ}W n	 ty,   Y q	w |d }||d  d }|› d|›   S d S )Nzcuda_runtime_api.húutf-8©Úencodingr   iè  é
   r   )r   ÚrglobÚ	read_textÚcudart_version_regexÚsearchÚintry   Ú
ValueError)	r1   r}   r'   ÚiÚrawr{   Zvers_intÚmajorÚminorr4   r4   r5   ru   ¹   s   ÿz-CudaDependency._read_cuda_runtime_api_versionc              
   C  sº   t j |d¡}z7t|dd&}| ¡ }| j |¡}|r+|  | d¡¡W  d   ƒ W S W d   ƒ W d S 1 s7w   Y  W d S  t	y\ } zt
 d|› d|›¡ W Y d }~d S d }~ww )Nzversion.txtr~   r   r   z+Could not read CUDA Toolkit's version file rs   )r&   r'   r*   ÚopenÚreadlineÚtoolkit_version_regexrx   rH   ry   rz   r   r.   )r1   r'   Zversion_file_pathZversion_fileÚversion_strr{   r|   r4   r4   r5   rt   Ê   s$   üýøý €ýz(CudaDependency._read_toolkit_version_txtr%   c                 C  s   d  | d¡d d… ¡S )Nr   r   )r*   Úsplit)r:   r%   r4   r4   r5   rH   Ø   s   z#CudaDependency._strip_patch_versionc                 C  sÌ   t | jjjjƒ}| jj| j }d}| ¡ r/dddœ}||vr&t| 	|d¡ƒ‚t
j d|| ¡S | ¡ rJdddddœ}||vrFt| 	|d	¡ƒ‚|| S | ¡ rbd
di}||vr^t| 	|d¡ƒ‚|| S tdƒ‚)NzC{} architecture is not supported in {} version of the CUDA Toolkit.ZWin32Úx64)Úx86Úx86_64ÚWindowsÚlibZlib64)r“   Úppc64Úaarch64Zloongarch64ÚLinuxr“   ÚmacOSz#CUDA Toolkit: unsupported platform.)r   Úenvr   r   ÚhostÚmachinesÚfor_machineÚ
is_windowsr	   r`   r&   r'   r*   Zis_linuxZ	is_darwin)r1   ÚarchÚmachinerR   Úlibdirsr4   r4   r5   r,   Ü   s&   
z"CudaDependency._detect_arch_libdirÚboolc                 C  st   d}| j D ]2}| j || j| jr| jgng ¡}|d u r'|  d|› d¡ d}qt d|› d|› ¡ || j|< q|S )NTz%Couldn't find requested CUDA module 'ú'FzLink args for CUDA module 'z' are )	r"   Úclib_compilerZfind_libraryrš   r-   rJ   r   r.   r    )r1   Z	all_foundÚmoduleÚargsr4   r4   r5   r0   ò   s   
 z(CudaDependency._find_requested_librariesc                 C  s   | j j| j  ¡ S ©N)rš   rœ   r   rž   rB   r4   r4   r5   rM      s   zCudaDependency._is_windowsrR   c                 C  ó   d S r§   r4   )r1   rR   r4   r4   r5   rJ     ó   z'CudaDependency._report_dependency_errorÚret_valc                 C  r¨   r§   r4   ©r1   rR   rª   r4   r4   r5   rJ     r©   NúT.Optional[TV_ResultTuple]c                 C  s   | j rt|ƒ‚t |¡ |S r§   )Úrequiredr	   r   r.   r«   r4   r4   r5   rJ   	  s   
c                 C  s   d  | j¡}d| S )Nri   z	modules: )r*   r"   )r1   Z
module_strr4   r4   r5   Úlog_details  s   zCudaDependency.log_detailsc                 C  s   | j r| j S dS )NÚ )r$   rB   r4   r4   r5   Úlog_info  s   zCudaDependency.log_infoc                 C  s,   t  |d¡}|D ]}t|tƒstdƒ‚q|S )NÚmodulesz%CUDA module argument is not a string.)r   Zextract_as_listÚ
isinstancer7   r	   )r1   r   Ú
candidatesr   r4   r4   r5   r!     s   
ÿzCudaDependency.get_requestedFr   r‰   c                 C  s:   g }| j r|| j | j ¡7 }| jD ]	}|| j| 7 }q|S r§   )r-   r¤   Zget_linker_search_argsr"   r    )r1   r   r‰   r¦   r•   r4   r4   r5   Úget_link_args  s   
zCudaDependency.get_link_args)r   r   r   r   r   r   )r   r6   r   r7   )r   r<   )rS   rU   rG   rV   rN   rW   r   r<   )r   rW   )r   rk   )r'   r7   r   r7   )r}   r7   r   rW   )r'   r7   r   rW   )r%   r7   r   r7   )r   r7   )r   r¢   )rR   r7   r   r   )rR   r7   rª   r<   r   r<   r§   )rR   r7   rª   r¬   r   r¬   )r   r   r   rV   )NF)r   rW   r‰   r¢   r   rV   )"Ú__name__Ú
__module__Ú__qualname__r   r   Úclassmethodr   r#   rL   rE   rK   rl   rm   ÚreÚcompilerŽ   rv   rw   r„   r?   ru   rt   rH   r,   r0   rM   ÚTÚoverloadrJ   r®   r°   r!   r´   Ú__classcell__r4   r4   r2   r5   r   "   s@    "

!
















r   )Ú
__future__r   rp   r¹   r&   Útypingr»   Úpathlibr   r¯   r   r   r   r   Úbaser	   r
   ÚTYPE_CHECKINGr   r   r   ÚTupleÚOptionalr7   r¢   r<   r   r4   r4   r4   r5   Ú<module>   s   