o
    uJAf+                     @  s   d dl mZ d dlZd dlm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mZmZmZ ddlmZmZ ejrwd dlmZ dd	lmZ dd
lmZ ddlmZ ejeeje eje f Z ejeejedf eej!ejeef  f Z"G dd dZ#dS )    )annotationsN)Thread   )mlog)
PerMachine
Popen_safeversion_compare
is_windows	OptionKey)find_external_programNonExistingExternalProgram)Path)Environment)MachineChoice)ExternalProgram.c                   @  s   e Zd ZU eddZded< eddZded< i Zded< dCdDddZdCdEddZ	dFddZ
dGdHd d!ZdId)d*ZdJd,d-ZdJd.d/ZdJd0d1ZdJd2d3ZdKdLd5d6ZdMd7d8ZdNd9d:ZdNd;d<ZdOd=d>ZdOd?d@ZdPdAdBZdS )QCMakeExecutorNz'PerMachine[T.Optional[ExternalProgram]]class_cmakebinzPerMachine[T.Optional[str]]class_cmakeverszT.Dict[T.Any, TYPE_result]class_cmake_cacheFenvironment'Environment'versionstrfor_machiner   silentboolc              
   C  s   || _ || _|| _| j| j|d\| _| _d| _d| _g | _g | _	| jd u r(d S t
| j| j sMtdt| j dt| jdt| j d d | _d S | jjjtd| jd	 j| _| jro|  j	d
d| jg7  _	d S d S )N)r   TFzThe version of CMakeiszbut versionzis requiredcmake_prefix_path)machinez-DCMAKE_PREFIX_PATH={};)min_versionr   r   find_cmake_binarycmakebin	cmakeversalways_capture_stderrprint_cmoutprefix_pathsextra_cmake_argsr   r   warningboldget_pathcoredataoptionsr
   valueformatjoin)selfr   r   r   r    r1   >/usr/lib/python3.10/site-packages/mesonbuild/cmake/executor.py__init__.   s,   
"zCMakeExecutor.__init__return7T.Tuple[T.Optional['ExternalProgram'], T.Optional[str]]c                 C  s0  t tj| j trtd| j d dS tj| j d ur(td| j d ndtj| j d u s2J td| j d t|| jdd|jdd	D ])}| 	|}|sRqH|sdt
d
t| d| d |tj| j< |tj| j<  n|s}t
d
td t tj| j< d tj| j< dS tj| j tj| j fS )NzCMake binary for z is cached as not foundNNz is cached.z is not cachedcmakeCMakeF)allow_default_for_crosszFound CMake:()NO)
isinstancer   r   r   r   r   debugr   default_cmakecheck_cmakelogr)   r*   r   red)r0   r   r   Zpotential_cmakebinZversion_if_okr1   r1   r2   r!   G   s6   



zCMakeExecutor.find_cmake_binaryr"   'ExternalProgram'T.Optional[str]c              
   C  s  |  std|j d S z&| }t|dg dd \}}|jdkr4tdd	| W d S W n2 t
yJ   tdd	| Y d S  tyg   dd	|}t s_|d	7 }t| Y d S w td
|}|d urw|dS td|d d S )NzDid not find CMake z	--versionr   r   z$Found CMake {!r} but couldn't run it z?We thought we found CMake {!r} but now it's not there. How odd!z7Found CMake {!r} but didn't have permissions to run it.zO

On Unix-like systems this is often caused by scripts that are not executable.z#(cmake|cmake3)\s*version\s*([\d.]+)zWe thought we found CMake z?, but it was missing the expected version string in its output.)foundr   rA   nameget_commandr   
returncoder(   r.   r/   FileNotFoundErrorPermissionErrorr	   researchgroup)r0   r"   cmdpoutmsgZcmversr1   r1   r2   r@   j   s:   


zCMakeExecutor.check_cmaker%   T.Optional[bool]r$   Nonec                 C  s$   |d ur|| _ |d ur|| _d S d S N)r%   r$   )r0   r%   r$   r1   r1   r2   set_exec_mode   s
   
zCMakeExecutor.set_exec_modeargsT.List[str]	build_dirr   envT.Optional[T.Dict[str, str]]TYPE_cache_keyc                 C  s8   |d ur
t | nt  }t|}| j || |fS rU   )	frozensetitemstupler"   r*   as_posix)r0   rW   rY   rZ   ZfenvZtargsr1   r1   r2   
_cache_key   s   zCMakeExecutor._cache_keyTYPE_resultc                   s  | j  | }tj|tjtjt||d d fdd}t|d}|  zGd}t	d}d	}		  j
 }
|
s8n&|
jdd}||rN||7 }|d }	n|	rU||7 }nt|d q0W  j
  |     n j
  |     w  jd |fS )NstdoutstderrcwdrZ   r4   rT   c                    s:   	  j  } | s	nt| jddd q j   d S )NTignoreerrors
)rd   readliner   rA   decodestripclose)lineprocr1   r2   print_stdout   s   
z6CMakeExecutor._call_cmout_stderr.<locals>.print_stdout)target z/^\s*(.*\.(cmake|txt))\(([0-9]+)\):\s*(\w+)\(.*$FTrg   rh   z )
rj   )r4   rT   )r"   rH   SPopenPIPEr   r   startrL   compilere   rk   rl   matchendswithr   r(   rm   rn   r/   waitrI   )r0   rW   rY   rZ   rO   rr   tZ	raw_traceZtline_start_regZinside_multiline_traceZline_rawro   r1   rp   r2   _call_cmout_stderr   s:   








z CMakeExecutor._call_cmout_stderrc                 C  st   | j  | }tj|tjtjt||d}	 |j }|snt	
|jddd q|j  |  |jd d fS )Nrc   Trg   rh   rj   )r"   rH   ru   rv   rw   STDOUTr   rd   rk   r   rA   rl   rm   rn   r|   rI   )r0   rW   rY   rZ   rO   rq   ro   r1   r1   r2   _call_cmout   s   

zCMakeExecutor._call_cmoutc           	   	   C  sh   |j ddd | j | }tj||t|dtjtjdd}|j}|jj	dd}|j
j	dd}|||fS )NT)parentsexist_okF)rZ   rf   	close_fdsrd   re   universal_newlinesrg   rh   )mkdirr"   rH   ru   runr   rw   rI   rd   rl   re   )	r0   rW   rY   rZ   rO   retrcrQ   errr1   r1   r2   _call_quiet   s   

zCMakeExecutor._call_quietc                 C  sp   t d| j  d| d |D ]}t d| d q| js'| |||S | jr1| |||S | |||S )NzCalling CMake (z) in z with:z  - "")	r   r>   r"   rH   r%   r   r$   r~   r   )r0   rW   rY   rZ   ir1   r1   r2   
_call_impl   s   zCMakeExecutor._call_impldisable_cachec                 C  sd   |d u r	t j }|| j }|r| |||S tj}| |||}||vr.| |||||< || S rU   )osenvironcopyr'   r   r   r   ra   )r0   rW   rY   rZ   r   cachekeyr1   r1   r2   call   s   

zCMakeExecutor.callc                 C  s
   | j d uS rU   )r"   r0   r1   r1   r2   rF         
zCMakeExecutor.foundc                 C     | j S rU   )r#   r   r1   r1   r2   r         zCMakeExecutor.versionc                 C  
   | j  S rU   )r"   r*   r   r1   r1   r2   executable_path   r   zCMakeExecutor.executable_pathc                 C  r   rU   )r"   rH   r   r1   r1   r2   rH      r   zCMakeExecutor.get_commandc                 C  r   rU   )r&   r   r1   r1   r2   get_cmake_prefix_paths   r   z$CMakeExecutor.get_cmake_prefix_pathsc                 C  r   rU   )r   r   r1   r1   r2   machine_choice   r   zCMakeExecutor.machine_choice)F)r   r   r   r   r   r   r   r   )r   r   r   r   r4   r5   )r"   rC   r4   rD   r6   )r%   rS   r$   rS   r4   rT   )rW   rX   rY   r   rZ   r[   r4   r\   )rW   rX   rY   r   rZ   r[   r4   rb   )NF)
rW   rX   rY   r   rZ   r[   r   r   r4   rb   )r4   r   )r4   r   )r4   rX   )r4   r   )__name__
__module____qualname__r   r   __annotations__r   r   r3   r!   r@   rV   ra   r~   r   r   r   r   rF   r   r   rH   r   r   r1   r1   r1   r2   r   '   s(   
 
#


+







r   )$
__future__r   
subprocessru   	threadingr   typingTrL   r   rt   r   mesonlibr   r   r   r	   r
   programsr   r   TYPE_CHECKINGpathlibr   r   r   r   r   TupleintOptionalr   rb   	FrozenSetr\   r   r1   r1   r1   r2   <module>   s"   ,