a
    p=icW                     @   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mZmZmZ dd	 Z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'< e+e*d(dZ,e,durre-d)e,. \Z/Z0e,e)e/e0 < [/[0[,W n e1y   e
2d* Y n0 d+d, Z3d;d.d/Z4d0d1 Z5d2d3 Z6d4d5 Z7d6d7 Z8d8d9 Z9d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)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   )Zmsvcr r
   q/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/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| ||| t  t }tdd}|s2|r<| d t }|rV| dd|  t dkrt| j	ddd	d
d n| j	ddddd dg| _
d S )NT)debugZNPY_MINGW_USE_CUSTOM_MSVCRZ__MSVCRT_VERSION__z0x%04iAMD64z#gcc -g -DDEBUG -DMS_WIN64 -O0 -Wallz7gcc -g -DDEBUG -DMS_WIN64 -O0 -Wall -Wstrict-prototypeszgcc -gzgcc -g -shared)compilercompiler_so
linker_exe	linker_sozgcc -O2 -Wallz!gcc -O2 -Wall -Wstrict-prototypeszg++ zg++ -sharedzg++)	distutilscygwinccompilerCygwinCCompiler__init__build_import_librarybuild_msvcr_libraryZdefine_macror   r	   set_executablescompiler_cxx)selfverbosedry_runforceZmsvcr_successZmsvcr_dbg_successZmsvcr_versionr
   r
   r   r   5   s4    


zMingw32CCompiler.__init__Nc                 C   sZ   t  }|r|sg }|| | |||||||d |	|
|||f}tj}||d |jj   d S )N)r   appendr   link__code__co_argcount)r   Ztarget_descobjectsZoutput_filename
output_dir	librarieslibrary_dirsruntime_library_dirsexport_symbolsr   Zextra_preargsZextra_postargs
build_tempZtarget_langZruntime_libraryargsfuncr
   r
   r   r!   m   s.    
zMingw32CCompiler.link c           	      C   s   |d u rd}g }|D ]}t jt j|\}}t j|\}}|rP|dd  }|| jddg vrrtd||f |rt j|}|dks|dkr|t j	||| | j
  q|t j	||| j
  q|S )Nr-      .rcz.resz"unknown file type '%s' (from '%s'))ospathsplitextnormcase
splitdriveZsrc_extensionsr   basenamer    joinZobj_extension)	r   Zsource_filenamesZ	strip_dirr%   Z	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   .   s       
@      
)  r   c            
      C   sN  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 g d}g }| D ]"}|D ]}|tj|| qfq^dtj	v r|tjtj	d d t
t jd d \}}t }|dkrd| | d	}n |d
krd| d}n
d| }td|  |D ]*}tj||}	tj|	r|	  S qtd||f d S )Nbase_prefixreal_prefix)r-   libbinZ
SYSTEMROOTZSystem32   CPythonpython.dllPyPyZlibpypyz-c.dllzUnknown platform zLooking for %sz%s not found in %s)sysprefixhasattrrA   r    rB   r0   r1   r6   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   s2    

r_   c                 C   s   t dd| g}|dS )Nzobjdump.exez-p   
)
subprocesscheck_outputsplit)r^   str
   r
   r   
dump_table   s    re   c           	      C   s,  t | }tt|D ]}t||  r q8qtdg }t|d t|D ]B}t||  }|r|t	|
d |
df qN qqNt|dkrtd|   t|d`}|dtj|   |d |d	 |d
 |D ]}|d|d   qW d   n1 s0    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.   rE   r   zNo symbols found in %swzLIBRARY        %s
z,;CODE          PRELOAD MOVEABLE DISCARDABLE
z;DATA          PRELOAD SINGLE
z	
EXPORTS
z%s
N)re   rangelen_STARTmatchdecoderT   _TABLEr    intgroupstripr   warnopenwriter0   r1   r5   )	r^   dfiledumpiZsymsjmdsr
   r
   r   generate_def   s(    $


rz   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 t |D ],\}}}| |v r2 |v r2t j||   S q2d S )NZWINDIRz
C:\WINDOWSZwinsxs)r0   r1   r6   rM   getrS   walk)dll_nameZwinsxs_pathrootdirsfilesarchr
   r   _find_dll_in_winsxs	  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;)	rJ   rK   r0   rM   rc   r1   r6   rS   abspath)r   r1   filepathr
   r
   r   _find_dll_in_path  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 }| 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 presentrx   zlib%s.alibsz(Skip building msvcr library: "%s" existsTrH   z*Cannot build msvcr library: "%s" not foundz	lib%s.defz&Building msvcr library: "%s" (from %s)dlltool-d-l)r0   namer   r   r   r   r1   r6   rJ   rK   isfiler   rp   inforz   ra   callremove)r   Z	msvcr_verZ
msvcr_nameZout_nameout_fileZmsvcr_dll_namedll_filedef_namedef_filecmdretcoder
   r
   r   r     sH    





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   r}   zUnhandled arch %s)r0   r   r	   _build_import_library_amd64_build_import_library_x86rT   r   r
   r
   r   r   Y  s    
r   c               
   C   s   t tjdd \} }g d}tjg}ttdrJtjtjkrJ|tj n"ttdrltjtjkrl|tj ddg}g }|D ]:}|| |f }|D ]$}|D ]}	|tj	
||	| qqq||D ]}
tj	|
rd|
f  S qd	|d
 fS )zACheck if an import library for the Python runtime already exists.NrE   )zlibpython%d%d.azlibpython%d%d.dll.azlibpython%d.%d.dll.arA   rB   r   rC   TFr   )rN   rJ   rO   rK   rL   rA   r    rB   r0   r1   r6   r   )rZ   r[   patternsrU   rV   
candidatespatfilenameZstem_dirrY   fullnamer
   r
   r   _check_for_import_libe  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.defrE   r   r   r   r   )r   r   r   r_   r   rN   rJ   rO   r0   r1   r6   rK   rz   ra   
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
|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   n1 sL0    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.librE   r   rA   rB   r-   z+Cannot build import library: "%s" not foundz(Building import library (ARCH=x86): "%s"r   )lib2defr   F)shellrf   r   z	--dllnamez--defz--output-libz:Failed to build import library for gcc. Linking will fail.)r   r   r   rN   rJ   rO   r0   r1   r6   rK   r   rL   rA   rB   rp   r   numpy.distutilsr   ZgetnmZ
DEFAULT_NMZparse_nmrq   Z
output_defZ
DEF_HEADERr_   ra   rb   )r   r   Zlib_nameZlib_fileZbase_libr   r   r   Z	nm_outputZdlistflistZfidr   r   statusr
   r
   r   r     sH    


2

r   win32z8.0.50727.4280z9.0.21022.8Z90z10.0.30319.460100ZCRT_ASSEMBLY_VERSIONz(\d+)\.(\d)z9Cannot import msvcrt: using manifest will not be possiblec                 C   sX   zt t| d |  }W n$ ty<   td| |f dY n0 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KeyErrorrT   textwrapdedent)r   r   r   templater
   r
   r   msvc_manifest_xml  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^   rE   exer.   zType %s not supportedz&#include "winuser.h"
%d RT_MANIFEST %s)rT   )r   typeZrctyper
   r
   r   manifest_rc  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   rm   rT   )msverr   r
   r
   r   !check_embedded_msvcr_match_linked.  s    
r   c                 C   s&   t j| dg d}t j|d S )NZyocr   )r0   r1   r5   Z_gen_temp_sourcefiler2   )configr8   r
   r
   r   configtest_name:  s    r   c                 C   s   t | }| jj}|| d S )Nz	.manifest)r   r   Zexe_extension)r   r   Zexextr
   r
   r   manifest_name>  s    r   c                 C   s   t | }|d S )Nr/   )r   )r   r   r
   r
   r   rc_nameD  s    r   c                 C   s   t  }|d ur|dkrt| t|d\}}tt|t|}tt| d*}| j	t|  |
| W d    n1 s0    Y  d S )N   .rf   )get_build_msvc_versionr   r   rc   r   rm   rq   r   Z
temp_filesr    rr   )r   r   Zma_strZmi_strZmanxmlmanr
   r
   r   generate_manifestI  s    r   )F)r^   ):r?   r0   rP   rJ   ra   rer   Znumpy.distutils.ccompilernumpyr   r   Zdistutils.cygwinccompilerr   Zdistutils.unixccompilerr   Zdistutils.msvccompilerr   r   distutils.errorsr   Znumpy.distutils.misc_utilr   r   r   r	   r   compileri   rl   r   r   r   r_   re   rz   r   r   r   r   r   r   r   msvcrtgetattrZcrt_verrj   groupsr   r   ImportErrorrp   r   r   r   r   r   r   r   r
   r
   r
   r   <module>   sb   	

 
) 
;$B
 
