
;>"^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  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 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' Gd 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    Z0 d! d"   Z1 d# d$   Z2 i  Z3 e j4 d% k ryu d d l5 Z5 d& e3 d' <d( e3 d) <d* e3 d+ <e6 e5 d,  rde5 j7 j8 d- d.  \ Z9 Z: Z; e5 j7 e3 e9 e: <[9 [: [; Wn e< k
 re j= d/  Yn Xd0 d1   Z> d2 d3 d4  Z? d5 d6   Z@ d7 d8   ZA d9 d:   ZB d; d<   ZC d= d>   ZD d S)?a  
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   R/home/birch/.local/lib/python3.5/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               @   sa   e  Z d  Z d Z d Z d d d d d  Z d d d d d d d d  Z d d	 d
 d  Z d S)Mingw32CCompilerzH A modified MingW32 compiler compatible with an MSVC built Python.

    mingw32r   c             C   sV  t  j j j |  | | |  |  j d  k r t j d d g d d d t j } | j j	   } | j j
   t j d |  } | r t | j d   |  _ |  j d k r d	 } n d
 } |  j d k r d |  _ n |  j d k r d |  _ t   t   } t d d  }	 | s|	 r$|  j d  t   }
 |
 rG|  j d d |
  t   d k r|  j d k  r|  j d d d d d d d d  qF|  j d d d d d d d d  n |  j d k r|  j d d  d d! d d" d d# |  j | f  nV |  j d k  r$|  j d d$ d d% d d" d d&  n" |  j d d' d d( d d) d d*  d g |  _ d  S)+Ngccz-dumpversionshellT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.0compilerz/gcc -g -DDEBUG -DMS_WIN64 -mno-cygwin -O0 -Wallcompiler_sozCgcc -g -DDEBUG -DMS_WIN64 -mno-cygwin -O0 -Wall -Wstrict-prototypes
linker_exezgcc -g -mno-cygwin	linker_sozgcc -g -mno-cygwin -sharedz#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   s   t    } | r( | s g  } | j |  |  | | | | | | | d  |	 |
 | | | f } |  j d k  ry t 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 |  \ } } | rw | d d   } | |  j d d g k r t 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
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)__name__
__module____qualname____doc__compiler_typer(   rE   rg   r   r   r   r   r   8   s   rr   c              C   s  t  j g }  t t  d  r@ t  j t  j k r@ |  j t  j  n1 t t  d  rq t  j t  j k rq |  j t  j  d d d g } g  } x; |  D]3 } x* | D]" } | j t j j | |   q Wq Wd t j	 k r | j t j j t j	 d d   t
 t  j d  d   \ } } d	 g } xh | D]` } | | | f }	 t d
 |	  x9 | D]1 } t j j | |	  }
 t j j |
  rP|
 SqPWq%Wt 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 d t  j } | j j   S)Nzobjdump.exez-pr   )r*   r+   r,   r   	readlines)r   str   r   r   
dump_table  s    !r   c       	      C   sw  t  |   } x@ t t |   D]  } t j | | j    r Pq Wt d   g  } xu t | d t |   D]Z } t j | | j    } | r | j t	 | j
 d  j    | j
 d  f  qo Pqo Wt |  d k r t j d |   t | d  } | j d t j j |    | j d  | j d	  | j d
  x# | D] } | j d | d  qJW| 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*     4r   c                sM   d d d d i t        f d d   } d d   } | |   pL | |   S)	Nr    amd64Intelx86c                s   t  j j t  j j d d  d  } t  j j |  s: d  SxK t  j |  D]: \ } } } |  | k rJ   | k rJ t  j j | |   SqJ Wd  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   sf   x_ t  j g t j d j d  D]= } t j j | |   } t j j |  r! t j j |  Sq! Wd  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   sn  t  j d k r d St   } | d  k r9 t j d  d S| d k  rV t j d  d St   } |  ro | 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 Sd | } 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   sV   t  j d k r d  St   }  |  d k r/ t   S|  d k rB t   St 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   sN  t  t j d d   \ }  } d d d g } t j g } t t d  rn t j t j k rn | j t j  n1 t t d  r t j t j k r | j t j  d d	 g } g  } x_ | D]W } | |  | f } x> | D]6 } x- | D]% }	 | j t j	 j
 | |	 |   q Wq Wq Wx* | D]" }
 t j	 j |
  rd
 |
 f SqWd | 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  St   } 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   s  t    \ }  } |  r) t j d |  d Sd t t j d d   } t j j t j	 d |  } t j j
 |  st t d  r t j j t j d |  } n3 t t d  r t j j t j d |  } n d } t j j
 |  r | } n t j d	 |  d St 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 |  } | rt 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   sp   y t  t |  d |  } Wn( t k
 rF t d |  | f   Yn Xt j d  } | d | d |  d | i 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   sH   | 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   sA   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   s5   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   s   t    } | d  k	 r | d k r t |  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  )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>	   sd   "&!;$?


 