B
    us^Cc                 @   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-dAddZ.dd Z/dd  Z0d!d" Z1d#d$ Z2i Z3ej4d%kry`ddl5Z5d&e3d'< d(e3d)< d*e3d+< d,e3d-< e6e5d.re5j78d/d0\Z9Z:Z;e5j7e3e9e: < [9[:[;W n  e<k
r   e=d1 Y nX d2d3 Z>dBd5d6Z?d7d8 Z@d9d: ZAd;d< ZBd=d> ZCd?d@ ZDdS )Ca  
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   )msvcr r   l/home/fristb/BIRCH/lib-linux-x86_64/python/lib64/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.

    mingw32r   c       
   	   C   s  t jj| ||| | jd krnytddg}W n ttfk
rL   d}Y nX t	
d|}|rnt|d| _| jdkr~d}nd}| jdkrd| _n| jdkrd	| _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rP| jdddd| j|f d n2| jdk rp| jdd dd!d n| jd"d#d$d%d d	g| _d S )&Ngccz-dumpversion z
(\d+\.\d+)r   z2.91.57z--entry _DllMain@12dllwrapzg++T)debugNPY_MINGW_USE_CUSTOM_MSVCR__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check_outputOSErrorCalledProcessErrorresearchr   group
linker_dlllinkerbuild_import_librarybuild_msvcr_librarydefine_macror   r   set_executablescompiler_cxx)
selfverbosedry_runforce
out_stringresultentry_pointmsvcr_successmsvcr_dbg_success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)r6   target_descobjectsoutput_filename
output_dir	librarieslibrary_dirsruntime_library_dirsexport_symbolsr   extra_preargsextra_postargs
build_temptarget_langruntime_libraryargsfuncr   r   r   rA      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 |rt 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
splitdrivesrc_extensionsr   basenamer@   joinobj_extension)	r6   source_filenames	strip_dirrG   	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&   rA   rc   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 qW qW td|	|f d S )Nbase_prefixreal_prefixr   libbin
SYSTEMROOTSystem32   zpython%d%d.dllzLooking for %sz%s not found in %s)sysprefixhasattrri   r@   rj   rS   rT   rZ   environtupleversion_infoprintexists
ValueError)stemssub_dirslib_dirsstemfoldermajor_versionminor_versionpatternspatdllnamedllr   r   r   find_python_dll   s,    





r   c             C   s   t dd| g}|dS )Nzobjdump.exez-p   
)r(   r)   split)r   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rtd|   t|dZ}|dtj|   |d |d	 |d
 x|D ]}|d|d   qW W dQ R X dS )zGiven 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   ro   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decoderx   _TABLEr@   intr.   stripr   warnopenwriterS   rT   rY   )	r   dfiledumpi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amd64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 )NWINDIRz
C:\WINDOWSwinsxs)rS   rT   rZ   rs   getrw   walk)dll_namewinsxs_pathrootdirsfiles)archr   r   _find_dll_in_winsxsH  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;)	rp   rq   rS   rs   r   rT   rZ   rw   abspath)r   rT   filepathr   r   r   _find_dll_in_pathS  s    z#find_dll.<locals>._find_dll_in_path)r   )r   r   r   r   )r   r   find_dllC  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
|rtd	|f  d
S |d }t|}|st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 MSVCP   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)rS   namer   r   r   r   rT   rZ   rp   rq   isfiler   r   infor   r(   callremove)r   	msvcr_ver
msvcr_nameout_nameout_filemsvcr_dll_namedll_filedef_namedef_filecmdretcoder   r   r   r2   ]  sB    






r2   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)rS   r   r   _build_import_library_amd64_build_import_library_x86rx   )r   r   r   r   r1     s    
r1   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	
||	| qW qW qW x |D ]}
tj	|
rd
|
fS qW d|d fS )zACheck if an import library for the Python runtime already exists.Nro   zlibpython%d%d.azlibpython%d%d.dll.azlibpython%d.%d.dll.ari   rj   r   rk   TFr   )rt   rp   ru   rq   rr   ri   r@   rj   rS   rT   rZ   r   )r~   r   r   ry   rz   
candidatesr   filenamestem_dirr}   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.defro   r   r   z-dz-l)r   r   r   r   r   rt   rp   ru   rS   rT   rZ   rq   r   r(   
check_call)
out_existsr   r   r   r   r   r   r   r   r     s    

r   c           	   C   sv  t  \} }| rtd| dS dttjdd  }tjtj	d|}tj
|sttdrptjtjd|}n"ttdrt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|}|j|j|g dd}||\}	}
t|d}||	|
|j| W dQ R X t }dd|d|d|g}t|}|rrtd dS )z; Build the import libraries for Mingw32-gcc on Windows
    z)Skip building import library: "%s" existsNzpython%d%d.libro   r   ri   rj   r   z+Cannot build import library: "%s" not foundz(Building import library (ARCH=x86): "%s"r   )lib2defzpython%d%d.defF)shellr   r   z	--dllnamez--defz--output-libz:Failed to build import library for gcc. Linking will fail.)r   r   r   rt   rp   ru   rS   rT   rZ   rq   r   rr   ri   rj   r   r   numpy.distutilsr   getnm
DEFAULT_NMparse_nmr   
output_def
DEF_HEADERr   r(   r)   )r   r   lib_namelib_filebase_libr   r   r   	nm_outputdlistflistfidr   r   statusr   r   r   r     sD    




r   win32z8.0.50727.42Z80z9.0.21022.8Z90z10.0.30319.460100z14.15.26726.0Z140CRT_ASSEMBLY_VERSION.ro   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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KeyErrorrx   textwrapdedent)r   r   r   templater   r   r   msvc_manifest_xml5  s    r   r   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

    r   ro   exer   zType %s not supportedz&#include "winuser.h"
%d RT_MANIFEST %s)rx   )r   typerctyper   r   r   manifest_rcU  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   r   rx   )msverr   r   r   r   !check_embedded_msvcr_match_linkedm  s    r   c             C   s&   t j| dg d}t j|d S )Nyocr   )rS   rT   rY   _gen_temp_sourcefilerU   )configr`   r   r   r   configtest_namey  s    r   c             C   s   t | }| jj}|| d S )Nz	.manifest)r   r   exe_extension)r   r   exextr   r   r   manifest_name}  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t|  || |	  d S )N   r   r   )
get_build_msvc_versionr   r   r   r   r   
temp_filesr@   r   close)r   r   mamimanxmlmanr   r   r   generate_manifest  s    

r  )F)r   )Erg   
__future__r   r   r   rS   rp   r(   r,   r   Znumpy.distutils.ccompilernumpyru   r   r   r   distutils.cygwinccompilerr#   distutils.versionr   distutils.unixccompilerr	   distutils.msvccompilerr
   r   distutils.errorsr   r   r   numpy.distutils.misc_utilr   r   r   r   r   r$   	get_msvcrcompiler   r   r%   r   r   r   r   r   r2   r1   r   r   r   r   platformmsvcrtrr   r   r   majorminorrestImportErrorr   r   r   r   r   r   r   r  r   r   r   r   <module>	   sh   

 B& 
;$B
 
