o
    He*@  ã                	   @  s`  U d Z 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Zddl	Z	ddl
ZddlmZ ddlmZ ddlmZmZ ejrXdd	lmZ dd
lmZ ddlmZ ddlmZ dZded< dZded< dD ]Zze e¡ZW  n eyy   Y qfw e	 d¡Zd\dd„Z d]dd„Z!ej"d^dd„ƒZ#ej"d_d"d„ƒZ#ej"d`d%d„ƒZ#dad(d„Z#ej$G d)d*„ d*ƒƒZ%ej$G d+d,„ d,ƒƒZ&ej$G d-d.„ d.ƒƒZ'ej$G d/d0„ d0e'ƒƒZ(ej$G d1d2„ d2e'ƒƒZ)ej$G d3d4„ d4e'ƒƒZ*ej$G d5d6„ d6e'ƒƒZ+ej$G d7d8„ d8e'ƒƒZ,ej$G d9d:„ d:ƒƒZ-dbdcd@dA„Z.dddCdD„Z/dedFdG„Z0dedHdI„Z1dfdPdQ„Z2dgdRdS„Z3dhdVdW„Z4didZd[„Z5dS )jaA  Interpreter for converting Cargo Toml definitions to Meson AST

There are some notable limits here. We don't even try to convert something with
a build.rs: there's so few limits on what Cargo allows a build.rs (basically
none), and no good way for us to convert them. In that case, an actual meson
port will be required.
é    )ÚannotationsNé   )Úbuilder)Úversioné   )ÚMesonExceptionÚ
Popen_safe)Ú
ModuleType)Úmanifest)Úmparser)ÚEnvironmentzT.Optional[ModuleType]ÚtomllibúT.Optional[str]Ú	toml2json)r   ÚtomliÚfilenameÚstrÚreturnúT.Dict[object, object]c                 C  s   t rt| dƒ}t  |¡}W d   ƒ n1 sw   Y  n td u r%tdƒ‚tt| gƒ\}}}|jdkr8td|ƒ‚t |¡}t	|t
ƒsFtdƒ‚|S )NÚrbz:Could not find an implementation of tomllib, nor toml2jsonr   z"toml2json failed to decode output
z3Cargo.toml isn't a dictionary? How did that happen?)r   ÚopenÚloadr   r   r   Ú
returncodeÚjsonÚloadsÚ
isinstanceÚdict)r   ÚfÚrawÚpÚoutÚerr© r"   úA/usr/lib/python3.10/site-packages/mesonbuild/cargo/interpreter.pyÚ	load_toml4   s   ÿ€



r$   Únamec                 C  s   |   dd¡S )z^Fixup a meson variable name

    :param name: The name to fix
    :return: the fixed name
    ú-Ú_)Úreplace)r%   r"   r"   r#   Úfixup_meson_varnameH   s   r)   Údúmanifest.BuildTargetúmanifest.FixedBuildTargetc                 C  ó   d S ©Nr"   ©r*   r"   r"   r#   Ú_fixup_raw_mappingsR   ó   r0   úmanifest.LibTargetúmanifest.FixedLibTargetc                 C  r-   r.   r"   r/   r"   r"   r#   r0   U   r1   úmanifest.Dependencyúmanifest.FixedDependencyc                 C  r-   r.   r"   r/   r"   r"   r#   r0   X   r1   úFT.Union[manifest.BuildTarget, manifest.LibTarget, manifest.Dependency]úUT.Union[manifest.FixedBuildTarget, manifest.FixedLibTarget, manifest.FixedDependency]c                 C  sN   dd„ |   ¡ D ƒ}d|v r!t|d tƒsJ dƒ‚t |d ¡|d< t d|¡S )a‡  Fixup raw cargo mappings to ones more suitable for python to consume.

    This does the following:
    * replaces any `-` with `_`, cargo likes the former, but python dicts make
      keys with `-` in them awkward to work with
    * Convert Dependndency versions from the cargo format to something meson
      understands

    :param d: The mapping to fix
    :return: the fixed string
    c                 S  ó   i | ]	\}}t |ƒ|“qS r"   ©r)   ©Ú.0ÚkÚvr"   r"   r#   Ú
<dictcomp>i   ó    z'_fixup_raw_mappings.<locals>.<dictcomp>r   zfor mypyr7   )Úitemsr   r   r   ÚconvertÚTÚcast)r*   r   r"   r"   r#   r0   [   s
   c                   @  s~  e Zd ZU dZded< ded< dZded< dZded< eje	d	Z
d
ed< dZded< dZded< dZded< dZded< dZded< dZded< dZded< dZded< eje	d	Zd
ed< eje	d	Zd
ed< dZded< dZded< dZded< eje	d	Zd
ed< eje	d	Zd
ed< dZded< ejed	Zd ed!< dZded"< dZded#< dZded$< dZ ded%< dZ!ded&< dS )'ÚPackagezARepresentation of a Cargo Package entry, with defaults filled in.r   r%   r   Nr   ÚdescriptionÚresolver©Údefault_factoryúT.List[str]ÚauthorsÚ2015úmanifest.EDITIONÚeditionÚrust_versionÚdocumentationÚreadmeÚhomepageÚ
repositoryÚlicenseÚlicense_fileÚkeywordsÚ
categoriesÚ	workspaceÚbuildÚlinksÚexcludeÚincludeTÚboolÚpublishzT.Dict[str, T.Dict[str, str]]ÚmetadataÚdefault_runÚautobinsÚautoexamplesÚ	autotestsÚautobenches)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rE   rF   ÚdataclassesÚfieldÚlistrJ   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r]   r   r^   r_   r`   ra   rb   rc   r"   r"   r"   r#   rD   p   s:   
 rD   c                   @  sœ   e Zd ZU dZded< dZded< dZded< dZded< dZded	< dZ	ded
< dZ
ded< dZded< dZded< ejedZded< eddd„ƒZdS )Ú
Dependencyz+Representation of a Cargo Dependency Entry.rI   r   Nr   ÚregistryÚgitÚbranchÚrevÚpathFr\   ÚoptionalÚpackageÚdefault_featuresrG   Úfeaturesr   úmanifest.DependencyVr   c                 C  s*   t |tƒr| t |¡ƒS | di t|ƒ¤ŽS )z/Create a dependency from a raw cargo dictionaryNr"   )r   r   r   rA   r0   )Úclsr   r"   r"   r#   Úfrom_raw¢   s   
zDependency.from_raw)r   rv   r   rl   )rd   re   rf   rg   rh   rm   rn   ro   rp   rq   rr   rs   rt   ri   rj   rk   ru   Úclassmethodrx   r"   r"   r"   r#   rl   ’   s   
 rl   c                   @  s¢   e Zd ZU ded< ejdd„ dZded< dZd	ed
< dZded< dZ	ded< dZ
ded< dZded< dZded< dZded< ejedZded< dZded< dS )ÚBuildTargetr   r%   c                   C  ó   dgS ©NÚlibr"   r"   r"   r"   r#   Ú<lambda>®   ó    zBuildTarget.<lambda>rG   úT.List[manifest.CRATE_TYPE]Ú
crate_typeNz$dataclasses.InitVar[T.Optional[str]]rq   Tr\   ÚtestFÚdoctestÚbenchÚdocÚharnessrK   rL   rM   rI   Úrequired_featuresÚplugin)rd   re   rf   rh   ri   rj   r   rq   r‚   rƒ   r„   r…   r†   rM   rk   r‡   rˆ   r"   r"   r"   r#   rz   ª   s   
 rz   c                   @  sZ   e Zd ZU dZdZded< dZded< dZded< ej	dd	„ d
Z
ded< dZded< dS )ÚLibraryz(Representation of a Cargo Library Entry.Tr\   rƒ   r…   FÚ
proc_macroc                   C  r{   r|   r"   r"   r"   r"   r#   r~   Ï   r   zLibrary.<lambda>rG   r€   r   Údoc_scrape_examplesN)rd   re   rf   rg   rƒ   rh   r…   rŠ   ri   rj   r   r‹   r"   r"   r"   r#   r‰   Ç   s   
 r‰   c                   @  ó   e Zd ZU dZdZded< dS )ÚBinaryz$Representation of a Cargo Bin Entry.Tr\   r…   N)rd   re   rf   rg   r…   rh   r"   r"   r"   r#   r   Ó   ó   
 r   c                   @  rŒ   )ÚTestz%Representation of a Cargo Test Entry.Tr\   r„   N)rd   re   rf   rg   r„   rh   r"   r"   r"   r#   r   Û   rŽ   r   c                   @  rŒ   )Ú	Benchmarkz*Representation of a Cargo Benchmark Entry.Tr\   r‚   N)rd   re   rf   rg   r‚   rh   r"   r"   r"   r#   r   ã   rŽ   r   c                   @  s*   e Zd ZU dZejdd„ dZded< dS )ÚExamplez(Representation of a Cargo Example Entry.c                   C  r{   )NÚbinr"   r"   r"   r"   r#   r~   ð   r   zExample.<lambda>rG   r€   r   N)rd   re   rf   rg   ri   rj   r   rh   r"   r"   r"   r#   r‘   ë   s   
 r‘   c                   @  s~   e Zd ZU dZded< ded< ded< ded< ded	< d
ed< ded< ded< ded< ded< ded< ded< dZded< dS )ÚManifestai  Cargo Manifest definition.

    Most of these values map up to the Cargo Manifest, but with default values
    if not provided.

    Cargo subprojects can contain what Meson wants to treat as multiple,
    interdependent, subprojects.

    :param subdir: the subdirectory that this cargo project is in
    :param path: the path within the cargo subproject.
    rD   rs   zT.Dict[str, Dependency]ÚdependenciesZdev_dependenciesZbuild_dependenciesr‰   r}   zT.List[Binary]r’   zT.List[Test]r‚   zT.List[Benchmark]r„   zT.List[Example]ÚexamplezT.Dict[str, T.List[str]]ru   z$T.Dict[str, T.Dict[str, Dependency]]Útargetr   ÚsubdirÚ rq   N)rd   re   rf   rg   rh   rq   r"   r"   r"   r#   r“   ó   s   
 r“   r˜   Úraw_manifestúmanifest.Manifestr—   rq   c                 C  s  t |  di ¡ƒ}| d| d d ¡ t ddd„ | d  ¡ D ƒ¡}ttdi |¤Ždd„ |  di ¡ ¡ D ƒd	d„ |  d
i ¡ ¡ D ƒdd„ |  di ¡ ¡ D ƒtdi |¤Ždd„ |  di ¡D ƒdd„ |  di ¡D ƒdd„ |  di ¡D ƒdd„ |  di ¡D ƒ|  di ¡dd„ |  di ¡ ¡ D ƒ||ƒS )Nr}   r%   rs   zmanifest.FixedPackagec                 S  r8   r"   r9   r:   r"   r"   r#   r>     r?   z%_convert_manifest.<locals>.<dictcomp>c                 S  ó   i | ]
\}}|t  |¡“qS r"   ©rl   rx   r:   r"   r"   r#   r>     ó    r”   c                 S  r›   r"   rœ   r:   r"   r"   r#   r>     r   zdev-dependenciesc                 S  r›   r"   rœ   r:   r"   r"   r#   r>      r   zbuild-dependenciesc                 S  ó   g | ]}t d i t|ƒ¤Ž‘qS ©r"   )r   r0   ©r;   Úbr"   r"   r#   Ú
<listcomp>"  ó    z%_convert_manifest.<locals>.<listcomp>r’   c                 S  rž   rŸ   )r   r0   r    r"   r"   r#   r¢   #  r£   r‚   c                 S  rž   rŸ   )r   r0   r    r"   r"   r#   r¢   $  r£   r„   c                 S  rž   rŸ   )r‘   r0   r    r"   r"   r#   r¢   %  r£   r•   ru   c                 S  s,   i | ]\}}|d d„ |  di ¡ ¡ D ƒ“qS )c                 S  r›   r"   rœ   )r;   Zk2Zv2r"   r"   r#   r>   '  r   z0_convert_manifest.<locals>.<dictcomp>.<dictcomp>r”   )Úgetr@   r:   r"   r"   r#   r>   '  s     ÿr–   r"   )	r0   r¤   Ú
setdefaultrB   rC   r@   r“   rD   r‰   )r™   r—   rq   r}   Úpkgr"   r"   r#   Ú_convert_manifest  s,   ÿ
ÿòr§   úT.Dict[str, Manifest]c           	   	     s  t j | d¡}t|ƒ}i }d|v r#t d|¡‰ tˆ | ƒ}|||jj< nt d|¡‰ dˆ v r…t  	¡ }t  
| ¡ ztj dd„ ˆ d d D ƒ¡}W t  
|¡ nt  
|¡ w d	ˆ d v rb‡ fd
d„|D ƒ}|D ] }t j | |d¡}t|ƒ}t d|¡‰ tˆ | |ƒ}|||jj< qd|S )Nú
Cargo.tomlrs   rš   zmanifest.VirtualManifestrW   c                 s  s    | ]}t   |¡V  qd S r.   )Úglob)r;   Úmr"   r"   r#   Ú	<genexpr>E  s   € 

ÿz"_load_manifests.<locals>.<genexpr>ÚmembersrZ   c                 3  s$    | ]}|ˆ d  d vr|V  qdS )rW   rZ   Nr"   )r;   Úx©r™   r"   r#   r¬   J  s   €" )Úosrq   Újoinr$   rB   rC   r§   rs   r%   ÚgetcwdÚchdirÚ	itertoolsÚchainÚfrom_iterable)	r—   r   r   Ú	manifestsZ	manifest_Úpwdr­   r«   Zmanr"   r¯   r#   Ú_load_manifests.  s2   



ÿr¹   Úpackage_namec                 C  s   |   d¡r| S | › dS )Nú-rs)Úendswith©rº   r"   r"   r#   Ú_dependency_nameW  s   r¾   c                 C  s   t | ƒ› dS )NZ_depr9   r½   r"   r"   r#   Ú_dependency_varname[  s   r¿   ÚcargorX   úbuilder.BuilderÚenvr   úT.List[mparser.BaseNode]c                 C  s¦   g }|  | | jj¡| d¡g¡ | | jj¡| d|jj› ¡| | d| jj› ¡g¡dœ}| jjr>| | jj¡|d< n| jj	rK| | jj	¡|d< | 
d||¡gS )zªCreate a function call

    :param cargo: The Manifest to generate from
    :param build: The AST builder
    :param env: Meson environment
    :return: a list nodes
    Úrustz>= z	rust_std=)r   Zmeson_versionZdefault_optionsrS   Úlicense_filesÚproject)ÚextendÚstringrs   r%   r   ZcoredataÚarrayrM   rS   rT   Úfunction)rÀ   rX   rÂ   ÚargsÚkwargsr"   r"   r#   Ú_create_project_  s   þúrÍ   c                   sr   g }| j  ¡ D ]/\}}|jp|}dˆ  ‡ fdd„|jD ƒ¡i}| ˆ  ˆ  dˆ  t	|ƒ¡g|¡t
|ƒ¡g¡ q|S )Nr   c                   s   g | ]}ˆ   |¡‘qS r"   )rÈ   )r;   Ús©rX   r"   r#   r¢     s    z(_create_dependencies.<locals>.<listcomp>Z
dependency)r”   r@   rs   rÉ   r   rÇ   ÚassignrÊ   rÈ   r¾   r¿   )rÀ   rX   Úastr%   Údeprº   Úkwr"   rÏ   r#   Ú_create_dependencies|  s    
ÿýúÿ
rÔ   r   úmanifest.CRATE_TYPEc              
   C  s€  g }i }| j  ¡ D ]#\}}|jp|}| | t|ƒ¡¡ ||kr,| |¡|| t|ƒ¡< q	| t| jjƒ¡| t	j
 dd¡¡g}| |¡| |¡dœ}	| jjsS|dkrn| | d¡| d¡g¡|	d< | d| d¡||	¡}
n'|d	v rud
}n|dv r|d}ntd|› ƒ‚|dv rŽ| d¡|	d< | |||	¡}
| |
d¡| |jdd| d¡idd¡| d| d¡| t| jjƒ¡| d¡g¡gS )NÚsrcúlib.rs)r”   Zrust_dependency_mapz
proc-macroz--externrŠ   Z	rust_argsrÄ   >   Ú	staticlibZrlibr}   Zstatic_library>   ÚcdylibÚdylibÚshared_libraryzUnsupported crate type >   rØ   rÙ   ÚcZrust_abir}   Zdeclare_dependencyZ	link_with)rÓ   rÒ   Zoverride_dependencyZmeson)r”   r@   rs   ÚappendÚ
identifierr¿   rÈ   r)   r%   r°   rq   r±   rÉ   r   r}   rŠ   Úmethodr   rÊ   rÐ   r¾   )rÀ   rX   r   r”   Zdependency_mapr%   rÒ   rº   ZposargsrÌ   r}   Ztarget_typer"   r"   r#   Ú_create_lib  sT   
€þþ

ÿþù	þýõrà   Ú	subp_nameúmparser.CodeBlockNodec           
   	   C  sö   |   d¡r| d d… n| }ttj |j|¡ƒ}| |¡}|s(td|›d|› ƒ‚tj |j|jd¡}t	 
|¡}t|||ƒ}|| | d| d¡g¡d¡g7 }|t||ƒ7 }tj tj |j|j|jdd	¡¡rv|jjD ]}	| t|||	ƒ¡ qj| |¡S )
Nr»   éýÿÿÿzCargo package z not found in r©   ÚimportrÄ   rÖ   r×   )r¼   r¹   r°   rq   r±   Ú
source_dirr¤   r   r—   r   ZBuilderrÍ   rÐ   rÊ   rÈ   rÔ   Úexistsr}   r   rÇ   rà   Úblock)
rá   r—   rÂ   rº   r·   rÀ   r   rX   rÑ   r   r"   r"   r#   Ú	interpretÈ  s   

""
rè   )r   r   r   r   )r%   r   r   r   )r*   r+   r   r,   )r*   r2   r   r3   )r*   r4   r   r5   )r*   r6   r   r7   )r˜   )r™   rš   r—   r   rq   r   r   r“   )r—   r   r   r¨   )rº   r   r   r   )rÀ   r“   rX   rÁ   rÂ   r   r   rÃ   )rÀ   r“   rX   rÁ   r   rÃ   )rÀ   r“   rX   rÁ   r   rÕ   r   rÃ   )rá   r   r—   r   rÂ   r   r   râ   )6rg   Ú
__future__r   ri   rª   Ú	importlibr´   r   r°   ÚshutilÚtypingrB   r˜   r   r   Zmesonlibr   r   ÚTYPE_CHECKINGÚtypesr	   r
   r   Úenvironmentr   r   rh   r   ÚtÚimport_moduleÚImportErrorÚwhichr$   r)   Úoverloadr0   Ú	dataclassrD   rl   rz   r‰   r   r   r   r‘   r“   r§   r¹   r¾   r¿   rÍ   rÔ   rà   rè   r"   r"   r"   r#   Ú<module>   s~   
ÿ




!

)



8