U
    aÊ¹j>Q  ã                	   @   s  d Z ddlZddlZddlZddlmZ ddlZe e¡� ddl	Z	W 5 Q R X ddl
mZ ddlmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZmZ d	d
„ Zdd„ ZdddddœZdd„ Zdd„ Zddd„ZdddddœZ dd„ Z!G dd„ deƒZ"dS )z¬distutils._msvccompiler

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

This module requires VS 2015 or later.
é    N)Úcounté   )Úlog)Ú	CCompilerÚgen_lib_options)ÚCompileErrorÚDistutilsExecErrorÚDistutilsPlatformErrorÚLibErrorÚ	LinkError)Úget_host_platformÚget_platformc                  C   s   zt jt jdt jt jB d�} W n  tk
r>   t d¡ Y dS X d}d }| �¦ tƒ D ]˜}zt  	| |¡\}}}W n tk
rˆ   Y  qîY nX |rT|t j
krTtj |¡rTztt|ƒƒ}W n ttfk
rÐ   Y qTY nX |dkrT||krT|| }}qTW 5 Q R X ||fS )Nz'Software\Microsoft\VisualStudio\SxS\VC7)ÚaccesszVisual C++ is not registered©NNr   é   )ÚwinregÚ	OpenKeyExÚHKEY_LOCAL_MACHINEZKEY_READZKEY_WOW64_32KEYÚOSErrorr   Údebugr   Z	EnumValueÚREG_SZÚosÚpathÚisdirÚintÚfloatÚ
ValueErrorÚ	TypeError)ÚkeyÚbest_versionÚbest_dirÚiÚvZvc_dirÚvtÚversion© r%   úb/var/www/html/bwave/python/venv/lib/python3.8/site-packages/setuptools/_distutils/_msvccompiler.pyÚ_find_vc2015&   s2    
ý



r'   c                  C   sØ   t j d¡pt j d¡} | s dS tƒ dkr.dnd}d|› �df}|D ]Ž}t tjtt	¡�t t 
t j | d	d
d¡ddd|ddddg	¡jddd� ¡ }t j |ddd¡}t j |¡rÈd|fW  5 Q R £   S W 5 Q R X qDdS )aJ  Returns "15, path" based on the result of invoking vswhere.exe
    If no install is found, returns "None, None"

    The version is returned to avoid unnecessarily changing the function
    result. It may be ignored when the path is not None.

    If vswhere.exe is not available, by definition, VS 2017 is not
    installed.
    zProgramFiles(x86)ZProgramFilesr   ú	win-arm64Úarm64zx86.x64z*Microsoft.VisualStudio.Component.VC.Tools.z)Microsoft.VisualStudio.Workload.WDExpresszMicrosoft Visual StudioZ	Installerzvswhere.exez-latestz-prereleasez	-requiresz	-propertyZinstallationPathz	-productsÚ*ÚmbcsÚstrict)ÚencodingÚerrorsZVCZ	AuxiliaryZBuildé   )r   ÚenvironÚgetr   Ú
contextlibÚsuppressÚ
subprocessÚCalledProcessErrorr   ÚUnicodeDecodeErrorÚcheck_outputr   ÚjoinÚdecodeÚstripr   )ÚrootÚvariantZsuitable_componentsÚ	componentr   r%   r%   r&   Ú_find_vc2017C   sJ    
þ  ÿ   ÿõ óÿ$r>   Úx86Úx64Úarmr)   )r?   Z	x86_amd64Zx86_armZ	x86_arm64c                 C   s\   t ƒ \}}|stƒ \}}|s*t d¡ dS tj |d¡}tj |¡sTt d|¡ dS |d fS )Nz$No suitable Visual C++ version foundr   zvcvarsall.batz%s cannot be found)r>   r'   r   r   r   r   r8   Úisfile)Ú	plat_specÚ_r    r   Ú	vcvarsallr%   r%   r&   Ú_find_vcvarsall}   s    


rF   c              
   C   sÄ   t  d¡rdd„ t j ¡ D ƒS t| ƒ\}}|s6tdƒ‚z,tjd|› d| › d�tjd�j	d	d
d�}W n@ tj
k
r¢ } z t |j¡ td|j› �ƒ‚W 5 d }~X Y nX dd„ dd„ | ¡ D ƒD ƒ}|S )NZDISTUTILS_USE_SDKc                 S   s   i | ]\}}|  ¡ |“qS r%   ©Úlower)Ú.0r   Úvaluer%   r%   r&   Ú
<dictcomp>’   s      z_get_vc_env.<locals>.<dictcomp>z•Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/zcmd /u /c "z" z && set)Ústderrzutf-16leÚreplace)r.   zError executing c                 S   s$   i | ]\}}}|r|r|  ¡ |“qS r%   rG   )rI   r   rD   rJ   r%   r%   r&   rK   ¥   s
    þ c                 s   s   | ]}|  d ¡V  qdS )ú=N)Ú	partition)rI   Úliner%   r%   r&   Ú	<genexpr>§   s     z_get_vc_env.<locals>.<genexpr>)r   Úgetenvr0   ÚitemsrF   r	   r4   r7   ÚSTDOUTr9   r5   r   ÚerrorÚoutputÚcmdÚ
splitlines)rC   rE   rD   ÚoutÚexcÚenvr%   r%   r&   Ú_get_vc_env�   s,    
ÿþ ý
"þr\   c                 C   sN   |st  d¡ t j¡}|D ].}t j t j |¡| ¡}t j |¡r|  S q| S )at  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'.
    r   )r   rR   ÚsplitÚpathsepr   r8   ÚabspathrB   )ÚexeÚpathsÚpÚfnr%   r%   r&   Ú	_find_exe®   s    	
rd   Úamd64)Úwin32z	win-amd64z	win-arm32r(   c                 C   s6   | dkrd} t |  }t | }||kr(|S |› d|› �S )aw  
    Given a host platform and platform, determine the spec for vcvarsall.

    Uses the native MSVC host if the host platform would need expensive
    emulation for x86.

    >>> _get_vcvars_spec('win-arm64', 'win32')
    'arm64_x86'
    >>> _get_vcvars_spec('win-arm64', 'win-amd64')
    'arm64_amd64'

    Otherwise, always cross-compile from x86 to work with the
    lighter-weight MSVC installs that do not include native 64-bit tools.

    >>> _get_vcvars_spec('win32', 'win32')
    'x86'
    >>> _get_vcvars_spec('win-arm32', 'win-arm32')
    'x86_arm'
    >>> _get_vcvars_spec('win-amd64', 'win-arm64')
    'x86_arm64'
    r(   rf   rD   )Ú_vcvars_names)Zhost_platformÚplatformZvc_hpZvc_platr%   r%   r&   Ú_get_vcvars_specÈ   s
    ri   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-‡ fdd„	Zedd„ ƒZedd„ ƒZd.dd„Ze‡ fdd„ƒZd/dd„Zd0dd„Zd1dd „Z‡ fd!d"„Zej‡ fd#d$„ƒZ d%d&„ Z!d'd(„ Z"d)d*„ Z#d2d+d,„Z$‡  Z%S )3ÚMSVCCompilerztConcrete 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.exeFc                    s    t ƒ  |||¡ d | _d| _d S )NF)ÚsuperÚ__init__Ú	plat_nameÚinitialized)ÚselfÚverboseÚdry_runÚforce©Ú	__class__r%   r&   rm     s    zMSVCCompiler.__init__c                 C   s,   |   | dd¡¡| _|   | dd¡¡| _dS )z3
        Set class-level include/lib dirs.
        ÚincludeÚ ÚlibN)Ú_parse_pathr1   Úinclude_dirsÚlibrary_dirs)ÚclsÚvc_envr%   r%   r&   Ú
_configure  s    zMSVCCompiler._configurec                 C   s   dd„ |   tj¡D ƒS )Nc                 S   s   g | ]}|r|  tj¡‘qS r%   )Úrstripr   Úsep)rI   Údirr%   r%   r&   Ú
<listcomp>  s      z,MSVCCompiler._parse_path.<locals>.<listcomp>)r]   r   r^   )Úvalr%   r%   r&   ry     s    zMSVCCompiler._parse_pathNc                 C   s¸  | j rtdƒ‚|d krtƒ }|tkr6tdttƒ› �ƒ‚ttƒ |ƒ}t|ƒ}|sVtdƒ‚|  	|¡ | 
dd¡| _| j tj¡}td|ƒ| _td|ƒ| _td|ƒ| _td	|ƒ| _td
|ƒ| _td|ƒ| _d | _ddddddg| _ddddddg| _dddg}ddddg}|d•| _|d •| _|d!•| _|d"•| _|•| _|•| _tj d f| jtj df| jtj df| jtj!d f| jtj!df| jtj!df| jtj"d f| jtj"df| jtj"df| ji	| _#d| _ d S )#Nzdon't init multiple timesz--plat-name must be one of z7Unable to find a compatible Visual Studio installation.r   rw   zcl.exezlink.exezlib.exezrc.exezmc.exezmt.exez/nologoz/O2z/W3z/GLz/DNDEBUGz/MDz/Odz/MDdz/Ziz/D_DEBUGz/INCREMENTAL:NOz/LTCGz/DEBUG:FULLú/MANIFEST:EMBED,ID=1ú/DLLú/MANIFEST:EMBED,ID=2ú/MANIFESTUAC:NOFT)r„   )r„   )r…   r†   r‡   )r…   r†   r‡   )$ro   ÚAssertionErrorr   rg   r	   Útupleri   r   r\   r~   r1   Ú_pathsr]   r   r^   rd   ÚccÚlinkerrx   ÚrcÚmcÚmtZpreprocess_optionsÚcompile_optionsÚcompile_options_debugZldflags_exeZldflags_exe_debugZldflags_sharedZldflags_shared_debugZldflags_staticZldflags_static_debugr   Z
EXECUTABLEZSHARED_OBJECTZSHARED_LIBRARYÚ_ldflags)rp   rn   rC   r}   ra   ÚldflagsZldflags_debugr%   r%   r&   Ú
initialize  s†    ÿÿ
ú	


   ü   ü         ÷zMSVCCompiler.initializec                    s"   t ƒ j‡ fdd„ˆ jˆ j D ƒ–S )Nc                    s   i | ]}|ˆ j “qS r%   )Úres_extension)rI   Úext©rp   r%   r&   rK   i  s   ÿ z/MSVCCompiler.out_extensions.<locals>.<dictcomp>)rl   Úout_extensionsÚ_rc_extensionsÚ_mc_extensionsr—   rt   r—   r&   r˜   e  s
    

þþzMSVCCompiler.out_extensionsc	                 C   s„  | j s|  ¡  |  ||||||¡}	|	\}}
}}}|p6g }| d¡ |rT| | j¡ n| | j¡ d}|
D �]}z|| \}}W n tk
r–   Y qhY nX |r¨tj	 
|¡}|| jkr¾d| }�nH|| jkrØd| }d}�n.|| jk�r@|}d| }z|  | jg| ||g ¡ W qh tk
�r: } zt|ƒ‚W 5 d }~X Y qhX qhnÆ|| jk�ròtj	 |¡}tj	 |¡}z\|  | jd|d||g¡ tj	 tj	 |¡¡\}}tj	 ||d	 ¡}|  | jd| |g¡ W qh tk
�rì } zt|ƒ‚W 5 d }~X Y qhX qhntd
|› d|› �ƒ‚| jg| | }|�r&| d¡ | |d| f¡ | |¡ z|  |¡ W qh tk
�r| } zt|ƒ‚W 5 d }~X Y qhX qh|
S )Nz/cFz/Tcz/TpTz/foz-hz-rrk   zDon't know how to compile z to z/EHscz/Fo)ro   r”   Z_setup_compileÚappendÚextendr‘   r�   ÚKeyErrorr   r   r_   Ú_c_extensionsÚ_cpp_extensionsr™   Úspawnr�   r   r   rš   ÚdirnamerŽ   ÚsplitextÚbasenamer8   r‹   )rp   ÚsourcesÚ
output_dirÚmacrosrz   r   Úextra_preargsÚextra_postargsÚdependsZcompile_infoÚobjectsZpp_optsÚbuildZcompile_optsZadd_cpp_optsÚobjÚsrcr–   Z	input_optZ
output_optÚmsgZh_dirZrc_dirÚbaserD   Zrc_fileÚargsr%   r%   r&   Úcompileo  sv         ÿ






zMSVCCompiler.compilec           	   
   C   s´   | j s|  ¡  |  ||¡\}}| j||d�}|  ||¡r¤|d| g }|rJz,t d| jd |¡¡ |  	| jg| ¡ W q° t
k
r  } zt|ƒ‚W 5 d }~X Y q°X nt d|¡ d S )N)r¥   ú/OUT:úExecuting "%s" %sú úskipping %s (up-to-date))ro   r”   Ú_fix_object_argsÚlibrary_filenameÚ
_need_linkr   r   rx   r8   r    r   r
   )	rp   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 k	rptj 	||¡}|  
||¡�r¨| j||	f }dd„ |p˜g D ƒ}|| | | d| g }tj |d ¡}|d k	�rtj tj |¡¡\}}tj 	||  |¡¡}| d| ¡ |
�r|
|d d…< |�r.| |¡ tj tj |¡¡}|  |¡ z,t d| jd 	|¡¡ |  | 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': c                 S   s   g | ]}d | ‘qS )z/EXPORT:r%   )rI   Úsymr%   r%   r&   r‚     s     z%MSVCCompiler.link.<locals>.<listcomp>r²   r   z/IMPLIB:r³   r´   rµ   )ro   r”   r¶   Z_fix_lib_argsÚwarnÚstrr   r   r   r8   r¸   r’   r¡   r¢   r£   r·   r›   rœ   r_   Úmkpathr   r   rŒ   r    r   r   )rp   Ztarget_descrª   rº   r¥   Ú	librariesr{   Úruntime_library_dirsÚexport_symbolsr   r§   r¨   Ú
build_tempr¹   Z
fixed_argsZlib_optsr“   Zexport_optsZld_argsZdll_nameZdll_extZimplib_filer®   r%   r%   r&   Úlinká  sL    
ÿÿÿ	

ÿ

zMSVCCompiler.linkc              
      sF   t tj| jd�}|  ||¡�}tƒ j||d�W  5 Q R £ S Q R X |jS )N)ÚPATH)r[   )Údictr   r0   rŠ   Ú_fallback_spawnrl   r    rJ   )rp   rW   r[   Úfallbackrt   r%   r&   r    &  s    "zMSVCCompiler.spawnc              
   #   s„   t ddi ƒƒ }z
|V  W n0 tk
rH } zdt|ƒkr8‚ W 5 d}~X Y nX dS t d¡ tj d|¡� tƒ  	|¡|_
W 5 Q R X dS )zÍ
        Discovered in pypa/distutils#15, some tools monkeypatch the compiler,
        so the 'env' kwarg causes a TypeError. Detect this condition and
        restore the legacy, unsafe behavior.
        ZBagr%   z!unexpected keyword argument 'env'Nz>Fallback spawn triggered. Please update distutils monkeypatch.z
os.environ)Útyper   r¾   Úwarningsr½   ÚmockÚpatchrÆ   rl   r    rJ   )rp   rW   r[   ZbagrZ   rt   r%   r&   rÇ   ,  s    

zMSVCCompiler._fallback_spawnc                 C   s   d| S )Nz	/LIBPATH:r%   ©rp   r�   r%   r%   r&   Úlibrary_dir_optionC  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_optionF  s    ÿz'MSVCCompiler.runtime_library_dir_optionc                 C   s
   |   |¡S )N)r·   )rp   rx   r%   r%   r&   Úlibrary_optionK  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)r   r   r8   r·   rB   )rp   Údirsrx   r   Z	try_namesr�   ÚnameZlibfiler%   r%   r&   Úfind_library_fileN  s    zMSVCCompiler.find_library_file)FFF)N)NNNFNNN)NFN)
NNNNNFNNNN)F)&Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úcompiler_typeZexecutablesrž   rŸ   r™   rš   Zsrc_extensionsr•   Zobj_extensionZstatic_lib_extensionÚshared_lib_extensionZstatic_lib_formatZshared_lib_formatZexe_extensionrm   Úclassmethodr~   Ústaticmethodry   r”   Úpropertyr˜   r±   r»   rÄ   r    r2   ÚcontextmanagerrÇ   rÎ   rÏ   rÐ   rÓ   Ú__classcell__r%   r%   rt   r&   rj   å   sh   



Q       ÷
_     ÿ
          ò
Erj   )N)#r×   r2   r   r4   Zunittest.mockrË   rÊ   r3   ÚImportErrorr   Ú	itertoolsr   Ú_logr   Z	ccompilerr   r   r.   r   r   r	   r
   r   Úutilr   r   r'   r>   ZPLAT_SPEC_TO_RUNTIMErF   r\   rd   rg   ri   rj   r%   r%   r%   r&   Ú<module>   s:   3ü
ü