o
    p.aL4                     @   s   d Z 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	Z	ej
ej
eZedejZdd ZG dd deZed	krSe	ee	jd
d  dS dS )zuUtility functions for Windows builds.

These functions are executed via gyp-win-tool when using the ninja generator.
    )print_functionNz/OUT:(?P<out>.+)$c                 C   s*   t  }|| }|d urt| d S d S N)WinToolDispatchsysexit)argsZexecutorZ	exit_code r	   ./usr/lib/python3/dist-packages/gyp/win_tool.pymain   s
   
r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd S )!r   zThis class performs all the Windows tooling steps. The methods can either
  be executed directly, or dispatched from an argument list.c              	   C   s|   t |dk r
td|d dkrdS d}|D ]}t|}|r1tddd|d	t f } nq|du r8dS ||d
< dS )zWAllows to use a unique instance of mspdbsrv.exe per linker instead of a
    shared one.   Not enough argumentsr   zlink.exeNz\W+ z%s_%doutZ_MSPDBSRV_ENDPOINT_)	len	Exception_LINK_EXE_OUT_ARGmatchresubgrouposgetpid)selfenvr   Zendpoint_nameargmr	   r	   r
   _UseSeparateMspdbsrv'   s    
zWinTool._UseSeparateMspdbsrvc                 C   s<   t |dk r
tdd| |d  }t| ||dd  S )z(Dispatches a string command to a method.r   r   zExec%sr   N)r   r   _CommandifyNamegetattr)r   r   methodr	   r	   r
   r   B   s   zWinTool.Dispatchc                 C   s   |  ddS )z@Transforms a tool name like recursive-mirror to RecursiveMirror.-r   )titlereplace)r   Zname_stringr	   r	   r
   r   J   s   zWinTool._CommandifyNamec                 C   s0   t | dd d}dd |D }t|S )z@Gets the saved environment from a file for a given architecture.N c                 S   s   g | ]}| d dqS )=r   )split).0itemr	   r	   r
   
<listcomp>T   s    z#WinTool._GetEnv.<locals>.<listcomp>)openreadr'   dict)r   archZpairsZkvsr	   r	   r
   _GetEnvN   s   zWinTool._GetEnvc                 C   s   t |d  dS )zSimple stamp command.wN)r+   close)r   pathr	   r	   r
   	ExecStampW   s   zWinTool.ExecStampc                 C   s   t j|r+t j|rdd }tj||d nt |t js&t |t	j
 t | t j|r9t|| dS t|| dS )z)Emulation of rm -rf out && cp -af in out.c                 S   s(   t |t jst |tj | | d S r   )r   accessW_OKchmodstatS_IWRITE)fnr2   excinfor	   r	   r
   	_on_error_   s   z.WinTool.ExecRecursiveMirror.<locals>._on_error)onerrorN)r   r2   existsisdirshutilrmtreer4   r5   r6   r7   r8   unlinkcopytreecopy2)r   sourcedestr;   r	   r	   r
   ExecRecursiveMirror[   s   
zWinTool.ExecRecursiveMirrorc           	      G   s   |  |}|dkr| || tjdkr"t|}|d dd|d< tj|tjdk|tjtj	d}|
 \}}| D ]}|dsP|dsP|d	sPt| q;|jS )
zFilter diagnostic output from link that looks like:
    '   Creating library ui.dll.lib and object ui.dll.exp'
    This happens when there are exports from the dll or exe.
    Truewin32r   /\shellr   stdoutstderrz   Creating library zGenerating codezFinished generating code)r/   r   r   platformlistr#   
subprocessPopenPIPESTDOUTcommunicate
splitlines
startswithprint
returncode)	r   r.   Zuse_separate_mspdbsrvr   r   linkr   _liner	   r	   r
   ExecLinkWrapperq   s&   

	
zWinTool.ExecLinkWrapperc	                 G   s^  t j|||||||d|	d	}
d}|	r1td|
  |dkr1td|
  td|
  d|
 }t||  |	rtd	|
  d
|
 }d|
 }t|d/}t|d}| dtj	}| dtj	}W d   n1 sqw   Y  W d   n1 sw   Y  ||krt
| dd }|| || || t jd|||f  dS dS dS )zZA wrapper for handling creating a manifest resource and then executing
    a link command. )	Zpythonr.   r   ldcmdresnamemtrcintermediate_manifest	manifestsr   zn%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo -manifest %(manifests)s -out:%(out)s.manifestrG   z`%(python)s gyp-win-tool manifest-to-rc %(arch)s %(out)s.manifest %(out)s.manifest.rc %(resname)szF%(python)s gyp-win-tool rc-wrapper %(arch)s %(rc)s %(out)s.manifest.rcz %(out)s.manifest.resz%(python)s gyp-win-tool manifest-wrapper %(arch)s %(mt)s -nologo -manifest %(out)s.manifest %(intermediate_manifest)s -out:%(out)s.assert.manifestz%(out)s.assert.manifestz%(out)s.manifestrNc                 S   sl   t | tjd t dtjd t| d}t | tjd t dtjd W d    d S 1 s/w   Y  d S )N)filez-----re   )rX   r   rN   r+   r,   )filenamefr	   r	   r
   dump   s   "z+WinTool.ExecLinkWithManifests.<locals>.dumpzLinker generated manifest "%s" added to final manifest "%s" (result in "%s"). Were /MANIFEST switches used in #pragma statements? r   )r   
executablejoinrQ   Z
check_callr+   r,   	translatestringZ
whitespacer   rA   rN   write)r   r.   Zembed_manifestr   r_   r`   ra   rb   rc   rd   Z	variablesZ	add_to_ldZassert_manifestZour_manifestZour_fZassert_fZour_dataZassert_datari   r	   r	   r
   ExecLinkWithManifests   sx   
zWinTool.ExecLinkWithManifestsc                 G   sV   |  |}tj|d|tjtjd}| \}}| D ]}|r'd|vr't| q|jS )zRun manifest tool with environment set. Strip out undesirable warning
    (some XML blocks are recognized by the OS loader, but not the manifest
    tool).TrK   z#manifest authoring warning 81010002)	r/   rQ   rR   rS   rT   rU   rV   rX   rY   r   r.   r   r   popenr   r[   r\   r	   r	   r
   ExecManifestWrapper   s   

zWinTool.ExecManifestWrapperc              	   G   s\   |\}}}t |d}|d|tj|ddf  W d   dS 1 s'w   Y  dS )zCreates a resource file pointing a SxS assembly manifest.
    |args| is tuple containing path to resource file, path to manifest file
    and resource name which can be "1" (for executables) or "2" (for DLLs).r0   z(#include <windows.h>
%s RT_MANIFEST "%s"rJ   rI   N)r+   rn   r   r2   abspathr#   )r   r.   r   Zmanifest_pathZresource_pathZresource_nameoutputr	   r	   r
   ExecManifestToRc   s   

"zWinTool.ExecManifestToRcc	                    s   ddgt |	 d|d|d|d|d|d||g }
| |}tj|
d	|tjtjd
}| \}}| }d t fdd|D }|D ]}|	 sP||vrPt
| qA|jS )zjFilter noisy filenames output from MIDL compile step that isn't
    quietable via command line flags.
    Zmidlz/nologoz/outz/tlbz/hz/dlldataz/iidz/proxyTrK   )zProcessing z64 bit Processing c                 3   s&    | ]}|  rtj|V  qd S r   )rW   r   r2   basename)r(   xprefixesr	   r
   	<genexpr>
  s    
z*WinTool.ExecMidlWrapper.<locals>.<genexpr>)rP   r/   rQ   rR   rS   rT   rU   rV   setrW   rX   rY   )r   r.   ZoutdirZtlbhZdlldataZiidproxyZidlflagsr   r   rq   r   r[   linesZ
processingr\   r	   rx   r
   ExecMidlWrapper   s0   

zWinTool.ExecMidlWrapperc                 G   sl   |  |}tj|d|tjtjd}| \}}| D ]}|ds2|ds2|ds2|r2t| q|j	S )z/Filter logo banner from invocations of asm.exe.TrK   #Copyright (C) Microsoft CorporationzMicrosoft (R) Macro Assemblerz Assembling: 
r/   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rp   r	   r	   r
   ExecAsmWrapper  s    


zWinTool.ExecAsmWrapperc                 G   sb   |  |}tj|d|tjtjd}| \}}| D ]}|ds-|ds-|r-t| q|j	S )zgFilter logo banner from invocations of rc.exe. Older versions of RC
    don't support the /nologo flag.TrK   z+Microsoft (R) Windows (R) Resource Compilerr   r   rp   r	   r	   r
   ExecRcWrapper  s   


zWinTool.ExecRcWrapperc                 G   s\   |  |}tj D ]\}}||vr|||< q
t| }|r#|d nd}tj|d||dS )zRuns an action command line from a response file using the environment
    for |arch|. If |dir| is supplied, use that as the working directory.r   NT)rL   r   cwd)r/   r   environitemsr+   r,   rQ   call)r   r.   Zrspfiledirr   kvr   r	   r	   r
   ExecActionWrapper-  s   
zWinTool.ExecActionWrapperc                    sJ   t j t |d} fdd|D }dg}|| tj|dtdS )zfExecuted by msvs-ninja projects when the 'ClCompile' target is used to
    build selected C/C++ files.;c                    s   g | ]}t j |d  qS )z^^)r   r2   rk   )r(   rg   project_dirr	   r
   r*   ?  s    z)WinTool.ExecClCompile.<locals>.<listcomp>z	ninja.exeT)rL   r   )r   r2   relpathBASE_DIRr'   extendrQ   r   )r   r   Zselected_filesZninja_targetscmdr	   r   r
   ExecClCompile:  s   


zWinTool.ExecClCompileN)__name__
__module____qualname____doc__r   r   r   r/   r3   rF   r]   ro   rr   ru   r   r   r   r   r   r	   r	   r	   r
   r   #   s"    	N
r   __main__r   )r   Z
__future__r   r   r   r?   rQ   r7   rm   r   r2   dirnamers   __file__r   compile
IGNORECASEr   r   objectr   r   r   argvr	   r	   r	   r
   <module>   s$     $