o
    Qfy^                     @  s   d dl m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mZ ddlmZmZ ddlmZ d d	lmZ d d
lZd d
lZd d
lZd d
lZejrid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 dd deZ#d
S )    )annotations)Path   )ExternalDependencyDependencyExceptionsort_libpathsDependencyTypeName   )	OptionKey
OrderedSet
PerMachine
Popen_safe)find_external_programExternalProgram)mlog)PurePathN)Environment)MachineChoice)EnvironOrDict)ImmutableListProtocolEnvironmentVariablesc                      s,  e Zd ZU eddZded< i Zded< d\d] fddZd^ddZe	d_ddZ
d`d!d"Ze	#dadbd&d'Ze	#dadcd)d*Zd\ddd,d-Zded.d/Zdfd1d2Zdgd3d4Zdhd8d9Zdgd:d;Zdid@dAZedjdDdEZdkdHdIZdldJdKZdldLdMZdldNdOZed^dPdQZdddddddRdmdZd[Z  ZS )nPkgConfigDependencyNz0PerMachine[T.Union[None, bool, ExternalProgram]]class_pkgbinzkT.Dict[T.Tuple[ExternalProgram, T.Tuple[str, ...], T.FrozenSet[T.Tuple[str, str]]], T.Tuple[int, str, str]]pkgbin_cachenamestrenvironment'Environment'kwargsT.Dict[str, T.Any]languageT.Optional[str]returnNonec           	   
     s8  t  jtd|||d || _d| _| | j| j| j| _	| j	du r:d | _	d| j d}| j
r3t|t| d S t| j	tsBJ td|| j	  | d|g\}| _}|dkr_d S d	| _z|   |   W d S  ty } z"td
| d|  | j
r g | _g | _d| _|| _W Y d }~d S d }~ww )N	pkgconfig)r!   FzPkg-config binary for machine z not found. Giving up.z;Determining dependency {!r} with pkg-config executable {!r}z--modversionr   Tzpkg-config error with 'z': )super__init__r   r   
is_libtool_detect_pkgbinsilentenvfor_machinepkgbinrequiredr   r   debug
isinstancer   formatget_path_call_pkgbinversionis_found
_set_cargs	_set_libscompile_args	link_argsreason)	selfr   r   r   r!   msgret_e	__class__ home/ych/rk3568/buildroot/output/rockchip_rk3568_recovery/host/lib/python3.10/site-packages/mesonbuild/dependencies/pkgconfig.pyr'   -   s@   

zPkgConfigDependency.__init__c                 C  s   d}| | jj| j| j| jS )Nz<{0} {1}: {2} {3}>)r1   rA   __name__r   r5   Zversion_reqs)r;   srB   rB   rC   __repr__U   s   zPkgConfigDependency.__repr__r*   boolr+   r   r,   r   $T.Union[None, bool, ExternalProgram]c                 C  s   | j | du rtd| d nd| j | d ur"td| d nS| j | d u s-J dtd| d t||dd|jdd	D ]#}| ||}|sLqA|s^td
t| d| d || j |<  n|sptdt	d d| j |< | j | S )NFzPkg-config binary for z is cached as not found.z is cached.zfor mypyz is not cached.r%   z
Pkg-config)Zallow_default_for_crosszFound pkg-config:()zFound Pkg-config:ZNO)
r   r   r/   r   Zdefault_pkgconfigcheck_pkgconfiglogZboldr2   Zred)clsr*   r+   r,   Zpotential_pkgbinZversion_if_okrB   rB   rC   r)   Z   s.   




z"PkgConfigDependency._detect_pkgbinargsT.List[str]T.Dict[str, str]T.Tuple[int, str, str]c           	      C  s   t | jtsJ | j | }t||d\}}}|j| | }}}d|}t	d| d|  |r@t	d| d |rKt	d| d |||fS )Nr+    zCalled `z` -> zstdout:
z
-----------zstderr:
)
r0   r-   r   get_commandr   
returncodestripjoinr   r/   )	r;   rN   r+   cmdpouterrrccallrB   rB   rC   _call_pkgbin_realz   s   

z%PkgConfigDependency._call_pkgbin_realFuninstalled'EnvironmentVariables'c           
      C  s   ddl m} | }td|d}| jj| jd d  }|r.t|  d }||vr.|	| |
d| | j|  }|rD|
d|g | j|  }	|	rS|
d|	 |S )	Nr	   r   pkg_config_pathmachinezmeson-uninstalledZPKG_CONFIG_PATHZPKG_CONFIG_SYSROOT_DIRZPKG_CONFIG_LIBDIR)buildr   r
   coredataoptionsvaluer   get_build_diras_posixappendset
propertiesZget_sys_rootZget_pkg_config_libdir)
r   r,   r_   r   r+   keyZextra_pathsZuninstalled_pathZsysrootZpkg_config_libdir_proprB   rB   rC   get_env   s    
zPkgConfigDependency.get_envr   c                 C  sN   t |||}|| } |  D ]\}}|dr$td| d|  q| S )NZPKG_zenv[z]: )r   rn   items
startswithr   r/   )r+   r   r,   r_   envvarsrm   rg   rB   rB   rC   	setup_env   s   

zPkgConfigDependency.setup_envT.Optional[EnvironOrDict]c                 C  s~   t | jtsJ |ptj}t|| j| j}t	|
 }t|}tj}| j||f|vr7| |||| j||f< || j||f S N)r0   r-   r   osenvironr   rr   r+   r,   	frozensetro   tupler   r^   )r;   rN   r+   ZfenvZtargscacherB   rB   rC   r3      s   
z PkgConfigDependency._call_pkgbinc              	   C  s   | j jj s	|S g }|D ]m}t }|dr#t|dd j}d}n5|dr4t|dd j}d}n$|drAt|j}d}n|d	sRt|dkrX|d
 dkrX|	dd}t|d
krut|d
 d
kru|
|d
 d|dd }|| q|S )a$  
        Both MSVC and native Python on Windows cannot handle MinGW-esque /c/foo
        paths so convert them to C:/foo. We cannot resolve other paths starting
        with / like /home/foo so leave them as-is so that the user gets an
        error/warning from the compiler/linker.
        z-L/r	   Nz-L{}:/{}z-I/z-I{}:/{}/z{}:/{})-Lz-Ir   :z\ rS   )r+   machinesrd   
is_windowsrx   rp   r   partslenreplacer1   rW   rj   )r;   rN   Z	convertedargZpargstmplrB   rB   rC   _convert_mingw_paths   s(   



"z(PkgConfigDependency._convert_mingw_pathsrX   c                 C  s
   t |S rt   )shlexsplit)r;   rX   rB   rB   rC   _split_args   s   
zPkgConfigDependency._split_argsc                 C  sp   d }| j dkrtj }d|d< | jd| jg|d\}}}|dkr-td| j d| d	| | || _	d S )
NZfortran1ZPKG_CONFIG_ALLOW_SYSTEM_CFLAGSz--cflagsrR   r   zCould not generate cargs for :

)
r!   ru   rv   copyr3   r   r   r   r   r8   )r;   r+   r=   rZ   r[   rB   rB   rC   r6      s   

zPkgConfigDependency._set_cargsrZ   out_raw!T.Tuple[T.List[str], T.List[str]]c                 C  s&  t  }| | |}|D ]'}|dr4|ds4|dd }tj|s/tj| j	 |}|
| q| jjjtd| jd j}| |}t tt||}t  }| | |}	|	D ]}|dreq]|dr{|dd |vr{|
|dd  q]t|t| }
t  }g }g }|	D ]}|drn|drq|dr||v rq|dd }d}|
D ]}tj||}tj|r|} nq|du r||v rqtd	|| j || n|}n|d
r:||v rq| jr| jj|dd | j|
| jdd}nd}|dur|
| |r|d d
s|d }nbq||v r"q| jr4td|dd | j || nC|dr}| |}tjtj||}tj|sctjtj|d|}tj|srtd| dd| _ |}||v r}q|| q|rdd |D | }||fS )aI  
        @out: PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 pkg-config --libs
        @out_raw: pkg-config --libs

        We always look for the file ourselves instead of depending on the
        compiler to find it with -lfoo or foo.lib (if possible) because:
        1. We want to be able to select static or shared
        2. We need the full path of the library to calculate RPATH values
        3. De-dup of libraries is easier when we have absolute paths

        Libraries that are provided by the toolchain or are not found by
        find_library() will be added with -L -l pairs.
        r{   )z-L-lz-L-Lr	   Nra   rb   z-l:   zJLibrary {!r} not found for dependency {!r}, may not be successfully linkedz-lF)Zlib_prefix_warningr   zOStatic library {!r} not found for dependency {!r}, may not be statically linkedz.laz.libszGot a libtools specific "zD" dependenciesbut we could not compute the actual sharedlibrary pathTc                 S  s   g | ]}d | qS )r{   rB   ).0lprB   rB   rC   
<listcomp>w  s    z4PkgConfigDependency._search_libs.<locals>.<listcomp>)!r   r   r   rp   ru   pathisabsrW   r+   rh   addre   rf   r
   r,   rg   r   listexistsr   warningr1   r   rj   Zclib_compilerZfind_libraryZlibtypestaticendswithextract_libtool_shlibdirnamer   r(   )r;   rZ   r   Zprefix_libpathsraw_link_argsr   r   ra   Zsystem_libpathsZ	full_argsZlibpathsZ
libs_foundZlibs_notfoundr9   libZlibfilenameZ	foundnamelibdirtargetrN   Zshared_libnameZ
shared_librB   rB   rC   _search_libs   s   












z PkgConfigDependency._search_libsc                 C  s   d }dg}| j s| jj| j dd dkr|d || j tj	 }d|d< | j
||d\}}}|dkrCtd	| j d
| d| 
|\}}}|dkrZtd	| j d| | ||\| _| _d S )Nz--libsZpkg_config_statictruez--staticr   ZPKG_CONFIG_ALLOW_SYSTEM_LIBSrR   r   zCould not generate libs for r   r   z:

)r   r+   rl   r,   getrj   r   ru   rv   r   r3   r   r   r9   r   )r;   r+   Zlibcmdr=   rZ   r[   r   Zerr_rawrB   rB   rC   r7   z  s    

zPkgConfigDependency._set_libsvariable_namedefine_variable'ImmutableListProtocol[str]'defaultc           
      C  s   d| | j g}|rdd| g| }| |\}}}d}|dkr1| jr0td| j  d| dn1| }|sb| d	| j g\}}}	td
| d |tjsb|d urU|}nt	
d| d| j  d t	d| d|  |S )Nz--variable=z--define-variable== r   zdependency z not found:
r   z--print-variables^$zpkgconfig variable 'z' not defined for dependency .zGot pkgconfig variable z : )r   rW   r3   r.   r   rV   research	MULTILINEr   r   r/   )
r;   r   r   r   rf   r=   rZ   r[   variabler>   rB   rB   rC   get_pkgconfig_variable  s$   z*PkgConfigDependency.get_pkgconfig_variabler-   r   c                 C  s  |  std|j d S d| }z@t| dg d }d|v r2td|d W d S t| dg d	d
 \}}|jd	krRtd|d W d S W | S  t	yi   td|d Y d S  t
y   d|d}| jj s|d7 }t| Y d S w )Nz Did not find pkg-config by name rS   z--helpr   z	Pure-Perlzfound pkg-config z7 but it is Strawberry Perl and thus broken. Ignoring...z	--versionr   r	   zFound pkg-config z but it failed when runzWe thought we found pkg-config z! but now it's not there. How odd!z' but didn't have permissions to run it.zO

On Unix-like systems this is often caused by scripts that are not executable.)foundr   rL   r   rW   rT   r   rU   r   FileNotFoundErrorPermissionErrorr}   rd   r~   rV   )r+   r-   Zcommand_as_stringZhelptextrY   rZ   r<   rB   rB   rC   rK     s2   

z#PkgConfigDependency.check_pkgconfigla_file	fieldnamec                 C  sx   t |dd,}|D ] }| d}|d |kr)|d dd   W  d    S q	W d    d S 1 s5w   Y  d S )Nzutf-8)encodingr   r   r   )openrV   r   )r;   r   r   flineZarrrB   rB   rC   extract_field  s   
z!PkgConfigDependency.extract_fieldc                 C     |  |dS )Ndlnamer   r;   r   rB   rB   rC   extract_dlname_field     z(PkgConfigDependency.extract_dlname_fieldc                 C  r   )Nr   r   r   rB   rB   rC   extract_libdir_field  r   z(PkgConfigDependency.extract_libdir_fieldc                 C  sd   |  |}|du rdS | jj| j  r,tj|}| |}|du r%|S tj	||S tj|S )z_
        Returns the path to the shared library
        corresponding to this .la file
        N)
r   r+   r}   r,   Z	is_darwinru   r   basenamer   rW   )r;   r   r   Z
dlbasenamer   rB   rB   rC   r     s   

z)PkgConfigDependency.extract_libtool_shlibc                   C  s   dS )Nr%   rB   rB   rB   rB   rC   	log_tried  s   zPkgConfigDependency.log_tried)cmaker%   
configtoolinternaldefault_valuepkgconfig_definer   r%   r   r   r   r   T.Optional[T.List[str]]c                C  sF   |rz
|  ||p	g |W S  ty   Y nw |d ur|S td| )Nz>Could not get pkg-config variable and no default provided for )r   r   )r;   r   r%   r   r   r   r   rB   rB   rC   get_variable  s   z PkgConfigDependency.get_variablert   )
r   r   r   r   r   r    r!   r"   r#   r$   )r#   r   )r*   rG   r+   r   r,   r   r#   rH   )rN   rO   r+   rP   r#   rQ   )F)r   r   r,   r   r_   rG   r#   r`   )
r+   r   r   r   r,   r   r_   rG   r#   rP   )rN   rO   r+   rs   r#   rQ   )rN   rO   r#   rO   )rX   r   r#   rO   )r#   r$   )rZ   r   r   r   r#   r   )r   r   r   r   r   r"   r#   r   )r+   r   r-   r   r#   r"   )r   r   r   r   r#   r"   )r   r   r#   r"   )r   r"   r%   r"   r   r"   r   r"   r   r"   r   r   r#   r   )rD   
__module____qualname__r   r   __annotations__r   r'   rF   classmethodr)   r^   staticmethodrn   rr   r3   r   r   r6   r   r7   r   rK   r   r   r   r   r   r   __classcell__rB   rB   r@   rC   r   #   sH   
 

(





 





r   )$
__future__r   pathlibr   baser   r   r   r   Zmesonlibr
   r   r   r   Zprogramsr   r   r   r   r   r   ru   r   typingTTYPE_CHECKINGr   r   r   Z
utils.corer   Z_typingr   rd   r   r   rB   rB   rB   rC   <module>   s$   