3
5^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ryXddl5Z5d&e3d'< d(e3d)< d*e3d+< e6e5d,re5j7j8d-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   )msvcr r   k/home/brian/BIRCH/lib-linux-x86_64/python/lib64/python3.6/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| ||| | 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rd
| _n| jdkrd| _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 )'Ngccz-dumpversionT)shellstdoutz
(\d+\.\d+)r   z2.91.57z--entry _DllMain@12 dllwrapzg++)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PopenPIPEr   readcloseresearchr   group
linker_dlllinkerbuild_import_librarybuild_msvcr_librarydefine_macror   r   set_executablescompiler_cxx)selfverbosedry_runforcep
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 }|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)r9   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   rE      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 |rt 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
splitdrivesrc_extensionsr   basenamerD   joinobj_extension)	r9   source_filenames	strip_dirrK   	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(   rE   rg   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 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hasattrrm   rD   rn   rW   rX   r^   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 jdd| gt jd}|jj S )Nzobjdump.exez-p)r   )r*   r+   r,   r   	readlines)r   str   r   r   
dump_table  s    r   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rtjd|   t|d}|jdtjj|   |jd |jd	 |jd
 x|D ]}|jd|d   qW |j  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   rs   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decoder|   _TABLErD   intr1   stripr   warnopenwriterW   rX   r]   r.   )	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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 )NWINDIRz
C:\WINDOWSwinsxs)rW   rX   r^   rw   getr{   walk)dll_name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;)	rt   ru   rW   rw   splitrX   r^   r{   abspath)r   rX   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
|rtjd	|f  d
S |d }t|}|st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 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)rW   namer   r   r   r   rX   r^   rt   ru   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   r5   ^  sB    






r5   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)rW   r   r   _build_import_library_amd64_build_import_library_x86r|   )r   r   r   r   r4     s    
r4   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
||	| qW qW qW x |D ]}
tj	j|
rd
|
fS qW d|d fS )zACheck if an import library for the Python runtime already exists.Nrs   zlibpython%d%d.azlibpython%d%d.dll.azlibpython%d.%d.dll.arm   rn   r   ro   TFr   )rx   rt   ry   ru   rv   rm   rD   rn   rW   rX   r^   r   )r   r   r   r}   r~   
candidatesr   filenamestem_dirr   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.defrs   r   r   z-dz-l)r   r   r   r   r   rx   rt   ry   rW   rX   r^   ru   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
|sttdrptjjtjd|}n"ttdrt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.librs   r   rm   rn   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   rx   rt   ry   rW   rX   r^   ru   r   rv   rm   rn   r   r   numpy.distutilsr   
DEFAULT_NMgetnmparse_nm
output_def
DEF_HEADERr   r   system)r   r   lib_namelib_filebase_libr   r   r   nm_cmd	nm_outputdlistflistr   rU   r   statusr   r   r   r     s>    






r   win32z8.0.50727.42Z80z9.0.21022.8Z90z10.0.30319.460100CRT_ASSEMBLY_VERSION.rs   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   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   rs   exer   zType %s not supportedz&#include "winuser.h"
%d RT_MANIFEST %s)r|   )r   type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   r   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 )Nyocr   )rW   rX   r]   _gen_temp_sourcefilerY   )configrd   r   r   r   configtest_nameu  s    r   c             C   s   t | }| jj}|| d S )Nz	.manifest)r   r!   exe_extension)r   r   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   r   )
get_build_msvc_versionr   r   r   r   r   
temp_filesrD   r   r.   )r   r   mamimanxmlmanr   r   r   generate_manifest  s    

r  )F)r   )Erk   
__future__r   r   r   rW   rt   r*   r/   r   Znumpy.distutils.ccompilernumpyry   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   r5   r4   r   r   r   r   platformmsvcrtrv   r   r   majorminorrestImportErrorr   r   r   r   r   r   r   r  r   r   r   r   <module>	   sf   

 B&!
;$?
 
