
§|jã�  ã               @   sw  d  Z  d d l 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 j ƒ  d k r  d d l	 m Z e j Z n Gd	 d
 „  d
 ƒ Z e ƒ  Z y d d l m Z Wn e k
 rä Yn Xd d „  Z d d d „ Z d d „  Z d d „  Z d d d „ Z Gd d „  d ƒ Z Gd d „  d ƒ Z Gd d „  d ƒ Z Gd 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 7.0 (x86, x64, ia64);
    Microsoft Windows SDK 6.1 (x86, x64, ia64)

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

Microsoft Visual C++ 14.0:
    Microsoft Visual C++ Build Tools 2015 (x86, x64, arm)
é    N)ÚLegacyVersion)Úfilterfalseé   )Úget_unpatchedÚWindows)Úwinregc               @   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   ú5/tmp/pip-build-5c7ija6t/setuptools/setuptools/msvc.pyr   &   s   r   )ÚRegc             C   sÉ   d } | d |  f } y t  j | d ƒ } WnS t k
 r� y& | d |  f } t  j | d ƒ } Wn t k
 r| d } Yn XYn X| r¹ t j j j j | d ƒ } t j j | ƒ r¹ | St t ƒ |  ƒ S)a+  
    Patched "distutils.msvc9compiler.find_vcvarsall" to use the standalone
    compiler build for Python (VCForPython). Fall back to original behavior
    when the standalone compiler is not available.

    Redirect the path of "vcvarsall.bat".

    Known supported compilers
    -------------------------
    Microsoft Visual C++ 9.0:
        Microsoft Visual C++ Compiler for Python 2.7 (x86, amd64)

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

    Return
    ------
    vcvarsall.bat path: str
    z-Software\%sMicrosoft\DevDiv\VCForPython\%0.1fÚ Ú
installdirzWow6432Node\Nzvcvarsall.bat)	r   Ú	get_valueÚKeyErrorÚosÚpathÚjoinÚisfiler   Úmsvc9_find_vcvarsall)ÚversionZVC_BASEÚkeyÚ
productdirÚ	vcvarsallr   r   r   r   4   s    r   Úx86c             O   s«   y# t  t ƒ } | |  | | | Ž SWn) t j j k
 r= Yn t k
 rN Yn Xy t | |  ƒ j ƒ  SWn> t j j k
 r¦ } z t | |  | ƒ ‚  WYd d } ~ Xn Xd S)a¤  
    Patched "distutils.msvc9compiler.query_vcvarsall" for support standalones
    compilers.

    Set environment without use of "vcvarsall.bat".

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

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

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

    Return
    ------
    environment: dict
    N)	r   Úmsvc9_query_vcvarsallÚ	distutilsÚerrorsÚDistutilsPlatformErrorÚ
ValueErrorÚEnvironmentInfoÚ
return_envÚ_augment_exception)ÚverÚarchÚargsÚkwargsÚorigÚexcr   r   r   r    _   s    r    c             C   s‹   y t  t ƒ |  ƒ SWn t j j k
 r. Yn Xy t |  d d ƒj ƒ  SWn; t j j k
 r† } z t | d ƒ ‚  WYd d } ~ Xn Xd S)a©  
    Patched "distutils._msvccompiler._get_vc_env" for support standalones
    compilers.

    Set environment without use of "vcvarsall.bat".

    Known supported compilers
    -------------------------
    Microsoft Visual C++ 14.0:
        Microsoft Visual C++ Build Tools 2015 (x86, x64, arm)

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

    Return
    ------
    environment: dict
    Ú
vc_min_verg      ,@N)r   Úmsvc14_get_vc_envr!   r"   r#   r%   r&   r'   )Z	plat_specr-   r   r   r   r/   Ž   s    r/   c              O   s_   d t  j k rL d d l } t | j ƒ t d ƒ k  rL | j j j |  | Ž  St 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+   Únpr   r   r   r3   ²   s
    r3   r   c             C   së   |  j  d } d | j ƒ  k s1 d | j ƒ  k rÛ d } | j t ƒ  �  } d } | d k rž | j ƒ  j d ƒ d k r‘ | d	 7} | | 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=%dg      "@Zia64r   z* Get it with "Microsoft Windows SDK 7.0": iB  z% Get it from http://aka.ms/vcpython27g      $@z* Get it with "Microsoft Windows SDK 7.1": iW   g      ,@zj Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-toolsNéÿÿÿÿ)r*   ÚlowerÚformatÚlocalsÚfind)r-   r   r)   ÚmessageÚtmplZ
msdownloadr   r   r   r'   ¿   s     $


r'   c               @   s—   e  Z d  Z d Z e j d d ƒ j ƒ  Z d d „  Z e	 d d „  ƒ Z
 d d	 „  Z d
 d „  Z d d d d „ Z d d d d „ Z d d d „ Z d S)ÚPlatformInfoz‚
    Current and Target Architectures informations.

    Parameters
    ----------
    arch: str
        Target architecture.
    Zprocessor_architecturer   c             C   s   | j  ƒ  j d d ƒ |  _ d  S)NÚx64Úamd64)r6   Úreplacer)   )Úselfr)   r   r   r   Ú__init__ï   s    zPlatformInfo.__init__c             C   s!   |  j  |  j  j d ƒ d d  … S)NÚ_r   )r)   r9   )r@   r   r   r   Ú
target_cpuò   s    zPlatformInfo.target_cpuc             C   s   |  j  d k S)Nr   )rC   )r@   r   r   r   Útarget_is_x86ö   s    zPlatformInfo.target_is_x86c             C   s   |  j  d k S)Nr   )Úcurrent_cpu)r@   r   r   r   Úcurrent_is_x86ù   s    zPlatformInfo.current_is_x86Fc             C   s=   |  j  d k r | r d S|  j  d k r2 | r2 d Sd |  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
        ------
        subfolder: str
            '	arget', or '' (see hidex86 parameter)
        r   r   r>   z\x64z\%s)rE   )r@   Úhidex86r=   r   r   r   Úcurrent_dirü   s    zPlatformInfo.current_dirc             C   s=   |  j  d k r | r d S|  j  d k r2 | r2 d Sd |  j  S)ul  
        Target 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
        ------
        subfolder: str
            '\current', or '' (see hidex86 parameter)
        r   r   r>   z\x64z\%s)rC   )r@   rG   r=   r   r   r   Ú
target_dir  s    zPlatformInfo.target_dirc             C   sB   | r d n |  j  } |  j | k r( d S|  j ƒ  j d d | ƒ S)ao  
        Cross platform specific subfolder.

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

        Return
        ------
        subfolder: str
            '' if target architecture is current architecture,
            '\current_target' if not.
        r   r   ú\z\%s_)rE   rC   rI   r?   )r@   Úforcex86Úcurrentr   r   r   Ú	cross_dir(  s    zPlatformInfo.cross_dirN)r   r	   r
   Ú__doc__Úsafe_envÚgetr6   rE   rA   ÚpropertyrC   rD   rF   rH   rI   rM   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 informations.

    Parameters
    ----------
    platform_info: PlatformInfo
        "PlatformInfo" instance.
    c             C   s   | |  _  d  S)N)Úpi)r@   Zplatform_infor   r   r   rA   M  s    zRegistryInfo.__init__c             C   s   d S)z<
        Microsoft Visual Studio root registry key.
        ZVisualStudior   )r@   r   r   r   ÚvisualstudioP  s    zRegistryInfo.visualstudioc             C   s   t  j j |  j d ƒ S)z;
        Microsoft Visual Studio SxS registry key.
        ZSxS)r   r   r   rT   )r@   r   r   r   ÚsxsW  s    zRegistryInfo.sxsc             C   s   t  j j |  j d ƒ S)z8
        Microsoft Visual C++ VC7 registry key.
        ZVC7)r   r   r   rU   )r@   r   r   r   Úvc^  s    zRegistryInfo.vcc             C   s   t  j j |  j d ƒ S)z;
        Microsoft Visual Studio VS7 registry key.
        ZVS7)r   r   r   rU   )r@   r   r   r   Úvse  s    zRegistryInfo.vsc             C   s   d S)z?
        Microsoft Visual C++ for Python registry key.
        zDevDiv\VCForPythonr   )r@   r   r   r   Úvc_for_pythonl  s    zRegistryInfo.vc_for_pythonc             C   s   d S)z-
        Microsoft SDK registry key.
        zMicrosoft SDKsr   )r@   r   r   r   Úmicrosoft_sdks  s    zRegistryInfo.microsoft_sdkc             C   s   t  j j |  j d ƒ S)z>
        Microsoft Windows/Platform SDK registry key.
        r   )r   r   r   rY   )r@   r   r   r   Úwindows_sdkz  s    zRegistryInfo.windows_sdkc             C   s   t  j j |  j d ƒ S)z<
        Microsoft .NET Framework SDK registry key.
        ZNETFXSDK)r   r   r   rY   )r@   r   r   r   Ú	netfx_sdk�  s    zRegistryInfo.netfx_sdkc             C   s   d S)z<
        Microsoft Windows Kits Roots registry key.
        zWindows Kits\Installed Rootsr   )r@   r   r   r   Úwindows_kits_rootsˆ  s    zRegistryInfo.windows_kits_rootsFc             C   s:   |  j  j ƒ  s | r d n d } t j j 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: value
        r   z\Wow6432NodeZSoftwareÚ	Microsoft)rS   rF   r   r   r   )r@   r   r   Znode64r   r   r   Ú	microsoft�  s    !zRegistryInfo.microsoftc             C   sõ   t  j } t  j } |  j } xÓ |  j D]È } y | | | | ƒ d | ƒ } Wnm t t f k
 r¹ |  j j ƒ  s² y" | | | | d ƒ d | ƒ } Wqµ t t f k
 r® w% Yqµ Xn w% Yn Xy t  j	 | | ƒ d SWq% t t f k
 rì Yq% Xq% Wd 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ÚOpenKeyr^   ÚHKEYSÚOSErrorÚIOErrorrS   rF   ÚQueryValueEx)r@   r   Únamer_   ZopenkeyÚmsÚhkeyÚbkeyr   r   r   Úlookup¡  s"    			"zRegistryInfo.lookupN)r   r	   r
   rN   r   r   r   r   r   ra   rA   rQ   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r^   ri   r   r   r   r   rR   ?  s"   rR   c               @   s`  e  Z d  Z d Z e j d d ƒ Z e j d d ƒ Z e j d e ƒ Z d 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! d" „  ƒ Z e d# d$ „  ƒ Z d% d& d' „ Z d S)(Ú
SystemInfozê
    Microsoft Windows and Visual Studio related system inormations.

    Parameters
    ----------
    registry_info: RegistryInfo
        "RegistryInfo" instance.
    vc_ver: float
        Required Microsoft Visual C++ version.
    ÚWinDirr   ÚProgramFileszProgramFiles(x86)Nc             C   sr   | |  _  |  j  j |  _ | r* | |  _ nD y |  j ƒ  d |  _ Wn* t k
 rm d } t j j | ƒ ‚ Yn Xd  S)Nr   z%No Microsoft Visual C++ version foundr5   )ÚrirS   Úvc_verÚfind_available_vc_versÚ
IndexErrorr!   r"   r#   )r@   Zregistry_inforn   Úerrr   r   r   rA   Ö  s    	zSystemInfo.__init__c             C   sk  |  j  j |  j  j f } g  } x@|  j  j D]2} x)| D]!} y t j | | d t j ƒ } Wn t t f k
 rz w8 Yn Xt j	 | ƒ \ } } } xb t
 | ƒ D]T }	 y9 t t j | |	 ƒ d ƒ }
 |
 | k rÞ | j |
 ƒ Wq  t k
 ró Yq  Xq  Wx^ t
 | ƒ D]P }	 y5 t t j | |	 ƒ ƒ }
 |
 | k r?| j |
 ƒ Wqt k
 rTYqXqWq8 Wq+ Wt | ƒ S)zC
        Find all available Microsoft Visual C++ versions.
        r   )rm   rV   rX   ra   r   r`   r_   rb   rc   ZQueryInfoKeyÚrangeÚfloatZ	EnumValueÚappendr$   ÚEnumKeyÚsorted)r@   ZvckeysZvc_versrg   r   rh   ZsubkeysÚvaluesrB   Úir(   r   r   r   ro   â  s0    	z!SystemInfo.find_available_vc_versc             C   sK   d |  j  } t j j |  j | ƒ } |  j j |  j j d |  j  ƒ pJ | S)z4
        Microsoft Visual Studio directory.
        zMicrosoft Visual Studio %0.1fz%0.1f)rn   r   r   r   ÚProgramFilesx86rm   ri   rW   )r@   re   Údefaultr   r   r   ÚVSInstallDirÿ  s    zSystemInfo.VSInstallDirc             C   sÓ   d |  j  } t j j |  j | ƒ } t j j |  j j d |  j  ƒ } |  j j | d ƒ } | rw t j j | d ƒ n | } |  j j |  j j d |  j  ƒ p¢ | } t j j	 | ƒ sÏ d } t
 j j | ƒ ‚ | S)z1
        Microsoft Visual C++ directory.
        z Microsoft Visual Studio %0.1f\VCz%0.1fr   ZVCz(Microsoft Visual C++ directory not found)rn   r   r   r   ry   rm   rX   ri   rV   Úisdirr!   r"   r#   )r@   rz   Zguess_vcZreg_pathZ	python_vcZ
default_vcr   Úmsgr   r   r   ÚVCInstallDir  s    "!(zSystemInfo.VCInstallDirc             C   sc   |  j  d k r d S|  j  d k r& d S|  j  d k r9 d S|  j  d k rL d S|  j  d k r_ d Sd S)z1
        Microsoft Windows SDK versions.
        g      "@ú7.0ú6.1ú6.0ag      $@ú7.1ú7.0ag      &@ú8.0ú8.0ag      (@ú8.1ú8.1ag      ,@ú10.0N)r   r€   r�   )r‚   rƒ   )r„   r…   )r†   r‡   )rˆ   r†   )rn   )r@   r   r   r   ÚWindowsSdkVersion"  s    zSystemInfo.WindowsSdkVersionc             C   sÌ  d } xL |  j  D]A } t j j |  j j d | ƒ } |  j j | d ƒ } | r Pq W| so t j j | ƒ rÁ t j j |  j j d |  j	 ƒ } |  j j | d ƒ } | rÁ t j j | d ƒ } | sÛ t j j | ƒ rBxd |  j  D]Y } | d | j
 d ƒ … } d	 | } t j j |  j | ƒ } t j j | ƒ rå | } qå W| s\t j j | ƒ rªxK |  j  D]@ } d
 | } t j j |  j | ƒ } t j j | ƒ rf| } qfW| sÈt j j |  j d ƒ } | S)z2
        Microsoft Windows SDK directory.
        r   zv%sÚinstallationfolderz%0.1fr   ZWinSDKNÚ.zMicrosoft SDKs\Windows Kits\%szMicrosoft SDKs\Windows\v%sZPlatformSDK)r‰   r   r   r   rm   rZ   ri   r|   rX   rn   Úrfindrl   r~   )r@   Úsdkdirr(   Úlocr   Úinstall_baseZintverÚdr   r   r   ÚWindowsSdkDir3  s6    "



zSystemInfo.WindowsSdkDirc       	      C   s2  |  j  d k r d } d } n< d } |  j  d k r9 d n d } |  j j d d d	 | ƒ } d
 | | j d d ƒ f } g  } |  j  d k rÄ x6 |  j D]+ } | t j j |  j j	 | | ƒ g 7} q• Wx: |  j
 D]/ } | t j j |  j j d | | ƒ g 7} qÎ Wx* | D]" } |  j j | d ƒ } | rPqW| S)z=
        Microsoft Windows SDK executable directory.
        g      &@é#   r   é(   g      (@TFr=   rG   zWinSDK-NetFx%dTools%srJ   ú-g      ,@zv%sArŠ   )rn   rS   rH   r?   ÚNetFxSdkVersionr   r   r   rm   r[   r‰   rZ   ri   )	r@   Znetfxverr)   rG   ZfxZregpathsr(   r   Zexecpathr   r   r   ÚWindowsSDKExecutablePathY  s$    	)-z#SystemInfo.WindowsSDKExecutablePathc             C   sA   d |  j  } t j j |  j j | ƒ } |  j j | d ƒ p@ d S)z0
        Microsoft Visual F# directory.
        z%0.1f\Setup\F#r   r   )rn   r   r   r   rm   rT   ri   )r@   r   r   r   r   ÚFSharpInstallDirx  s    zSystemInfo.FSharpInstallDirc             C   s_   |  j  d k r d } n f  } x4 | D], } |  j j |  j j d | ƒ } | r% Pq% W| p^ d S)z8
        Microsoft Universal CRT SDK directory.
        g      ,@Ú10Ú81z
kitsroot%sr   )r˜   r™   )rn   rm   ri   r\   )r@   Úversr(   r�   r   r   r   ÚUniversalCRTSdkDir�  s    	zSystemInfo.UniversalCRTSdkDirc             C   s   |  j  d k r d Sf  Sd S)z8
        Microsoft .NET Framework SDK versions.
        g      ,@ú4.6.1ú4.6N)rœ   r�   )rn   )r@   r   r   r   r•   ”  s    zSystemInfo.NetFxSdkVersionc             C   sU   xH |  j  D]= } t j j |  j j | ƒ } |  j j | d ƒ } | r
 Pq
 W| pT d S)z9
        Microsoft .NET Framework SDK directory.
        Zkitsinstallationfolderr   )r•   r   r   r   rm   r[   ri   )r@   r(   rŽ   r�   r   r   r   ÚNetFxSdkDirŸ  s    zSystemInfo.NetFxSdkDirc             C   s7   t  j j |  j d ƒ } |  j j |  j j d ƒ p6 | S)z;
        Microsoft .NET Framework 32bit directory.
        zMicrosoft.NET\FrameworkZframeworkdir32)r   r   r   rk   rm   ri   rV   )r@   Úguess_fwr   r   r   ÚFrameworkDir32«  s    zSystemInfo.FrameworkDir32c             C   s7   t  j j |  j d ƒ } |  j j |  j j d ƒ p6 | S)z;
        Microsoft .NET Framework 64bit directory.
        zMicrosoft.NET\Framework64Zframeworkdir64)r   r   r   rk   rm   ri   rV   )r@   rŸ   r   r   r   ÚFrameworkDir64¶  s    zSystemInfo.FrameworkDir64c             C   s   |  j  d ƒ S)z:
        Microsoft .NET Framework 32bit versions.
        é    )Ú_find_dot_net_versions)r@   r   r   r   ÚFrameworkVersion32Á  s    zSystemInfo.FrameworkVersion32c             C   s   |  j  d ƒ S)z:
        Microsoft .NET Framework 64bit versions.
        é@   )r£   )r@   r   r   r   ÚFrameworkVersion64È  s    zSystemInfo.FrameworkVersion64r¢   c             C   s±   |  j  j |  j  j d | ƒ p" d } |  j d k rC | d f } nU |  j d k rƒ | j ƒ  d d … d k rt d	 n | d
 f } n |  j d k r˜ d } |  j d k r­ d } | S)z¡
        Find Microsoft .NET Framework versions.

        Parameters
        ----------
        bits: int
            Platform number of bits: 32 or 64.
        zframeworkver%dr   g      (@zv4.0g      $@Né   Zv4z
v4.0.30319úv3.5g      "@ú
v2.0.50727g       @úv3.0)r¨   r©   )rª   r©   )rm   ri   rV   rn   r6   )r@   Úbitsr(   Zframeworkverr   r   r   r£   Ï  s    
%%z!SystemInfo._find_dot_net_versions)r   r	   r
   rN   rO   rP   rk   rl   ry   rA   ro   rQ   r{   r~   r‰   r‘   r–   r—   r›   r•   rž   r    r¡   r¤   r¦   r£   r   r   r   r   rj   Ä  s(   
&	rj   c               @   sØ  e  Z d  Z d Z d 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 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/ d0 d1 „ Z d2 d3 „  Z d d4 d5 „ Z d6 d7 „  Z d S)8r%   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.0.

    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.
    Nc             C   sg   t  | ƒ |  _ t |  j ƒ |  _ t |  j | ƒ |  _ | rc |  j | k  rc d } t j j	 | ƒ ‚ d  S)Nz.No suitable Microsoft Visual C++ version found)
r<   rS   rR   rm   rj   Úsirn   r!   r"   r#   )r@   r)   rn   r.   rq   r   r   r   rA      s    zEnvironmentInfo.__init__c             C   s
   |  j  j S)z/
        Microsoft Visual C++ version.
        )r¬   rn   )r@   r   r   r   rn   
  s    zEnvironmentInfo.vc_verc                sx   d d g } ˆ  j  d k ra ˆ  j j d d d d ƒ } | d g 7} | d g 7} | d	 | g 7} ‡  f d
 d †  | Dƒ S)z/
        Microsoft Visual Studio Tools
        zCommon7\IDEzCommon7\Toolsg      ,@rG   Tr=   z1Common7\IDE\CommonExtensions\Microsoft\TestWindowzTeam Tools\Performance ToolszTeam Tools\Performance Tools%sc                s+   g  |  ]! } t  j j ˆ  j j | ƒ ‘ q Sr   )r   r   r   r¬   r{   )Ú.0r   )r@   r   r   ú
<listcomp>  s   	 z+EnvironmentInfo.VSTools.<locals>.<listcomp>)rn   rS   rH   )r@   ÚpathsÚarch_subdirr   )r@   r   ÚVSTools  s    zEnvironmentInfo.VSToolsc             C   s4   t  j j |  j j d ƒ t  j j |  j j d ƒ g S)zL
        Microsoft Visual C++ & Microsoft Foundation Class Includes
        ÚIncludezATLMFC\Include)r   r   r   r¬   r~   )r@   r   r   r   Ú
VCIncludes   s    zEnvironmentInfo.VCIncludesc                s`   ˆ  j  j d d ƒ } d | d | g } ˆ  j d k rI | d | g 7} ‡  f d d †  | Dƒ S)	zM
        Microsoft Visual C++ & Microsoft Foundation Class Libraries
        rG   TzLib%szATLMFC\Lib%sg      ,@zLib\store%sc                s+   g  |  ]! } t  j j ˆ  j j | ƒ ‘ q Sr   )r   r   r   r¬   r~   )r­   r   )r@   r   r   r®   3  s   	 z/EnvironmentInfo.VCLibraries.<locals>.<listcomp>)rS   rI   rn   )r@   r°   r¯   r   )r@   r   ÚVCLibraries(  s
    zEnvironmentInfo.VCLibrariesc             C   s/   |  j  d k  r g  St j j |  j j d ƒ g S)zA
        Microsoft Visual C++ store references Libraries
        g      ,@zLib\store\references)rn   r   r   r   r¬   r~   )r@   r   r   r   ÚVCStoreRefs5  s    zEnvironmentInfo.VCStoreRefsc             C   sç   |  j  } t j j | j d ƒ g } |  j d k r9 d n d } |  j j | ƒ } | rz | t j j | j d | ƒ g 7} |  j d k rÄ d |  j j d d ƒ } | t j j | j | ƒ g 7} n | t j j | j d ƒ g 7} | S)	z,
        Microsoft Visual C++ Tools
        Z
VCPackagesg      $@TFzBin%sg      ,@rG   ÚBin)	r¬   r   r   r   r~   rn   rS   rM   rH   )r@   r¬   ÚtoolsrK   r°   r   r   r   r   ÚVCTools>  s    	#"zEnvironmentInfo.VCToolsc             C   s­   |  j  d k rJ |  j j d d d d ƒ } t j j |  j j d | ƒ g S|  j j d d ƒ } t j j |  j j d ƒ } |  j | ƒ } t j j | d | | f ƒ g Sd S)	z1
        Microsoft Windows SDK Libraries
        g      $@rG   Tr=   zLib%sÚlibz%sum%sN)	rn   rS   rI   r   r   r   r¬   r‘   Ú_get_content_dirname)r@   r°   r¹   Zlibverr   r   r   ÚOSLibrariesT  s     zEnvironmentInfo.OSLibrariesc             C   s´   t  j j |  j j d ƒ } |  j d k rC | t  j j | d ƒ g S|  j d k rd |  j | ƒ } n d } t  j j | d | ƒ t  j j | d | ƒ t  j j | d | ƒ g Sd	 S)
z/
        Microsoft Windows SDK Include
        Úincludeg      $@Úglg      ,@r   z%ssharedz%sumz%swinrtN)r   r   r   r¬   r‘   rn   rº   )r@   r¼   Zsdkverr   r   r   Ú
OSIncludesc  s    zEnvironmentInfo.OSIncludesc             C   s  t  j j |  j j d ƒ } g  } |  j d k r= | |  j 7} |  j d k rh | t  j j | d ƒ g 7} |  j d k r	| | t  j j |  j j d ƒ t  j j | d d ƒ t  j j | d	 d ƒ t  j j | d
 d ƒ t  j j |  j j d d d |  j d d d ƒ g 7} | S)z7
        Microsoft Windows SDK Libraries Paths
        Z
Referencesg      "@g      &@zCommonConfiguration\Neutralg      ,@ZUnionMetadataz'Windows.Foundation.UniversalApiContractz1.0.0.0z%Windows.Foundation.FoundationContractz,Windows.Networking.Connectivity.WwanContractZExtensionSDKszMicrosoft.VCLibsz%0.1fZCommonConfigurationZneutral)r   r   r   r¬   r‘   rn   r»   )r@   ÚrefÚlibpathr   r   r   Ú	OSLibpathv  s>    					
zEnvironmentInfo.OSLibpathc             C   s)  |  j  d k r d n d } t j j |  j j | ƒ g } |  j j ƒ  s‰ |  j j d d ƒ } d | } | t j j |  j j | ƒ g 7} |  j  d k s§ |  j  d k r|  j j	 ƒ  r¿ d } n |  j j d	 d d d ƒ } d
 | } | t j j |  j j | ƒ g 7} |  j j
 r%| |  j j
 g 7} | S)z-
        Microsoft Windows SDK Tools
        g      &@r¶   zBin\x86r=   TzBin%sg      $@r   rG   zBin\NETFX 4.0 Tools%s)rn   r   r   r   r¬   r‘   rS   rF   rH   rD   r–   )r@   Úbin_dirr·   r°   r   r   r   r   ÚSdkTools£  s    
"	
"zEnvironmentInfo.SdkToolsc             C   s/   |  j  d k r g  St j j |  j j d ƒ g S)z-
        Microsoft Windows SDK Setup
        g      "@ZSetup)rn   r   r   r   r¬   r‘   )r@   r   r   r   ÚSdkSetup½  s    zEnvironmentInfo.SdkSetupc                sÐ   |  j  } |  j ‰  |  j d k rD d } | j ƒ  o> | j ƒ  } n6 | j ƒ  pY | j ƒ  } | j d k pw | j d k } g  } | r¦ | ‡  f d d †  ˆ  j Dƒ 7} | rÌ | ‡  f d d †  ˆ  j Dƒ 7} | S)z0
        Microsoft .NET Framework Tools
        g      $@Tr>   c                s(   g  |  ] } t  j j ˆ  j | ƒ ‘ q Sr   )r   r   r   r    )r­   r(   )r¬   r   r   r®   Ø  s   	z+EnvironmentInfo.FxTools.<locals>.<listcomp>c                s(   g  |  ] } t  j j ˆ  j | ƒ ‘ q Sr   )r   r   r   r¡   )r­   r(   )r¬   r   r   r®   Û  s   	)	rS   r¬   rn   rD   rF   rE   rC   r¤   r¦   )r@   rS   Z	include32Z	include64r·   r   )r¬   r   ÚFxToolsÇ  s    		zEnvironmentInfo.FxToolsc             C   sU   |  j  d k  s |  j j r  g  S|  j j d d ƒ } t j j |  j j d | ƒ g S)z8
        Microsoft .Net Framework SDK Libraries
        g      ,@r=   Tzlib\um%s)rn   r¬   rž   rS   rI   r   r   r   )r@   r°   r   r   r   ÚNetFxSDKLibrariesß  s    z!EnvironmentInfo.NetFxSDKLibrariesc             C   s<   |  j  d k  s |  j j r  g  St j j |  j j d ƒ g S)z7
        Microsoft .Net Framework SDK Includes
        g      ,@z
include\um)rn   r¬   rž   r   r   r   )r@   r   r   r   ÚNetFxSDKIncludesê  s    z EnvironmentInfo.NetFxSDKIncludesc             C   s   t  j j |  j j d ƒ g S)z>
        Microsoft Visual Studio Team System Database
        zVSTSDB\Deploy)r   r   r   r¬   r{   )r@   r   r   r   ÚVsTDbô  s    zEnvironmentInfo.VsTDbc             C   sW   |  j  d k  r g  S|  j j d d ƒ } d |  j  | f } t j j |  j j | ƒ g S)z(
        Microsoft Build Engine
        g      (@rG   TzMSBuild\%0.1f\bin%s)rn   rS   rH   r   r   r   r¬   ry   )r@   r°   r   r   r   r   ÚMSBuildû  s
    zEnvironmentInfo.MSBuildc             C   s/   |  j  d k  r g  St j j |  j j d ƒ g S)z.
        Microsoft HTML Help Workshop
        g      &@zHTML Help Workshop)rn   r   r   r   r¬   ry   )r@   r   r   r   ÚHTMLHelpWorkshop  s    z EnvironmentInfo.HTMLHelpWorkshopc             C   sr   |  j  d k  r g  S|  j j d d ƒ } t j j |  j j d ƒ } |  j | ƒ } t j j | d | | f ƒ g S)z3
        Microsoft Universal CRT Libraries
        g      ,@r=   Tr¹   z%sucrt%s)	rn   rS   rI   r   r   r   r¬   r›   rº   )r@   r°   r¹   Úucrtverr   r   r   ÚUCRTLibraries  s    zEnvironmentInfo.UCRTLibrariesc             C   sW   |  j  d k  r g  St j j |  j j d ƒ } |  j | ƒ } t j j | d | ƒ g S)z1
        Microsoft Universal CRT Include
        g      ,@r¼   z%sucrt)rn   r   r   r   r¬   r›   rº   )r@   r¼   rË   r   r   r   ÚUCRTIncludes  s
    zEnvironmentInfo.UCRTIncludesc             C   s,   |  j  d k  r" |  j  d k r" g  S|  j j S)z%
        Microsoft Visual F#
        g      &@g      (@)rn   r¬   r—   )r@   r   r   r   ÚFSharp*  s    zEnvironmentInfo.FSharpc             C   sM   |  j  j d d ƒ } d } | | |  j |  j f } t j j |  j j | ƒ S)zA
        Microsoft Visual C++ runtime redistribuable dll
        r=   Tz-redist%s\Microsoft.VC%d0.CRT\vcruntime%d0.dll)rS   rI   rn   r   r   r   r¬   r~   )r@   r°   Z	vcruntimer   r   r   ÚVCRuntimeRedist4  s    zEnvironmentInfo.VCRuntimeRedistTc             C   s  t  d |  j d |  j |  j |  j |  j g | ƒ d |  j d |  j |  j |  j |  j	 |  j
 g | ƒ d |  j d |  j |  j |  j |  j g | ƒ d |  j d |  j |  j |  j |  j |  j |  j |  j |  j |  j g	 | ƒ ƒ } |  j d k rt j j |  j ƒ r|  j | d <| S)z—
        Return environment dict.

        Parameters
        ----------
        exists: bool
            It True, only return existing paths.
        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Î   rn   r   r   r   rÏ   )r@   ÚexistsÚenvr   r   r   r&   >  sD    												$zEnvironmentInfo.return_envc       
      C   s°   t  j j | ƒ } t j | d ƒ j t j ƒ } t  j | | ƒ } | rc t t	 t j
 j | ƒ ƒ n | } | s‘ d | j ƒ  } t j j | ƒ ‚ |  j | ƒ }	 t j j |	 ƒ S)a  
        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.
        r   z %s environment variable is empty)Ú	itertoolsÚchainÚfrom_iterablerO   rP   Úsplitr   ÚpathsepÚlistÚfilterr   r|   Úupperr!   r"   r#   Ú_unique_everseenr   )
r@   re   Zspec_path_listsrÓ   Z
spec_pathsZ	env_pathsr¯   Zextant_pathsr}   Zunique_pathsr   r   r   rÒ   k  s    	'zEnvironmentInfo._build_pathsc             c   s‰   t  ƒ  } | j } | d k rM xd t | j | ƒ D] } | | ƒ | Vq1 Wn8 x5 | D]- } | | ƒ } | | k rT | | ƒ | VqT Wd 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)ÚsetÚaddr   Ú__contains__)r@   Úiterabler   ÚseenZseen_addÚelementÚkr   r   r   rÝ     s    			

z EnvironmentInfo._unique_everseenc             C   sL   y) t  j | ƒ } | r$ d | d Sd SWn t t f k
 rG d SYn Xd S)zù
        Return name of the first dir in path or '' if no dir found.

        Parameters
        ----------
        path: str
            Path where search dir.

        Return
        ------
        foldername: str
            "name" or ""
        z%s\r   r   N)r   Úlistdirrb   rc   )r@   r   re   r   r   r   rº   •  s    z$EnvironmentInfo._get_content_dirname)r   r	   r
   rN   rA   rQ   rn   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%   è  s6   
	-




-r%   )rN   r   r0   ÚplatformrÕ   Zdistutils.errorsr!   Z&pkg_resources.extern.packaging.versionr   Zsetuptools.extern.six.movesr   Zmonkeyr   Úsystemr   ÚenvironrO   rÑ   Zdistutils.msvc9compilerr   ÚImportErrorr   r    r/   r3   r'   r<   rR   rj   r%   r   r   r   r   Ú<module>   s6   	+/$%[…ÿ %