B
    ^ Çc/w  ã               @   sR  d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZ ddlZejZejZejZejZejejejejfZ ej!dko²ej"dkZ#e#rÆd	Z$d
Z%dZ&ndZ$dZ%dZ&dddœZ'G dd„ dƒZ(G dd„ dƒZ)dd„ Z*dd„ Z+dd„ Z,dd„ Z-d$dd„Z.e*ƒ Z/e/d k r>ed!e/ ƒ‚G d"d#„ d#eƒZ0dS )%a  distutils.msvc9compiler

Contains MSVCCompiler, an implementation of the abstract CCompiler class
for the Microsoft Visual Studio 2008.

The module is compatible with VS 2005 and VS 2008. You can find legacy support
for older versions of VS in distutils.msvccompiler.
é    N)ÚDistutilsExecErrorÚDistutilsPlatformErrorÚCompileErrorÚLibErrorÚ	LinkError)Ú	CCompilerÚgen_preprocess_optionsÚgen_lib_options)Úlog)Úget_platformÚwin32l        z1Software\Wow6432Node\Microsoft\VisualStudio\%0.1fz5Software\Wow6432Node\Microsoft\Microsoft SDKs\Windowsz,Software\Wow6432Node\Microsoft\.NETFrameworkz%Software\Microsoft\VisualStudio\%0.1fz)Software\Microsoft\Microsoft SDKs\Windowsz Software\Microsoft\.NETFrameworkÚx86Zamd64)r   z	win-amd64c               @   sP   e Zd ZdZdd„ ZeeƒZdd„ ZeeƒZdd„ ZeeƒZdd	„ Ze	eƒZd
S )ÚRegz2Helper class to read values from the registry
    c             C   s:   x,t D ]$}|  ||¡}|r||kr|| S qW t|ƒ‚d S )N)ÚHKEYSÚread_valuesÚKeyError)ÚclsÚpathÚkeyÚbaseÚd© r   ú(lib/python3.7/distutils/msvc9compiler.pyÚ	get_value@   s
    
zReg.get_valuec             C   sn   yt ||ƒ}W n tk
r"   dS X g }d}x<yt||ƒ}W n tk
rR   P Y nX | |¡ |d7 }q.W |S )zReturn list of registry keys.Nr   é   )ÚRegOpenKeyExÚRegErrorÚ
RegEnumKeyÚappend)r   r   r   ÚhandleÚLÚiÚkr   r   r   Ú	read_keysH   s    
zReg.read_keysc       	      C   s†   yt ||ƒ}W n tk
r"   dS X i }d}xTyt||ƒ\}}}W n tk
rX   P Y nX | ¡ }|  |¡||  |¡< |d7 }q.W |S )z`Return dict of registry keys and values.

        All names are converted to lowercase.
        Nr   r   )r   r   ÚRegEnumValueÚlowerÚconvert_mbcs)	r   r   r   r   r   r!   ÚnameÚvalueÚtyper   r   r   r   Z   s    zReg.read_valuesc             C   s:   t | dd ƒ}|d k	r6y|dƒ} W n tk
r4   Y nX | S )NÚdecodeÚmbcs)ÚgetattrÚUnicodeError)ÚsZdecr   r   r   r&   p   s    zReg.convert_mbcsN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr#   r   r&   Ústaticmethodr   r   r   r   r   <   s   r   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚMacroExpanderc             C   s   i | _ t| | _|  |¡ d S )N)ÚmacrosÚVS_BASEÚvsbaseÚload_macros)ÚselfÚversionr   r   r   Ú__init__|   s    
zMacroExpander.__init__c             C   s   t  ||¡| jd| < d S )Nz$(%s))r   r   r6   )r:   Zmacror   r   r   r   r   Ú	set_macro   s    zMacroExpander.set_macroc          	   C   s  |   d| jd d¡ |   d| jd d¡ |   dtd¡ y$|dkrP|   d	td
¡ ntd
ƒ‚W n tk
rv   tdƒ‚Y nX |dkr |   d| jd¡ |   dtd¡ ndd}x^tD ]V}yt||ƒ}W n tk
rÒ   wªY nX t	|dƒ}t
 |d||f ¡}|d | jd< qªW d S )NZVCInstallDirz	\Setup\VCÚ
productdirZVSInstallDirz	\Setup\VSZFrameworkDirZinstallrootg       @ZFrameworkSDKDirzsdkinstallrootv2.0a  Python was built with Visual Studio 2008;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2008 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.g      "@ZFrameworkVersionzclr versionZWindowsSdkDirZcurrentinstallfolderz.Software\Microsoft\NET Framework Setup\Productr   z%s\%sr;   z$(FrameworkVersion))r=   r8   ÚNET_BASEr   r   ÚWINSDK_BASEr   r   r   r   r   r   r6   )r:   r;   Úpr   Úhr   r   r   r   r   r9   „   s.    

zMacroExpander.load_macrosc             C   s(   x"| j  ¡ D ]\}}| ||¡}qW |S )N)r6   ÚitemsÚreplace)r:   r.   r"   Úvr   r   r   Úsub£   s    zMacroExpander.subN)r/   r0   r1   r<   r=   r9   rF   r   r   r   r   r5   z   s   r5   c              C   sš   d} t j | ¡}|dkrdS |t| ƒ }t j|d…  dd¡\}}t|dd… ƒd }|dkrf|d7 }t|d	d
… ƒd }|dkr†d}|dkr–|| S dS )zÃReturn the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    zMSC v.éÿÿÿÿé   Nú r   éþÿÿÿé   é   é   g      $@r   )Úsysr;   ÚfindÚlenÚsplitÚint)Úprefixr!   r.   ÚrestZmajorVersionZminorVersionr   r   r   Úget_build_version¨   s    rU   c             C   s4   g }x*| D ]"}t j |¡}||kr
| |¡ q
W |S )znReturn a list of normalized paths with duplicates removed.

    The current order of paths is maintained.
    )Úosr   Únormpathr   )ÚpathsZreduced_pathsrA   Znpr   r   r   Únormalize_and_reduce_pathsÁ   s    
rY   c             C   s@   |   tj¡}g }x|D ]}||kr| |¡ qW tj |¡}|S )z8Remove duplicate values of an environment variable.
    )rQ   rV   Úpathsepr   Újoin)ZvariableZoldListZnewListr!   ZnewVariabler   r   r   ÚremoveDuplicatesÏ   s    
r\   c             C   s  t |  }yt d| d¡}W n" tk
r>   t d¡ d}Y nX |rPtj |¡sÆd|  }tj	 
|d¡}|r¸tj |¡r¸tj |tjtjd¡}tj |¡}tj |¡sÆt d| ¡ dS nt d| ¡ |sØt d	¡ dS tj |d
¡}tj |¡rö|S t d¡ dS )z¨Find the vcvarsall.bat file

    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    z%s\Setup\VCr>   z%Unable to find productdir in registryNzVS%0.f0COMNTOOLSZVCz%s is not a valid directoryz Env var %s is not set or invalidzNo productdir foundzvcvarsall.batzUnable to find vcvarsall.bat)r7   r   r   r   r
   ÚdebugrV   r   ÚisdirÚenvironÚgetr[   ÚpardirÚabspathÚisfile)r;   r8   r>   ZtoolskeyZtoolsdirÚ	vcvarsallr   r   r   Úfind_vcvarsallÚ   s2    





re   c             C   s<  t | ƒ}ddddh}i }|dkr(tdƒ‚t d|| ¡ tjd||f tjtjd	}z¨| ¡ \}}| ¡ d
krzt| 	d¡ƒ‚| 	d¡}xr| 
d¡D ]d}t |¡}d|kr¨q| ¡ }| 
dd¡\}	}
|	 ¡ }	|	|kr|
 tj¡rè|
dd… }
t|
ƒ||	< qW W d|j ¡  |j ¡  X t|ƒt|ƒkr8ttt| ¡ ƒƒƒ‚|S )zDLaunch vcvarsall.bat and read the settings from its environment
    ÚincludeÚlibZlibpathr   NzUnable to find vcvarsall.batz'Calling 'vcvarsall.bat %s' (version=%s)z"%s" %s & set)ÚstdoutÚstderrr   r+   Ú
ú=r   rG   )re   r   r
   r]   Ú
subprocessÚPopenÚPIPEZcommunicateÚwaitr*   rQ   r   r&   Ústripr%   ÚendswithrV   rZ   r\   rh   Úcloseri   rP   Ú
ValueErrorÚstrÚlistÚkeys)r;   Zarchrd   ZinterestingÚresultÚpopenrh   ri   Úliner   r(   r   r   r   Úquery_vcvarsallý   s<    



rz   g       @z(VC %0.1f is not supported by this modulec            
   @   sÞ   e Zd ZdZdZi ZdgZdddgZdgZdgZ	ee e e	 Z
d	Zd
ZdZdZd ZZdZd.dd„Zd/dd„Zd0dd„Zd1dd„Zd2dd„Zd3dd„Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd4d*d+„Zd,d-„ ZdS )5ÚMSVCCompilerzwConcrete class that implements an interface to Microsoft Visual C++,
       as defined by the CCompiler abstract class.Zmsvcz.cz.ccz.cppz.cxxz.rcz.mcz.resz.objz.libz.dllz%s%sz.exer   c             C   s8   t  | |||¡ t| _d| _g | _d | _d | _d| _d S )NzSoftware\Microsoft\VisualStudioF)	r   r<   ÚVERSIONÚ_MSVCCompiler__versionZ_MSVCCompiler__rootÚ_MSVCCompiler__pathsÚ	plat_nameÚ_MSVCCompiler__archÚinitialized)r:   ÚverboseZdry_runZforcer   r   r   r<   I  s    zMSVCCompiler.__init__Nc             C   s,  | j rtdƒ‚|d krtƒ }d}||kr6td|f ƒ‚dtjkrtdtjkrt|  d¡rtd| _d| _d| _	d	| _
d
| _nÂ|tƒ ks†|dkrt| }nttƒ  d t|  }tt|ƒ}|d  tj¡| _|d tjd< |d tjd< t| jƒdkrútd| j ƒ‚|  d¡| _|  d¡| _|  d¡| _	|  d	¡| _
|  d
¡| _y,x&tjd  d¡D ]}| j |¡ qJW W n tk
rx   Y nX t| jƒ| _d | j¡tjd< d | _| jdkrÎdddddg| _ddddddg| _n&ddddddg| _dddddddg| _dddg| _| jd krddd!d"g| _dg| _d#| _ d S )$Nzdon't init multiple times)r   z	win-amd64z--plat-name must be one of %sZDISTUTILS_USE_SDKZMSSdkzcl.exezlink.exezlib.exezrc.exezmc.exer   Ú_r   rg   rf   r   zxPython was built with %s, and extensions need to be built with the same version of the compiler, but it isn't installed.ú;r   z/nologoz/Oxz/MDz/W3z/DNDEBUGz/Odz/MDdz/Z7z/D_DEBUGz/GS-z/DLLz/INCREMENTAL:NOé   z/INCREMENTAL:noz/DEBUGT) r   ÚAssertionErrorr   r   rV   r_   Úfind_exeÚccÚlinkerrg   ÚrcÚmcÚPLAT_TO_VCVARSrz   r|   rQ   rZ   r~   rP   Z_MSVCCompiler__productr   r   rY   r[   Zpreprocess_optionsr€   Úcompile_optionsÚcompile_options_debugÚldflags_sharedr}   Úldflags_shared_debugZldflags_static)r:   r   Zok_platsZ	plat_specZvc_envrA   r   r   r   Ú
initializeT  sf    







zMSVCCompiler.initializeÚ c             C   sâ   |d krd}g }xÌ|D ]Ä}t j |¡\}}t j |¡d }|t j |¡d … }|| jkrdtd| ƒ‚|rtt j |¡}|| jkrš| 	t j 
||| j ¡¡ q|| jkrÀ| 	t j 
||| j ¡¡ q| 	t j 
||| j ¡¡ qW |S )Nr’   r   zDon't know how to compile %s)rV   r   ÚsplitextÚ
splitdriveÚisabsÚsrc_extensionsr   ÚbasenameÚ_rc_extensionsr   r[   Úres_extensionÚ_mc_extensionsÚobj_extension)r:   Zsource_filenamesZ	strip_dirÚ
output_dirZ	obj_namesZsrc_namer   Úextr   r   r   Úobject_filenames©  s(     



zMSVCCompiler.object_filenamesc	             C   st  | j s|  ¡  |  ||||||¡}	|	\}}
}}}|p6g }| d¡ |rT| | j¡ n| | j¡ x|
D ]}y|| \}}W n tk
r”   whY nX |r¦tj	 
|¡}|| jkr¼d| }nT|| jkrÒd| }n>|| jkr>|}d| }y"|  | jg| |g |g ¡ W qh tk
r8 } zt|ƒ‚W d d }~X Y qhX qhnÒ|| jkr tj	 |¡}tj	 |¡}yl|  | jgd|d|g |g ¡ tj	 tj	 |¡¡\}}tj	 ||d ¡}|  | jgd| g |g ¡ W qh tk
rú } zt|ƒ‚W d d }~X Y qhX qhntd||f ƒ‚d	| }y&|  | jg| | ||g | ¡ W qh tk
rj } zt|ƒ‚W d d }~X Y qhX qhW |
S )
Nz/cz/Tcz/Tpz/foz-hz-rz.rcz"Don't know how to compile %s to %sz/Fo)r   r‘   Z_setup_compiler   ÚextendrŽ   r   r   rV   r   rb   Ú_c_extensionsÚ_cpp_extensionsr˜   ÚspawnrŠ   r   r   rš   Údirnamer‹   r“   r—   r[   rˆ   )r:   Zsourcesrœ   r6   Zinclude_dirsr]   Úextra_preargsÚextra_postargsZdependsZcompile_infoÚobjectsZpp_optsZbuildZcompile_optsÚobjÚsrcr   Z	input_optZ
output_optÚmsgZh_dirZrc_dirr   rƒ   Zrc_filer   r   r   ÚcompileÈ  sj    




zMSVCCompiler.compilec       	   
   C   sž   | j s|  ¡  |  ||¡\}}| j||d}|  ||¡rŽ|d| g }|rJy|  | jg| ¡ W qš tk
rŠ } zt|ƒ‚W d d }~X Y qšX nt	 
d|¡ d S )N)rœ   z/OUT:zskipping %s (up-to-date))r   r‘   Ú_fix_object_argsÚlibrary_filenameÚ
_need_linkr¢   rg   r   r   r
   r]   )	r:   r¦   Zoutput_libnamerœ   r]   Útarget_langÚoutput_filenameZlib_argsr©   r   r   r   Úcreate_static_lib  s    zMSVCCompiler.create_static_libc          
   C   sX  | j s|  ¡  |  ||¡\}}|  |||¡}|\}}}|rL|  dt|ƒ ¡ t| |||ƒ}|d k	rptj 	||¡}|  
||¡rH|tjkr¬|	rœ| jdd … }q¾| jdd … }n|	r¸| j}n| j}g }x|pÊg D ]}| d| ¡ qÌW || | | d| g }tj |d ¡}|d k	rPtj tj |¡¡\}}tj 	||  |¡¡}| d| ¡ |  |||¡ |
rp|
|d d…< |r€| |¡ |  tj |¡¡ y|  | jg| ¡ W n, tk
rÔ } zt|ƒ‚W d d }~X Y nX |  ||¡}|d k	rT|\}}d||f }y|  dd	d
||g¡ W n, tk
rD } zt|ƒ‚W d d }~X Y nX nt d|¡ d S )Nz5I don't know what to do with 'runtime_library_dirs': r   z/EXPORT:z/OUT:r   z/IMPLIB:z-outputresource:%s;%szmt.exez-nologoz	-manifestzskipping %s (up-to-date))r   r‘   r«   Z_fix_lib_argsÚwarnrt   r	   rV   r   r[   r­   r   Ú
EXECUTABLEr   r   r   r£   r“   r—   r¬   Úmanifest_setup_ldargsrŸ   Zmkpathr¢   r‰   r   r   Úmanifest_get_embed_infor
   r]   )r:   Útarget_descr¦   r¯   rœ   Z	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsr]   r¤   r¥   Ú
build_tempr®   Z
fixed_argsZlib_optsZldflagsZexport_optsZsymÚld_argsZdll_nameZdll_extZimplib_filer©   ZmfinfoZ
mffilenameÚmfidZout_argr   r   r   Úlink6  sl    





zMSVCCompiler.linkc             C   s,   t j |t j |¡d ¡}| d| ¡ d S )Nz	.manifestz/MANIFESTFILE:)rV   r   r[   r—   r   )r:   r¯   r¶   r·   Útemp_manifestr   r   r   r³   •  s    z"MSVCCompiler.manifest_setup_ldargsc             C   s`   x,|D ] }|  d¡r| dd¡d }P qW d S |tjkr>d}nd}|  |¡}|d krXd S ||fS )Nz/MANIFESTFILE:ú:r   rL   )Ú
startswithrQ   r   r²   Ú_remove_visual_c_ref)r:   rµ   r·   Úargrº   r¸   r   r   r   r´   ¡  s    



z$MSVCCompiler.manifest_get_embed_infoc             C   s²   y˜t |ƒ}z| ¡ }W d | ¡  X t dtj¡}t |d|¡}d}t |d|¡}t dtj¡}t ||¡d krrd S t |dƒ}z| |¡ |S | ¡  X W n t	k
r¬   Y nX d S )NzU<assemblyIdentity.*?name=("|')Microsoft\.VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)r’   z*<dependentAssembly>\s*</dependentAssembly>zI<assemblyIdentity.*?name=(?:"|')(.+?)(?:"|').*?(?:/>|</assemblyIdentity>)Úw)
ÚopenÚreadrr   Úrerª   ÚDOTALLrF   ÚsearchÚwriteÚOSError)r:   Zmanifest_fileZ
manifest_fZmanifest_bufÚpatternr   r   r   r½   ¹  s.    	


z!MSVCCompiler._remove_visual_c_refc             C   s   d| S )Nz	/LIBPATH:r   )r:   Údirr   r   r   Úlibrary_dir_optionä  s    zMSVCCompiler.library_dir_optionc             C   s   t dƒ‚d S )Nz<don't know how to set runtime library search path for MSVC++)r   )r:   rÈ   r   r   r   Úruntime_library_dir_optionç  s    z'MSVCCompiler.runtime_library_dir_optionc             C   s
   |   |¡S )N)r¬   )r:   rg   r   r   r   Úlibrary_optionë  s    zMSVCCompiler.library_optionc             C   s`   |r|d |g}n|g}xB|D ]6}x0|D ](}t j ||  |¡¡}t j |¡r(|S q(W qW d S d S )NZ_d)rV   r   r[   r¬   Úexists)r:   Údirsrg   r]   Z	try_namesrÈ   r'   Zlibfiler   r   r   Úfind_library_fileï  s    

zMSVCCompiler.find_library_filec             C   sz   x4| j D ]*}tj tj |¡|¡}tj |¡r|S qW x>tjd  d¡D ]*}tj tj |¡|¡}tj |¡rH|S qHW |S )aŒ  Return path to an MSVC executable program.

        Tries to find the program in several places: first, one of the
        MSVC program search paths from the registry; next, the directories
        in the PATH environment variable.  If any of those work, return an
        absolute path that is known to exist.  If none of them work, just
        return the original program name, 'exe'.
        ZPathr„   )r~   rV   r   r[   rb   rc   r_   rQ   )r:   ZexerA   Úfnr   r   r   r‡     s    	zMSVCCompiler.find_exe)r   r   r   )N)r   r’   )NNNr   NNN)Nr   N)
NNNNNr   NNNN)r   ) r/   r0   r1   r2   Zcompiler_typeZexecutablesr    r¡   r˜   rš   r–   r™   r›   Zstatic_lib_extensionZshared_lib_extensionZstatic_lib_formatZshared_lib_formatZexe_extensionr<   r‘   rž   rª   r°   r¹   r³   r´   r½   rÉ   rÊ   rË   rÎ   r‡   r   r   r   r   r{   +  sR   


W 
 
V  
         
R+
r{   )r   )1r2   rV   rl   rN   rÂ   Zdistutils.errorsr   r   r   r   r   Zdistutils.ccompilerr   r   r	   Z	distutilsr
   Zdistutils.utilr   ÚwinregZ	OpenKeyExr   ZEnumKeyr   Z	EnumValuer$   Úerrorr   Z
HKEY_USERSÚHKEY_CURRENT_USERÚHKEY_LOCAL_MACHINEZHKEY_CLASSES_ROOTr   ÚplatformÚmaxsizeZNATIVE_WIN64r7   r@   r?   rŒ   r   r5   rU   rY   r\   re   rz   r|   r{   r   r   r   r   Ú<module>   sL   >.#
)
