U
    @{?^׶                     @   sX  d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZ ddlZddlZddlZddlZddlmZ ddlmZ dd	lmZ e d
krddlmZ ddlmZ nG dd dZe ZeejjfZzddlm Z  W n ek
r   Y nX dd Z!d$ddZ"dd Z#dd Z$d%ddZ%G dd dZ&G dd dZ'G d d! d!Z(G d"d# d#Z)dS )&a  
Improved support for Microsoft Visual C++ compilers.

Known supported compilers:
--------------------------
Microsoft Visual C++ 9.0:
    Microsoft Visual C++ Compiler for Python 2.7 (x86, amd64)
    Microsoft Windows SDK 6.1 (x86, x64, ia64)
    Microsoft Windows SDK 7.0 (x86, x64, ia64)

Microsoft Visual C++ 10.0:
    Microsoft Windows SDK 7.1 (x86, x64, ia64)

Microsoft Visual C++ 14.X:
    Microsoft Visual C++ Build Tools 2015 (x86, x64, arm)
    Microsoft Visual Studio Build Tools 2017 (x86, x64, arm, arm64)
    Microsoft Visual Studio Build Tools 2019 (x86, x64, arm, arm64)

This may also support compilers shipped with compatible Visual Studio versions.
    N)open)listdirpathsep)joinisfileisdirdirname)LegacyVersion)filterfalse   )get_unpatchedWindows)winreg)environc                   @   s   e Zd ZdZdZdZdZdS )r   N)__name__
__module____qualname__
HKEY_USERSHKEY_CURRENT_USERHKEY_LOCAL_MACHINEHKEY_CLASSES_ROOT r   r   1/usr/lib/python3/dist-packages/setuptools/msvc.pyr   *   s   r   )Regc                 C   s   d}|d| f }zt |d}W nJ tk
rj   z|d| f }t |d}W n tk
rd   d}Y nX Y nX |rt|d}t|r|S tt| S )a  
    Patched "distutils.msvc9compiler.find_vcvarsall" to use the standalone
    compiler build for Python
    (VCForPython / Microsoft Visual C++ Compiler for Python 2.7).

    Fall back to original behavior when the standalone compiler is not
    available.

    Redirect the path of "vcvarsall.bat".

    Parameters
    ----------
    version: float
        Required Microsoft Visual C++ version.

    Return
    ------
    str
        vcvarsall.bat path
    z-Software\%sMicrosoft\DevDiv\VCForPython\%0.1f 
installdirzWow6432Node\Nzvcvarsall.bat)r   Z	get_valueKeyErrorr   r   r   msvc9_find_vcvarsall)versionZvc_basekey
productdir	vcvarsallr   r   r   r   A   s    
r   x86c              
   O   s   zt t}|| |f||W S  tjjk
r4   Y n tk
rF   Y nX zt||  W S  tjjk
r } zt|| |  W 5 d}~X Y nX dS )ao  
    Patched "distutils.msvc9compiler.query_vcvarsall" for support extra
    Microsoft Visual C++ 9.0 and 10.0 compilers.

    Set environment without use of "vcvarsall.bat".

    Parameters
    ----------
    ver: float
        Required Microsoft Visual C++ version.
    arch: str
        Target architecture.

    Return
    ------
    dict
        environment
    N)	r   msvc9_query_vcvarsall	distutilserrorsDistutilsPlatformError
ValueErrorEnvironmentInfo
return_env_augment_exception)verarchargskwargsZorigexcr   r   r   r#   k   s    r#   c              
   C   sr   zt t| W S  tjjk
r&   Y nX zt| dd W S  tjjk
rl } zt|d  W 5 d}~X Y nX dS )a*  
    Patched "distutils._msvccompiler._get_vc_env" for support extra
    Microsoft Visual C++ 14.X compilers.

    Set environment without use of "vcvarsall.bat".

    Parameters
    ----------
    plat_spec: str
        Target architecture.

    Return
    ------
    dict
        environment
          ,@)
vc_min_verN)r   msvc14_get_vc_envr$   r%   r&   r(   r)   r*   )Z	plat_specr/   r   r   r   r2      s    
r2   c                  O   sB   dt jkr4ddl}t|jtdk r4|jjj| |S tt	| |S )z
    Patched "distutils._msvccompiler.gen_lib_options" for fix
    compatibility between "numpy.distutils" and "distutils._msvccompiler"
    (for Numpy < 1.11.2)
    znumpy.distutilsr   Nz1.11.2)
sysmodulesZnumpyr	   __version__r$   Z	ccompilerZgen_lib_optionsr   msvc14_gen_lib_options)r-   r.   Znpr   r   r   r6      s
    
r6   r   c                 C   s   | j d }d| ks"d| krd}|jf t }d}|dkrf| ddkr\|d	7 }q|d
7 }n.|dkr|d7 }||d 7 }n|dkr|d7 }|f| _ dS )zl
    Add details to the exception message to help guide the user
    as to what action will resolve it.
    r   r!   zvisual cz0Microsoft Visual C++ {version:0.1f} is required.z-www.microsoft.com/download/details.aspx?id=%d      "@Zia64z( Get it with "Microsoft Windows SDK 7.0"z% Get it from http://aka.ms/vcpython27      $@z* Get it with "Microsoft Windows SDK 7.1": iW   r0   z[ Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/N)r-   lowerformatlocalsfind)r/   r   r,   messageZtmplZ
msdownloadr   r   r   r*      s    


r*   c                   @   sb   e Zd ZdZedd Zdd Ze	dd Z
dd	 Zd
d ZdddZdddZdddZdS )PlatformInfoz
    Current and Target Architectures information.

    Parameters
    ----------
    arch: str
        Target architecture.
    Zprocessor_architecturer   c                 C   s   |  dd| _d S )Nx64amd64)r:   replacer,   )selfr,   r   r   r   __init__   s    zPlatformInfo.__init__c                 C   s   | j | j dd d S )zs
        Return Target CPU architecture.

        Return
        ------
        str
            Target CPU
        _r   N)r,   r=   rC   r   r   r   
target_cpu   s    
zPlatformInfo.target_cpuc                 C   s
   | j dkS )z
        Return True if target CPU is x86 32 bits..

        Return
        ------
        bool
            CPU is x86 32 bits
        r"   rG   rF   r   r   r   target_is_x86   s    	zPlatformInfo.target_is_x86c                 C   s
   | j dkS )z
        Return True if current CPU is x86 32 bits..

        Return
        ------
        bool
            CPU is x86 32 bits
        r"   current_cpurF   r   r   r   current_is_x86  s    	zPlatformInfo.current_is_x86Fc                 C   s.   | j dkr|rdS | j dkr$|r$dS d| j  S )uk  
        Current platform specific subfolder.

        Parameters
        ----------
        hidex86: bool
            return '' and not '' if architecture is x86.
        x64: bool
            return 'd' and not 'md64' if architecture is amd64.

        Return
        ------
        str
            subfolder: '	arget', or '' (see hidex86 parameter)
        r"   r   rA   \x64\%srJ   rC   hidex86r@   r   r   r   current_dir  s    zPlatformInfo.current_dirc                 C   s.   | j dkr|rdS | j dkr$|r$dS d| j  S )ar  
        Target platform specific subfolder.

        Parameters
        ----------
        hidex86: bool
            return '' and not '\x86' if architecture is x86.
        x64: bool
            return '\x64' and not '\amd64' if architecture is amd64.

        Return
        ------
        str
            subfolder: '\current', or '' (see hidex86 parameter)
        r"   r   rA   rM   rN   rH   rO   r   r   r   
target_dir(  s    zPlatformInfo.target_dirc                 C   s0   |rdn| j }| j|krdS |  dd| S )ap  
        Cross platform specific subfolder.

        Parameters
        ----------
        forcex86: bool
            Use 'x86' as current architecture even if current architecture is
            not x86.

        Return
        ------
        str
            subfolder: '' if target architecture is current architecture,
            '\current_target' if not.
        r"   r   \z\%s_)rK   rG   rR   rB   )rC   forcex86Zcurrentr   r   r   	cross_dir>  s    zPlatformInfo.cross_dirN)FF)FF)F)r   r   r   __doc__r   getr:   rK   rD   propertyrG   rI   rL   rQ   rR   rU   r   r   r   r   r?      s   


r?   c                   @   s   e Zd ZdZejejejejfZ	dd Z
edd Zedd Zedd	 Zed
d Zedd Zedd Zedd Zedd Zedd ZdddZdd ZdS )RegistryInfoz
    Microsoft Visual Studio related registry information.

    Parameters
    ----------
    platform_info: PlatformInfo
        "PlatformInfo" instance.
    c                 C   s
   || _ d S N)pi)rC   Zplatform_infor   r   r   rD   c  s    zRegistryInfo.__init__c                 C   s   dS )z
        Microsoft Visual Studio root registry key.

        Return
        ------
        str
            Registry key
        ZVisualStudior   rF   r   r   r   visualstudiof  s    
zRegistryInfo.visualstudioc                 C   s   t | jdS )z
        Microsoft Visual Studio SxS registry key.

        Return
        ------
        str
            Registry key
        ZSxS)r   r\   rF   r   r   r   sxsr  s    
zRegistryInfo.sxsc                 C   s   t | jdS )z|
        Microsoft Visual C++ VC7 registry key.

        Return
        ------
        str
            Registry key
        ZVC7r   r]   rF   r   r   r   vc~  s    
zRegistryInfo.vcc                 C   s   t | jdS )z
        Microsoft Visual Studio VS7 registry key.

        Return
        ------
        str
            Registry key
        ZVS7r^   rF   r   r   r   vs  s    
zRegistryInfo.vsc                 C   s   dS )z
        Microsoft Visual C++ for Python registry key.

        Return
        ------
        str
            Registry key
        zDevDiv\VCForPythonr   rF   r   r   r   vc_for_python  s    
zRegistryInfo.vc_for_pythonc                 C   s   dS )zq
        Microsoft SDK registry key.

        Return
        ------
        str
            Registry key
        zMicrosoft SDKsr   rF   r   r   r   microsoft_sdk  s    
zRegistryInfo.microsoft_sdkc                 C   s   t | jdS )z
        Microsoft Windows/Platform SDK registry key.

        Return
        ------
        str
            Registry key
        r   r   rb   rF   r   r   r   windows_sdk  s    
zRegistryInfo.windows_sdkc                 C   s   t | jdS )z
        Microsoft .NET Framework SDK registry key.

        Return
        ------
        str
            Registry key
        ZNETFXSDKrc   rF   r   r   r   	netfx_sdk  s    
zRegistryInfo.netfx_sdkc                 C   s   dS )z
        Microsoft Windows Kits Roots registry key.

        Return
        ------
        str
            Registry key
        zWindows Kits\Installed Rootsr   rF   r   r   r   windows_kits_roots  s    
zRegistryInfo.windows_kits_rootsFc                 C   s$   | j  s|rdnd}td|d|S )a  
        Return key in Microsoft software registry.

        Parameters
        ----------
        key: str
            Registry key path where look.
        x86: str
            Force x86 software registry.

        Return
        ------
        str
            Registry key
        r   ZWow6432NodeZSoftwareZ	Microsoft)r[   rL   r   )rC   r   r"   Znode64r   r   r   	microsoft  s    zRegistryInfo.microsoftc                 C   s   t j}t j}| j}| jD ]}z||||d|}W n` ttfk
r   | j sz||||dd|}W q ttfk
r   Y Y qY qX nY qY nX zt 	||d W   S  ttfk
r   Y qX qdS )a  
        Look for values in registry in Microsoft software registry.

        Parameters
        ----------
        key: str
            Registry key path where look.
        name: str
            Value name to find.

        Return
        ------
        str
            value
        r   TN)
r   KEY_READOpenKeyrg   HKEYSOSErrorIOErrorr[   rL   ZQueryValueEx)rC   r   nameZkey_readZopenkeymshkeybkeyr   r   r   lookup  s"    


zRegistryInfo.lookupN)F)r   r   r   rV   r   r   r   r   r   rj   rD   rX   r\   r]   r_   r`   ra   rb   rd   re   rf   rg   rq   r   r   r   r   rY   U  s6   









rY   c                   @   s<  e Zd ZdZeddZeddZedeZd7ddZ	d	d
 Z
dd Zdd Zedd Zedd Zedd Zdd Zdd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Zed-d. Zed/d0 Zed1d2 Z d3d4 Z!ed8d5d6Z"dS )9
SystemInfoz
    Microsoft Windows and Visual Studio related system information.

    Parameters
    ----------
    registry_info: RegistryInfo
        "RegistryInfo" instance.
    vc_ver: float
        Required Microsoft Visual C++ version.
    WinDirr   ProgramFileszProgramFiles(x86)Nc                 C   s2   || _ | j j| _|  | _|p$|   | _| _d S rZ   )rir[   find_programdata_vs_versknown_vs_paths_find_latest_available_vs_vervs_vervc_ver)rC   Zregistry_inforz   r   r   r   rD     s
    


zSystemInfo.__init__c                 C   s>   |   }|s| jstjdt|}|| j t|d S )zm
        Find the latest VC version

        Return
        ------
        float
            version
        z%No Microsoft Visual C++ version foundr8   )find_reg_vs_versrw   r$   r%   r&   setupdatesorted)rC   Zreg_vc_versZvc_versr   r   r   rx   %  s    	
z(SystemInfo._find_latest_available_vs_verc                 C   s$  | j j}| j j| j j| j jf}g }| j jD ]}|D ]}zt|||dtj}W n t	t
fk
rl   Y q2Y nX t|\}}}	t|D ]D}
z*tt||
d }||kr|| W q tk
r   Y qX qt|D ]B}
z&tt||
}||kr|| W q tk
r   Y qX qq2q*t|S )z
        Find Microsoft Visual Studio versions available in registry.

        Return
        ------
        list of float
            Versions
        r   )ru   rg   r_   ra   r`   rj   r   ri   rh   rk   rl   ZQueryInfoKeyrangefloatZ	EnumValueappendr'   ZEnumKeyr~   )rC   rn   ZvckeysZvs_versro   r   rp   ZsubkeysvaluesrE   ir+   r   r   r   r{   8  s2    	
zSystemInfo.find_reg_vs_versc           	      C   s   i }d}zt |}W n ttfk
r0   | Y S X |D ]}z\t||d}t|ddd}t|}W 5 Q R X |d }t t|d ||| |d < W q6 tttfk
r   Y q6Y q6X q6|S )	z
        Find Visual studio 2017+ versions from information in
        "C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances".

        Return
        ------
        dict
            float version as key, path as value.
        z9C:\ProgramData\Microsoft\VisualStudio\Packages\_Instancesz
state.jsonZrtzutf-8)encodingZinstallationPathVC\Tools\MSVCZinstallationVersion)	r   rk   rl   r   r   jsonload_as_float_versionr   )	rC   Zvs_versionsZinstances_dirZhashed_namesrm   Z
state_pathZ
state_filestateZvs_pathr   r   r   rv   [  s*    

z#SystemInfo.find_programdata_vs_versc                 C   s   t d| ddd S )z
        Return a string version as a simplified float version (major.minor)

        Parameters
        ----------
        version: str
            Version.

        Return
        ------
        float
            version
        .N   )r   r   split)r   r   r   r   r     s    zSystemInfo._as_float_versionc                 C   s.   t | jd| j }| j| jjd| j p,|S )zp
        Microsoft Visual Studio directory.

        Return
        ------
        str
            path
        zMicrosoft Visual Studio %0.1f%0.1f)r   ProgramFilesx86ry   ru   rq   r`   )rC   defaultr   r   r   VSInstallDir  s    zSystemInfo.VSInstallDirc                 C   s,   |   p|  }t|s(d}tj||S )zm
        Microsoft Visual C++ directory.

        Return
        ------
        str
            path
        z(Microsoft Visual C++ directory not found)	_guess_vc_guess_vc_legacyr   r$   r%   r&   )rC   pathmsgr   r   r   VCInstallDir  s
    
zSystemInfo.VCInstallDirc              
   C   s   | j dkrdS z| j| j  }W n tk
r8   | j}Y nX t|d}z$t|d }| || _t||W S  tt	t
fk
r   Y dS X dS )zl
        Locate Visual C++ for VS2017+.

        Return
        ------
        str
            path
        r0   r   r   r8   N)ry   rw   r   r   r   r   r   rz   rk   rl   
IndexError)rC   Zvs_dirZguess_vcrz   r   r   r   r     s    	

zSystemInfo._guess_vcc                 C   sb   t | jd| j }t | jjd| j }| j|d}|rBt |dn|}| j| jjd| j p`|S )z{
        Locate Visual C++ for versions prior to 2017.

        Return
        ------
        str
            path
        z Microsoft Visual Studio %0.1f\VCr   r   ZVC)r   r   ry   ru   ra   rq   r_   )rC   r   Zreg_pathZ	python_vcZ
default_vcr   r   r   r     s    	zSystemInfo._guess_vc_legacyc                 C   sJ   | j dkrdS | j dkrdS | j dkr*dS | j dkr8dS | j d	krFd
S dS )z
        Microsoft Windows SDK versions for specified MSVC++ version.

        Return
        ------
        tuple of str
            versions
        r7   )z7.0z6.1z6.0ar9   )z7.1z7.0a      &@)z8.0z8.0a      (@)8.1z8.1ar0   )z10.0r   Nry   rF   r   r   r   WindowsSdkVersion  s    





zSystemInfo.WindowsSdkVersionc                 C   s   |  t| jdS )zt
        Microsoft Windows SDK last version.

        Return
        ------
        str
            version
        lib)_use_last_dir_namer   WindowsSdkDirrF   r   r   r   WindowsSdkLastVersion   s    
z SystemInfo.WindowsSdkLastVersionc                 C   s  d}| j D ],}t| jjd| }| j|d}|r
 q8q
|rDt|stt| jjd| j }| j|d}|rtt|d}|rt|s| j D ]6}|d|d }d	| }t| j	|}t|r|}q|rt|s| j D ]$}d
| }t| j	|}t|r|}q|st| j
d}|S )zn
        Microsoft Windows SDK directory.

        Return
        ------
        str
            path
        r   zv%sinstallationfolderr   r   ZWinSDKNr   zMicrosoft SDKs\Windows Kits\%szMicrosoft SDKs\Windows\v%sZPlatformSDK)r   r   ru   rd   rq   r   ra   rz   rfindrt   r   )rC   sdkdirr+   locr   Zinstall_baseZintverdr   r   r   r     s6    




zSystemInfo.WindowsSdkDirc           	      C   s   | j dkrd}d}n&d}| j dkr&dnd}| jjd|d}d	||d
df }g }| j dkr~| jD ]}|t| jj||g7 }qb| jD ]}|t| jj	d| |g7 }q|D ]}| j
|d}|r|  S qdS )zy
        Microsoft Windows SDK executable directory.

        Return
        ------
        str
            path
        r   #   r   (   r   TF)r@   rP   zWinSDK-NetFx%dTools%srS   -r0   zv%sAr   N)ry   r[   rQ   rB   NetFxSdkVersionr   ru   re   r   rd   rq   )	rC   Znetfxverr,   rP   ZfxZregpathsr+   r   Zexecpathr   r   r   WindowsSDKExecutablePath7  s"    



z#SystemInfo.WindowsSDKExecutablePathc                 C   s&   t | jjd| j }| j|dp$dS )zl
        Microsoft Visual F# directory.

        Return
        ------
        str
            path
        z%0.1f\Setup\F#r    r   )r   ru   r\   ry   rq   )rC   r   r   r   r   FSharpInstallDirZ  s    
zSystemInfo.FSharpInstallDirc                 C   sF   | j dkrdnd}|D ]*}| j| jjd| }|r|p:d  S qdS )zt
        Microsoft Universal CRT SDK directory.

        Return
        ------
        str
            path
        r0   )Z10Z81r   z
kitsroot%sr   N)ry   ru   rq   rf   )rC   Zversr+   r   r   r   r   UniversalCRTSdkDirg  s    zSystemInfo.UniversalCRTSdkDirc                 C   s   |  t| jdS )z
        Microsoft Universal C Runtime SDK last version.

        Return
        ------
        str
            version
        r   )r   r   r   rF   r   r   r   UniversalCRTSdkLastVersion{  s    
z%SystemInfo.UniversalCRTSdkLastVersionc                 C   s   | j dkrdS dS )z
        Microsoft .NET Framework SDK versions.

        Return
        ------
        tuple of str
            versions
        r0   )	z4.7.2z4.7.1z4.7z4.6.2z4.6.1z4.6z4.5.2z4.5.1z4.5r   r   rF   r   r   r   r     s    zSystemInfo.NetFxSdkVersionc                 C   s8   d}| j D ](}t| jj|}| j|d}|r
 q4q
|S )zu
        Microsoft .NET Framework SDK directory.

        Return
        ------
        str
            path
        r   Zkitsinstallationfolder)r   r   ru   re   rq   )rC   r   r+   r   r   r   r   NetFxSdkDir  s    

zSystemInfo.NetFxSdkDirc                 C   s"   t | jd}| j| jjdp |S )zw
        Microsoft .NET Framework 32bit directory.

        Return
        ------
        str
            path
        zMicrosoft.NET\FrameworkZframeworkdir32r   rs   ru   rq   r_   rC   Zguess_fwr   r   r   FrameworkDir32  s    zSystemInfo.FrameworkDir32c                 C   s"   t | jd}| j| jjdp |S )zw
        Microsoft .NET Framework 64bit directory.

        Return
        ------
        str
            path
        zMicrosoft.NET\Framework64Zframeworkdir64r   r   r   r   r   FrameworkDir64  s    zSystemInfo.FrameworkDir64c                 C   s
   |  dS )z
        Microsoft .NET Framework 32bit versions.

        Return
        ------
        tuple of str
            versions
            _find_dot_net_versionsrF   r   r   r   FrameworkVersion32  s    
zSystemInfo.FrameworkVersion32c                 C   s
   |  dS )z
        Microsoft .NET Framework 64bit versions.

        Return
        ------
        tuple of str
            versions
        @   r   rF   r   r   r   FrameworkVersion64  s    
zSystemInfo.FrameworkVersion64c                 C   s   | j | j jd| }t| d| }|p6| |dp6d}| jdkrJ|dfS | jdkrt| dd	 d
krldn|dfS | jdkrdS | jdkrdS dS )z
        Find Microsoft .NET Framework versions.

        Parameters
        ----------
        bits: int
            Platform number of bits: 32 or 64.

        Return
        ------
        tuple of str
            versions
        zframeworkver%dzFrameworkDir%dvr   r   zv4.0r9   Nr   Zv4z
v4.0.30319v3.5r7   )r   
v2.0.50727g       @)zv3.0r   )ru   rq   r_   getattrr   ry   r:   )rC   bitsZreg_verZdot_net_dirr+   r   r   r   r     s    

 

z!SystemInfo._find_dot_net_versionsc                    s*    fddt t D }t|dp(dS )a)  
        Return name of the last dir in path or '' if no dir found.

        Parameters
        ----------
        path: str
            Use dirs in this path
        prefix: str
            Use only dirs starting by this prefix

        Return
        ------
        str
            name
        c                 3   s*   | ]"}t t |r|r|V  qd S rZ   )r   r   
startswith).0Zdir_namer   prefixr   r   	<genexpr>  s   
z0SystemInfo._use_last_dir_name.<locals>.<genexpr>Nr   )reversedr   next)r   r   Zmatching_dirsr   r   r   r     s    
zSystemInfo._use_last_dir_name)N)r   )#r   r   r   rV   r   rW   rs   rt   r   rD   rx   r{   rv   staticmethodr   rX   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rr   	  sZ   

#*





*
"








rr   c                   @   sb  e Zd ZdZd?ddZedd Zedd	 Zed
d Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zdd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zd@d9d:Zd;d< Z e!dAd=d>Z"dS )Br(   aY  
    Return environment variables for specified Microsoft Visual C++ version
    and platform : Lib, Include, Path and libpath.

    This function is compatible with Microsoft Visual C++ 9.0 to 14.X.

    Script created by analysing Microsoft environment configuration files like
    "vcvars[...].bat", "SetEnv.Cmd", "vcbuildtools.bat", ...

    Parameters
    ----------
    arch: str
        Target architecture.
    vc_ver: float
        Required Microsoft Visual C++ version. If not set, autodetect the last
        version.
    vc_min_ver: float
        Minimum Microsoft Visual C++ version.
    Nr   c                 C   sB   t || _t| j| _t| j|| _| j|k r>d}tj	|d S )Nz.No suitable Microsoft Visual C++ version found)
r?   r[   rY   ru   rr   sirz   r$   r%   r&   )rC   r,   rz   r1   errr   r   r   rD   0  s    

zEnvironmentInfo.__init__c                 C   s   | j jS )zk
        Microsoft Visual Studio.

        Return
        ------
        float
            version
        )r   ry   rF   r   r   r   ry   9  s    
zEnvironmentInfo.vs_verc                 C   s   | j jS )zp
        Microsoft Visual C++ version.

        Return
        ------
        float
            version
        )r   rz   rF   r   r   r   rz   E  s    
zEnvironmentInfo.vc_verc                    sV   ddg} j dkrD jjddd}|dg7 }|dg7 }|d| g7 } fd	d
|D S )zu
        Microsoft Visual Studio Tools.

        Return
        ------
        list of str
            paths
        zCommon7\IDEzCommon7\Toolsr0   TrP   r@   z1Common7\IDE\CommonExtensions\Microsoft\TestWindowzTeam Tools\Performance ToolszTeam Tools\Performance Tools%sc                    s   g | ]}t  jj|qS r   r   r   r   r   r   rF   r   r   
<listcomp>c  s     z+EnvironmentInfo.VSTools.<locals>.<listcomp>)ry   r[   rQ   )rC   pathsarch_subdirr   rF   r   VSToolsQ  s    



zEnvironmentInfo.VSToolsc                 C   s   t | jjdt | jjdgS )z
        Microsoft Visual C++ & Microsoft Foundation Class Includes.

        Return
        ------
        list of str
            paths
        ZIncludezATLMFC\Includer   r   r   rF   r   r   r   
VCIncludese  s    
zEnvironmentInfo.VCIncludesc                    sb    j dkr jjdd}n jjdd}d| d| g} j dkrP|d| g7 } fd	d
|D S )z
        Microsoft Visual C++ & Microsoft Foundation Class Libraries.

        Return
        ------
        list of str
            paths
              .@Tr@   rP   Lib%szATLMFC\Lib%sr0   zLib\store%sc                    s   g | ]}t  jj|qS r   r   r   rF   r   r   r     s     z/EnvironmentInfo.VCLibraries.<locals>.<listcomp>)ry   r[   rR   )rC   r   r   r   rF   r   VCLibrariesr  s    


zEnvironmentInfo.VCLibrariesc                 C   s   | j dk rg S t| jjdgS )z
        Microsoft Visual C++ store references Libraries.

        Return
        ------
        list of str
            paths
        r0   zLib\store\references)ry   r   r   r   rF   r   r   r   VCStoreRefs  s    

zEnvironmentInfo.VCStoreRefsc                 C   s   | j }t|jdg}| jdkr"dnd}| j|}|rL|t|jd| g7 }| jdkr|d| jjdd }|t|j|g7 }n| jdkr| j rd	nd
}|t|j|| jjdd g7 }| jj	| jj
kr|t|j|| jjdd g7 }n|t|jdg7 }|S )zr
        Microsoft Visual C++ Tools.

        Return
        ------
        list of str
            paths
        Z
VCPackagesr9   TFBin%sr0   r   r   zbin\HostX86%szbin\HostX64%sr   Bin)r   r   r   ry   r[   rU   rQ   rL   rR   rK   rG   )rC   r   toolsrT   r   r   Zhost_dirr   r   r   VCTools  s0    


  
zEnvironmentInfo.VCToolsc                 C   sh   | j dkr.| jjddd}t| jjd| gS | jjdd}t| jjd}| j}t|d||f gS dS )	zw
        Microsoft Windows SDK Libraries.

        Return
        ------
        list of str
            paths
        r9   Tr   r   r   r   z%sum%sN)ry   r[   rR   r   r   r   _sdk_subdir)rC   r   r   Zlibverr   r   r   OSLibraries  s    

zEnvironmentInfo.OSLibrariesc                 C   sh   t | jjd}| jdkr&|t |dgS | jdkr8| j}nd}t |d| t |d| t |d| gS d	S )
zu
        Microsoft Windows SDK Include.

        Return
        ------
        list of str
            paths
        includer9   Zglr0   r   z%ssharedz%sumz%swinrtN)r   r   r   ry   r   )rC   r   sdkverr   r   r   
OSIncludes  s    


zEnvironmentInfo.OSIncludesc                 C   s   t | jjd}g }| jdkr&|| j7 }| jdkr@|t |dg7 }| jdkr||t | jjdt |ddt |d	dt |d
dt | jjddd| j dddg7 }|S )z}
        Microsoft Windows SDK Libraries Paths.

        Return
        ------
        list of str
            paths
        Z
Referencesr7   r   zCommonConfiguration\Neutralr0   ZUnionMetadataz'Windows.Foundation.UniversalApiContractz1.0.0.0z%Windows.Foundation.FoundationContractz,Windows.Networking.Connectivity.WwanContractZExtensionSDKszMicrosoft.VCLibsr   ZCommonConfigurationZneutral)r   r   r   ry   r   )rC   reflibpathr   r   r   	OSLibpath  s@    




  
     zEnvironmentInfo.OSLibpathc                 C   s   t |  S )zs
        Microsoft Windows SDK Tools.

        Return
        ------
        list of str
            paths
        )list
_sdk_toolsrF   r   r   r   SdkTools
  s    
zEnvironmentInfo.SdkToolsc                 c   s   | j dk r,| j dkrdnd}t| jj|V  | j s\| jjdd}d| }t| jj|V  | j dkr| j rvd	}n| jjddd
}d| }t| jj|V  nB| j dkrt| jjd}| jjdd}| jj}t|d||f V  | jj	r| jj	V  dS )z
        Microsoft Windows SDK Tools paths generator.

        Return
        ------
        generator of str
            paths
        r   r   r   zBin\x86Tr   r   )r9   r   r   r   zBin\NETFX 4.0 Tools%sz%s%sN)
ry   r   r   r   r[   rL   rQ   rI   r   r   )rC   Zbin_dirr   r   r   r   r   r   r     s(    	




zEnvironmentInfo._sdk_toolsc                 C   s   | j j}|rd| S dS )zu
        Microsoft Windows SDK version subdir.

        Return
        ------
        str
            subdir
        %s\r   )r   r   rC   ucrtverr   r   r   r   9  s    
zEnvironmentInfo._sdk_subdirc                 C   s   | j dkrg S t| jjdgS )zs
        Microsoft Windows SDK Setup.

        Return
        ------
        list of str
            paths
        r7   ZSetup)ry   r   r   r   rF   r   r   r   SdkSetupF  s    

zEnvironmentInfo.SdkSetupc                    s   | j }| j | jdkr0d}|  o,|  }n$| p>| }|jdkpR|jdk}g }|rt| fdd jD 7 }|r| fdd jD 7 }|S )zv
        Microsoft .NET Framework Tools.

        Return
        ------
        list of str
            paths
        r9   TrA   c                    s   g | ]}t  j|qS r   )r   r   r   r+   r   r   r   r   k  s   z+EnvironmentInfo.FxTools.<locals>.<listcomp>c                    s   g | ]}t  j|qS r   )r   r   r   r   r   r   r   n  s   )	r[   r   ry   rI   rL   rK   rG   r   r   )rC   r[   Z	include32Z	include64r   r   r   r   FxToolsU  s"    

zEnvironmentInfo.FxToolsc                 C   s8   | j dk s| jjsg S | jjdd}t| jjd| gS )z~
        Microsoft .Net Framework SDK Libraries.

        Return
        ------
        list of str
            paths
        r0   Tr   zlib\um%s)ry   r   r   r[   rR   r   )rC   r   r   r   r   NetFxSDKLibrariesr  s    
z!EnvironmentInfo.NetFxSDKLibrariesc                 C   s&   | j dk s| jjsg S t| jjdgS )z}
        Microsoft .Net Framework SDK Includes.

        Return
        ------
        list of str
            paths
        r0   z
include\um)ry   r   r   r   rF   r   r   r   NetFxSDKIncludes  s    
z EnvironmentInfo.NetFxSDKIncludesc                 C   s   t | jjdgS )z
        Microsoft Visual Studio Team System Database.

        Return
        ------
        list of str
            paths
        zVSTSDB\Deployr   rF   r   r   r   VsTDb  s    
zEnvironmentInfo.VsTDbc                 C   sv   | j dk rg S | j dk r0| jj}| jjdd}n| jj}d}d| j |f }t||g}| j dkrr|t||dg7 }|S )zn
        Microsoft Build Engine.

        Return
        ------
        list of str
            paths
        r   r   Tr   r   zMSBuild\%0.1f\bin%sZRoslyn)ry   r   r   r[   rQ   r   r   )rC   	base_pathr   r   Zbuildr   r   r   MSBuild  s    



zEnvironmentInfo.MSBuildc                 C   s   | j dk rg S t| jjdgS )zt
        Microsoft HTML Help Workshop.

        Return
        ------
        list of str
            paths
        r   zHTML Help Workshop)ry   r   r   r   rF   r   r   r   HTMLHelpWorkshop  s    

z EnvironmentInfo.HTMLHelpWorkshopc                 C   sD   | j dk rg S | jjdd}t| jjd}| j}t|d||f gS )z
        Microsoft Universal C Runtime SDK Libraries.

        Return
        ------
        list of str
            paths
        r0   Tr   r   z%sucrt%s)ry   r[   rR   r   r   r   _ucrt_subdir)rC   r   r   r   r   r   r   UCRTLibraries  s    

zEnvironmentInfo.UCRTLibrariesc                 C   s.   | j dk rg S t| jjd}t|d| j gS )z
        Microsoft Universal C Runtime SDK Include.

        Return
        ------
        list of str
            paths
        r0   r   z%sucrt)ry   r   r   r   r   )rC   r   r   r   r   UCRTIncludes  s    

zEnvironmentInfo.UCRTIncludesc                 C   s   | j j}|rd| S dS )z
        Microsoft Universal C Runtime SDK version subdir.

        Return
        ------
        str
            subdir
        r   r   )r   r   r   r   r   r   r     s    
zEnvironmentInfo._ucrt_subdirc                 C   s(   d| j   krdkrn ng S | jjgS )zk
        Microsoft Visual F#.

        Return
        ------
        list of str
            paths
        r   r   )ry   r   r   rF   r   r   r   FSharp  s    
zEnvironmentInfo.FSharpc           
      C   s   d| j  }| jjddd}g }| jj}t|dd}t|rft	|t
|d }||t	|dg7 }|t	|d	g7 }d
| j d  d
t| jd  f}t||D ]&\}}t	||||}	t|	r|	  S qdS )z
        Microsoft Visual C++ runtime redistributable dll.

        Return
        ------
        str
            path
        zvcruntime%d0.dllTr   rS   z\Toolsz\Redistr8   ZonecoreZredistzMicrosoft.VC%d.CRT
   N)rz   r[   rR   stripr   r   r   rB   r   r   r   intry   	itertoolsproductr   )
rC   Z	vcruntimer   prefixesZ
tools_pathZredist_pathZcrt_dirsr   Zcrt_dirr   r   r   r   VCRuntimeRedist  s     

zEnvironmentInfo.VCRuntimeRedistTc                 C   s   t | d| j| j| j| jg|| d| j| j| j| j	| j
g|| d| j| j| j| jg|| d| j| j| j| j| j| j| j| j| jg	|d}| jdkrt| jr| j|d< |S )z
        Return environment dict.

        Parameters
        ----------
        exists: bool
            It True, only return existing paths.

        Return
        ------
        dict
            environment
        r   r   r   r   )r   r   r   r      Zpy_vcruntime_redist)dict_build_pathsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r   r   )rC   existsenvr   r   r   r)   )  sV    	 
zEnvironmentInfo.return_envc           
      C   sp   t j|}t|dt}t ||}|r<ttt	|n|}|s\d|
  }tj|| |}	t|	S )aC  
        Given an environment variable name and specified paths,
        return a pathsep-separated string of paths containing
        unique, extant, directories from those paths and from
        the environment variable. Raise an error if no paths
        are resolved.

        Parameters
        ----------
        name: str
            Environment variable name
        spec_path_lists: list of str
            Paths
        exists: bool
            It True, only return existing paths.

        Return
        ------
        str
            Pathsep-separated paths
        r   z %s environment variable is empty)r   chainfrom_iterabler   rW   r   r   r   filterr   upperr$   r%   r&   _unique_everseenr   )
rC   rm   Zspec_path_listsr   Z
spec_pathsZ	env_pathsr   Zextant_pathsr   Zunique_pathsr   r   r   r   [  s    
zEnvironmentInfo._build_pathsc                 c   sb   t  }|j}|dkr6t|j| D ]}|| |V  q n(| D ]"}||}||kr:|| |V  q:dS )z
        List unique elements, preserving order.
        Remember all elements ever seen.

        _unique_everseen('AAAABBBCCDAABBB') --> A B C D

        _unique_everseen('ABBCcAD', str.lower) --> A B C D
        N)r|   addr
   __contains__)iterabler   seenZseen_addelementkr   r   r   r  }  s    

z EnvironmentInfo._unique_everseen)Nr   )T)N)#r   r   r   rV   rD   rX   ry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r   r   r  r   r   r   r   r(     sn   
	






$


#
#












"
2"r(   )r"   )r   )*rV   r   ior   osr   r   os.pathr   r   r   r   r3   platformr   Zdistutils.errorsr$   Z#setuptools.extern.packaging.versionr	   Zsetuptools.extern.six.movesr
   Zmonkeyr   systemr   r   r   ImportErrorr%   r&   Z_msvc9_suppress_errorsZdistutils.msvc9compilerr   r   r#   r2   r6   r*   r?   rY   rr   r(   r   r   r   r   <module>   sJ   	*
& 
$s 5    