a
    äIþfô[  ã                   @   s~  d 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Zz,ddlZdZeZejZejZejZejZW nd eyà   z4ddlZddlZdZeZejZejZejZejZW n eyÚ   e d¡ Y n0 Y n0 erúejejejej fZ!d	d
„ Z"dd„ Z#dd„ Z$G dd„ dƒZ%dd„ Z&dd„ Z'dd„ Z(G dd„ de
ƒZ)e&ƒ dkrze *d¡ e)Z+ddl,m)Z) ddl,m%Z% dS )z‚distutils.msvccompiler

Contains MSVCCompiler, an implementation of the abstract CCompiler class
for the Microsoft Visual Studio.
é    N)ÚDistutilsExecErrorÚDistutilsPlatformErrorÚCompileErrorÚLibErrorÚ	LinkError)Ú	CCompilerÚgen_lib_options)ÚlogFTzWarning: Can't read registry to find the necessary compiler setting
Make sure that Python modules winreg, win32api or win32con are installed.c                 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 )zReturn list of registry keys.Nr   é   )ÚRegOpenKeyExÚRegErrorÚ
RegEnumKeyÚappend)ÚbaseÚkeyÚhandleÚLÚiÚk© r   ú'lib/python3.9/distutils/msvccompiler.pyÚ	read_keys7   s    


r   c                 C   s~   zt | |ƒ}W n ty"   Y dS 0 i }d}zt||ƒ\}}}W n tyV   Y qzY n0 | ¡ }t|ƒ|t|ƒ< |d7 }q,|S )zXReturn dict of registry keys and values.

    All names are converted to lowercase.
    Nr   r
   )r   r   ÚRegEnumValueÚlowerÚconvert_mbcs)r   r   r   Údr   ÚnameÚvalueÚtyper   r   r   Úread_valuesH   s    

r   c                 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   ]   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 | _ |  |¡ d S ©N)ÚmacrosÚload_macros)ÚselfÚversionr   r   r   Ú__init__g   s    zMacroExpander.__init__c                 C   s2   t D ](}t||ƒ}|r|| | jd| <  q.qd S )Nz$(%s))ÚHKEYSr   r'   )r)   ZmacroÚpathr   r   r   r   r   r   Ú	set_macrok   s
    
zMacroExpander.set_macroc           
   
   C   sø   d| }|   d|d d¡ |   d|d d¡ d}|   d|d	¡ z*|d
krX|   d|d¡ n|   d|d¡ W n, ty” } ztdƒ‚W Y d }~n
d }~0 0 d}tD ]T}zt||ƒ}W n tyÆ   Y qžY n0 t|dƒ}t|d||f ƒ}	|	d | jd< qžd S )Nz%Software\Microsoft\VisualStudio\%0.1fZVCInstallDirz	\Setup\VCZ
productdirZVSInstallDirz	\Setup\VSz Software\Microsoft\.NETFrameworkZFrameworkDirZinstallrootg      @ZFrameworkSDKDirzsdkinstallrootv1.1Zsdkinstallroota  Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.z.Software\Microsoft\NET Framework Setup\Productr   z%s\%sr*   z$(FrameworkVersion))	r.   ÚKeyErrorr   r,   r   r   r   r   r'   )
r)   r*   ZvsbaseZnetÚexcÚpr   Úhr   r   r   r   r   r(   r   s,    ÿ

zMacroExpander.load_macrosc                 C   s$   | j  ¡ D ]\}}| ||¡}q
|S r&   )r'   ÚitemsÚreplace)r)   r$   r   Úvr   r   r   ÚsubŽ   s    zMacroExpander.subN)Ú__name__Ú
__module__Ú__qualname__r+   r.   r(   r6   r   r   r   r   r%   f   s   r%   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    rH   c                  C   s@   d} t j | ¡}|dkrdS t j d|¡}t j|t| ƒ |… S )zUReturn the processor architecture.

    Possible results are "Intel" or "AMD64".
    z bit (r:   ÚIntelú))rA   r*   rB   rC   )rF   r   Újr   r   r   Úget_build_architecture¬   s    rL   c                 C   s0   g }| D ]"}t j |¡}||vr| |¡ q|S )znReturn a list of normalized paths with duplicates removed.

    The current order of paths is maintained.
    )Úosr-   Únormpathr   )ÚpathsZreduced_pathsr1   Znpr   r   r   Únormalize_and_reduce_paths¹   s    rP   c                
   @   sÔ   e Zd 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„ Zd,dd„Zd-dd„Zd.dd„Zd/dd„Zdd„ Zdd„ Zd d!„ Zd0d"d#„Zd$d%„ Zd1d'd(„Zd)d*„ ZdS )2ÚMSVCCompilerzwConcrete class that implements an interface to Microsoft Visual C++,
       as defined by the CCompiler abstract class.Zmsvcz.c)z.ccz.cppz.cxxú.rcz.mcz.resz.objz.libz.dllz%s%sz.exer   c                 C   sv   t  | |||¡ tƒ | _tƒ | _| jdkr\| jdkrHd| _t| jƒ| _nd| _d| j | _	nd| jd  | _	d| _
d S )	NrI   é   zSoftware\Microsoft\VisualStudiozSoftware\Microsoft\DevstudiozVisual Studio version %szMicrosoft SDK compiler %sr;   F)r   r+   rH   Ú_MSVCCompiler__versionrL   Ú_MSVCCompiler__archÚ_MSVCCompiler__rootr%   Ú_MSVCCompiler__macrosÚ_MSVCCompiler__productÚinitialized)r)   ÚverboseZdry_runZforcer   r   r   r+   æ   s    

zMSVCCompiler.__init__c                 C   sŽ  g | _ dtjv rDdtjv rD|  d¡rDd| _d| _d| _d| _d| _nx|  	d¡| _ t
| j ƒd	krltd
| j ƒ‚|  d¡| _|  d¡| _|  d¡| _|  d¡| _|  d¡| _|  d¡ |  d¡ z&tjd  d¡D ]}| j  |¡ qÎW n tyô   Y n0 t| j ƒ| _ d | j ¡tjd< d | _| jdkr<g d¢| _g d¢| _ng d¢| _g d¢| _g d¢| _| jdkrrg d¢| _n
g d¢| _dg| _d| _d S )NZDISTUTILS_USE_SDKZMSSdkzcl.exezlink.exezlib.exezrc.exezmc.exer-   r   zxPython was built with %s, and extensions need to be built with the same version of the compiler, but it isn't installed.ÚlibZincludeú;rI   )ú/nologoú/Oxú/MDú/W3ú/GXú/DNDEBUG)r]   ú/Odú/MDdr`   ra   ú/Z7ú/D_DEBUG)r]   r^   r_   r`   ú/GS-rb   )r]   rc   rd   r`   rg   re   rf   )ú/DLLr]   z/INCREMENTAL:NOrS   )rh   r]   ú/INCREMENTAL:noú/DEBUG)rh   r]   ri   z	/pdb:Nonerj   r]   T)Ú_MSVCCompiler__pathsrM   ÚenvironÚfind_exeÚccÚlinkerr[   ÚrcÚmcÚget_msvc_pathsrC   r   rX   Úset_path_env_varrD   r   r/   rP   ÚjoinZpreprocess_optionsrU   Úcompile_optionsÚcompile_options_debugÚldflags_sharedrT   Úldflags_shared_debugZldflags_staticrY   )r)   r1   r   r   r   Ú
initializeø   sL    ý






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 )Nrz   r
   zDon't know how to compile %s)rM   r-   ÚsplitextÚ
splitdriveÚisabsÚsrc_extensionsr   ÚbasenameÚ_rc_extensionsr   rt   Úres_extensionÚ_mc_extensionsÚobj_extension)r)   Zsource_filenamesZ	strip_dirÚ
output_dirZ	obj_namesZsrc_namer   Úextr   r   r   Úobject_filenames8  s,    

ÿ
ÿÿzMSVCCompiler.object_filenamesNc	                 C   st  | j s|  ¡  |  ||||||¡}	|	\}}
}}}|p6g }| d¡ |rT| | j¡ n| | j¡ |
D ]}z|| \}}W n ty   Y qdY n0 |r¢tj	 
|¡}|| jv r¸d| }nX|| jv rÎd| }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-rrR   z"Don't know how to compile %s to %sz/Fo)rY   ry   Z_setup_compiler   Úextendrv   ru   r/   rM   r-   ÚabspathÚ_c_extensionsÚ_cpp_extensionsr€   Úspawnrp   r   r   r‚   Údirnamerq   r{   r   rt   rn   )r)   Zsourcesr„   r'   Zinclude_dirsÚdebugÚ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   Ú_Zrc_filer   r   r   ÚcompileW  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 }~qœd }~0 0 nt	 
d|¡ d S )N)r„   ú/OUT:úskipping %s (up-to-date))rY   ry   Ú_fix_object_argsÚlibrary_filenameÚ
_need_linkr‹   r[   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   sÔ  | j s|  ¡  |  ||¡\}}|  |||¡}|\}}}|rL|  dt|ƒ ¡ t| |||ƒ}|d urptj 	||¡}|  
||¡rÄ|tjkr¬|	rœ| jdd … }q¾| jdd … }n|	r¸| j}n| j}g }|pÈg D ]}| d| ¡ qÊ|| | | d| g }|d urHtj tj |¡¡\}}tj 	tj |d ¡|  |¡¡}| d| ¡ |
rZ|
|d d…< |rj| |¡ |  tj |¡¡ z|  | jg| ¡ W n. tyÀ } 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:r—   )rY   ry   r˜   Z_fix_lib_argsÚwarnÚstrr   rM   r-   rt   rš   r   Z
EXECUTABLErx   rw   r   r{   r   rŒ   r™   r‡   Zmkpathr‹   ro   r   r   r	   r   )r)   Ztarget_descr   rœ   r„   Z	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsr   rŽ   r   Z
build_tempr›   Z
fixed_argsZlib_optsZldflagsZexport_optsZsymZld_argsZdll_nameZdll_extZimplib_filer“   r   r   r   ÚlinkÅ  sh    ÿ
ÿþ

ÿÿ

ÿþ
 zMSVCCompiler.linkc                 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)   r[   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)rM   r-   rt   r™   Úexists)r)   Údirsr[   r   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 )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'.
        ÚPathr\   )rk   rM   r-   rt   rˆ   Úisfilerl   rD   )r)   Zexer1   Úfnr   r   r   rm   5  s    	


zMSVCCompiler.find_exeÚx86c                 C   sÂ   t sg S |d }| jdkr,d| j| jf }nd| j|f }tD ]H}t||ƒ}|r>| jdkrt| j || ¡ d¡  S ||  d¡  S q>| jdkr¾tD ]&}t|d| j ƒdur–|  d	¡  q¾q–g S )
zÐGet a list of devstudio directories (include, lib or path).

        Return a list of strings.  The list will be empty if unable to
        access the registry or appropriate registry keys not found.
        z dirsrS   z6%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directoriesz?%s\6.0\Build System\Components\Platforms\Win32 (%s)\Directoriesr\   r;   z%s\6.0Nz´It seems you have Visual Studio 6 installed, but the expected registry settings are not present.
You must at least run the Visual Studio GUI once so that these entries are created.)	Ú_can_read_regrT   rV   r,   r   rW   r6   rD   rž   )r)   r-   Úplatformr   r   r   r   r   r   rr   K  s,    

ÿÿ



zMSVCCompiler.get_msvc_pathsc                 C   s6   |dkr|   d¡}n
|   |¡}|r2d |¡tj|< dS )z¡Set environment variable 'name' to an MSVC path type value.

        This is equivalent to a SET command prior to execution of spawned
        commands.
        r[   Zlibraryr\   N)rr   rt   rM   rl   )r)   r   r1   r   r   r   rs   o  s
    
zMSVCCompiler.set_path_env_var)r   r   r   )r   rz   )NNNr   NNN)Nr   N)
NNNNNr   NNNN)r   )r¬   )r7   r8   r9   Ú__doc__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+   ry   r†   r•   r   r    r£   r¤   r¥   r¨   rm   rr   rs   r   r   r   r   rQ   È   s`   ÿÿ
B  ý
   þ
X   û
          ó
S

$rQ   g       @z3Importing new compiler from distutils.msvc9compiler)rQ   )r%   )-r¯   rA   rM   Zdistutils.errorsr   r   r   r   r   Zdistutils.ccompilerr   r   Z	distutilsr	   r­   ÚwinregZhkey_modZ	OpenKeyExr   ZEnumKeyr   Z	EnumValuer   Úerrorr   ÚImportErrorZwin32apiZwin32conÚinfoZ
HKEY_USERSÚHKEY_CURRENT_USERÚHKEY_LOCAL_MACHINEZHKEY_CLASSES_ROOTr,   r   r   r   r%   rH   rL   rP   rQ   r   ZOldMSVCCompilerZdistutils.msvc9compilerr   r   r   r   Ú<module>   s`   



ý	-   9
