o
    uJAfa4                     @  s   d dl mZ ddlmZ d dlZeejd< 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	 ZG d
d dZdd Zdd Zdd Zdd Zdd Zedkr^ee  dS dS )    )annotations   )_pathlibNpathlib)MesonExceptionMesonBugException)mlogc                   s   dd l }t tr)t  t }|d urtdt| tj	
dr' dS tj	
dr1 |  |dkr;dS t trHtd  jS d}t fd	d
dD r`t| j j j nt| t  dS )Nr   z
A full log can be found atZMESON_FORCE_BACKTRACEr   	runpython   zeUnhandled python OSError. This is probably not a Meson bug, but an issue with your build environment.zUnhandled python exceptionc                 3  s     | ]}t  |d d uV  qd S N)getattr).0ae 9/usr/lib/python3.10/site-packages/mesonbuild/mesonmain.py	<genexpr>=   s    zerrorhandler.<locals>.<genexpr>)filelinenocolno)	traceback
isinstancer   r   	exceptionshutdownlogZboldosenvirongetZ	print_excOSErrorerrnoallr   r   r   r   )r   commandr   Zlogfilemsgr   r   r   errorhandler    s.   




r$   c                   @  sH   e Zd Zdd ZdddZddd	Zd
d Zdd Zdd Zdd Z	dS )CommandLineParserc                   s  ddl m}m}m}m}m}m}m}m m	}m
}	m}
m} ddlm} ddlm} dd l}| j_fdd_i _g _tjdjd	_jjd
ddd_jd|j|jdd jd|j|jdd jd|j|jdd jd|j|jdd jd|j|jdd jd|j|jdd jd|j|jdd jd|j|jdd jd|j|jd d jd! fd"d jd#d jd$|
j|
jd%d jd&|j|jd'd jd(|j|jd)d jd*j j!d+d jd,j"j#tj$d jd-|	j|	jtj$d d S ).Nr   )mconfmdistminitminstallmintromsetupmtestrewritermsubprojectsmunstable_coredatamcompilemdevenv)	env2mfile)wraptoolr   c                   s   t j| t jd  jdS )Nr
   )Zmax_help_positionwidth)argparseZHelpFormatterint
term_width)prog)selfr   r   <lambda>O   s    z,CommandLineParser.__init__.<locals>.<lambda>mesonr8   formatter_classZCommandsr"   z8If no command is specified it defaults to setup command.)titledestdescriptionsetupzConfigure the project)help_msgZ	configurezChange project optionsdistzGenerate release archiveinstallzInstall the projectZ
introspectzIntrospect projectinitzCreate a new projecttestz	Run testswrapz
Wrap toolsZsubprojectszManage subprojectsZrewritec                   s     | jS r   )add_arguments	formatter)parserr-   r9   r   r   r:   h   s    zModify the project definitioncompilezBuild the projectZdevenvz%Run commands in developer environmentr2   z5Convert current environment to a cross or native filehelpzPrint help of a subcommandr	   zunstable-coredata)% r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   Zscriptsr2   rG   r3   shutilget_terminal_sizecolumnsr7   rI   commandshidden_commandsr5   ArgumentParserrJ   Zadd_subparsers
subparsersadd_commandrH   runadd_help_argumentsrun_help_commandadd_runpython_argumentsrun_runpython_commandSUPPRESS)r9   r&   r'   r(   r)   r*   r+   r,   r.   r/   r0   r1   r2   r3   rO   r   rK   r   __init__G   sx   8

zCommandLineParser.__init__Nc                 C  s|   |pg }|t jkrt jd| | jd}| j| n| jj|||| jd}|| |j|d |g| D ]}|| j	|< q4d S )Nzmeson r<   )rM   aliasesr=   )run_func)
r5   r\   rT   rI   rS   appendrU   Z
add_parserZset_defaultsrR   )r9   nameZadd_arguments_funcr_   rB   r^   pir   r   r   rV   z   s   
zCommandLineParser.add_commandrJ   argparse.ArgumentParserc                 C  sD   |j ddddd |j ddt d | d	 |j d
tjd d S )Nz-c
store_trueeval_argF)actionr?   defaultz	--versionversion)rg   ri   script_filescript_args)nargs)add_argumentplatformZpython_versionr5   Z	REMAINDERr9   rJ   r   r   r   rZ      s   
z)CommandLineParser.add_runpython_argumentsc                 C  sX   |j tjdd < |jrt|j dS dd l}tjdt	j
|j |j|jdd dS )Nr   r   __main__)Zrun_name)rk   sysargvrf   execrj   runpypathinsertr   dirnameZrun_path)r9   optionsrt   r   r   r   r[      s   
z'CommandLineParser.run_runpython_commandc                 C  s   |j ddt| j d d S )Nr"   ?)rl   choices)rm   listrR   keysro   r   r   r   rX      s   z$CommandLineParser.add_help_argumentsc                 C  s(   |j r| j|j    dS | j  dS )Nr   )r"   rR   Z
print_helprJ   )r9   rx   r   r   r   rY      s
   
z"CommandLineParser.run_help_commandc           
   
   C  sx  d}t | j ddg }|r|d |vrd}dg| }|d | jv r3|d }| j| }|dd  }n| j}d }ddlm} ||}||}|d u rO|j	}|d	v oWt
jd
k }zLz||W W |ritjddd |rrtjddd t  S  ty }	 z"t|	|W  Y d }	~	W |rtjddd |rtjddd t  S d }	~	ww |rtjddd |rtjddd t  w )NFz-hz--helpr   TrA   r   mesonlib>   rL   rF   rA   rD   )      znRunning the setup command as `meson [options]` instead of `meson setup [options]` is ambiguous and deprecated.)ZfatalzdYou are using Python 3.6 which is EOL. Starting with v0.62.0, Meson will require Python 3.7 or newer)r{   rR   r|   rS   rJ   rN   r~   Zexpand_arguments
parse_argsr"   rq   version_infor_   r   ZwarningZnoticer   	Exceptionr$   )
r9   argsZimplicit_setup_command_noticeZknown_commandsr"   rJ   r~   rx   Z!pending_python_deprecation_noticer   r   r   r   rW      sf   






zCommandLineParser.runr   )rJ   rd   )
__name__
__module____qualname__r]   rV   rZ   r[   rX   rY   rW   r   r   r   r   r%   F   s    
3

r%   c              
   C  s   ddddddd}| | | }z	td| }W n ty0 } zt| W Y d }~d	S d }~ww z||W S  tyX } ztd
|  d t| W Y d }~d	S d }~ww )NZ	meson_exeZmeson_installZdelwithsuffixZgtkdochelperZhotdochelperZregen_checker)ZexerD   Z	delsuffixZgtkdocZhotdocZ
regencheckzmesonbuild.scripts.r   z	Error in z helper script:)	r   	importlibimport_moduleModuleNotFoundErrorr   r   rW   r   error)Zscript_namerk   Z
script_mapZmodule_namemoduler   r   r   r   run_script_command   s.   

r   c                   C  s4   t jjrt jj dst jjdd d S d S d S )NzUTF-surrogateescape)errors)rq   stdoutencodingupper
startswithreconfigurer   r   r   r   ensure_stdout_accepts_unicode   s   r   c                 C  s   ddl m} ||  d S )Nr   r}   )rN   r~   set_meson_command)mainfiler~   r   r   r   r      s   r   c              
   C  st  t jdr"dd l}ttttfD ]
}|jd|dd q|jddd t	j
d	krAt jd
rAdd l}|jdtdd |jddtd t  t	jdkr^t jdddvr^td td dS | d d  }t|dkr|d dkr|d dkrt| ddlm} z|dg|dd   W S  ty } zt|dW  Y d }~S d }~ww t|d |dd  S t| t |S )NZMESON_SHOW_DEPRECATIONSr   r   Z
mesonbuild)categoryr   ignorez.*importlib-resources.*)message)r   
   ZMESON_RUNNING_IN_PROJECT_TESTSz)UTF-8 Mode affects .*getpreferredencoding)r   r   cygwinZMSYSTEMrN   )ZMSYSrN   zYThis python3 seems to be msys/python on MSYS2 Windows, but you are in a MinGW environmentzYPlease install and use mingw-w64-x86_64-python3 and/or mingw-w64-x86_64-meson with Pacmanr
   z
--internalr   Z
regenerate)r+   z--reconfigurerA   )r   r   r   warningsDeprecationWarningSyntaxWarningFutureWarningPendingDeprecationWarningfilterwarningsrq   r   EncodingWarningr   rn   r   r   lenr   rN   r+   rW   r   r$   r   r%   )Zoriginal_argsr   r   typr   r+   r   r   r   r   rW      s8   

rW   c                  C  sJ   dt jv rtjt jsJ t j} n	tjt jd } tt jdd  | S )Nz	meson.exer   r   )rq   
executabler   ru   isabsabspathrr   rW   )Zlauncherr   r   r   main  s
   
r   rp   )Z
__future__r   rN   r   rq   modulesZos.pathr   rn   r   r5   Z
utils.corer   r   r   r$   r%   r   r   r   rW   r   r   exitr   r   r   r   <module>   s*   
& 	1	