o
    bƽ                     @   s2  d 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Z	ddl
Z	ze W n ey3   eZY nw e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G dd deZdd ZG dd deZda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 )/zg
This module helps emulate Visual Studio 2008 behavior on top of other
build systems, primarily ninja.
    N)
OrderedSetz(\\*)"c                 C   sP   |  ddks| ddkrtj| } tdd | } | dd} d|  d S )	zQuote a command line argument so that it appears as one argument when
  processed via cmd.exe and parsed by CommandLineToArgvW (as is typical for
  Windows programs)./r      c                 S   s   d|  d d S )N   r   \")group)mo r	   4/usr/lib/python3/dist-packages/gyp/msvs_emulation.py<lambda>/   s    z!QuoteForRspFile.<locals>.<lambda>%z%%")findcountospathnormpathwindows_quoter_regexsubreplace)argr	   r	   r
   QuoteForRspFile   s
   r   c                 C   st   | sdS | d  dr | d dd\}}|d tj| }ntj| d }|d ddd | dd D  S )	z6Process a list of arguments using QuoteCmdExeArgument. r   zcall  r   c                 s   s    | ]}t |V  qd S N)r   ).0r   r	   r	   r
   	<genexpr>M       z$EncodeRspFileList.<locals>.<genexpr>N)
startswithsplitr   r   r   join)argscallprogramr	   r	   r
   EncodeRspFileList@   s   $r$   c                 C   s.   | s|S |s| S t | |d ||dd S )zGiven a list of dictionary keys |path| and a tree of dicts |root|, find
  value at path, or return |default| if any of the path doesn't exist.r   r   N)_GenericRetrieveget)rootdefaultr   r	   r	   r
   r%   P   s
   r%   c                    s<   | du r| S t | tjrt | ts fdd| D S  |  S )zDAdd |prefix| to |element| or each subelement if element is iterable.Nc                    s   g | ]} | qS r	   r	   )r   eprefixr	   r
   
<listcomp>`       z_AddPrefix.<locals>.<listcomp>)
isinstancecollectionsIterable
basestring)elementr+   r	   r*   r
   
_AddPrefixZ   s   r3   c                    s\    dur,| dur,t  s j t| tjr(t| ts(td fdd| D } | S  | } | S )zIf |element| then remap it through |map|. If |element| is iterable then
  each item will be remapped. Any elements not found will be removed.Nc                    s   g | ]} |qS r	   r	   )r   elemmapr	   r
   r,   m   r-   z _DoRemapping.<locals>.<listcomp>)callabler&   r.   r/   r0   r1   filter)r2   r6   r	   r5   r
   _DoRemappinge   s   r9   c                 C   sF   | dur!|dur!t |tjrt |ts| | dS | | dS |S )zIf |append| is None, simply return |element|. If |append| is not None,
  then add |element| to it, adding each item in |element| if it's a list or
  tuple.N)r.   r/   r0   r1   extendappend)r;   r2   r	   r	   r
   _AppendOrReturns   s   r<   c                  C   sv   t tdrtjS tjd} | s6g d}tj|tjtjd}|	 d 
 D ]}d|v r5|dd d	 } q&| t_| S )
zTry to find an installation location for the DirectX SDK. Check for the
  standard environment variable, and if that doesn't exist, try to find
  via the registry. May return None if not found in either location.	dxsdk_dir	DXSDK_DIR)zreg.exequeryzHKLM\Software\Microsoft\DirectXz/s)stdoutstderrr   InstallPathz       \)hasattr_FindDirectXInstallationr=   r   environr&   
subprocessPopenPIPEcommunicate
splitlinesr   )r=   cmdpliner	   r	   r
   rF      s   
rF   c                 C   s^   i }|   r|   |d< tj|   dd |d< t }|r |nd|d< tjdd|d< |S )	zGet a dict of variables mapping internal VS macro names to their gyp
  equivalents. Returns all variables that are independent of the target.z$(VSInstallDir)VCrD   z$(VCInstallDir)r   z$(DXSDK_DIR)WDK_DIRz
$(WDK_DIR))Pathr   r   r    rF   rG   r&   )
vs_versionenvr=   r	   r	   r
   GetGlobalVSMacroEnv   s   rU   c                    s   t | d dg  | dd D ]}|dg } t |@  q s#dS tt|t fdd D }tdd |D r?dS | D ]}|dg }|rX fdd|D }||d< qA|S )	zFinds msvs_system_include_dirs that are common to all targets, removes
  them from all targets, and returns an OrderedSet containing them.r   msvs_system_include_dirsr   Nc                    s   g | ]}t | qS r	   )ExpandMacrosr   include)rT   r	   r
   r,      s    z3ExtractSharedMSVSSystemIncludes.<locals>.<listcomp>c                 S   s   g | ]}d |v qS )$r	   rX   r	   r	   r
   r,      r-   c                    s   g | ]}| vr|qS r	   r	   )r   i)all_system_includesr	   r
   r,          )r   r&   rU   GetVSVersionany)configsgenerator_flagsconfigsystem_includesexpanded_system_includesincludesnew_includesr	   )r\   rT   r
   ExtractSharedMSVSSystemIncludes   s*   rg   c                   @   s|  e Zd ZdZdd Zdd ZdZddZdZd	d
Zdd Zdd Z	G dd de
Zd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"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dS )\MsvsSettingszA class that understands the gyp 'msvs_...' values (especially the
  msvs_settings field). They largely correpond to the VS2008 IDE DOM. This
  class helps map those settings to command line options.c              	   C   s   || _ t|| _dtfdtfdtfdtfdtfdtfdtfdtfg}|d	 }|D ]\}}t| |i  | D ]\}}||| t	| ||< q6q(|d
dg| _
ddg}	g }
|	D ]}| D ]}||v ro|
d||d f g7 }
q^qX|
rztd|
d S )Nmsvs_configuration_attributesmsvs_settingsrV   msvs_disabled_warningsmsvs_precompiled_headermsvs_precompiled_sourcemsvs_configuration_platformmsvs_target_platformconfigurationsmsvs_cygwin_dirs.msvs_prebuildmsvs_postbuildz%s not supported (target %s).target_name
)specr^   rS   dictliststrsetattritemsr&   getattrrq   values	Exceptionr    )selfrw   ra   supported_fieldsr`   fieldr(   
confignamerb   unsupported_fieldsunsupportedr	   r	   r
   __init__   sB   


zMsvsSettings.__init__c                 C   s,   | j dd}|r|S tjj| j d dS )zReturns the extension for the target, with no leading dot.

    Uses 'product_extension' if specified, otherwise uses MSVS defaults based on
    the target type.
    product_extensionNtyper   )rw   r&   gypMSVSUtilTARGET_TYPE_EXT)r   extr	   r	   r
   GetExtension   s   zMsvsSettings.GetExtensionNc           	      C   s   |  |dkr	dnd}| jdd| jd| jd  }|r"|d nd}d	|   }|| }d
dd||d| jd ||||tj||d}|t| j	 |S )zUGet a dict of variables mapping internal VS macro names to their gyp
    equivalents.x86Win32x64product_prefixr   product_nameru   rD   rr   z${root}z	${source}z$!INTERMEDIATE_DIR)z$(InputName)z$(InputPath)z	$(IntDir)z
$(OutDir)\z$(PlatformName)z$(ProjectDir)\z$(ProjectName)z$(TargetDir)\z$(TargetExt)z$(TargetFileName)z$(TargetName)z$(TargetPath))
GetArchrw   r&   r   r   r   r    updaterU   rS   )	r   base_to_buildrb   target_platformru   
target_dir
target_exttarget_file_namereplacementsr	   r	   r
   GetVSMacroEnv   s,   zMsvsSettings.GetVSMacroEnvc                 C   s   | j ||d}t||S )z4Convert from VS macro names to something equivalent.rb   )r   rW   )r   sr   rb   rT   r	   r	   r
   ConvertVSMacros  s   
zMsvsSettings.ConvertVSMacrosc                 C   s   dd |D }dd |D S )z2Strip -l from library if it's specified with that.c                 S   s&   g | ]}| d r|dd n|qS )z-lr   Nr   r   libr	   r	   r
   r,     s   & z0MsvsSettings.AdjustLibraries.<locals>.<listcomp>c                 S   s4   g | ]}|  d s|  ds|d  n|qS )z.libz.objlowerendswithr   r	   r	   r
   r,     s    
r	   )r   	librarieslibsr	   r	   r
   AdjustLibraries  s   zMsvsSettings.AdjustLibrariesc                 C   s*   t |||}t||}t||}t||S )a  Retrieve a value from |field| at |path| or return |default|. If
    |append| is specified, and the item is found, it will be appended to that
    object instead of returned. If |map| is specified, results will be
    remapped through |map| before being returned or appended.)r%   r9   r3   r<   )r   r   r   r(   r+   r;   r6   resultr	   r	   r
   _GetAndMunge   s   


zMsvsSettings._GetAndMungec                   @   s    e Zd ZdddZdddZdS )	zMsvsSettings._GetWrapperNc                 C   s   || _ || _|g| _|| _d S r   )parentr   	base_pathr;   )r   r   r   r   r;   r	   r	   r
   r   +  s   
z!MsvsSettings._GetWrapper.__init__r   c                 C   s$   | j j| j| j|g ||| j|dS )N)r(   r+   r;   r6   )r   r   r   r   r;   )r   namer6   r+   r(   r	   r	   r
   __call__0  s   
z!MsvsSettings._GetWrapper.__call__r   )Nr   N)__name__
__module____qualname__r   r   r	   r	   r	   r
   _GetWrapper*  s    
r   c                 C   s6   | j |d}| j|d}|s|}ddd|dS )zrGet architecture based on msvs_configuration_platform and
    msvs_target_platform. Returns either 'x86' or 'x64'.r   r   r   )r   r   )rn   r&   ro   )r   rb   configuration_platformplatformr	   r	   r
   r   4  s
   zMsvsSettings.GetArchc                 C   sZ   | j jdkr|S | |}|dkr|ds|d7 }|dkr+|dr+|ddd }|S )z*Returns the target-specific configuration.i  r   _x64r   _r   r   )rS   
short_namer   r   rsplit)r   rb   archr	   r	   r
   _TargetConfig>  s   
zMsvsSettings._TargetConfigr   c                 C      |  | j| |||||S )z_GetAndMunge for msvs_settings.)r   rj   r   r   rb   r(   r+   r;   r6   r	   r	   r
   _SettingP  s   zMsvsSettings._Settingc                 C   r   )z/_GetAndMunge for msvs_configuration_attributes.)r   ri   r   r	   r	   r
   _ConfigAttribV  s   
zMsvsSettings._ConfigAttribc                    B      |j   }|jd g d  fdd|D S )zyUpdates include_dirs to expand VS specific paths, and adds the system
    include dirs used for platform SDK and similar.)VCCLCompilerToolAdditionalIncludeDirectoriesr(   c                       g | ]	}j | d qS r   r   r   rN   rb   r   r	   r
   r,   d      z2MsvsSettings.AdjustIncludeDirs.<locals>.<listcomp>r   rV   r:   r   )r   include_dirsrb   re   r	   r   r
   AdjustIncludeDirs]     
zMsvsSettings.AdjustIncludeDirsc                    r   )z~Updates midl_include_dirs to expand VS specific paths, and adds the
    system include dirs used for platform SDK and similar.)
VCMIDLToolr   r   c                    r   r   r   r   r   r	   r
   r,   m  r   z6MsvsSettings.AdjustMidlIncludeDirs.<locals>.<listcomp>r   )r   midl_include_dirsrb   re   r	   r   r
   AdjustMidlIncludeDirsf  r   z"MsvsSettings.AdjustMidlIncludeDirsc                 C   s`   |  |}g }| dg|dkr|d | dg|dkr#|d || jd|g d |S )z`Returns the set of defines that are injected to the defines list based
    on other VS settings.CharacterSet1)_UNICODEUNICODE2_MBCS)r   PreprocessorDefinitionsr   )r   r   r:   r;   r   )r   rb   definesr	   r	   r
   GetComputedDefineso  s   


zMsvsSettings.GetComputedDefinesc                 C   s,   |  |}| d|}|r|| |}|S )zvGet the pdb file name that should be used for compiler invocations, or
    None if there's no explicit name specified.)r   ProgramDataBaseFileNamer   r   r   )r   rb   expand_specialpdbnamer	   r	   r
   GetCompilerPdbName|  s   
zMsvsSettings.GetCompilerPdbNamec                 C   0   |  |}| d|}|r|| j||d}|S )z]Gets the explicitly overriden map file name for a target or returns None
    if it's not set.)VCLinkerToolMapFileNamer   r   )r   rb   r   map_filer	   r	   r
   GetMapFileName  s
   
zMsvsSettings.GetMapFileNamec                 C   sN   |  |}| jd }|dkrdnd}| |df|}|r%|| j||d}|S )zcGets the explicitly overridden output name for a target or returns None
    if it's not overridden.r   static_libraryVCLibrarianToolr   
OutputFiler   )r   rw   r   r   )r   rb   r   r   r'   output_filer	   r	   r
   GetOutputName  s   

zMsvsSettings.GetOutputNamec                 C   sH   |  |}| d|}| d|}|dkr"|r || j||dS |S dS )zGets the explicitly overridden pdb name for a target or returns
    default if it's not overridden, or if no pdb will be generated.)r   ProgramDatabaseFile)r   GenerateDebugInformationtruer   Nr   )r   rb   r   r(   r   generate_debug_infor	   r	   r
   
GetPDBName  s   
zMsvsSettings.GetPDBNamec                 C      |  |}| d|}|dkS )z\If NoImportLibrary: true, ninja will not expect the output to include
    an import library.)NoImportLibraryr   r   r   )r   rb   noimplibr	   r	   r
   GetNoImportLibrary     
zMsvsSettings.GetNoImportLibraryc                 C   s0   |  |}g }| d|}|dkr|d |S )z:Returns the flags that need to be added to ml invocations.)MASMUseSafeExceptionHandlersr   z/safeseh)r   r   r;   )r   rb   asmflagssafesehr	   r	   r
   GetAsmflags  s   

zMsvsSettings.GetAsmflagsc                 C   s~  |  |}g }|dd | j| D  | j| | j| d|d}|ddddd	d
ddd |ddd |ddd |dddid |dddid |dddddd |dddddd |d d!d"d#dd |d$d%d&d'd(d)d*d |d+dd,dd |d-dd.id |d/d0d |d1dd2id |d3dd4d5d6d
d7d |d8d9d:d;d<d=d |d>d?d@dAd |dBdCdDdAd |dEddFid |dGdHdIdAd |dJd"dKddLdMd |dNdOdPdQdRd
dSd |dTdUdVd#dWd |dXddYid |dZdddd[d |d\dd]id |d^dd |d_d`dadbdcdddedfd |dgd | jdh|g diD  | jjdjkr6|dk dld |D }|S )mzCReturns the flags that need to be added to .c and .cc compilations.c                 S      g | ]}d | qS )/wdr	   )r   wr	   r	   r
   r,     r-   z*MsvsSettings.GetCflags.<locals>.<listcomp>r   r;   Optimizationdr   r   x)0r   r   3z/Or6   r+   r(   InlineFunctionExpansionz/Obr*   DisableSpecificWarningsr   StringPoolingr   z/GFr5   EnableFiberSafeOptimizationsz/GTOmitFramePointers-r   falser   z/Oyr6   r+   EnableIntrinsicFunctionsz/OiFavorSizeOrSpeedtr   r   r   FloatingPointModelprecisestrictfastr   r   r   z/fp:r   CompileAsManagedz/clrWholeProgramOptimizationz/GLWarningLevelz/WWarnAsError/WXCallingConventionrzvz/GDebugInformationFormat7r[   I)r   r   4z/ZRuntimeTypeInfoz/GRz/GR-r   r  EnableFunctionLevelLinkingz/Gyz/Gy-MinimalRebuildz/GmBufferSecurityCheckz/GSz/GS-BasicRuntimeChecksu)r   r   r   z/RTCRuntimeLibraryTTdDDdz/MExceptionHandlingscaz/EHDefaultCharIsUnsignedz/JTreatWChar_tAsBuiltInTypez/Zc:wchar_tEnablePREfastz/analyzeAdditionalOptionsEnableEnhancedInstructionSetSSESSE2AVXIA32AVX2)r   r   r   r  5z/arch:c                 S   r   )/FIr	   r   fr	   r	   r
   r,     r-   )r   ForcedIncludeFilesr   g      (@z/FSc                 S   s   g | ]	}| d s|qS )z/MPr   r   r   r	   r	   r
   r,     r   )	r   r:   rk   r   rj   r   rS   project_versionr;   )r   rb   cflagsclr	   r	   r
   	GetCflags  sp   


zMsvsSettings.GetCflagsc                 C   sl   |  |}| j| r4tj| j| d }t||r4| j| }tj|d }d| d| d| d gS g S )zLGet the flags to be added to the cflags for precompiled header support.
    r   z/Yur:  z/Fp${pchprefix}.z.pch)r   rl   r   r   splitextrm   _LanguageMatchesForPchr   )r   rb   	extension
source_extpchpchbaser	   r	   r
   _GetPchFlags  s   



zMsvsSettings._GetPchFlagsc                 C   s   |  |}| |dS )z;Returns the flags that need to be added to .c compilations..cr   rI  r   rb   r	   r	   r
   
GetCflagsC   s   
zMsvsSettings.GetCflagsCc                 C   s   |  |}dg| |d S )z<Returns the flags that need to be added to .cc compilations.z/TP.ccrK  rL  r	   r	   r
   GetCflagsCC  s   
zMsvsSettings.GetCflagsCCc                    sB      j|df g d} fdd|D }dd |D S )zPGet and normalize the list of paths in AdditionalLibraryDirectories
    setting.AdditionalLibraryDirectoriesr   c              	      &   g | ]}t jj| d qS r   r   r   r   r   r   rb   gyp_to_build_pathr   r	   r
   r,     
    zAMsvsSettings._GetAdditionalLibraryDirectories.<locals>.<listcomp>c                 S   s   g | ]}d | d qS )z
/LIBPATH:"r   r	   r   r	   r	   r
   r,     r]   r   )r   r'   rb   rT  libpathsr	   rS  r
    _GetAdditionalLibraryDirectories
  s   

z-MsvsSettings._GetAdditionalLibraryDirectoriesc                 C   sl   |  |}g }| j| | j| d|d}|| d|| |dddid |ddd	d
ddd |d |S )z8Returns the flags that need to be added to lib commands.r   r   LinkTimeCodeGenerationr   /LTCGr5   TargetMachineX86X64ARMr   17r   	/MACHINE:r	  r2  )r   r   rj   r:   rW  )r   rb   rT  libflagsr   r	   r	   r
   GetLibFlags  s   
zMsvsSettings.GetLibFlagsc                 C   sX   | j }|d dv r*dd |dg D }t|dkr ||d S t|dkr*tdd	S )
zDReturns the .def file from sources, if any.  Otherwise returns None.r   )shared_libraryloadable_module
executablec                 S   s   g | ]}|  d r|qS )z.defr   r   r   r	   r	   r
   r,   '  s    
z+MsvsSettings.GetDefFile.<locals>.<listcomp>sourcesr   r   zMultiple .def filesN)rw   r&   lenr   )r   rT  rw   	def_filesr	   r	   r
   
GetDefFile#  s   zMsvsSettings.GetDefFilec                 C   s$   |  |}|r|d|  dS dS )z.def files get implicitly converted to a ModuleDefinitionFile for the
    linker in the VS generator. Emulate that behaviour here.z	/DEF:"%s"N)rj  r;   )r   ldflagsrT  def_filer	   r	   r
   _GetDefFileAsLdflags/  s   
z!MsvsSettings._GetDefFileAsLdflagsc                 C   r   )z`Gets the explicitly overridden pgd name for a target or returns None
    if it's not overridden.)r   ProfileGuidedDatabaser   r   )r   rb   r   r   r	   r	   r
   
GetPGDName6  s   
zMsvsSettings.GetPGDNamec                 C   sp  |  |}g }| j| | j| d|d}	| || |	dddid |	ddd	d
ddd || d|| |	ddd |	dddddd | ||}
|
rU|d|
  | |||d }|rg|d|  | 	||}|rv|d|  | 
||}|	dd|rd| ndid |	dddid |	d dd | jd!|dd"}|rd#| }|	d$d%| d&| d'd(d | jd)|dd"}|r| jd*|dd"}|rd#| }|d+||f  |	d,ddd'd-d |	d.ddd'd/d |	d0d1d |	d2ddd'd3d |	d4ddd'd5d |	d6ddd'd7d |	d8d9d:d'd;d |	d<d=d |	d>d?d@d'd;d |	dAddBdCdDdEdFd |	dGdHd |	dIddJid |	dKdLd |	dMddNid |	dOddd'dPd |	dQdd | |dRkrjd}ndS}|	dTdddUdV|dW tdXdY |D s|d5 tdZdY |D s|d7 td[dY |D }| ||||o| |\}}}|| |||fS )\zUReturns the flags that need to be added to link commands, and the
    manifest files.r   r   r   r   z/DEBUGr5   rZ  r[  r\  r]  r^  r`  r	  DelayLoadDLLsz/DELAYLOAD:r*   TreatLinkerWarningAsErrorsr  r   z:NOr!  r+   r6   z/OUT:z.pdbz/PDB:z/PGD:GenerateMapFilez/MAP:z/MAP
MapExportsz/MAPINFO:EXPORTSr2  )r   MinimumRequiredVersionr   ,	SubSystemz	CONSOLE%sz	WINDOWS%sr  z/SUBSYSTEM:)r   StackReserveSize)r   StackCommitSizez/STACK:%s%sTerminalServerAwarez/TSAWARELinkIncrementalz/INCREMENTALBaseAddressz/BASE:FixedBaseAddress/FIXEDRandomizedBaseAddressz/DYNAMICBASEDataExecutionPreventionz	/NXCOMPATOptimizeReferencesNOREFREFz/OPT:ForceSymbolReferencesz	/INCLUDE:EnableCOMDATFoldingNOICFICFrX  z:PGINSTRUMENTz:PGOPTIMIZEz	:PGUPDATE)r   r   r   r  rY  IgnoreDefaultLibraryNamesz/NODEFAULTLIB:ResourceOnlyDLLz/NOENTRYEntryPointSymbolz/ENTRY:Profilez/PROFILELargeAddressAwarez/LARGEADDRESSAWAREAdditionalDependenciesr   NImageHasSafeExceptionHandlersr  z/SAFESEHr   c                 s   s     | ]}d |v p|dkV  qdS )DYNAMICBASEr~  Nr	   r   flagr	   r	   r
   r     s    z*MsvsSettings.GetLdflags.<locals>.<genexpr>c                 s   s    | ]}d |v V  qdS )NXCOMPATNr	   r  r	   r	   r
   r     r   c                 s   s    | ]}| d V  qdS )z/DEF:Nr   r  r	   r	   r
   r     s    )r   r   rj   rm  r:   rW  r   r;   r   ro  r   r   r   r_   _GetLdManifestFlags)r   rb   rT  r   manifest_base_nameoutput_nameis_executable	build_dirrk  ldoutpdbpgdr   minimum_required_versionstack_reserve_sizestack_commit_sizesafeseh_defaulthave_def_filemanifest_flagsintermediate_manifestmanifest_filesr	   r	   r
   
GetLdflagsA  s   









zMsvsSettings.GetLdflagsc                 C   s$  | j d|dd}|dkrdgg g fS |d }dd| g}|d | |}| j d	|dd}	g }
d
}|	dkrU| j d|dd}dddd}| j d|dd}d|| |f }nd}|| }|d }tj||}tj| tj	|}|
| |  |g}
|r|d |
| ||7 }
|||
fS )aj  Returns a 3-tuple:
    - the set of flags that need to be added to the link to generate
      a default manifest
    - the intermediate manifest that the linker will generate that should be
      used to assert it doesn't add anything to the merged one.
    - the list of all the manifest files to be merged by the manifest tool and
      included into the link.)r   GenerateManifestr   r   z/MANIFEST:NOz.intermediate.manifestz	/MANIFESTz/ManifestFile:z/MANIFESTUAC:NO)r   	EnableUACz<?xml version='1.0' encoding='UTF-8' standalone='yes'?><assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>%s</assembly>)r   UACExecutionLevelr   	asInvokerhighestAvailablerequireAdministratorr  )r   UACUIAccessr  z
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  <security>
    <requestedPrivileges>
      <requestedExecutionLevel level='%s' uiAccess='%s' />
    </requestedPrivileges>
  </security>
</trustInfo>r   z.generated.manifestz/ALLOWISOLATION)r   r;   r   r   r   r    r   commonEnsureDirExistsWriteOnDiffwriteclose_GetAdditionalManifestFiles)r   rb   r   rT  allow_isolationr  generate_manifestr  flags
enable_uacr  generated_manifest_outerexecution_levelexecution_level_map	ui_accessinnergenerated_manifest_contentsgenerated_namebuild_dir_generated_namer<  r	   r	   r
   r    s^   	


	


z MsvsSettings._GetLdManifestFlagsc                    s:   j d g d}t|tr|d} fdd|D S )z]Gets additional manifest files that are added to the default one
    generated by the linker.)VCManifestToolAdditionalManifestFilesr   ;c              	      rQ  r   rR  r;  rS  r	   r
   r,     rU  z<MsvsSettings._GetAdditionalManifestFiles.<locals>.<listcomp>)r   r.   rz   r   )r   rb   rT  filesr	   rS  r
   r    s   

z(MsvsSettings._GetAdditionalManifestFilesc                 C   r   )zzReturns whether the target should be linked via Use Library Dependency
    Inputs (using component .objs of a given .lib).)r   UseLibraryDependencyInputsr   r   )r   rb   uldir	   r	   r
   IsUseLibraryDependencyInputs  r   z)MsvsSettings.IsUseLibraryDependencyInputsc                 C   s"   |  |}| jd|dd}|dkS )z6Returns whether manifest should be linked into binary.)r  EmbedManifestr   r   r   )r   rb   embedr	   r	   r
   IsEmbedManifest  s
   
zMsvsSettings.IsEmbedManifestc                 C   s   |  |}| d|}|dkS )z:Returns whether the target should be linked incrementally.)r   r{  r   r   )r   rb   link_incr	   r	   r
   IsLinkIncremental  s   
zMsvsSettings.IsLinkIncrementalc                 C   sh   |  |}g }| j| | j| d|d}|d|dd |d|d  |ddd	 |d
ddd d |S )zTReturns the flags that need to be added to invocations of the resource
    compiler.VCResourceCompilerToolr   r   z/Ir	  rr   r   z/dr*   Culturez/lc                 S   s   t t| dd  S )Nr   )hexintr   r	   r	   r
   r   '  r-   z)MsvsSettings.GetRcflags.<locals>.<lambda>rr  )r   r   rj   r;   )r   rb   gyp_to_ninja_pathrcflagsrcr	   r	   r
   
GetRcflags  s   
zMsvsSettings.GetRcflagsc                 C   sj   t jt j|| jd }d| dd}dd |D }dd |D }d|}d	| d
||f  }|S )aO  Build a command line that runs args via cygwin bash. We assume that all
    incoming paths are in Windows normpath'd form, so they need to be
    converted to posix style for the part of the command line that's passed to
    bash. We also have to do some Visual Studio macro emulation here because
    various rules use magic VS names for things. Also note that rules that
    contain ninja variables cannot be fixed here (for example ${source}), so
    the outer generator needs to make sure that the paths that are written out
    are in posix style, if the command line will be used here.r   zcd %srD   r   c                 S   s    g | ]}| d d ddqS )rD   r   r   r   r   r   r.  r	   r	   r
   r,   6  s     z;MsvsSettings.BuildCygwinBashCommandLine.<locals>.<listcomp>c                 S   s   g | ]
}d | dd qS )z'%s''z'\''r  r  r	   r	   r
   r,   7      r   z1call "%s\setup_env.bat" && set CYGWIN=nontsec && zbash -c "%s ; %s")r   r   r   r    rq   r   )r   r!   path_to_base
cygwin_dircdbash_cmdrM   r	   r	   r
   BuildCygwinBashCommandLine*  s   	

z'MsvsSettings.BuildCygwinBashCommandLinec                 C   s   t |d| jdddkS )zkDetermine if an action should be run under cygwin. If the variable is
    unset, or set to 1 we use cygwin.msvs_cygwin_shellr   r   )r  r&   rw   )r   ruler	   r	   r
   IsRuleRunUnderCygwin>  s
   z!MsvsSettings.IsRuleRunUnderCygwinc                 C   s(   | dg D ]}|d |kr dS qdS )zADetermine if there's an explicit rule for a particular extension.rulesrE  TFr&   )r   rw   rE  r  r	   r	   r
   _HasExplicitRuleForExtensionD  s
   z)MsvsSettings._HasExplicitRuleForExtensionc                 C   s   t dd |dg D S )z:Determine if an action should not run midl for .idl files.c                 S   s   g | ]}| d dqS )explicit_idl_actionr   r  )r   actionr	   r	   r
   r,   M  s    z7MsvsSettings._HasExplicitIdlActions.<locals>.<listcomp>actions)r_   r&   r   rw   r	   r	   r
   _HasExplicitIdlActionsK  s   
z#MsvsSettings._HasExplicitIdlActionsc                 C   s   |  |dp
| |S )zDetermine if there's an explicit rule or action for idl files. When
    there isn't we need to generate implicit rules to build MIDL .idl files.idl)r  r  r  r	   r	   r
   HasExplicitIdlRulesOrActionsP  s   z)MsvsSettings.HasExplicitIdlRulesOrActionsc                 C   s   |  |dS )zDetermine if there's an explicit rule for asm files. When there isn't we
    need to generate implicit rules to assemble .asm files.asm)r  r  r	   r	   r
   HasExplicitAsmRulesV  s   z MsvsSettings.HasExplicitAsmRulesc                    s      j  dd fdd	}|ddd}|dd	d}|d
dd}|ddd}|ddd}|ddd}	||||g}
d|fd|fd|fd|fd|fg} dkr\dnd}ddd|dg}|	|
||fS )zDetermine the implicit outputs for an idl file. Returns output
    directory, outputs, and variables and flags that are required.r   Nc                    s   j | |d dS )Nr   r   r   )r   r(   rb   midl_getr   r	   r
   midl`  s   z*MsvsSettings.GetIdlBuildData.<locals>.midlTypeLibraryNamez${root}.tlbr   HeaderFileNamez	${root}.hDLLDataFileNamez	dlldata.cInterfaceIdentifierFileNamez${root}_i.cProxyFileNamez${root}_p.cOutputDirectoryr   tlbhdlldataiidproxyr   win32r   z/charsignedz/envz/Oicfr   )r   r   rj   r   )r   sourcerb   r  r  headerr  r  r  outdiroutput	variablesr   r  r	   r  r
   GetIdlBuildData[  s&   
zMsvsSettings.GetIdlBuildData)NN)Nr   NN)0r   r   r   __doc__r   r   r   r   r   r   objectr   r   r   r   r   r   r   r   r   r   r   r   r   r   rB  rI  rM  rO  rW  rb  rj  rm  ro  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r	   r	   r	   r
   rh      s^    #






		
		6eWrh   c                 C   s(   d}d}| |v r||v p| |v o||v S )N)rJ  )rN  z.cxxz.cppr	   )rF  pch_source_extc_extscc_extsr	   r	   r
   rD  w  s
   rD  c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )PrecompiledHeaderzHelper to generate dependencies and build rules to handle generation of
  precompiled headers. Interface matches the GCH handler in xcode_emulation.py.
  c           	      C   sJ   || _ || _| j j| j }||| _tj|\}}|||  | _d S r   )	settingsrb   rm   
pch_sourcer   r   rC  r   
output_obj)	r   r  rb   rT  gyp_to_unique_outputobj_extr  filenamer   r	   r	   r
   r     s   
zPrecompiledHeader.__init__c                 C   s   | j j| j S )zMGet the header that will appear in an #include line for all source
    files.)r  rl   rb   )r   r	   r	   r
   
_PchHeader  s   zPrecompiledHeader._PchHeaderc                 C   s`   |du sJ |   sg S tj| jd }|D ]}ttj|d |r-dd| jfg  S qg S )a  Given a list of sources files and the corresponding object files,
    returns a list of the pch files that should be depended upon. The
    additional wrapping in the return value is for interface compatibility
    with make.py on Mac, and xcode_emulation.py.Nr   )r  r   r   rC  r  rD  r	  )r   rg  objsr   pch_extr  r	   r	   r
   GetObjDependencies  s   z$PrecompiledHeader.GetObjDependenciesc                 C   s   g S )zNot used on Windows as there are no additional build steps required
    (instead, existing steps are modified in GetFlagsModifications below).r	   )r   r   r	   r	   r
   GetPchBuildCommands  s   z%PrecompiledHeader.GetPchBuildCommandsc           	      C   sj   || j kr0d|   g}|dkrdt||| fg| jg fS |dkr0dt||| fg| jg fS g ||fS )zgGet the modified cflags and implicit dependencies that should be used
    for the pch compilation step.z/Yccxx	cflags_cccccflags_c)r  r  r6   r	  )	r   inputr  implicitcommandr  r  r   
pch_outputr	   r	   r
   GetFlagsModifications  s   

z'PrecompiledHeader.GetFlagsModificationsN)	r   r   r   r  r   r  r  r  r  r	   r	   r	   r
   r  ~  s    	r  c                 C   s    t stjj| dddda t S )Nmsvs_versionautoF)allow_fallback)rS   r   MSVSVersionSelectVisualStudioVersionr&   )ra   r	   r	   r
   r^     s   
r^   c                 C   s   t | }| S r   )r^   SetupScript)ra   r   vsr	   r	   r
   _GetVsvarsSetupArgs  s   r"  c                 C   s:   d| v r|  D ]\}}d|vsJ || ||} q| S )z{Expand $(Variable) per expansions dict. See MsvsSettings.GetVSMacroEnv
  for the canonical way to retrieve a suitable dict.rZ   z$()r|   r   )string
expansionsoldnewr	   r	   r
   rW     s
   rW   c                 C   s   d}i }|  ddkrtd|  |  D ]1}|D ],}t|d | rE|dd\}}|dkr=tj	t
jtj | }||| <  nqqdD ]}||vrUtd| qI|S )	z{Extracts environment variables required for the toolchain to run from
  a textual dump output by the cmd.exe 'set' command.)	zgoma_.*rY   r   libpathr   pathext
systemroottemptmp=r   z#Invalid output_of_set. Value is:
%sr   r   )
SYSTEMROOTTEMPTMPz:Environment variable "%s" required to be set to valid path)r   r   rL   rematchr   r   r   r   dirnamesysre  pathsepupper)output_of_setenvvars_to_saverT   rO   envvarvarsettingrequiredr	   r	   r
   _ExtractImportantEnvironment  s*   
r<  c                 C   s:   d}d}|   D ]\}}||d | | 7 }q||7 }|S )zFormat as an 'environment block' directly suitable for CreateProcess.
  Briefly this is a list of key=value , terminated by an additional  . See
  CreateProcess documentation for more details.r    r,  )r|   )envvar_dictblocknulkeyvaluer	   r	   r
   _FormatAsEnvironmentBlock  s   rC  c                 C   s8   |    D ]}|dr|tdd     S qdS )zGets the path to cl.exe based on the output of calling the environment
  setup batch file, followed by the equivalent of `where`.zLOC:N)striprL   r   rh  )output_of_whererO   r	   r	   r
   _ExtractCLPath  s
   
rF  c                 C   s:  d}| ddri }|D ]}d||< q|S t|}i }|D ]}}||}|d tj|dtjtjd}	|	 \}
}|	j	dkrHt
d||	j	f t|
}|rb|t| d	d
dB }d||d	< t|}|tj| d| d}|| |  ||}|d tj|dtjd}	|	 \}}t|||< q|S )a  It's not sufficient to have the absolute path to the compiler, linker,
  etc. on Windows, as those tools rely on .dlls being in the PATH. We also
  need to support both x86 and x64 compilers within the same build (to support
  msvs_target_platform hackery). Different architectures require a different
  compiler binary, and different supporting environment variables (INCLUDE,
  LIB, LIBPATH). So, we extract the environment here, wrap all invocations
  of compiler tools (cl, link, lib, rc, midl, etc.) via win_tool.py which
  sets up the environment, and then we do not prefix the compiler with
  an absolute path, instead preferring something like "cl.exe" in the rule
  which will then run whichever the environment setup has put in the path.
  When the following procedure to generate environment files does not
  meet your requirement (e.g. for custom toolchains), you can pass
  "-G ninja_use_custom_environment_files" to the gyp to suppress file
  generation and use custom environment files prepared by yourself.)r   r   "ninja_use_custom_environment_filesr   zcl.exe)&&setT)shellr@   rA   z"%s" failed with error %dINCLUDEr   r  zenvironment.r   )rH  forz%iinz(cl.exe)doz@echozLOC:%~$PATH:i)rJ  r@   )r&   r^   r   r:   rH   rI   rJ   STDOUTrK   
returncoder   r<  r   r   r    rC  r   r   r  r  rF  )toplevel_build_dirra   rc   open_outarchscl_pathsr   r!  r!   popenr  r   rT   	env_blockr<  r  r	   r	   r
   GenerateEnvironmentFiles   sB   






rW  c                    sl   t |ddr2tdd | } fdd|D }dd |D }|r4dd |D }td	d
| dS dS )ae  Emulate behavior of msvs_error_on_missing_sources present in the msvs
  generator: Check that all regular source files, i.e. not created at run time,
  exist on disk. Missing files cause needless recompilation when building via
  VS, and we want this check to match for people/bots that build using ninja,
  so they're not surprised when the VS build fails.msvs_error_on_missing_sourcesr   c                 S   s   d| vS )NrZ   r	   r  r	   r	   r
   r   >  s    z&VerifyMissingSources.<locals>.<lambda>c                    s   g | ]}t j |qS r	   )r   r   r    rf  r  gyp_to_ninjar	   r
   r,   ?  s    z(VerifyMissingSources.<locals>.<listcomp>c                 S   s   g | ]
}t j|s|qS r	   )r   r   existsr>  r	   r	   r
   r,   @  r  c                 S   s   g | ]}t j|qS r	   )r   r   r   r>  r	   r	   r
   r,   D  r]   zMissing input files:
%srv   N)r  r&   r8   r   r    )rg  r  ra   rZ  no_specialsrelativemissing
cleaned_upr	   rY  r
   VerifyMissingSources7  s   r`  c                 C   s`   | di }tj|}| | d< dtj ddv s$dtj ddv r*d| d< d S d	| d< d S )
Nra   MSVS_VERSION64PROCESSOR_ARCHITECTUREr   PROCESSOR_ARCHITEW6432@   MSVS_OS_BITS    )r&   r   msvs_emulationr^   	ShortNamer   rG   )default_variablesparamsra   r  r	   r	   r
   CalculateCommonVariablesI  s   rl  )'r  r/   r   r0  rH   r3  
gyp.commonr   gyp.MSVSUtilr   gyp.MSVSVersionr1   	NameErrorrz   compiler   r   r$   r%   r3   r9   r<   rF   rU   rg   r  rh   rD  r  rS   r^   r"  rW   r<  rC  rF  rW  r`  rl  r	   r	   r	   r
   <module>   sT   
"
     54	%7