o
    6aJc                     @   s  d 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ddlZ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 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 Z)dd Z*d d! Z+i Z,ejd"krz/ddl-Z-d#e,d$< d%e,d&< d'e,d(< d)e,d*< e.e-d+re-j/0d,d-\Z1Z2Z3e-j/e,e1e2 < [1[2[3W n e4y   e
5d. Y nw d/d0 Z6d?d2d3Z7d4d5 Z8d6d7 Z9d8d9 Z:d:d; Z;d<d= Z<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

    N)log)StrictVersion)UnixCCompiler)get_build_version)UnknownFileError)msvc_runtime_librarymsvc_runtime_versionmsvc_runtime_majorget_build_architecturec                  C   s   t  } | du r	g S | gS )zBReplacement for outdated version of get_msvcr from cygwinccompilerN)r   )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                   @   sH   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 u r6z	tddg}W n ttfy%   d}Y nw t	
d|}|r6t|d| _| jdkr>d}nd}| jdkrId| _n| jdkrQd	| _t  t }td
d}|s`|re| d t }	|	rr| dd|	  t dkr| jdk r| jddddd n6| jddddd n,| jdkr| jdddd| j|f d n| jdk r| jdd dd!d n	| jd"d#d$d%d d	g| _d S )&Ngccz-dumpversion z
(\d+\.\d+)   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 -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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!   9   s   









zMingw32CCompiler.__init__Nc                 C   sp   t  }|r|s	g }|| | |||||||d |	|
|||f}| jdk r)tjjj}ntj}||d |jj	   d S )Nr   )
r   appendr"   r   r   r    linkr   __code__co_argcount)r1   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   r<      s2   

zMingw32CCompiler.linkr   c           	      C   s   |d u rd}g }|D ]\}t jt j|\}}t j|\}}|r(|dd  }|| jddg vr9td||f |rAt j|}|dksI|dkrY|t j	||| | j
  q
|t j	||| j
  q
|S )Nr   r   .rcz.resz"unknown file type '%s' (from '%s'))ospathsplitextnormcase
splitdrivesrc_extensionsr   basenamer;   joinobj_extension)	r1   source_filenames	strip_dirrB   	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!   r<   r_   r   r   r   r   r   2   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r't jt jkr'| t j g d}g }| D ]}|D ]}|tj|| q3q/dtj	v rS|tjtj	d d t
t jd d \}}t }|dkrod| | d	}n|d
krzd| d}nd}td|  |D ]}tj||}	tj|	r|	  S qtd||f )Nbase_prefixreal_prefix)r   libbin
SYSTEMROOTSystem32   CPythonpython.dllPyPylibpypyz-c.dllz!Unknown platform {implementation}zLooking for %sz%s not found in %s)sysprefixhasattrre   r;   rf   rO   rP   rV   environtupleversion_infoplatformpython_implementationprintexists
ValueError)
stemssub_dirslib_dirsstemfoldermajor_versionminor_versionimplementationdllnamedllr   r   r   find_python_dll   s6   
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 | }tt|D ]}t||  r nq
tdg }t|d t|D ]}t||  }|rF|t	|
d |
df q' t|dkrTtd|   t|d1}|dtj|   |d |d	 |d
 |D ]}|d|d   qvW d   dS 1 sw   Y  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   rk   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{   _TABLEr;   intr)   stripr   warnopenwriterO   rP   rU   )	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 }|| p|| S )Namd64x86)r   Intelc                    sd   t jt jddd}t j|sd S t |D ]\}}}| |v r/ |v r/t j||   S qd S )NWINDIRz
C:\WINDOWSwinsxs)rO   rP   rV   rt   getrz   walk)dll_namewinsxs_pathrootdirsfilesarchr   r   _find_dll_in_winsxsE  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;)	rq   rr   rO   rt   r   rP   rV   rz   abspath)r   rP   filepathr   r   r   _find_dll_in_pathP  s   z#find_dll.<locals>._find_dll_in_path)r
   )r   r   r   r   r   r   find_dll@  s   r   Fc                 C   s  t jdkrdS t }|d u rtd dS |dk r td dS t }| r)|d7 }d| }t jtj	d|}t j
|rFtd	|f  d
S |d }t|}|sYt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" existsTrn   z*Cannot build msvcr library: "%s" not foundz	lib%s.defz&Building msvcr library: "%s" (from %s)dlltool-d-l)rO   namer	   r   r   r   rP   rV   rq   rr   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   r-   Z  sH   





r-   c                  C   s<   t jdkrd S t } | dkrt S | dkrt S td|  )Nr   r   r   zUnhandled arch %s)rO   r   r
   _build_import_library_amd64_build_import_library_x86r{   r   r   r   r   r,     s   
r,   c               
   C   s   t tjdd \} }g d}tjg}ttdr%tjtjkr%|tj nttdr6tjtjkr6|tj ddg}g }|D ]}|| |f }|D ]}|D ]}	|tj	
||	| qLqHq>|D ]}
tj	|
rld|
f  S q^d	|d
 fS )zACheck if an import library for the Python runtime already exists.Nrk   )zlibpython%d%d.azlibpython%d%d.dll.azlibpython%d.%d.dll.are   rf   r   rg   TFr   )ru   rq   rv   rr   rs   re   r;   rf   rO   rP   rV   r   )r   r   patternsr|   r}   
candidatespat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 )	N)Skip building import library: "%s" existsz4Building import library (arch=AMD64): "%s" (from %s)python%d%d.defrk   r   r   r   r   )r   r   r   r   r   ru   rq   rv   rO   rP   rV   rr   r   r#   
check_call)
out_existsr   r   r   r   r   r   r   r   r     s   

r   c                  C   s  t  \} }| rtd| dS dttjdd  }tjtj	d|}tj
|sZttdr8tjtjd|}nttdrGtjtjd|}nd}tj
|rR|}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   n1 sw   Y  t }dd|d|d|g}t|}|rtd dS )z; Build the import libraries for Mingw32-gcc on Windows
    r   Nzpython%d%d.librk   r   re   rf   r   z+Cannot build import library: "%s" not foundz(Building import library (ARCH=x86): "%s"r   )lib2defr   F)shellr   r   z	--dllnamez--defz--output-libz:Failed to build import library for gcc. Linking will fail.)r   r   r   ru   rq   rv   rO   rP   rV   rr   r   rs   re   rf   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     sJ   




r   win32z8.0.50727.4280z9.0.21022.890z10.0.30319.460100z14.15.26726.0140CRT_ASSEMBLY_VERSION.rk   z9Cannot import msvcrt: using manifest will not be possiblec                 C   sT   zt t| d |  }W n ty   td| |f dw 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 yetNa          <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_xml2  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   rk   exer   zType %s not supportedz&#include "winuser.h"
%d RT_MANIFEST %s)r{   )r   typerctyper   r   r   manifest_rcR  s   r   c                 C   s2   t  }|r|t| kstdt| |f dS 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_linkedj  s   
r   c                 C   s&   t j| dg d}t j|d S )Nyocr   )rO   rP   rU   _gen_temp_sourcefilerQ   )configr\   r   r   r   configtest_namev  s   r  c                 C   s   t | }| jj}|| d S )Nz	.manifest)r  r   exe_extension)r  r   exextr   r   r   manifest_namez  s   r  c                 C   s   t | }|d S )NrN   )r  )r  r   r   r   r   rc_name  s   r  c                 C   s   t  }|d urH|dkrJt| t|d\}}tt|t|}tt| d}| j	t|  |
| W d    d S 1 sAw   Y  d S d S d S )N   r   r   )get_build_msvc_versionr   r   r   r   r   r   r  
temp_filesr;   r   )r  r   ma_strmi_strmanxmlmanr   r   r   generate_manifest  s   "r  )F)r   )=rc   rO   rw   rq   r#   r'   r   numpy.distutils.ccompilernumpyr   r   distutils.cygwinccompilerr   distutils.versionr   distutils.unixccompilerr   distutils.msvccompilerr   r	  distutils.errorsr   numpy.distutils.misc_utilr   r   r	   r
   r   r   	get_msvcrcompiler   r   r    r   r   r   r   r   r-   r,   r   r   r   r   msvcrtrs   r   r   majorminorrestImportErrorr   r   r   r   r  r  r  r  r   r   r   r   <module>   sj    	

 B) 
;$B


 