o
    uJAfg                     @  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Zd dlZd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mZmZmZmZmZmZmZ dd
lmZ ddlmZmZ ddlmZm Z m!Z! ddl"m#Z#m$Z$m%Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddlm9Z9m:Z: ddl;m<Z<m=Z= ej>rd dl?m@Z@mAZA ddl	mBZB ddlmCZCmDZD ddlmEZE ddl"mFZF ddlGmHZH ddlImJZJmZK ddlLmMZMmNZN G dd de@ZOG dd deJZPG dd  d eKZQejRe=d!f ZSd"d#hZTeTUe eTd$d%h8 ZTd&d' e,D ZVG d(d! d!eZWe5d)eXe*fZYe5d"eZd*d+Z[e5d#eZd*d,d-Z\e5d"eZe*fZ]G d.d/ d/e(d! Z^G d0d1 d1e
Z_d7d5d6Z`dS )8    )annotationsN   )ExtensionModule
ModuleInfo   )mesonlib)mlog)UserFeatureOption)known_shmod_kwargsCustomTargetCustomTargetIndexBuildTargetGeneratedListStructuredSourcesExtractedObjectsSharedModule)NotFoundDependency)get_dep_identifierfind_external_dependency)BasicPythonExternalProgrampython_factory_PythonDependencyBase)extract_required_kwargpermitted_dependency_kwargs
primitives)_ExternalProgramHolder)NoneTypePRESERVE_PATH_KWSHARED_MOD_KWS)	noPosargsnoKwargspermittedKwargsContainerTypeInfoInvalidArgumentstyped_pos_argstyped_kwargs	KwargInfo
FeatureNewFeatureNewKwargsdisablerIfNotFound)MachineChoice	OptionKey)ExternalProgramNonExistingExternalProgram)	TypedDictNotRequired)ModuleState)BuildData)
Dependency)Interpreter)BuildTargetSource)ExtractRequiredr   )TYPE_varTYPE_kwargsc                   @  &   e Zd ZU ded< ded< ded< dS )PyInstallKwT.Optional[bool]purestrsubdirT.Optional[str]install_tagN__name__
__module____qualname____annotations__ rF   rF   >/usr/lib/python3.10/site-packages/mesonbuild/modules/python.pyr:   1      
 r:   c                   @  r9   )FindInstallationKwbooldisablerzT.List[str]modulesr;   r<   NrA   rF   rF   rF   rG   rI   7   rH   rI   c                   @  s   e Zd ZU ded< dS )ExtensionModuleKwzNotRequired[T.Optional[str]]r>   NrA   rF   rF   rF   rG   rM   =   s   
 rM   PythonExternalProgramr>   limited_apiname_prefixname_suffixc                 C  s   g | ]	}|j d vr|qS )>   rQ   rP   )name).0krF   rF   rG   
<listcomp>H   s    rU   c                      s8   e Zd ZU i Zded< dd fdd	ZdddZ  ZS )rN   zT.ClassVar[T.Dict[str, bool]]run_bytecompileNstateT.Optional['ModuleState']returnrJ   c                   s.   t   }|r| |d| _| |d| _|S )Nplatlibpurelib)supersanity	_get_pathrZ   r[   )selfrW   ret	__class__rF   rG   r]   R   s
   
zPythonExternalProgram.sanitykeyr=   c                 C  s   | j d | dd  }|s|S td|j| ddd}|r1|jdddr/td| d	|S |jddd}|d
krE| j d rCdnd}|dkrWtj	| j d d |}|S |dkrl| j d setd| j d | }|S )Ninstall_pathsr   r=   dirpythonmoduleinstall_envzpython.z1dir and python.install_env are mutually exclusiveautoZis_venvvenvsystem	variablesprefixzDpython.install_env cannot be set to "venv" unless you are in a venv!Zsysconfig_paths)
infoTcast
get_optionis_user_defined_optionr   MesonExceptionospathjoin)r_   rW   rc   rel_pathvalueri   rF   rF   rG   r^   Y   s&   

zPythonExternalProgram._get_pathN)rW   rX   rY   rJ   )rW   rX   rc   r=   rY   r=   )rB   rC   rD   rV   rE   r]   r^   __classcell__rF   rF   ra   rG   rN   K   s   
 r<    defaultz1.3.0r~   sincec                      s  e Zd ZdMddZeeedeeej	e
eeeeefdedgeeeR d	d
idNddZdOddZdPddZeeedhB edddgedQd"d#Zed$eej	fded%eeeed&ee fd'd(dRd,d-Z!eed.eedSd/d0Z"dTd5d6Z#ee$dUd7d8Z%ed9ee$dVd;d<Z&ed=ee'gd>e$dWdAdBZ(edCee$dVdDdEZ)edFee'gd>e$dWdGdHZ*ee$e+dIdJdU fdKdLZ,  Z-S )XPythonInstallationrf   'PythonExternalProgram'interpreter'Interpreter'c                 C  s   t | || |j}| jjjtd}t	|t
sJ d|d | _|d | _|d | _|d | _|j| _tj||j| _tj||j| _|d | _|d | _|d	 | _|d
 | _| j| j| j| j| j | j!| j"| j#| j$| j%| j&| j'd d S )Nrn   for mypyrm   suffixlimited_api_suffixpathsversionplatformis_pypylink_libpython)Zextension_module
dependencyZinstall_sourcesget_install_dirZlanguage_versionfoundZhas_pathget_pathZhas_variableget_variablerv   )(r   __init__ro   r   environmentcoredatarr   r   r+   
isinstancer=   rm   r   r   r   r<   ru   rv   rw   rZ   platlib_install_pathr[   purelib_install_pathr   r   r   r   methodsupdateextension_module_methoddependency_methodinstall_sources_methodget_install_dir_methodlanguage_version_methodfound_methodhas_path_methodget_path_methodhas_variable_methodget_variable_methodpath_method)r_   rf   r   ro   rn   rF   rF   rG   r   y   s8   







zPythonInstallation.__init__zpython.extension_module)varargsallow_unknownTargs'T.Tuple[str, T.List[BuildTargetSource]]kwargsrM   rY   'SharedModule'c                 C  s  d|v r|d d urt dn|dpd}| d||d< | j}t|d}tdd |D d }|d u rS| i }| sCt	d	|
| td
d| jd| j |d}| jjjtddd}|dkr|r| j}| ||j}	d|	 }
t|d}|
|
 ||d< t|d}|
|
 ||d< |d }| jjjj| }tdd | D rt|}|j| jdd | st	d|| |
| |jdd}d| d}d| d}t|d}| jjjjtd j }|r|
| n|
| ||d< ||d< |!dd\}}|d  | |d f}d|d!< ||d"< |d# dkr;| j"s7t#| jd$r;d%|d#< | j$| j||t%S )&Ninstall_dirr>   z1"subdir" and "install_dir" are mutually exclusiver|   Fdependenciesc                 s  s    | ]
}t |tr|V  qd S rz   )r   r   )rS   deprF   rF   rG   	<genexpr>   s    z=PythonInstallation.extension_module_method.<locals>.<genexpr>Python dependency not foundzGpython_installation.extension_module with implicit dependency on pythonz0.63.0z$use python_installation.dependency()rO   allow_limited_apirf   rg   z-DPy_LIMITED_API=c_argscpp_argsnativec                 s  s    | ]	}|  d kV  qdS )msvcN)get_id)rS   compilerrF   rF   rG   r      s    T)rO   z2Python dependency supporting limited API not found.z/NODEFAULTLIB:pythonz_d.libz.lib	link_argsdebugr   r   rP   rQ   gnu_symbol_visibilityz>=3.9inlineshidden)&r#   pop_get_install_dir_implr   r   extract_as_listnext_dependency_method_implr   rt   appendr'   
single_use
subprojectcurrent_noder   r   r   rr   r+   r   &_convert_api_version_to_py_version_hexr   	compilersanyvaluescopyZfind_libpy_windowsenvremovereplaceoptionsry   rsplitr   version_comparebuild_targetr   )r_   r   r   r>   Ztarget_suffixnew_depsZpydepZlimited_api_versionr   Zlimited_api_version_hexZlimited_api_definitionZ
new_c_argsZnew_cpp_argsfor_machiner   Z
pydep_copyZpyverZ#python_windows_debug_link_exceptionZ%python_windows_release_link_exceptionZnew_link_argsis_debugsplitrF   rF   rG   r      sx   











z*PythonInstallation.extension_module_methodapi_versionr=   detected_versionc                 C  s   t d}||}|std| dt|dr"td| dt|d| r5td| d	| d
|d}t|d }t|d }d||S )Nz[0-9]\.[0-9]{1,2}zPython API version invalid: "z".z<3.2z$Python Limited API version invalid: z (must be greater than 3.2)>z%Python Limited API version too high: z (detected )r   r   r   z0x{:02x}{:02x}0000)	recompile	fullmatchr#   r   r   r   intformat)r_   r   r   Zpython_api_version_formatZdecimal_matchZversion_componentsmajorminorrF   rF   rG   r      s   


z9PythonInstallation._convert_api_version_to_py_version_hexr8   r3   c                 C  s   | j |}t|  |}| j jj| |}|d ur|S | }d|d< t| j j	||| j
}td| j j	||}| j jj| || |S )NFrequiredrf   )r   machine_from_native_kwargr   
_full_pathr   depsgetr   r   r   held_objectr   put)r_   r   r   
identifierr   Z
new_kwargs
candidatesrF   rF   rG   r     s   z*PythonInstallation._dependency_method_implZembedzpython_installation.dependencyz0.53.0T.List['TYPE_var']'TYPE_kwargs''Dependency'c                 C  sf   t || j\}}}|r!tdtddt|d td| jjS | |}|r1|	 s1t
d|S )Nr3   rf   zskipped: featuredisabledr   )r   r   r   logboldr   r   r   r   r   r   rt   )r_   r   r   r   r   featurer   rF   rF   rG   r     s   

z$PythonInstallation.dependency_methodinstall_dataz#python_installation.install_sourcesr@   z0.60.0)r   ,T.Tuple[T.List[T.Union[str, mesonlib.File]]]'PyInstallKw''Data'c              	   C  sr   d| j j| j< |d pd}|d d ur|d n| j}| ||d }| jj| j|d |t	 d |d|d d	S )
NTr@   python-runtimer<   r>   r   rf   preserve_path)renametaginstall_data_typer   )
r   rV   r   r<   r   r   install_data_implsource_strings_to_filesr   FileMode)r_   r   r   r   r<   r   rF   rF   rG   r      s   
z)PythonInstallation.install_sources_methodzpython_installation.install_dirc                 C  s8   d| j j| j< |d d ur|d n| j}| ||d S )NTr<   r>   )r   rV   r   r<   r   )r_   r   r   r<   rF   rF   rG   r   4  s   z)PythonInstallation.get_install_dir_methodr<   rJ   r>   P_OBJ.OptionStringc                 C  s:   |r| j }d}n| j}d}ttj||tj||S )N{py_purelib}{py_platlib})r   r   P_OBJOptionStringru   rv   rw   )r_   r<   r>   baserR   rF   rF   rG   r   ;  s    z(PythonInstallation._get_install_dir_implc                 C  s   | j S rz   )r   r_   r   r   rF   rF   rG   r   E  s   z*PythonInstallation.language_version_methodzpython_installation.has_pathT.Tuple[str]c                 C     |d | j v S Nr   )r   r  rF   rF   rG   r   J     z"PythonInstallation.has_path_methodzpython_installation.get_pathoptargs$T.Tuple[str, T.Optional['TYPE_var']]
'TYPE_var'c                 C  B   |\}}z| j | W S  ty    |d ur| Y S t| dw )Nz is not a valid path name)r   KeyErrorr#   )r_   r   r   Z	path_namefallbackrF   rF   rG   r   O     z"PythonInstallation.get_path_methodz python_installation.has_variablec                 C  r  r  )rm   r  rF   rF   rG   r   Z  r  z&PythonInstallation.has_variable_methodz python_installation.get_variablec                 C  r  )Nz is not a valid variable name)rm   r  r#   )r_   r   r   var_namer  rF   rF   rG   r   _  r  z&PythonInstallation.get_variable_methodzPython module path methodz0.50.0c                   s   t  ||S rz   )r\   r   r  ra   rF   rG   r   j  s   zPythonInstallation.path_method)rf   r   r   r   )r   r   r   rM   rY   r   )r   r=   r   r=   rY   r=   )r   r8   rY   r3   )r   r   r   r   rY   r   )r   r   r   r   rY   r   )r   r   r   r   rY   r=   )r<   rJ   r>   r=   rY   r   )r   r   r   r   rY   r=   )r   r  r   r   rY   rJ   )r   r	  r   r   rY   r
  ).rB   rC   rD   r   r!   
mod_kwargsr$   r=   r   Filer   r   r   r   r   r   r%   _MOD_KWARGS_DEFAULTABLE_SUBDIR_KW_LIMITED_API_KWr   r   r   r)   r   r(   r   r   _PURE_KW
_SUBDIR_KWr   r&   r   r   r   r   r    r   r   objectr   r   r   r'   r   r{   rF   rF   ra   rG   r   x   sZ    

W



		r   c                      s   e Zd ZeddZd/ fddZd0d
dZd1ddZed2ddZ	d3ddZ
eedegdededeefdd ed!ed"d#d$ed%eeedg d&d'ejdd(d$d4d-d.Z  ZS )5PythonModulerf   z0.46.0r   r   rY   Nonec                   s(   t  | i | _| jd| ji d S )Nfind_installation)r\   r   installationsr   r   r  )r_   r   ra   rF   rG   r   u  s
   
zPythonModule.__init__(T.List[mesonlib.ExecutableSerialisation]c                 C  s  | j j}g }| j jjtjddd}|dkr|S ttj	
 s"|S | }g }dddd	}|jD ]}||jrM||jtj|j|jtj|jf q1|jD ]}||jrg||jtj|j|jf qQ|jD ]}||jd
r||jtj|j|jf qkdd l}	tj| j j d}
t|
d}||	jdd W d    n1 sw   Y  | j 
 D ]}t!|tr=|j	|j"d  r=t#$d|}d|j"d  d}g }|D ]\}}|%tj|j|j&tj|j|j'fr|| qttj| j j |ddd}t()|| W d    n	1 sw   Y  |j*|
|t+|g }|j,|d
d|j'|j&dd}|| q|S )Nbytecompilerf   rg   FisdirrJ   c                 S  s   |  do| dp|S )N)r   r   z.py)
startswithendswith)fr  rF   rF   rG   should_append  s   z8PythonModule._get_install_scripts.<locals>.should_appendTr   zpycompile.pywbzmesonbuild.scriptsr   rN   zpython-z-installed.jsonwzutf-8)encodingr   )Z
py_purelibZ
py_platlib)verboser   installdir_map)F)r  rJ   )-r   backendr   r   rr   r   r+   r   rN   rV   r   create_install_datatargetsout_namer   ru   rv   rw   rn   outdirbasenamefnamedatainstall_path_nameinstall_pathinstall_subdirsimportlib.resourcesget_scratch_diropenwrite	resourcesread_binaryr  r   ro   rp   rq   r   rZ   r[   jsondumpcommandr=   get_executable_serialisation)r_   r)  r`   ZoptlevelinstalldataZpy_filesr#  td	importlibZ	pycompiler"  imanifestZmanifest_jsonrR   cmdscriptrF   rF   rG   _get_install_scripts|  s\   

*


*
"

z!PythonModule._get_install_scriptsbr1   c                 C  s   |j |   d S rz   )install_scriptsextendrF  )r_   rG  rF   rF   rG   postconf_hook  s   zPythonModule.postconf_hookname_or_pathr=   r?   c                 C  sp   |  dsd S tdsd S d| dd   }d|ddg}t|\}}}| }tj|r6tj	|dS d S )N)python2python3py-   -cz;import sysconfig; print(sysconfig.get_config_var('BINDIR'))rf   )
r   shutilwhichr   
Popen_safestripru   rv   existsrw   )rK  verrD  _stdout	directoryrF   rF   rG   _get_win_pythonpath  s   

z PythonModule._get_win_pythonpathrW   'ModuleState'display_namer   rJ   MaybePythonProgc           	      C  s   |s	t dtj}n7t||}t ||d}| s,t r,| |}|d ur,|}t |}| s@|dv r@t|d}t ||d}| r_||rK|S | d}|rWt	|t
j||jd t|jS )NrM  )Zext_prog>   rM  rL  rf   z1 is not a valid python or it is missing distutils)location)rN   r   python_commandr,   
from_entryr   
is_windowsr[  r]   rt   r   warningr   r-   rR   )	r_   rW   r]  rK  r   rf   Z
tmp_pythonZ
pythonpathZ	sanitymsgrF   rF   rG   _find_installation_impl  s(   




z$PythonModule._find_installation_implzpython.find_installationr  Tr}   rK   Fz0.49.0r   rL   z0.51.0)listifyr~   r   z0.64.0r   T.Tuple[T.Optional[str]]r   'FindInstallationKw'c              
   C  sT  t dd}t||j|\}}}|jtjdpg }|d }	|	p d}
|s*|	d ur*|	g}|r0|d nd }|rJtd|p:ddt	ddt
|d	 t S | j|}|s_| ||
||}|| j|< |d
 }g }g }| r|r|D ]!}t|jdd| g ^}}|jdkr|| qo|| qod|jg}|r|dd| |d | r|s|tddd|jg n|t	d |r|d |d| tj|  | s|rtd|pdt|jS |r
|rtd|pdd|t|jS t|tsJ dt|}|d |_|j |j!d d |S )Nz-Passing "feature" option to find_installationz0.48.0rf   r   Programzfound:NOz(disabled by:r   rL   rQ  zimport z({})z, YES zmodules:z{} not foundz{} is missing modules: {}r   r<   r   F)#r'   r   r   r   lookup_binary_entryr*   HOSTr   r   redr   r-   r  r   rd  r   r   rT  r<  
returncoder   rR   r   rw   rI  greenrt   r   rN   r   r<   rV   
setdefaultro   MesonBugException)r_   rW   r   r   feature_checkr   r   r   Znpr  r]  rK  rf   Zwant_modulesfound_modulesZmissing_modulesmodprX  msgrF   rF   rG   r    sh   
&



$





zPythonModule.find_installation)r   r   rY   r  )rY   r  )rG  r1   rY   r  )rK  r=   rY   r?   )
rW   r\  r]  r=   rK  r=   r   rJ   rY   r^  )rW   r\  r   rf  r   rg  rY   r^  )rB   rC   rD   r   INFOr   rF  rJ  staticmethodr[  rd  r)   r$   r=   r%   r&   rJ   r	   r"   listr  evolver  r{   rF   rF   ra   rG   r  q  s$    


1
!r  r   r   rY   c                 C  s   t | }|jtt |S rz   )r  r   append_holder_maprN   r   )r   ru  rF   rF   rG   
initialize3  s   r}  )r   r   rY   r  )a
__future__r   r   r:  ru   rR  r   typingrp   r|   r   r   r   r   r   r	   buildr
   r   r   r   r   r   r   r   r   r   Zdependencies.detectr   r   Zdependencies.pythonr   r   r   r   r   r   r   r   Zinterpreter.interpreterobjectsr   Zinterpreter.type_checkingr   r   r   interpreterbaser   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   programsr,   r-   TYPE_CHECKINGZtyping_extensionsr.   r/   r0   r1   r2   r3   r4   Zinterpreter.interpreterr5   Zinterpreter.kwargsr6   ZSharedModuleKwZinterpreterbase.baseobjectsr7   r8   r:   rI   rM   Unionr^  r  r   r  rN   rJ   r  r=   r  r  r  r   r  r}  rF   rF   rF   rG   <module>   sX   ((4
( z C