3
ßL]�b  ã               @   s  d Z ddl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	Z	ddl
Zejd dk rdddlmZ nddlmZ ddlZddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZmZm Z m!Z! dd„ Z"e"ej#_$ej%dƒZ&ej%dƒZ'G dd„ dej#j(ƒZ)dd„ Z*dd„ Z+dd„ Z,dd„ Z-d?dd„Z.dd„ Z/dd „ Z0d!d"„ Z1d#d$„ Z2i Z3ej4d%k�rÔyXddl5Z5d&e3d'< d(e3d)< d*e3d+< e6e5d,ƒ�r°e5j7j8d-d.ƒ\Z9Z:Z;e5j7e3e9e: < [9[:[;W n  e<k
�rÒ   ej=d/ƒ Y nX d0d1„ Z>d@d3d4„Z?d5d6„ Z@d7d8„ ZAd9d:„ ZBd;d<„ ZCd=d>„ ZDdS )Aa  
Support code for building Python extensions on Windows.

    # NT stuff
    # 1. Make sure libpython<version>.a exists for gcc.  If not, build it.
    # 2. Force windows to use gcc (we're struggling with MSVC and g77 support)
    # 3. Force windows to use g77

é    )ÚdivisionÚabsolute_importÚprint_functionNé   é   )Úlog)ÚStrictVersion)ÚUnixCCompiler)Úget_build_version)ÚDistutilsExecErrorÚCompileErrorÚUnknownFileError)Úmsvc_runtime_libraryÚmsvc_runtime_versionÚmsvc_runtime_majorÚget_build_architecturec              C   s   t ƒ } | dkrg S | gS )zBReplacement for outdated version of get_msvcr from cygwinccompilerN)r   )Zmsvcr© r   úC/tmp/pip-install-q_7g0qgj/Numpy/numpy/distutils/mingw32ccompiler.pyÚget_msvcr_replacement*   s    r   z\[Ordinal/Name Pointer\] Tablez$^\s+\[([\s*[0-9]*)\] ([a-zA-Z0-9_]*)c               @   s2   e Zd ZdZdZddd„Zddd„Zdd
d„ZdS )ÚMingw32CCompilerzH A modified MingW32 compiler compatible with an MSVC built Python.

    Zmingw32r   c             C   sˆ  t jjj| |||ƒ | jd krhtjddgdtjd�}|jj	ƒ }|jj
ƒ  tjd|ƒ}|rht|jdƒƒ| _| jdkrxd}nd	}| jd
krŽd
| _n| jdkržd| _tƒ  tƒ }tdd�}	|s¼|	rÆ| jdƒ tƒ }
|
rà| jdd|
 ƒ tƒ dk�r | jdk �r| jddddd� n| jddddd� n\| jdk�rJ| jdddd| j|f d� n2| jdk �rj| jd d!dd"d� n| jd#d$d%d&d� dg| _d S )'NZgccz-dumpversionT)ÚshellÚstdoutz
(\d+\.\d+)r   z2.91.57z--entry _DllMain@12Ú Zdllwrapzg++)ÚdebugZNPY_MINGW_USE_CUSTOM_MSVCRZ__MSVCRT_VERSION__z0x%04iÚAMD64z4.0z/gcc -g -DDEBUG -DMS_WIN64 -mno-cygwin -O0 -WallzCgcc -g -DDEBUG -DMS_WIN64 -mno-cygwin -O0 -Wall -Wstrict-prototypeszgcc -g -mno-cygwinzgcc -g -mno-cygwin -shared)ÚcompilerÚcompiler_soÚ
linker_exeÚ	linker_soz#gcc -g -DDEBUG -DMS_WIN64 -O0 -Wallz7gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypeszgcc -gzgcc -g -sharedz3.0.0zgcc -mno-cygwin -O2 -wz0gcc -mno-cygwin -mdll -O2 -w -Wstrict-prototypeszg++ -mno-cygwinz%s -mno-cygwin -mdll -static %szgcc -mno-cygwin -O2 -Wallz-gcc -mno-cygwin -O2 -Wall -Wstrict-prototypeszg++ -mno-cygwin -sharedzgcc -O2 -Wallz!gcc -O2 -Wall -Wstrict-prototypeszg++ zg++ -shared)Ú	distutilsÚcygwinccompilerÚCygwinCCompilerÚ__init__Úgcc_versionÚ
subprocessÚPopenÚPIPEr   ÚreadÚcloseÚreÚsearchr   ÚgroupZ
linker_dllZlinkerÚbuild_import_libraryÚbuild_msvcr_libraryZdefine_macror   r   Úset_executablesÚcompiler_cxx)ÚselfÚverboseÚdry_runÚforceÚpZ
out_stringÚresultZentry_pointZmsvcr_successZmsvcr_dbg_successZmsvcr_versionr   r   r   r"   ?   sp    











zMingw32CCompiler.__init__Nc             C   sp   t ƒ }|r|sg }|j|ƒ | |||||||d |	|
|||f}| jdk rRtjjj}ntj}||d |jj	… Ž  d S )Nz3.0.0)
r   Úappendr#   r   r    r!   Úlinkr	   Ú__code__Úco_argcount)r0   Ztarget_descZobjectsZoutput_filenameÚ
output_dirÚ	librariesÚlibrary_dirsÚruntime_library_dirsÚexport_symbolsr   Zextra_preargsZextra_postargsÚ
build_tempZtarget_langZruntime_libraryÚargsÚfuncr   r   r   r7   ¬   s0    

zMingw32CCompiler.linkr   c       	      C   sÖ   |d krd}g }xÀ|D ]¸}t jjt jj|ƒƒ\}}t jj|ƒ\}}|rR|dd … }|| jddg krttd||f ƒ‚|r„t jj|ƒ}|dks”|dkr´|jt jj	||| | j
 ƒƒ q|jt jj	||| j
 ƒƒ qW |S )Nr   r   z.rcz.resz"unknown file type '%s' (from '%s'))ÚosÚpathÚsplitextÚnormcaseÚ
splitdriveZsrc_extensionsr   Úbasenamer6   ÚjoinZobj_extension)	r0   Zsource_filenamesZ	strip_dirr:   Z	obj_namesÚsrc_nameÚbaseÚextÚdrvr   r   r   Úobject_filenamesÖ   s(     
z!Mingw32CCompiler.object_filenames)r   r   r   )Nr   NNNN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úcompiler_typer"   r7   rM   r   r   r   r   r   8   s     
r     
 r   c              C   s2  t jg} tt dƒr,t jt jkr,| jt jƒ n"tt dƒrNt jt jkrN| jt jƒ dddg}g }x.| D ]&}x |D ]}|jtjj||ƒƒ qlW qbW dtj	kr°|jtjjtj	d dƒƒ t
t jd d… ƒ\}}d	g}xP|D ]H}|||f }	td
|	 ƒ x*|D ]"}tjj||	ƒ}
tjj|
ƒrô|
S qôW qÒW td|	|f ƒ‚d S )NÚbase_prefixÚreal_prefixr   ÚlibÚbinZ
SYSTEMROOTZSystem32é   zpython%d%d.dllzLooking for %sz%s not found in %s)ÚsysÚprefixÚhasattrrS   r6   rT   rB   rC   rH   ÚenvironÚtupleÚversion_infoÚprintÚexistsÚ
ValueError)ÚstemsÚsub_dirsÚlib_dirsZstemÚfolderÚmajor_versionÚminor_versionÚpatternsÚpatÚdllnameÚdllr   r   r   Úfind_python_dllù   s,    





rk   c             C   s    t jdd| gt jd�}|jjƒ S )Nzobjdump.exez-p)r   )r$   r%   r&   r   Ú	readlines)rj   Ústr   r   r   Ú
dump_table  s    rn   c       	      C   s  t | ƒ}x0tt|ƒƒD ]}tj|| jƒ ƒrP qW tdƒ‚g }xVt|d t|ƒƒD ]@}tj|| jƒ ƒ}|r�|jt	|j
dƒjƒ ƒ|j
dƒfƒ qRP qRW t|ƒdkr°tjd|  ƒ t|dƒ}|jdtjj| ƒ ƒ |jdƒ |jd	ƒ |jd
ƒ x|D ]}|jd|d  ƒ qôW |jƒ  dS )zŠGiven a dll file location,  get all its exported symbols and dump them
    into the given def file.

    The .def file will be overwrittenzSymbol table not foundr   rW   r   zNo symbols found in %sÚwzLIBRARY        %s
z,;CODE          PRELOAD MOVEABLE DISCARDABLE
z;DATA          PRELOAD SINGLE
z	
EXPORTS
z%s
N)rn   ÚrangeÚlenÚ_STARTÚmatchÚdecoder`   Ú_TABLEr6   Úintr+   Ústripr   ÚwarnÚopenÚwriterB   rC   rG   r(   )	rj   ÚdfileÚdumpÚiZsymsÚjÚmÚdÚsr   r   r   Úgenerate_def#  s*    $




r‚   c                s4   dddœt ƒ  ‰ ‡ fdd„}dd„ }|| ƒp2|| ƒS )NÚamd64Zx86)r   ÚIntelc                sd   t jjt jjddƒdƒ}t jj|ƒs(d S x6t j|ƒD ](\}}}| |kr4ˆ |kr4t jj|| ƒS q4W d S )NZWINDIRz
C:\WINDOWSZwinsxs)rB   rC   rH   r[   Úgetr_   Úwalk)Údll_nameZwinsxs_pathÚrootÚdirsÚfiles)Úarchr   r   Ú_find_dll_in_winsxsI  s    z%find_dll.<locals>._find_dll_in_winsxsc             S   sL   xFt jgtjd jdƒ D ]*}tjj|| ƒ}tjj|ƒrtjj|ƒS qW d S )NÚPATHú;)	rX   rY   rB   r[   ÚsplitrC   rH   r_   Úabspath)r‡   rC   Úfilepathr   r   r   Ú_find_dll_in_pathT  s    z#find_dll.<locals>._find_dll_in_path)r   )r‡   rŒ   r’   r   )r‹   r   Úfind_dllD  s
    r“   Fc             C   s  t jdkrdS tƒ }|d kr*tjdƒ dS |dk r@tjdƒ dS tƒ }| rR|d7 }d| }t jjtj	d|ƒ}t jj
|ƒrŒtjd	|f ƒ d
S |d }t|ƒ}|s²tjd| ƒ dS d| }t jjtj	d|ƒ}tjd||f ƒ t||ƒ dd|d|g}	tj|	ƒ}
t j|ƒ |
 S )NÚntFz?Skip building import library: Runtime is not compiled with MSVCéP   z=Skip building msvcr library: custom functionality not presentr€   zlib%s.aÚlibsz(Skip building msvcr library: "%s" existsTz.dllz*Cannot build msvcr library: "%s" not foundz	lib%s.defz&Building msvcr library: "%s" (from %s)Údlltoolz-dz-l)rB   Únamer   r   r   r   rC   rH   rX   rY   Úisfiler“   rx   Úinfor‚   r$   ÚcallÚremove)r   Z	msvcr_verZ
msvcr_nameZout_nameÚout_fileZmsvcr_dll_nameÚdll_fileÚdef_nameÚdef_fileÚcmdÚretcoder   r   r   r-   ^  sB    






r-   c              C   s@   t jdkrd S tƒ } | dkr"tƒ S | dkr0tƒ S td|  ƒ‚d S )Nr”   r   r„   zUnhandled arch %s)rB   r˜   r   Ú_build_import_library_amd64Ú_build_import_library_x86r`   )r‹   r   r   r   r,   ™  s    
r,   c              C   sô   t tjdd… ƒ\} }dddg}tjg}ttdƒrLtjtjkrL|jtjƒ n"ttdƒrntjtjkrn|jtjƒ dd	g}g }xJ|D ]B}|| |f }x0|D ](}x"|D ]}	|jtj	j
||	|ƒƒ q W q–W q€W x |D ]}
tj	j|
ƒrÌd
|
fS qÌW d|d fS )zACheck if an import library for the Python runtime already exists.NrW   zlibpython%d%d.azlibpython%d%d.dll.azlibpython%d.%d.dll.arS   rT   r–   rU   TFr   )r\   rX   r]   rY   rZ   rS   r6   rT   rB   rC   rH   r™   )re   rf   rg   ra   rb   Ú
candidatesrh   ÚfilenameZstem_dirrd   Úfullnamer   r   r   Ú_check_for_import_lib¥  s(    


"
r¨   c              C   s„   t ƒ \} }| rtjd|ƒ d S tƒ }tjd||f ƒ dttjd d… ƒ }tj	j
tjd|ƒ}t||ƒ dd|d|g}tj|ƒ d S )	Nz)Skip building import library: "%s" existsz4Building import library (arch=AMD64): "%s" (from %s)zpython%d%d.defrW   r–   r—   z-dz-l)r¨   r   r   rk   rš   r\   rX   r]   rB   rC   rH   rY   r‚   r$   r%   )Ú
out_existsr�   rž   rŸ   r    r¡   r   r   r   r£   É  s    

r£   c              C   sh  t ƒ \} }| rtjd|ƒ dS dttjdd… ƒ }tjjtj	d|ƒ}tjj
|ƒs´ttdƒrptjjtjd|ƒ}n"ttdƒrŽtjjtjd|ƒ}nd}tjj
|ƒr¤|}ntjd	|ƒ dS tjd
|ƒ ddlm} dttjdd… ƒ }tjjtj	d|ƒ}d|j|f }|j|ƒ}	|j|	ƒ\}
}|j|
||jt|dƒƒ tƒ }|||f}d| }tj|ƒ}|�rdtjdƒ dS )z; Build the import libraries for Mingw32-gcc on Windows
    z)Skip building import library: "%s" existsNzpython%d%d.librW   r–   rS   rT   r   z+Cannot build import library: "%s" not foundz(Building import library (ARCH=x86): "%s"r   )Úlib2defzpython%d%d.defz%s %sro   z3dlltool --dllname "%s" --def "%s" --output-lib "%s"z:Failed to build import library for gcc. Linking will fail.)r¨   r   r   r\   rX   r]   rB   rC   rH   rY   r™   rZ   rS   rT   rx   rš   Únumpy.distutilsrª   Z
DEFAULT_NMZgetnmZparse_nmZ
output_defZ
DEF_HEADERry   rk   Úsystem)r©   r�   Zlib_nameZlib_fileZbase_librª   rŸ   r    Znm_cmdZ	nm_outputZdlistÚflistr‡   r@   r¡   Ústatusr   r   r   r¤   Ý  s>    






r¤   Úwin32z8.0.50727.42Ú80z9.0.21022.8Z90z10.0.30319.460Z100ÚCRT_ASSEMBLY_VERSIONÚ.rW   z9Cannot import msvcrt: using manifest will not be possiblec             C   sX   yt t| d | ƒ }W n$ tk
r<   td| |f ƒ‚Y nX tjdƒ}||| |dœ S )zUGiven a major and minor version of the MSVCR, returns the
    corresponding XML file.é
   z)Version %d,%d of MSVCRT not supported yetaÃ          <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
          <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
            <security>
              <requestedPrivileges>
                <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
              </requestedPrivileges>
            </security>
          </trustInfo>
          <dependency>
            <dependentAssembly>
              <assemblyIdentity type="win32" name="Microsoft.VC%(maj)d%(min)d.CRT" version="%(fullver)s" processorArchitecture="*" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
            </dependentAssembly>
          </dependency>
        </assembly>)ÚfullverÚmajÚmin)Ú_MSVCRVER_TO_FULLVERÚstrÚKeyErrorr`   ÚtextwrapÚdedent)rµ   r¶   r´   Útemplater   r   r   Úmsvc_manifest_xml1  s    r½   rj   c             C   s4   |dkrd}n|dkrd}nt d| ƒ‚d|| f S )aW  Return the rc file used to generate the res file which will be embedded
    as manifest for given manifest file name, of given type ('dll' or
    'exe').

    Parameters
    ----------
    name : str
            name of the manifest file to embed
    type : str {'dll', 'exe'}
            type of the binary which will embed the manifest

    rj   rW   Zexer   zType %s not supportedz&#include "winuser.h"
%d RT_MANIFEST %s)r`   )r˜   ÚtypeZrctyper   r   r   Úmanifest_rcQ  s    r¿   c             C   s.   t ƒ }|r*|t| ƒks*tdt| ƒ|f ƒ‚dS )z6msver is the ms runtime version used for the MANIFEST.zKDiscrepancy between linked msvcr (%d) and the one about to be embedded (%d)N)r   rv   r`   )Úmsverrµ   r   r   r   Ú!check_embedded_msvcr_match_linkedi  s    rÁ   c             C   s&   t jj| jdg dƒƒ}t jj|ƒd S )NZyoÚcr   )rB   rC   rG   Z_gen_temp_sourcefilerD   )ÚconfigrJ   r   r   r   Úconfigtest_nameu  s    rÄ   c             C   s   t | ƒ}| jj}|| d S )Nz	.manifest)rÄ   r   Zexe_extension)rÃ   rˆ   Zexextr   r   r   Úmanifest_namey  s    rÅ   c             C   s   t | ƒ}|d S )Nz.rc)rÄ   )rÃ   rˆ   r   r   r   Úrc_name  s    rÆ   c             C   st   t ƒ }|d k	rp|dkrpt|ƒ t|ƒ}t|| d ƒ}t||ƒ}tt| ƒdƒ}| jjt| ƒƒ |j|ƒ |j	ƒ  d S )Né   r³   ro   )
Úget_build_msvc_versionrÁ   rv   r½   ry   rÅ   Z
temp_filesr6   rz   r(   )rÃ   rÀ   ÚmaÚmiZmanxmlZmanr   r   r   Úgenerate_manifest„  s    

rË   )F)rj   )ErQ   Ú
__future__r   r   r   rB   rX   r$   r)   rº   Znumpy.distutils.ccompilerZnumpyr]   r   r   r«   Zdistutils.cygwinccompilerr   Zdistutils.versionr   Zdistutils.unixccompilerr	   Zdistutils.msvccompilerr
   rÈ   Údistutils.errorsr   r   r   Znumpy.distutils.misc_utilr   r   r   r   r   r    Z	get_msvcrÚcompilerr   ru   r!   r   rk   rn   r‚   r“   r-   r,   r¨   r£   r¤   r·   ÚplatformÚmsvcrtrZ   r±   r�   ÚmajorÚminorÚrestÚImportErrorrx   r½   r¿   rÁ   rÄ   rÅ   rÆ   rË   r   r   r   r   Ú<module>	   sf   

 B&!
;$?
 
