U
    A`\                     @   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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 nh ek
r   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k
r   ed Y nX Y nX 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re+d e*Z,ddl-m*Z* ddl-m&Z& dS )zdistutils.msvccompiler

Contains MSVCCompiler, an implementation of the abstract CCompiler class
for the Microsoft Visual Studio.
    N)DistutilsExecErrorDistutilsPlatformErrorCompileErrorLibError	LinkError)	CCompilergen_preprocess_options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   sn   zt | |}W n tk
r$   Y dS X g }d}zt||}W n tk
rT   Y qjY nX || |d7 }q.|S )zReturn list of registry keys.Nr      )RegOpenKeyExRegError
RegEnumKeyappend)basekeyhandleLik r   ,/usr/lib/python3.8/distutils/msvccompiler.py	read_keys7   s    


r   c                 C   s   zt | |}W n tk
r$   Y dS X i }d}zt||\}}}W n tk
rZ   Y q~Y nX | }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   s:   t | dd }|d k	r6z|d} W n tk
r4   Y nX | 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k
r } ztdW 5 d }~X Y nX d}tD ]V}zt||}W n tk
r   Y qY nX t|d}t|d||f }	|	d | jd< qd 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)   r7   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rd}|dkr|| S dS )zReturn 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    rI   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))rB   r+   rC   rD   )rG   r   jr   r   r   get_build_architecture   s    rM   c                 C   s0   g }| D ]"}t j|}||kr|| q|S )znReturn a list of normalized paths with duplicates removed.

    The current order of paths is maintained.
    )osr.   normpathr   )pathsZreduced_pathsr2   Znpr   r   r   normalize_and_reduce_paths   s    rQ   c                
   @   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 Zd.ddZd/ddZd0ddZd1ddZdd Zd d! Zd"d# Zd2d$d%Zd&d' Zd3d)d*Zd+d, ZdS )4MSVCCompilerzwConcrete class that implements an interface to Microsoft Visual C++,
       as defined by the CCompiler abstract class.Zmsvcz.cz.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 )	NrJ      zSoftware\Microsoft\VisualStudiozSoftware\Microsoft\DevstudiozVisual Studio version %szMicrosoft SDK compiler %sr<   F)r   r,   rI   _MSVCCompiler__versionrM   _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krDdtjk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 | qW n tk
r   Y nX t| j | _ d| j tjd< d | _| jdkrPddddddg| _d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rddddg| _ndddddg| _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;rJ   z/nologoz/Oxz/MDz/W3z/GXz/DNDEBUGz/Odz/MDdz/Z7z/D_DEBUGz/GS-z/DLLz/INCREMENTAL:NOrT   z/INCREMENTAL:noz/DEBUGz	/pdb:NoneT)_MSVCCompiler__pathsrN   environfind_execclinkerr\   rcmcget_msvc_pathsrD   r   rY   set_path_env_varrE   r   r0   rQ   joinZpreprocess_optionsrV   compile_optionscompile_options_debugldflags_sharedrU   ldflags_shared_debugZldflags_staticrZ   )r*   r2   r   r   r   
initialize   sr    



 

        zMSVCCompiler.initialize c                 C   s   |d krd}g }|D ]}t j|\}}t j|d }|t j|d  }|| jkrbtd| |rrt j|}|| jkr|	t j
||| j  q|| jkr|	t j
||| j  q|	t j
||| j  q|S )Nrm   r   zDon't know how to compile %s)rN   r.   splitext
splitdriveisabssrc_extensionsr   basename_rc_extensionsr   rg   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   sp  | j s|   | ||||||}	|	\}}
}}}|p6g }|d |rT|| j n|| j |
D ]}z|| \}}W n tk
r   Y qdY nX |rtj	
|}|| jkrd| }nT|| jkrd| }n>|| jkr<|}d| }z"| | jg| |g |g  W qd tk
r6 } zt|W 5 d }~X Y qdX qdn|| jkrt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k
r } zt|W 5 d }~X Y qdX qdntd||f d	| }z&| | jg| | ||g |  W qd tk
rh } zt|W 5 d }~X Y qdX qd|
S )
Nz/cz/Tcz/Tpz/foz-hz-rrS   z"Don't know how to compile %s to %sz/Fo)rZ   rl   Z_setup_compiler   extendri   rh   r0   rN   r.   abspath_c_extensions_cpp_extensionsrs   spawnrc   r   r   ru   dirnamerd   rn   rr   rg   ra   )r*   Zsourcesrw   r(   Zinclude_dirsdebugextra_preargsextra_postargsZdependsZcompile_infoobjectsZpp_optsZbuildZcompile_optsobjsrcrx   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k
r } zt|W 5 d }~X Y qX nt	
d| d S )N)rw   /OUT:skipping %s (up-to-date))rZ   rl   _fix_object_argslibrary_filename
_need_linkr~   r\   r   r   r
   r   )	r*   r   Zoutput_libnamerw   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 k	rptj	||}| 
||r|tjkr|	r| jdd  }q| jdd  }n|	r| j}n| j}g }|pg D ]}|d|  q|| | | d| g }|d k	rHtjtj|\}}tj	tj|d | |}|d|  |
rZ|
|d d< |rj|| | tj| z| | jg|  W n, tk
r } zt|W 5 d }~X Y nX ntd| d S )Nz5I don't know what to do with 'runtime_library_dirs': r   z/EXPORT:r   r   z/IMPLIB:r   )rZ   rl   r   Z_fix_lib_argswarnstrr	   rN   r.   rg   r   r   Z
EXECUTABLErk   rj   r   rn   rr   r   r   rz   Zmkpathr~   rb   r   r   r
   r   )r*   Ztarget_descr   r   rw   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  sj    
 




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)rN   r.   rg   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]   )r^   rN   r.   rg   r{   isfiler_   rE   )r*   Zexer2   fnr   r   r   r`   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rtD ]&}t|d| j dk	r| d	  qqg S )
zGet 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 dirsrT   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.0NzIt 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_regrU   rW   r-   r    rX   r7   rE   r   )r*   r.   platformr   r   r   r   r   r   re   K  s,    





zMSVCCompiler.get_msvc_pathsc                 C   s6   |dkr|  d}n
|  |}|r2d|tj|< dS )zSet 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)re   rg   rN   r_   )r*   r   r2   r   r   r   rf   o  s
    
zMSVCCompiler.set_path_env_var)r   r   r   )r   rm   )NNNr   NNN)Nr   N)
NNNNNr   NNNN)r   )r   )r8   r9   r:   __doc__Zcompiler_typeZexecutablesr|   r}   rs   ru   rq   rt   rv   Zstatic_lib_extensionZshared_lib_extensionZstatic_lib_formatZshared_lib_formatZexe_extensionr,   rl   ry   r   r   r   r   r   r   r   r`   re   rf   r   r   r   r   rR      sj   

B  
             
X   
          
S

$rR   g       @z3Importing new compiler from distutils.msvc9compiler)rR   )r&   ).r   rB   rN   Zdistutils.errorsr   r   r   r   r   Zdistutils.ccompilerr   r   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&   rI   rM   rQ   rR   r   ZOldMSVCCompilerZdistutils.msvc9compilerr   r   r   r   <module>   s`   



	-   9
