o
    lda4                     @  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 shome/ych/rk3568/buildroot/output/rockchip_rk3568_recovery/host/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get	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>Zmesonr9   formatter_classZCommandsr#   z8If no command is specified it defaults to setup command.)titledestdescriptionsetupzConfigure the project)help_msg	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.   r:   r   r   r;   h   s    zModify the project definitioncompilezBuild the projectZdevenvz%Run commands in developer environmentr3   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/   r0   r1   r2   scriptsr3   rH   r4   shutilget_terminal_sizecolumnsr8   rJ   commandshidden_commandsr6   ArgumentParserrK   Zadd_subparsers
subparsersadd_commandrI   runadd_help_argumentsrun_help_commandadd_runpython_argumentsrun_runpython_commandSUPPRESS)r:   r'   r(   r)   r*   r+   r,   r-   r/   r0   r1   r2   r3   r4   rQ   r   rL   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<   )rN   aliasesr=   )run_func)
r6   r^   rV   rJ   rU   appendrW   Z
add_parserset_defaultsrT   )r:   nameZadd_arguments_funcra   rB   r`   pir   r   r   rX   z   s   
zCommandLineParser.add_commandrK   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)rj   rl   script_filescript_args)nargs)add_argumentplatformpython_versionr6   Z	REMAINDERr:   rK   r   r   r   r\      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)rn   sysargvri   execrm   runpypathinsertr   dirnameZrun_path)r:   optionsrx   r   r   r   r]      s   
z'CommandLineParser.run_runpython_commandc                 C  s   |j ddt| j d d S )Nr#   ?)ro   choices)rp   listrT   keysrs   r   r   r   rZ      s   z$CommandLineParser.add_help_argumentsc                 C  s(   |j r| j|j    dS | j  dS )Nr   )r#   rT   
print_helprK   )r:   r|   r   r   r   r[      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>   rE   rG   rA   rM   )      znRunning the setup command as `meson [options]` instead of `meson setup [options]` is ambiguous and deprecated.)fatalzdYou are using Python 3.6 which is EOL. Starting with v0.62.0, Meson will require Python 3.7 or newer)r   rT   r   rU   rK   rO   r   Zexpand_arguments
parse_argsr#   ru   version_infora   r   warningZnoticer   	Exceptionr%   )
r:   argsZimplicit_setup_command_noticeZknown_commandsr#   rK   r   r|   Z!pending_python_deprecation_noticer   r   r   r   rY      sf   






zCommandLineParser.runr   )rK   rg   )
__name__
__module____qualname__r_   rX   r\   r]   rZ   r[   rY   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)exerE   Z	delsuffixZgtkdocZhotdocZ
regencheckzmesonbuild.scripts.r   z	Error in z helper script:)	r   	importlibimport_moduleModuleNotFoundErrorr   r   rY   r   error)script_namern   Z
script_map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)ru   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   )rO   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MSYSTEMrO   )ZMSYSrO   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filterwarningsru   r   EncodingWarningr   rq   r   r   lenr   rO   r,   rY   r   r%   r   r&   )Zoriginal_argsr   r   typr   r,   r   r   r   r   rY      s8   

rY   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   )ru   
executabler   ry   isabsabspathrv   rY   )Zlauncherr   r   r   main  s
   
r   rt   )
__future__r   rO   r   ru   modulesos.pathr   rq   r   r6   Z
utils.corer   r   r   r%   r&   r   r   r   rY   r   r   exitr   r   r   r   <module>   s*   
& 	1	