o
    e%                     @  s   U d dl mZ d dl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mZmZmZ ddlmZmZmZ ejrfddl
mZ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! g g ddgddgddgddgddgdZ"de#d< G dd deZ$G dd de$Z%dS )    )annotationsN   )coredata)EnvironmentExceptionMesonExceptionPopen_safe_logged	OptionKey   )Compilerrust_buildtype_argsclike_debug_args)MutableKeyedOptionDictTypeKeyedOptionDictType)MachineInfo)Environment)DynamicLinker)MachineChoice)ExternalProgram)
Dependency-Czopt-level=0zopt-level=1zopt-level=2zopt-level=3zopt-level=s)plain0g123sT.Dict[str, T.List[str]]rust_optimization_argsc                      s`  e Zd ZU dZdZddgg g ddgdZded< g g d	d
gd	dgd	dgdZded< 			dgdh fd!d"Zdid$d%Z	djd*d+Z
dkd.d/Zdld1d2Zdmd3d4Zdnd6d7Zdod9d:Zdpd=d>Zdqd@dAZedrdBdCZdsdEdFZdtdIdJZdudMdNZdvdPdQZdvdRdSZdwdUdVZdx fdWdXZdxdYdZZdyd\d]Zdxd^d_Zdxd`daZdxdbdcZdzdedfZ   Z!S ){RustCompilerZrustZrustcz-Awarningsz-W)r   r   r   r   r   _WARNING_LEVELSz-lzdylib=msvcrtdzdylib=libcmtzdylib=libcmtd)noneZmdZmddmtZmtdzT.Mapping[str, T.List[str]]MSVCRT_ARGSNexelistT.List[str]versionstrfor_machiner   is_crossboolinfo'MachineInfo'exe_wrapperT.Optional['ExternalProgram']full_versionT.Optional[str]linkerT.Optional['DynamicLinker']c	           	   
     s^   t  jg |||||||d || _| jdd dD  d| jjv r*| jtd g | _	d S )N)r*   r0   r2   c                 S  s   h | ]}t |qS  )r   ).0or4   r4   >/usr/lib/python3.10/site-packages/mesonbuild/compilers/rust.py	<setcomp>N   s    z(RustCompiler.__init__.<locals>.<setcomp>)Z
b_coloroutZb_ndebuglinkZb_vscrt)
super__init__r.   Zbase_optionsupdater2   idaddr   native_static_libs)	selfr%   r'   r)   r*   r,   r.   r0   r2   	__class__r4   r7   r;   E   s   
zRustCompiler.__init__returnc                 C  s   dS )NFr4   r@   r4   r4   r7   needs_static_linkerS   s   z RustCompiler.needs_static_linkerwork_direnvironment'Environment'Nonec                   sv  t j|d}t j|d}t|ddd}|td W d    n1 s(w   Y  | jd||g }t||d\}}}	|j	d	krMt
d
|   d| jr`| jd u rWd S | j |g }n|g}tj|tjtjd}
|
  |
j	d	krt
d|   d| jdddd|g }t||d\}}}	|j	d	krtd|	tj}|rh d  fdd|d D | _d S d S d S )Nz	sanity.rsZrusttestwzutf-8)encodingz.fn main() {
                }
                -o)cwdr   zRust compiler z cannot compile programs.)stdoutstderrz%Executables created by Rust compiler z are not runnable.z--crate-typeZ	staticlib--printznative-static-libsznative-static-libs: (.*)$>   z
-ladvapi32z-lcz
-lkernel32z-lgcc_sc                   s   g | ]}| vr|qS r4   r4   )r5   iexcluder4   r7   
<listcomp>y   s    z-RustCompiler.sanity_check.<locals>.<listcomp>r	   )ospathjoinopenwritetextwrapdedentr%   r   
returncoder   Zname_stringr*   r.   Zget_command
subprocessPopenDEVNULLwaitresearch	MULTILINEgroupsplitr?   )r@   rF   rG   Zsource_nameZoutput_nameZofileZcmdlistZpcstdostdepepmatchr4   rR   r7   sanity_checkV   s:   



"zRustCompiler.sanity_check	outtargetoutfilec                 C  s   d|gS )Nz
--dep-infor4   )r@   rl   rm   r4   r4   r7   get_dependency_gen_args{      z$RustCompiler.get_dependency_gen_args	buildtypec                 C     t | S N)r   )r@   rp   r4   r4   r7   get_buildtype_args~   ro   zRustCompiler.get_buildtype_argsc                 C  s4   | j ddddg }t|\}}}|jdddd S )	NF)ZccacherP   Zsysroot
r	   )maxsplitr   )Zget_exelistr   re   )r@   cmdri   rf   rg   r4   r4   r7   get_sysroot   s   zRustCompiler.get_sysrootis_debugc                 C  rq   rr   )r   )r@   rx   r4   r4   r7   get_debug_args   ro   zRustCompiler.get_debug_argsoptimization_levelc                 C  rq   rr   )r   )r@   rz   r4   r4   r7   get_optimization_args   ro   z"RustCompiler.get_optimization_argsparameter_list	build_dirc                 C  s   t |D ]<\}}|d d dkr@dD ]-}t|d }|d | d| dkr?|d | tjtj|||d   ||<  nqq|S )Nr   z-L)Z
dependencyZcrateZnative	frameworkall   =)	enumeratelenrU   rV   normpathrW   )r@   r|   r}   idxrQ   jZcombined_lenr4   r4   r7   &compute_parameters_with_absolute_paths   s   .z3RustCompiler.compute_parameters_with_absolute_paths
outputnamec                 C  s   d|gS )NrL   r4   )r@   r   r4   r4   r7   get_output_args   ro   zRustCompiler.get_output_argsc                 C  s   dd| gS )Nr   zlinker=r4   )clsr2   r'   r4   r4   r7   use_linker_args   s   zRustCompiler.use_linker_args'MutableKeyedOptionDictType'c                 C  s(   t d| j| jd}|tdg ddiS )NstdmachinelangzRust edition to use)r"   Z2015Z2018Z2021r"   )r   r)   languager   ZUserComboOption)r@   keyr4   r4   r7   get_options   s   zRustCompiler.get_optionsdep'Dependency'c                 C     g S rr   r4   )r@   r   r4   r4   r7   get_dependency_compile_args   s   z(RustCompiler.get_dependency_compile_argsoptions'KeyedOptionDictType'c                 C  s<   g }t d| j| jd}|| }|jdkr|d|j  |S )Nr   r   r"   z
--edition=)r   r)   r   valueappend)r@   r   argsr   r   r4   r4   r7   get_option_compile_args   s   
z$RustCompiler.get_option_compile_argscrt_valc                 C  r   rr   r4   r@   r   rp   r4   r4   r7   get_crt_compile_args      z!RustCompiler.get_crt_compile_argsc                 C  s"   | j jdvrg S | j| || S )N>   r9   zlld-link)r2   r=   r$   Zget_crt_valr   r4   r4   r7   get_crt_link_args   s   zRustCompiler.get_crt_link_args	colortypec                 C  s"   |dv r
d| gS t d| )N>   alwaysneverautoz--color=zInvalid color type for rust )r   )r@   r   r4   r4   r7   get_colorout_args   s   zRustCompiler.get_colorout_argsc                   s,   g }t   D ]}|dd| g q|S )Nr   z	link-arg=)r:   get_linker_always_argsextend)r@   r   arA   r4   r7   r      s   z#RustCompiler.get_linker_always_argsc                 C  s   ddgS )Nz-Dr    r4   rD   r4   r4   r7   get_werror_args   s   zRustCompiler.get_werror_argslevelc                 C  s
   | j | S rr   r!   )r@   r   r4   r4   r7   get_warn_args   s   
zRustCompiler.get_warn_argsc                 C  s
   | j d S )Nr   r   rD   r4   r4   r7   get_no_warn_args   s   
zRustCompiler.get_no_warn_argsc                 C  r   rr   r4   rD   r4   r4   r7   get_pic_args   r   zRustCompiler.get_pic_argsc                 C  r   rr   r4   rD   r4   r4   r7   get_pie_args   s   zRustCompiler.get_pie_argsdisablec                 C  s   |rdnd}dd| ddgS )Nnoyesr   zdebug-assertions=zoverflow-checks=nor4   )r@   r   actionr4   r4   r7   get_assert_args   s   zRustCompiler.get_assert_args)NNN)r%   r&   r'   r(   r)   r   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   )rC   r+   )rF   r(   rG   rH   rC   rI   )rl   r(   rm   r(   rC   r&   )rp   r(   rC   r&   )rC   r(   )rx   r+   rC   r&   )rz   r(   rC   r&   )r|   r&   r}   r(   rC   r&   )r   r(   rC   r&   )r2   r(   r'   r(   rC   r&   )rC   r   )r   r   rC   r&   )r   r   rC   r&   )r   r(   rp   r(   rC   r&   )r   r(   rC   r&   )rC   r&   )r   r(   rC   r&   )r   r+   rC   r&   )"__name__
__module____qualname__r   r=   r!   __annotations__r$   r;   rE   rk   rn   rs   rw   ry   r{   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r4   r4   rA   r7   r   -   sT   
 



%

















r   c                   @  s   e Zd ZdZdZdS )ClippyRustCompilerzSClippy is a linter that wraps Rustc.

    This just provides us a different id
    zclippy-driver rustcN)r   r   r   __doc__r=   r4   r4   r4   r7   r      s    r   )&
__future__r   r]   os.pathrU   rZ   ra   typingT r   Zmesonlibr   r   r   r   	compilersr
   r   r   TYPE_CHECKINGr   r   Z	envconfigr   rG   r   Zlinkers.linkersr   r   Zprogramsr   dependenciesr   r   r   r   r   r4   r4   r4   r7   <module>   s6   
 =