a
    zev                     @   sJ  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 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oej dkZ!e!rd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 r6ed e- G d!d" d"eZ.dS )$    N)DistutilsExecErrorDistutilsPlatformErrorCompileErrorLibError	LinkError)	CCompiler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                   @   sL   e 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 )
Regc                 C   s:   t D ](}| ||}|r||v r||   S qt|d S N)HKEYSread_valuesKeyError)clspathkeybased r   -/usr/lib/python3.9/distutils/msvc9compiler.py	get_value?   s
    zReg.get_valuec                 C   sj   zt ||}W n ty"   Y d S 0 g }d}zt||}W n tyP   Y qfY n0 || |d7 }q,|S Nr      )RegOpenKeyExRegError
RegEnumKeyappend)r   r   r   handleLikr   r   r   	read_keysG   s    


zReg.read_keysc           	      C   s   zt ||}W n ty"   Y d S 0 i }d}zt||\}}}W n tyV   Y q~Y n0 | }| ||| |< |d7 }q,|S r   )r   r   RegEnumValuelowerconvert_mbcs)	r   r   r   r!   r   r#   namevaluetyper   r   r   r   Y   s    

zReg.read_valuesc                 C   s8   t | dd }|d ur4z|d} W n ty2   Y n0 | S )Ndecodembcs)getattrUnicodeError)sZdecr   r   r   r(   o   s    zReg.convert_mbcsN)	__name__
__module____qualname__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 r   )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   r7   )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 z$|dkrP|  d	td
 ntd
W n tyt   tdY n0 |dkr|  d| jd |  dtd n`d}tD ]V}zt||}W n ty   Y qY n0 t	|d}t
|d||f }|d | jd< qd S )NZVCInstallDirz	\Setup\VC
productdirZVSInstallDirz	\Setup\VSZFrameworkDirZinstallroot       @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>   r9   NET_BASEr   r   WINSDK_BASEr   r   r   r   r   r   r7   )r;   r<   pr   hr   r   r   r   r   r:      s2    


zMacroExpander.load_macrosc                 C   s$   | j  D ]\}}|||}q
|S r   )r7   itemsreplace)r;   r0   r$   vr   r   r   sub   s    zMacroExpander.subN)r1   r2   r3   r=   r>   r:   rH   r   r   r   r   r6   y   s   r6   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rd}|dkr|| S d S )NzMSC v.    r            g      $@r   )sysr<   findlensplitint)prefixr#   r0   restZmajorVersionZminorVersionr   r   r   get_build_version   s    rW   c                 C   s0   g }| D ]"}t j|}||vr|| q|S r   )osr   normpathr    )pathsZreduced_pathsrC   Znpr   r   r   normalize_and_reduce_paths   s    r[   c                 C   s<   |  tj}g }|D ]}||vr|| qtj|}|S r   )rS   rX   pathsepr    join)variableZoldListZnewListr#   ZnewVariabler   r   r   removeDuplicates   s    r_   c                 C   s  t |  }ztd| d}W n  ty<   td d }Y n0 |rNtj|sd|  }tj	
|d }|rtj|rtj|tjtjd}tj|}tj|std|  d S ntd|  |std d S tj|d	}tj|r|S td
 d S )Nz%s\Setup\VCr?   z%Unable to find productdir in registryzVS%0.f0COMNTOOLSZVCz%s is not a valid directoryz Env var %s is not set or invalidzNo productdir foundzvcvarsall.batUnable to find vcvarsall.bat)r8   r   r   r   r	   debugrX   r   isdirenvirongetr]   pardirabspathisfile)r<   r9   r?   ZtoolskeyZtoolsdir	vcvarsallr   r   r   find_vcvarsall   s4    




ri   c                 C   sH  t | }h d}i }|d u r$tdtd||  tjd||f tjtjd}z| \}}| dkrvt|	d|	d}|
dD ]d}t|}d	|vrq| }|
d	d
\}	}
|	 }	|	|v r|
tjr|
d d }
t|
||	< qW |j  |j  n|j  |j  0 t|t|krDttt| |S )N>   includelibr   Zlibpathr`   z'Calling 'vcvarsall.bat %s' (version=%s)z"%s" %s & set)stdoutstderrr   r-   
=r   rI   )ri   r   r	   ra   
subprocessPopenPIPEcommunicatewaitr,   rS   r   r(   stripr'   endswithrX   r\   r_   rl   closerm   rR   
ValueErrorstrlistkeys)r<   Zarchrh   Zinterestingresultpopenrl   rm   liner   r*   r   r   r   query_vcvarsall   sB    



r   r@   z(VC %0.1f is not supported by this modulec                
   @   s   e Zd ZdZi ZdgZg d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d-ddZd.ddZd/ddZd0ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd1d'd(Zd)d* ZdS )2MSVCCompilerZmsvcz.c)z.ccz.cppz.cxx.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;   verbosedry_runforcer   r   r   r=   H  s    zMSVCCompiler.__init__Nc                 C   s  |d u rt  }d}||vr(td|f dtjv rfdtjv rf| drfd| _d| _d| _d| _d	| _	n|t  ksx|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rtd| j | d| _| d| _| d| _| d| _| d	| _	z(tjd dD ]}| j| q:W n tyd   Y n0 t| j| _d| jtjd< d | _| jdkrg d| _g d| _ng d| _g d| _g d| _| jdkrg d| _dg| _d| _d S )Nr   z--plat-name must be one of %sZDISTUTILS_USE_SDKZMSSdkzcl.exezlink.exezlib.exezrc.exezmc.exer   _r   rk   rj   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   )/nologo/Ox/MD/W3/DNDEBUG)r   /Od/MDdr   /Z7/D_DEBUG)r   r   r   r   /GS-r   )r   r   r   r   r   r   r   )/DLLr   z/INCREMENTAL:NO   )r   r   z/INCREMENTAL:noz/DEBUGr   T)r
   r   rX   rc   find_execclinkerrk   rcmcPLAT_TO_VCVARSr   r   rS   r\   r   rR   Z_MSVCCompiler__productr    r   r[   r]   Zpreprocess_optionsr   compile_optionscompile_options_debugldflags_sharedr   ldflags_shared_debugZldflags_staticr   )r;   r   Zok_platsZ	plat_specZvc_envrC   r   r   r   
initializeS  sb    






zMSVCCompiler.initialize c                 C   s   |d u rd}g }|D ]}t j|\}}t j|d }|t j|d  }|| jvrbtd| |rrt j|}|| jv r|	t j
||| j  q|| jv r|	t j
||| j  q|	t j
||| j  q|S )Nr   r   zDon't know how to compile %s)rX   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 |
D ]}z|| \}}W n ty   Y qdY n0 |rtj	
|}|| jv rd| }nX|| jv rd| }nB|| jv r<|}d| }z"| | jg| |g |g  W qd ty6 } zt|W Y d }~qdd }~0 0 qdn|| jv r tj	|}tj	|}zl| | jgd|d|g |g  tj	tj	|\}}tj	||d }| | jgd| g |g  W qd ty } zt|W Y d }~qdd }~0 0 qdntd||f d	| }z&| | jg| | ||g |  W qd tyl } zt|W Y d }~qdd }~0 0 qd|
S )
Nz/cz/Tcz/Tpz/foz-hz-rr   z"Don't know how to compile %s to %sz/Fo)r   r   Z_setup_compiler    extendr   r   r   rX   r   rf   _c_extensions_cpp_extensionsr   spawnr   r   r   r   dirnamer   r   r   r]   r   )r;   Zsourcesr   r7   Zinclude_dirsra   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  s    











 zMSVCCompiler.compilec           	   
   C   s   | j s|   | ||\}}| j||d}| ||r|d| g }|rJz| | jg|  W q ty } zt|W Y d }~qd }~0 0 nt	
d| d S )N)r   /OUT:skipping %s (up-to-date))r   r   _fix_object_argslibrary_filename
_need_linkr   rk   r   r   r	   ra   )	r;   r   Zoutput_libnamer   ra   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 urptj	||}| 
||rH|tjkr|	r| jdd  }q| jdd  }n|	r| j}n| j}g }|pg D ]}|d|  q|| | | d| g }tj|d }|d urLtjtj|\}}tj	|| |}|d|  | ||| |
rl|
|d d< |r||| | tj| z| | jg|  W n. ty } zt|W Y d }~n
d }~0 0 | ||}|d urT|\}}d||f }z| dd	d
||g W n. tyD } zt|W Y d }~n
d }~0 0 ntd| d S )Nz5I don't know what to do with 'runtime_library_dirs': r   z/EXPORT:r   r   z/IMPLIB:z-outputresource:%s;%szmt.exez-nologoz	-manifestr   )r   r   r   Z_fix_lib_argswarnry   r   rX   r   r]   r   r   
EXECUTABLEr   r   r    r   r   r   r   manifest_setup_ldargsr   Zmkpathr   r   r   r   manifest_get_embed_infor	   ra   )r;   target_descr   r   r   Z	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsra   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   link5  s    








 zMSVCCompiler.linkc                 C   s,   t j|t j|d }|d|  d S )Nz	.manifest/MANIFESTFILE:)rX   r   r]   r   r    )r;   r   r   r   temp_manifestr   r   r   r     s
    z"MSVCCompiler.manifest_setup_ldargsc                 C   s^   |D ]"}| dr|ddd } q,qd S |tjkr<d}nd}| |}|d u rVd S ||fS )Nr   :r   rN   )
startswithrS   r   r   _remove_visual_c_ref)r;   r   r   argr   r   r   r   r   r     s    


z$MSVCCompiler.manifest_get_embed_infoc                 C   s   zt |}z| }W |  n
|  0 tdtj}t|d|}d}t|d|}tdtj}t||d u r|W d S t |d}z|| |W |  W S |  0 W n t	y   Y n0 d S )NzU<assemblyIdentity.*?name=("|')Microsoft\.VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)r   z*<dependentAssembly>\s*</dependentAssembly>zI<assemblyIdentity.*?name=(?:"|')(.+?)(?:"|').*?(?:/>|</assemblyIdentity>)w)
openreadrw   rer   DOTALLrH   searchwriteOSError)r;   Zmanifest_fileZ
manifest_fZmanifest_bufpatternr   r   r   r     s6    	


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   runtime_library_dir_option  s    z'MSVCCompiler.runtime_library_dir_optionc                 C   s
   |  |S r   )r   )r;   rk   r   r   r   library_option  s    zMSVCCompiler.library_optionc                 C   s\   |r|d |g}n|g}|D ]:}|D ]0}t j|| |}t j|r$|    S q$qd S )NZ_d)rX   r   r]   r   exists)r;   dirsrk   ra   Z	try_namesr   r)   Zlibfiler   r   r   find_library_file  s    zMSVCCompiler.find_library_filec                 C   sz   | j D ].}tjtj||}tj|r|  S qtjd dD ].}tjtj||}tj|rF|  S qF|S )NPathr   )r   rX   r   r]   rf   rg   rc   rS   )r;   ZexerC   fnr   r   r   r      s    	


zMSVCCompiler.find_exe)r   r   r   )N)r   r   )NNNr   NNN)Nr   N)
NNNNNr   NNNN)r   )r1   r2   r3   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   *  s`   

W  
   
X   
          
_+
r   )r   )/rX   rp   rP   r   Zdistutils.errorsr   r   r   r   r   Zdistutils.ccompilerr   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_WIN64r8   rB   rA   r   r   r6   rW   r[   r_   ri   r   r   r   r   r   r   r   <module>   sN   >.#
)
