o
    uJAf(                     @  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	 ddl
mZmZ ddlmZmZmZmZmZmZmZ ddl
mZ d dlZejrQddlmZ d	d
hZd;ddZd<ddZd=ddZd>ddZd?d!d"Z d@d(d)Z!dAd,d-Z"dBdCd4d5Z#dDd9d:Z$dS )E    )annotationsN)Path   )buildminstall)EnvironmentVariablesMesonException
is_windowssetup_vsenv	OptionKeyget_wine_shortpathMachineChoice)mlog)InstallDatazpwsh.exezpowershell.exeparserargparse.ArgumentParserreturnNonec                 C  sh   | j ddtddd | j ddtd dd	 | j d
dddd | j ddg ddd | j dtjddd d S )Nz-Cbuilddir.zPath to build directory)desttypedefaulthelpz	--workdirz-wzDDirectory to cd into before running (default: builddir, Since 1.0.0))r   r   r   z--dump?TzXOnly print required environment (Since 0.62.0) Takes an optional file path (Since 1.1.0))nargsconstr   z--dump-formatexport)shr   Zvscodez%Format used with --dump (Since 1.1.0))r   choicesr   devcmdcommandzDCommand to run in developer environment (default: interactive shell))r   metavarr   )add_argumentr   argparse	REMAINDER)r    r&   7/usr/lib/python3.10/site-packages/mesonbuild/mdevenv.pyadd_arguments   s    

r(   T.Optional[str]c               
   C  sl   t tj} | d d }tD ]&}z|ddddt|g}t|}|  W   S  tj	t
fy3   Y qw d S )Nscriptszcmd_or_ps.ps1z
-noprofilez-executionpolicyZbypassz-file)r   __file__parentPOWERSHELL_EXESstr
subprocesscheck_outputdecodestripCalledProcessErrorOSError)
mesonbuildscriptshellr!   resultr&   r&   r'   get_windows_shell%   s   

r9   envT.Dict[str, str]c                 C  sR   |  d}|s	d S tdptd}|sd S t|g|d| d< td d S )NWINEPATHZwine64wine;z4Meson detected wine and has set WINEPATH accordingly)getshutilwhichr   splitr   log)r:   Zwinepathwinecmdr&   r&   r'   reduce_winepath1   s   
rE   bbuild.Builddump_fmt%T.Tuple[T.Dict[str, str], T.Set[str]]c                 C  s   t  }|ddg |d| jg | jjtj  }|r$|d|g |r(i ntj	
 }|dv r3dnd }t }t| j|hD ]}|||}|| O }q@t| ||fS )NZMESON_DEVENV1ZMESON_PROJECT_NAMEZQEMU_LD_PREFIX>   r   r   z${0})r   setproject_nameenvironment
propertiesr   HOSTget_sys_rootosenvironcopy	itertoolschaindevenvget_env	get_namesrE   )rF   rH   	extra_envZsysrootr:   default_fmtvarnamesir&   r&   r'   rW   ;   s   rW   install_data'InstallData'T.List[str]c                 C  s   ddl m} g }|d| jdddd}| rm| jjtd}t|ts)J d	| jjtd
}t|ts;J d	t	j
||}|jdd
|ffd}t|tsUJ d	t|}	|jD ]}
|	t|
jjv rl||
j
 q\|S )Nr   )PkgConfigDependencyzbash-completionFTz>=2.10)requiredsilentversionprefixzfor mypydatadircompletionsdir)	pkgconfigpkgconfig_define)dependencies.pkgconfigr`   rM   foundcoredata
get_optionr   
isinstancer.   rQ   pathjoinget_variabler   datainstall_pathparentsappend)rF   r]   r`   r8   deprd   re   Zdatadir_absrf   Zcompletionsdir_pathfr&   r&   r'   bash_completion_filesO   s&   

rw   autoload_pathr   
gdb_helperr.   fnamec              	   C  s~   | |j  }|jddd z%t r!t|t|tj|  W d S t	|t|tj|  W d S  t
tjfy>   Y d S w )NT)rs   exist_ok)r,   mkdirr	   r@   rS   r.   rQ   rn   basenamesymlinkFileExistsErrorSameFileError)rx   ry   rz   destdirr&   r&   r'   add_gdb_auto_loadb   s   
""r   
privatedirworkdirc              	   C  s  t dsd S | j}| d }t|g|jdd  R  }d}|jD ]9}|jds4|jds4|jdrYt|jj	ddd	 }|j
D ]}	t|	j}
|
j|krXt||j|
 d
}qCq |rd| d}|d }d}z&|jddd}|| vr~|| d
}W d    n1 sw   Y  W n ty   |j|dd d
}Y nw |r| }| }||}tdtt| td tdtd|j dtd |j|krtdtt|jdtd|  d S d S d S d S )Ngdbzgdb-auto-loadr   Fz-gdb.pyz-gdb.gdbz-gdb.scm-r   Tz add-auto-load-scripts-directory 
z.gdbinitzr+utf-8encodingz.Meson detected GDB helpers and added config inz+To load it automatically you might need to:z - Addzadd-auto-load-safe-path inz
~/.gdbinitz - Change current workdir tozor usez--init-command )r@   rA   r,   r   partsrq   rn   endswithnamersplittargetsrz   r   open	readlineswriteFileNotFoundError
write_textresolverelative_tor   rC   boldr.   )r   r]   r   bdirZautoload_basedirrx   Zhave_gdb_helpersdlibnametrn   Zgdbinit_lineZgdbinit_pathZ
first_timerv   Zworkdir_pathrel_pathr&   r&   r'   write_gdb_scriptn   s^   

$







r   rV   r[   
T.Set[str]dump_formatoutputT.Optional[T.TextIO]c                 C  sD   |D ]}t | d| |  d|d |dkrt d| |d qd S )Nz="")filer   zexport )print)rV   r[   r   r   r   r&   r&   r'   dump   s   r   optionsargparse.Namespaceintc              
   C  s6  t | jd }|d }| std| jdt| j}| jp#| j}td|j	j
td}t| | jr;| jnd }t||\}}| jr}| jrNtd| jdu r[t||| dS t| jd	d
d}	t||||	 W d    dS 1 svw   Y  dS |j	 rd}
|j	 }|rd| }|
d| 7 }
t|
 tt|d }t||| | j}|sSd|j d}tj d}|rtj!"|r|g}nCt# rt$ }|st%d |t&v r|ddg}d| d}|d|g7 }ntj ddg}|dd| dg7 }ntj dtj!'dg}d |d v rRt(j)d	d!}|*d" tj d#s1|*d$| d% t+||D ]}|*d&| d' q6|,  |-d( |-|j. nt/j0|d | d)d d*}|pf|d |d< z
t1j2|d+||d,W S  t1j3y } z	|j4W  Y d }~S d }~w t5y   td-|d  w ).Nzmeson-privatez	build.datz
Directory z- does not seem to be a Meson build directory.boolvsenvz3--dump option does not allow running other command.Twr   r   r   z'An executable wrapper could be required z: zinstall.dat[]ZSHELLz6Failed to determine Windows shell, fallback to cmd.exez-NoLogoz-NoExitzfunction global:prompt {  "z PS " + $PWD + "> "}z-CommandZCOMSPECzC:\WINDOWS\system32\cmd.exez/kzprompt z $P$Gz/bin/shZbash)modez [ -e ~/.bashrc ] && . ~/.bashrc
ZMESON_DISABLE_PS1_OVERRIDEzexport PS1="z $PS1"
z. "z"
z--rcfilePATH)rn   F)	close_fdsr:   cwdzCommand not found: )6r   r   is_filer   r   loadr   TcastrM   rk   rl   r   r
   r   r   rW   r    r   need_exe_wrapperget_exe_wrapperro   get_commandr   rC   r   load_install_datar.   r   rL   rQ   rR   r?   rn   existsr	   r9   warningr-   realpathtempfileNamedTemporaryFiler   rw   flushrt   r   r@   rA   r/   callr3   
returncoder   )r   r   	buildfilerF   r   
need_vsenvrH   rV   r[   r   mexe_wrappercmdr]   argsZprompt_prefixZ	shell_envr7   promptZtmprcrv   Zabs_pather&   r&   r'   run   s   








r   )r   r   r   r   )r   r)   )r:   r;   r   r   )rF   rG   rH   r)   r   rI   )rF   rG   r]   r^   r   r_   )rx   r   ry   r.   rz   r   r   r   )r   r   r]   r^   r   r   r   r   )N)
rV   r;   r[   r   r   r)   r   r   r   r   )r   r   r   r   )%
__future__r   rQ   r/   r$   r   r@   rT   pathlibr    r   r   mesonlibr   r   r	   r
   r   r   r   r   typingr   TYPE_CHECKINGbackendsr   r-   r(   r9   rE   rW   rw   r   r   r   r   r&   r&   r&   r'   <module>   s.    $







)