o
    uJAfE                     @  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m	Z	m
Z
mZ ddlmZ ddlmZmZmZ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mZmZmZmZmZm Z  ddl!m"Z" ej#rwddlm$Z$ ddlm%Z% ddl&m'Z' g dZ(G dd dej)Z*G dd de"Z+dS )    )annotationsN   )	compilersenvironmentmesonliboptinterpreter)coredata)
ExecutableJarSharedLibrarySharedModuleStaticLibrary)detect_compiler_for)InvalidArguments)MachineChoice	OptionKey)BaseNodeArithmeticNode	ArrayNodeElementaryNodeIdNodeFunctionNodeBaseStringNode   )AstInterpreter)BuildTarget)	TYPE_nvar)
AstVisitor)
executablejarlibraryshared_libraryshared_modulestatic_libraryboth_librariesc                      s(   e Zd Zd fddZdd	d
Z  ZS )IntrospectionHelper
cross_filestrc                   s    t    || _d | _i | _d S N)super__init__r&   native_filecmd_line_options)selfr&   	__class__ A/usr/lib/python3.10/site-packages/mesonbuild/ast/introspection.pyr*   /   s   

zIntrospectionHelper.__init__otherobjectreturnboolc                 C  s   t S r(   )NotImplemented)r-   r2   r0   r0   r1   __eq__5   s   zIntrospectionHelper.__eq__)r&   r'   )r2   r3   r4   r5   )__name__
__module____qualname__r*   r7   __classcell__r0   r0   r.   r1   r%   -   s    r%   c                      s   e Zd Z					dHdI fddZdJddZdKddZdJdd ZdLd&d'ZdJd(d)ZdMd.d/Z	dNd0d1Z
dNd2d3ZdNd4d5ZdNd6d7ZdNd8d9ZdNd:d;ZdNd<d=ZdNd>d?ZdNd@dAZdOdBdCZdPdDdEZdQdFdGZ  ZS )RIntrospectionInterpreterN subprojectssource_rootr'   subdirbackendvisitorsT.Optional[T.List[AstVisitor]]r&   T.Optional[str]
subprojectsubproject_direnv#T.Optional[environment.Environment]c	           
        s   |d ur|ng }t  j||||d t|}	|| _|d u r&t|d |	| _n|| _|| _| j | _|| _	t
d| j	i| _i | _g | _g | _d | _| j| j| j| j| j| j| j| j| j| j| jd
 d S )N)rB   rA   )
add_languages
dependencyr   r   r    projectr!   r"   r#   r$   )r)   r*   r%   r&   r   EnvironmentrF   get_coredatar   rA   r   default_optionsproject_datatargetsdependenciesproject_nodefuncsupdatefunc_add_languagesfunc_dependencyfunc_executablefunc_jarfunc_libraryfunc_projectfunc_shared_libfunc_shared_modulefunc_static_libfunc_both_lib)
r-   r?   r@   rA   rB   r&   rE   rF   rG   optionsr.   r0   r1   r*   ;   s6   	z!IntrospectionInterpreter.__init__noder   argsT.List[TYPE_nvar]kwargsT.Dict[str, TYPE_nvar]r4   Nonec                 C  s  | j rtd|| _ t|dk rtd|d }|dd}| |dd  }t|tr/|j}t|ts6d}||d| _	t
j| j| jd}t
j|sVt
j| j| jd	}t
j|rnt| j}|| | j|j | |d
g }	t|	}
t|
| j| _| j| j | j| j| j| j |   sd|v r|d }t|t!rt|jtsJ |j| _"|   sg | j	d< t
j| j| j"}t
j#|rt
$|D ]}t
j#t
j||r| %| q| j&| j' dd | jj( D }| j)| | *|dt+j, | *|dt+j- d S )NzSecond call to project()r   zCNot enough arguments to project(). Needs at least the project name.r   version	undefined)descriptive_namerf   zmeson.optionszmeson_options.txtrN   rF   r>   c                 S  s   i | ]\}}|  r||qS r0   )
is_backend.0kvr0   r0   r1   
<dictcomp>       z9IntrospectionInterpreter.func_project.<locals>.<dictcomp>T).rR   r   lengetflatten_args
isinstancer   valuer'   rO   ospathjoinr?   r@   existsr   OptionInterpreterrE   processr   update_project_optionsr_   r   stringlistifycdatacreate_options_dictproject_default_optionsrN   rT   set_default_optionsr   is_subprojectr   rF   isdirlistdirdo_subprojectinit_backend_optionsrA   itemsset_options_add_languagesr   HOSTBUILD)r-   r`   ra   rc   	proj_nameZ	proj_vers
proj_langsZoptfileoiZdef_optsZ_project_default_options	spdirnameZsubprojects_dirir_   r0   r0   r1   rZ   c   sV   






z%IntrospectionInterpreter.func_projectdirnamec              
   C  s   t j| j | j}t j||}z(t|d| j| j|| j| j| j	d}|
  ||jd< | jd  |jg7  < W d S  tjtfyG   Y d S w )Nr=   )r&   rE   rF   rG   rB   namer>   )ru   rv   rw   r   get_source_dirrF   r<   rA   r&   rB   analyzerO   r   MesonExceptionRuntimeError)r-   r   Zsubproject_dir_absZsubprsubir0   r0   r1   r      s   "
z&IntrospectionInterpreter.do_subprojectc                 C  s   |  |}|dd}t|tjr| }d|v r1|dd}| |||r+tj d S tj	 d S tjtj	fD ]	}| ||| q7d S )NrequiredTnativeF)
flatten_kwargsrq   rs   r}   UserFeatureOption
is_enabledr   r   r   r   )r-   r`   ra   rc   r   r   for_machiner0   r0   r1   rU      s   
$z+IntrospectionInterpreter.func_add_languages	raw_langsr   r5   r   r   c              	   C  s   g }|  |D ]}t|tr|| qt|tr||j qt|tjdD ]P}|	 }|| j
j| vrwz
t| j||d}W n tjyL   |rJ Y q'w | jrwi }| D ]}	t| j
j|	 }
|	j| jd}	|
||	< qV| j
|||| j q'd S )N)keyTrE   )rr   rs   r'   appendr   rt   sortedr   
sort_clinklowerr   r   r   r   r   rE   get_optionscopyr_   evolveadd_compiler_options)r-   r   r   r   langsllangcompr_   rl   rm   r0   r0   r1   r      s4   


z'IntrospectionInterpreter._add_languagesc              	   C  s   |  |}| |}|sd S |d }d|v }|dd}|dg }t|ts*|g}t|tr2|j}t|ts9d}|  j|||||j	dk|dg7  _d S )Nr   fallbackr   Trf   F)r   r   rf   has_fallbackconditionalr`   )
rr   r   rq   rs   listr   rt   r5   rQ   condition_level)r-   r`   ra   rc   r   r   r   rf   r0   r0   r1   rV      s,   




z(IntrospectionInterpreter.func_dependency
kwargs_rawtargetclassT.Type[BuildTarget]T.Optional[T.Dict[str, T.Any]]c                   st    |}|rt|d tsd S |d }|g}g }d|v r&|t|d 7 }d|v r3|t|d 7 } |d}d fdd	}	|	|}
|	|}fd
d| D }dd | D }dd | D }tj}g }g }d|d< | j	 j
||g | j jj| |
}|  | | | tjtj j j	tj j	|j| | |
|||d}  j|g7  _|S )Nr   sourcesextra_filesTinqueueT.List[BaseNode]r4   c                   s  g }| r|  d}d }t|tsJ t|tr|j}n>t|tr$|j}n5t|trLt|jts1J |j}| j	v rK j	| }t|tttfrK| |g7 } nt|t
rY| |j|jg7 } |d u r^q|j }t|trs|jjtv rs| d dd |D }| dd |D 7 } |r||g7 }| s|S )Nr   c                 S  s   g | ]}t |ttfr|qS r0   )rs   r'   r   rk   xr0   r0   r1   
<listcomp>
  ro   zQIntrospectionInterpreter.build_target.<locals>.traverse_nodes.<locals>.<listcomp>c                 S  s"   g | ]}t |ttttfr|qS r0   )rs   r   r   r   r   r   r0   r0   r1   r     s   " )poprs   r   r   ra   r   r   rt   r'   assignmentsr   leftright	argumentsr   	func_nameBUILD_TARGET_FUNCTIONS)r   rescurrZarg_nodevar_nameZtmp_nodeZ	arg_nodesZelementary_nodesr-   r0   r1   traverse_nodes   s<   










z=IntrospectionInterpreter.build_target.<locals>.traverse_nodesc                   s(   i | ]\}}| j v r|d v r||qS )>   build_alwaysbuild_by_defaultinstall)known_kwargsrj   )r   r0   r1   rn     s   ( z9IntrospectionInterpreter.build_target.<locals>.<dictcomp>c                 S  s&   i | ]\}}|t |tr|jn|qS r0   )rs   r   rt   rj   r0   r0   r1   rn     s   & c                 S  s    i | ]\}}t |ts||qS r0   )rs   r   rj   r0   r0   r1   rn     s     _allow_no_sources)r   idtype
defined_inr@   r   	installedoutputsr   r   rc   r`   )r   r   r4   r   )rr   rs   r'   r   listifyr   r   r   r   r@   rE   r   r   r   process_compilers_lateget_basenameget_idget_typenameru   rv   normpathrw   r?   build_filenamer   should_installget_outputsrP   )r-   r`   ra   r   r   r   ZsrcqueueZextra_queuerc   r   Zsource_nodesZextraf_nodesZkwargs_reducedr   objectsZempty_sourcestarget
new_targetr0   )r-   r   r1   build_target   sP   
 z%IntrospectionInterpreter.build_targetc                 C  s\   | j td}|dkr| |||tS |dkr | |||tS |dkr,| |||tS d S )Ndefault_librarysharedstaticboth)r   
get_optionr   r   r   r   )r-   r`   ra   rc   r   r0   r0   r1   build_library2  s   z&IntrospectionInterpreter.build_libraryc                 C     |  |||tS r(   )r   r	   r-   r`   ra   rc   r0   r0   r1   rW   <     z(IntrospectionInterpreter.func_executablec                 C  r   r(   )r   r   r   r0   r0   r1   r]   ?  r   z(IntrospectionInterpreter.func_static_libc                 C  r   r(   r   r   r   r0   r0   r1   r[   B  r   z(IntrospectionInterpreter.func_shared_libc                 C  r   r(   r   r   r0   r0   r1   r^   E  r   z&IntrospectionInterpreter.func_both_libc                 C  r   r(   )r   r   r   r0   r0   r1   r\   H  r   z+IntrospectionInterpreter.func_shared_modulec                 C  s   |  |||S r(   )r   r   r0   r0   r1   rY   K  s   z%IntrospectionInterpreter.func_libraryc                 C  r   r(   )r   r
   r   r0   r0   r1   rX   N  r   z!IntrospectionInterpreter.func_jarc                 C  s   d|vrd S | d}t|tr|j}|dkr| |||tS |dkr+| |||tS |dkr7| |||tS |dkrC| |||tS |dkrN| |||S |dkrZ| |||t	S d S )Ntarget_typer   r!   r#   r$   r    r   )
r   rs   r   rt   r   r	   r   r   r   r
   )r-   r`   ra   rc   r   r0   r0   r1   func_build_targetQ  s$   

z*IntrospectionInterpreter.func_build_targetc                 C  s
   | j dkS )Nr=   r   r   r0   r0   r1   r   e  s   
z&IntrospectionInterpreter.is_subprojectc                 C  s$   |    |   |   |   d S r(   )load_root_meson_filesanity_check_astparse_projectrunr   r0   r0   r1   r   h  s   z IntrospectionInterpreter.analyzec                 C  sp   | j jsdS | j jd }t|tsdS |jj D ]\}}t|ts&J d|jdkr5t|t	r5|j  S qdS )zFast path to extract subproject_dir kwarg.
           This is faster than self.parse_project() which also initialize options
           and also calls parse_project() on every subproject.
        Nr   zfor mypyrF   )
astlinesrs   r   ra   rc   r   r   rt   r   )r-   rK   kwvalr0   r0   r1   extract_subproject_dirn  s   



z/IntrospectionInterpreter.extract_subproject_dir)NNr=   r>   N)r?   r'   r@   r'   rA   r'   rB   rC   r&   rD   rE   r'   rF   r'   rG   rH   )r`   r   ra   rb   rc   rd   r4   re   )r   r'   r4   re   )r   rb   r   r5   r   r   r4   re   )
r`   r   ra   rb   r   rd   r   r   r4   r   )r`   r   ra   rb   rc   rd   r4   r   )r4   r5   )r4   re   )r4   rD   )r8   r9   r:   r*   rZ   r   rU   r   rV   r   r   rW   r]   r[   r^   r\   rY   rX   r   r   r   r   r;   r0   r0   r.   r1   r<   8   s2    
(
2




S










r<   ),
__future__r   argparser   ru   typingTr=   r   r   r   r   r   r}   buildr	   r
   r   r   r   r   interpreterbaser   r   r   mparserr   r   r   r   r   r   r   interpreterr   TYPE_CHECKINGr   r   visitorr   r   	Namespacer%   r<   r0   r0   r0   r1   <module>   s(   $