B
    V&t\Zb  ã               @   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
ejd dk r\ddlmZ nddlmZ ddlZddlmZ ddl	mZ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„ Z0d d!„ Z1d"d#„ Z2d$d%„ Z3i Z4ej5d&krÜyXddl6Z6d'e4d(< d)e4d*< d+e4d,< e7e6d-ƒr¸e6j8 9d.d/¡\Z:Z;Z<e6j8e4e:e; < [:[;[<W n  e=k
rÚ   e >d0¡ Y nX d1d2„ Z?dAd4d5„Z@d6d7„ ZAd8d9„ ZBd:d;„ ZCd<d=„ ZDd>d?„ ZEdS )Ba  
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)Úgen_preprocess_optionsÚgen_lib_options)Ú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   ú?lib/python3.7/site-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 -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   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!   ?   sp    











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 )Nz3.0.0)
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   r2   ¬   s0    

zMingw32CCompiler.linkr   c       	      C   sÖ   |d krd}g }xÀ|D ]¸}t j t j |¡¡\}}t j |¡\}}|rR|dd … }|| jddg krttd||f ƒ‚|r„t j |¡}|dks”|dkr´| t j 	||| | j
 ¡¡ q| t j 	||| j
 ¡¡ qW |S )Nr   r   z.rcz.resz"unknown file type '%s' (from '%s'))ÚosÚpathÚsplitextÚnormcaseÚ
splitdriveZsrc_extensionsr   Úbasenamer1   ÚjoinZobj_extension)	r-   Zsource_filenamesZ	strip_dirr5   Z	obj_namesZsrc_nameÚbaseZextZdrvr   r   r   Úobject_filenamesÖ   s(     
z!Mingw32CCompiler.object_filenames)r   r   r   )Nr   NNNN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zcompiler_typer!   r2   r@   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,|  t j¡ n"tt dƒrNt jt jkrN|  t j¡ dddg}g }x.| D ]&}x |D ]}| tj ||¡¡ qlW qbW dtj	kr°| tj tj	d d¡¡ t
t jd d… ƒ\}}d	g}xP|D ]H}|||f }	td
|	 ƒ x*|D ]"}tj ||	¡}
t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ÚhasattrrE   r1   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 | ƒ}x0tt|ƒƒD ]}t ||  ¡ ¡rP qW tdƒ‚g }xVt|d t|ƒƒD ]@}t ||  ¡ ¡}|r| t	| 
d¡ ¡ ƒ| 
d¡f¡ qRP qRW t|ƒdkr°t d|  ¡ t|dƒ}| dtj | ¡ ¡ | d¡ | d	¡ | d
¡ x|D ]}| d|d  ¡ qôW | ¡  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   Ú_TABLEr1   Ú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 x6t  |¡D ](\}}}| |kr4ˆ |kr4t j || ¡S q4W d S )NZWINDIRz
C:\WINDOWSZwinsxs)r8   r9   r>   rM   ÚgetrQ   Ú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  d¡ D ]*}tj || ¡}tj |¡rtj |¡S qW 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)Údlltoolz-dz-l)r8   Únamer   r   r   r   r9   r>   rJ   rK   Úisfiler‚   rh   Úinforr   r#   Z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,   ^  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   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 }xJ|D ]B}|| |f }x0|D ](}x"|D ]}	| tj	 
||	|¡¡ q W q–W q€W x |D ]}
tj	 |
¡rÌd
|
fS qÌW 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   r1   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 )	Nz)Skip building import library: "%s" existsz4Building import library (arch=AMD64): "%s" (from %s)zpython%d%d.defrI   r…   r†   z-dz-l)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
    z)Skip building import library: "%s" existsNzpython%d%d.librI   r…   rE   rF   r   z+Cannot build import library: "%s" not foundz(Building import library (ARCH=x86): "%s"r   )Úlib2defzpython%d%d.defz%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   r6   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   sR   yt t| d | ƒ }W n$ tk
r<   td| |f ƒ‚Y nX 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 yetaS  <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   )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   Z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 )Nz.rc)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_filesr1   rj   r'   )r©   r¦   ZmaZmiZmanxmlZmanr   r   r   Úgenerate_manifest„  s    

r¯   )F)rZ   )FrD   Z
__future__r   r   r   r8   rJ   r#   r(   Znumpy.distutils.ccompilerZnumpyrO   r   r   r—   Zdistutils.cygwinccompilerr   Zdistutils.versionr   r	   r
   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&!
;$?
 
