U
    ¤pÅ]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 %d¡Z&e %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ÔzXddl5Z5d&e3d'< d(e3d)< d*e3d+< e6e5d,ƒr°e5j7 8d-d.¡\Z9Z:Z;e5j7e3e9e: < [9[:[;W n  e<k
rÒ   e =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   úB/usr/lib/python3/dist-packages/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d krhtjddgdtjd}|j 	¡ }|j 
¡  t d|¡}|rht| 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Æ|  d¡ tƒ }
|
rà|  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)ÚcompilerZcompiler_soZ
linker_exeZ	linker_soz#gcc -g -DDEBUG -DMS_WIN64 -O0 -Wallz7gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypeszgcc -gzgcc -g -sharedú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   Zset_executablesZcompiler_cxx)ÚselfÚverboseZdry_runZforceÚpZ
out_stringÚresultZentry_pointZmsvcr_successZmsvcr_dbg_successZmsvcr_versionr   r   r   r    ?   s‚     ÿ
ÿ






ûüÿûûýzMingw32CCompiler.__init__Nc                 C   sp   t ƒ }|r|sg }| |¡ | |||||||d |	|
|||f}| jdk rRtjjj}ntj}||d |jj	… Ž  d S )Nr   )
r   Úappendr!   r   r   r   Úlinkr	   Ú__code__Úco_argcount)r,   Ztarget_descZobjectsZoutput_filenameÚ
output_dirZ	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsr   Zextra_preargsZextra_postargsZ
build_tempZtarget_langZruntime_libraryÚargsÚfuncr   r   r   r1   ¬   s2    
ó
zMingw32CCompiler.linkr   c           	      C   sÒ   |d krd}g }|D ]¸}t j t j |¡¡\}}t j |¡\}}|rP|dd … }|| jddg krrtd||f ƒ‚|r‚t j |¡}|dks’|dkr²| t j 	||| | j
 ¡¡ q| t j 	||| j
 ¡¡ q|S )Nr   r   ú.rcz.resz"unknown file type '%s' (from '%s'))ÚosÚpathÚsplitextÚnormcaseÚ
splitdriveZsrc_extensionsr   Úbasenamer0   ÚjoinZobj_extension)	r,   Zsource_filenamesZ	strip_dirr4   Z	obj_namesZsrc_nameÚbaseZextZdrvr   r   r   Úobject_filenamesÖ   s0     ÿÿÿÿz!Mingw32CCompiler.object_filenames)r   r   r   )Nr   NNNN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zcompiler_typer    r1   r@   r   r   r   r   r   8   s       ý
u      ó
,  ýr   c                  C   s*  t jg} tt dƒr,t jt jkr,|  t j¡ n"tt dƒrNt jt jkrN|  t j¡ dddg}g }| D ]"}|D ]}| tj ||¡¡ qhq`dtj	kr¨| tj tj	d d¡¡ t
t jd d… ƒ\}}d	g}|D ]L}|||f }	td
|	 ƒ |D ]*}tj ||	¡}
tj |
¡rè|
    S qèqÈ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ÚhasattrrE   r0   rF   r8   r9   r>   ÚenvironÚtupleÚversion_infoÚprintÚexistsÚ
ValueError)ÚstemsÚsub_dirsZlib_dirsZstemÚfolderÚmajor_versionÚminor_versionÚpatternsÚpatZdllnameÚdllr   r   r   Úfind_python_dllù   s,    

r[   c                 C   s    t jdd| gt jd}|j ¡ S )Nzobjdump.exez-p)r   )r"   r#   r$   r   Ú	readlines)rZ   Ústr   r   r   Ú
dump_table  s    r^   c           	      C   s  t | ƒ}tt|ƒƒD ]}t ||  ¡ ¡r q8qtdƒ‚g }t|d t|ƒƒD ]B}t ||  ¡ ¡}|rŒ| t	| 
d¡ ¡ ƒ| 
d¡f¡ qN q’qNt|ƒdkr¬t d|  ¡ t|dƒ}| dtj | ¡ ¡ | d¡ | d	¡ | d
¡ |D ]}| d|d  ¡ qî| ¡  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   rI   r   zNo symbols found in %sÚwzLIBRARY        %s
z,;CODE          PRELOAD MOVEABLE DISCARDABLE
z;DATA          PRELOAD SINGLE
z	
EXPORTS
z%s
N)r^   ÚrangeÚlenÚ_STARTÚmatchÚdecoderR   Ú_TABLEr0   Úintr)   Ústripr   ÚwarnÚopenÚwriter8   r9   r=   r&   )	rZ   ÚdfileÚdumpÚiZsymsÚjÚmÚdÚsr   r   r   Úgenerate_def#  s*    $



rr   c                    s4   dddœt ƒ  ‰ ‡ fdd„}dd„ }|| ƒp2|| ƒS )NZamd64Zx86)r   ÚIntelc                    sd   t j t j dd¡d¡}t j |¡s(d S t  |¡D ],\}}}| |kr2ˆ |kr2t j || ¡  S q2d S )NZWINDIRz
C:\WINDOWSZwinsxs)r8   r9   r>   rM   ÚgetrQ   Úwalk)Údll_nameZwinsxs_pathÚrootÚdirsÚfiles©Zarchr   r   Ú_find_dll_in_winsxsI  s    ÿz%find_dll.<locals>._find_dll_in_winsxsc                 S   sL   t jgtjd  d¡ D ].}tj || ¡}tj |¡rtj |¡  S qd S )NÚPATHú;)	rJ   rK   r8   rM   Úsplitr9   r>   rQ   Úabspath)rv   r9   Úfilepathr   r   r   Ú_find_dll_in_pathT  s    z#find_dll.<locals>._find_dll_in_path)r   )rv   r{   r   r   rz   r   Úfind_dllD  s    ÿÿr‚   Fc                 C   s  t jdkrdS tƒ }|d kr*t d¡ dS |dk r@t d¡ dS tƒ }| rR|d7 }d| }t j tj	d|¡}t j 
|¡rŒt d	|f ¡ d
S |d }t|ƒ}|s²t d| ¡ dS d| }t j tj	d|¡}t d||f ¡ t||ƒ dd|d|g}	t |	¡}
t  |¡ |
 S )NÚntFz?Skip building import library: Runtime is not compiled with MSVCéP   z=Skip building msvcr library: custom functionality not presentrp   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)Údlltoolú-dú-l)r8   Únamer   r   r   r   r9   r>   rJ   rK   Úisfiler‚   rh   Úinforr   r"   ÚcallÚremove)r   Z	msvcr_verZ
msvcr_nameZout_nameÚout_fileZmsvcr_dll_nameÚdll_fileÚdef_nameÚdef_fileÚcmdZretcoder   r   r   r+   ^  sH    


ÿÿÿ


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   rs   zUnhandled arch %s)r8   r‰   r   Ú_build_import_library_amd64Ú_build_import_library_x86rR   rz   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| tj¡ n"ttdƒrntjtjkrn| tj¡ dd	g}g }|D ]:}|| |f }|D ]$}|D ]}	| tj	 
||	|¡¡ qšq’q~|D ]}
tj	 |
¡r¾d
|
f  S q¾d|d fS )zACheck if an import library for the Python runtime already exists.NrI   zlibpython%d%d.azlibpython%d%d.dll.azlibpython%d.%d.dll.arE   rF   r…   rG   TFr   )rN   rJ   rO   rK   rL   rE   r0   rF   r8   r9   r>   rŠ   )rV   rW   rX   rS   rT   Z
candidatesrY   ÚfilenameZstem_dirrU   Úfullnamer   r   r   Ú_check_for_import_lib¥  s*    þr—   c                  C   s„   t ƒ \} }| rt d|¡ d S tƒ }t d||f ¡ dttjd d… ƒ }tj	 
tjd|¡}t||ƒ dd|d|g}t |¡ d S )	Nú)Skip building import library: "%s" existsz4Building import library (arch=AMD64): "%s" (from %s)úpython%d%d.defrI   r…   r†   r‡   rˆ   )r—   r   r   r[   r‹   rN   rJ   rO   r8   r9   r>   rK   rr   r"   r#   )Ú
out_existsrŽ   r   r   r‘   r’   r   r   r   r“   É  s    
ÿ
r“   c                  C   sh  t ƒ \} }| rt d|¡ dS dttjdd… ƒ }tj tj	d|¡}tj 
|¡s´ttdƒrptj tjd|¡}n"ttdƒrŽtj tjd|¡}nd}tj 
|¡r¤|}nt d	|¡ dS t d
|¡ ddlm} dttjdd… ƒ }tj tj	d|¡}d|j|f }| |¡}	| |	¡\}
}| |
||jt|dƒ¡ tƒ }|||f}d| }t |¡}|rdt d¡ dS )z; Build the import libraries for Mingw32-gcc on Windows
    r˜   Nzpython%d%d.librI   r…   rE   rF   r   z+Cannot build import library: "%s" not foundz(Building import library (ARCH=x86): "%s"r   )Úlib2defr™   z%s %sr_   z3dlltool --dllname "%s" --def "%s" --output-lib "%s"z:Failed to build import library for gcc. Linking will fail.)r—   r   r   rN   rJ   rO   r8   r9   r>   rK   rŠ   rL   rE   rF   rh   r‹   Únumpy.distutilsr›   Z
DEFAULT_NMZgetnmZparse_nmZ
output_defZ
DEF_HEADERri   r[   Úsystem)rš   rŽ   Zlib_nameZlib_fileZbase_libr›   r   r‘   Znm_cmdZ	nm_outputZdlistZflistrv   r5   r’   Zstatusr   r   r   r”   Ý  s>    






r”   Zwin32z8.0.50727.42Z80z9.0.21022.8Z90z10.0.30319.460Z100ÚCRT_ASSEMBLY_VERSIONÚ.rI   z9Cannot import msvcrt: using manifest will not be possiblec                 C   sX   zt t| d | ƒ }W n$ tk
r<   td| |f ƒ‚Y nX t 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ÚKeyErrorrR   ÚtextwrapÚdedent)r¢   r£   r¡   Útemplater   r   r   Úmsvc_manifest_xml1  s    ÿ
rª   rZ   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

    rZ   rI   Úexer   zType %s not supportedz&#include "winuser.h"
%d RT_MANIFEST %s)rR   )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   rf   rR   )Úmsverr¢   r   r   r   Ú!check_embedded_msvcr_match_linkedi  s    
þÿr¯   c                 C   s&   t j |  dg d¡¡}t j |¡d S )NZyoÚcr   )r8   r9   r=   Z_gen_temp_sourcefiler:   )Úconfigr?   r   r   r   Úconfigtest_nameu  s    r²   c                 C   s   t | ƒ}| jj}|| d S )Nz	.manifest)r²   r   Zexe_extension)r±   rw   Zexextr   r   r   Úmanifest_namey  s    r³   c                 C   s   t | ƒ}|d S )Nr7   )r²   )r±   rw   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 t| ƒ¡ | |¡ | 	¡  d S )Né   r    r_   )
Úget_build_msvc_versionr¯   rf   rª   ri   r³   Z
temp_filesr0   rj   r&   )r±   r®   ZmaÚmiZmanxmlZmanr   r   r   Úgenerate_manifest„  s    

r¸   )F)rZ   )ErD   Z
__future__r   r   r   r8   rJ   r"   r'   r§   Znumpy.distutils.ccompilerZnumpyrO   r   r   rœ   Zdistutils.cygwinccompilerr   Zdistutils.versionr   Zdistutils.unixccompilerr	   Zdistutils.msvccompilerr
   r¶   Zdistutils.errorsr   r   r   Znumpy.distutils.misc_utilr   r   r   r   r   r   Z	get_msvcrÚcompilerb   re   r   r   r[   r^   rr   r‚   r+   r*   r—   r“   r”   r¤   ÚplatformZmsvcrtrL   rž   r~   ÚmajorÚminorÚrestÚImportErrorrh   rª   r­   r¯   r²   r³   r´   r¸   r   r   r   r   Ú<module>   sf   	

 B&!
;$?
 
