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)argsexecutor	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out_MSPDBSRV_ENDPOINT_)	len	Exception_LINK_EXE_OUT_ARGmatchresubgrouposgetpid)selfenvr   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   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   archpairskvsr   r   r   _GetEnvN   s   zWinTool._GetEnvc                 C   s   t |d  dS )zSimple stamp command.wN)r0   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)fnr9   excinfor   r   r   	_on_error_   s   z.WinTool.ExecRecursiveMirror.<locals>._on_error)onerrorN)r   r9   existsisdirshutilrmtreer;   r<   r=   r>   r?   unlinkcopytreecopy2)r   sourcedestrB   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)r6   r!   r   platformlistr'   
subprocessPopenPIPESTDOUTcommunicate
splitlines
startswithprint
returncode)	r   r3   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. )	pythonr3   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.manifestrN   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-----rn   )r_   r   rU   r0   r1   )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joinrX   
check_callr0   r1   	translatestring
whitespacer   rH   rU   write)r   r3   embed_manifestr   rh   ri   rj   rk   rl   rm   	variables	add_to_ldassert_manifestour_manifestour_fassert_four_dataassert_datarr   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).TrR   z#manifest authoring warning 81010002)	r6   rX   rY   rZ   r[   r\   r]   r_   r`   r   r3   r   r   popenr   rc   rd   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).r7   z(#include <windows.h>
%s RT_MANIFEST "%s"rQ   rP   N)r0   ry   r   r9   abspathr'   )r   r3   r   manifest_pathresource_path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.
    midlz/nologoz/outz/tlbz/hz/dlldataz/iidz/proxyTrR   )zProcessing z64 bit Processing c                 3   s&    | ]}|  rtj|V  qd S r   )r^   r   r9   basename)r-   xprefixesr   r   	<genexpr>
  s    
z*WinTool.ExecMidlWrapper.<locals>.<genexpr>)rW   r6   rX   rY   rZ   r[   r\   r]   setr^   r_   r`   )r   r3   outdirtlbhdlldataiidproxyidlflagsr   r   r   r   rc   lines
processingrd   r   r   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.TrR   #Copyright (C) Microsoft CorporationzMicrosoft (R) Macro Assemblerz Assembling: 
r6   rX   rY   rZ   r[   r\   r]   r^   r_   r`   r   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.TrR   z+Microsoft (R) Windows (R) Resource Compilerr   r   r   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)rS   r   cwd)r6   r   environitemsr0   r1   rX   call)r   r3   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   r9   rt   )r-   rp   project_dirr   r   r/   ?  s    z)WinTool.ExecClCompile.<locals>.<listcomp>z	ninja.exeT)rS   r   )r   r9   relpathBASE_DIRr,   extendrX   r   )r   r   selected_filesninja_targetscmdr   r   r   ExecClCompile:  s   


zWinTool.ExecClCompileN)__name__
__module____qualname____doc__r!   r   r"   r6   r:   rM   re   r   r   r   r   r   r   r   r   r   r   r   r   r   #   s"    	N
r   __main__r   )r   
__future__r   r   r   rF   rX   r>   rw   r   r9   dirnamer   __file__r   compile
IGNORECASEr   r   objectr   r   r   argvr   r   r   r   <module>   s$     $