a
    p=icm                     @   s  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mZ d dl	m
Z
mZmZmZmZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZm Z m!Z!m"Z"m#Z#m$Z$ d dl%Z%da&e%' Z(e) Z*dd Z+dd Z,dd Z-e,ede- dMddZ.e,ede. dNddZ/e,ede/ dOddZ0e,ede0 dPddZ1e,ede1 d d! Z2d"d# Z3e,ed$e3 dQd%d&Z4e,ed'e4 dRd)d*Z5d+d gfd,d-Z6e,ed.e6 d/d0 Z7e,ed1e7 d2e
d3< d4e
d5< d6e
d7< d8e
d9< d:e
d;< d<e
d=< d>e
d?< e j8d@7  _8ej9dAkrNdBe
dC< e! rNe:dD dEej8 e_8eZ;dSdFdGZee_eZ<dHdI Zee_dJD ]0Z=ej>?dKe= dL Z@e@durxeAe@dIe qxdS )T    N)copy)	ccompiler)compiler_classgen_lib_optionsget_default_compilernew_compiler	CCompiler)DistutilsExecErrorDistutilsModuleErrorDistutilsPlatformErrorCompileErrorUnknownFileError)customize_compiler)LooseVersion)log)filepath_from_subprocess_outputforward_bytes_to_stdout)	cyg2win32is_sequencemingw32get_num_build_jobs_commandline_dep_stringsanitize_cxx_flagsc                 C   s   | d }t j|sdS t|d}| }W d   n1 s@0    Y  t|||}|d }||krjdS d|dd }	dd tj|	dd	D }
z2t 	| j
}|
D ]}t 	|j
|kr W dS qW n ty   Y dS 0 d
S )z
    Check if an objects needs to be rebuild based on its dependencies

    Parameters
    ----------
    obj : str
        object file

    Returns
    -------
    bool
    .dTrN c                 S   s"   g | ]}|d kr| ds|qS )
:)endswith).0x r"   j/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/numpy/distutils/ccompiler.py
<listcomp>E   s   z _needs_build.<locals>.<listcomp>)posixF)ospathexistsopen	readlinesr   joinshlexsplitstatst_mtimeOSError)objcc_argsextra_postargspp_optsZdep_fileflinesZcmdlineZlast_cmdlinecontentsdepsZt_objr"   r"   r#   _needs_build%   s&    &r9   c                    s    fdd}t | || d S )Nc                    s    | g|R i |S Nr"   )selfargskwfuncr"   r#   <lambda>Y       z replace_method.<locals>.<lambda>)setattr)klassmethod_namer?   mr"   r>   r#   replace_methodW   s    rF   c                 C   s   dS )z
    Does nothing here, but is called by the get_version method and can be
    overridden by subclasses. In particular it is redefined in the `FCompiler`
    class where more documentation can be found.

    Nr"   r;   r"   r"   r#   CCompiler_find_executablesb   s    rH   find_executablesc           	      C   s`  |dur|nt tj}|du r8|}t|r8dt|}t| z,| jrZt	j
||d nt	j
|t	j|d W n t	jy } z|j}|j}W Y d}~nrd}~0  ty } zLd| d}z|tjj}W n ty   |d}Y n0 d}W Y d}~nd}~0 0 dS t|r"dt|}| jr2t| td	|rFd
}nd}td|||f dS )a  
    Execute a command in a sub-process.

    Parameters
    ----------
    cmd : str
        The command to execute.
    display : str or sequence of str, optional
        The text to add to the log file kept by `numpy.distutils`.
        If not given, `display` is equal to `cmd`.
    env : a dictionary for environment variables, optional

    Returns
    -------
    None

    Raises
    ------
    DistutilsExecError
        If the command failed, i.e. the exit status was not 0.

    N )env)stderrrK   z

z


utf8   s   Too many open filesz2
Try rerunning setup command until build succeeds.r   z)Command "%s" failed with exit status %d%s)dictr&   environr   r+   listr   infoverbose
subprocesscheck_outputSTDOUTCalledProcessErroroutput
returncoder0   encodesysstdoutencodingAttributeErrorr   researchr	   )	r;   cmddisplayrK   excosemsgr"   r"   r#   CCompiler_spawnp   s>    

rh   spawnr   c                 C   s   |du rd}g }|D ]}t jt j|\}}t j|d }|t j|d }|dr|dd }|d| }	t jt j	|	}	|	||d  }|| j
vrtd||f |rt j|}t j||| j }
||
 q|S )a  
    Return the name of the object files for the given source files.

    Parameters
    ----------
    source_filenames : list of str
        The list of paths to source files. Paths can be either relative or
        absolute, this is handled transparently.
    strip_dir : bool, optional
        Whether to strip the directory from the returned paths. If True,
        the file name prepended by `output_dir` is returned. Default is False.
    output_dir : str, optional
        If given, this path is prepended to the returned paths to the
        object files.

    Returns
    -------
    obj_names : list of str
        The list of paths to the object files corresponding to the source
        files in `source_filenames`.

    Nr      z..   z"unknown file type '%s' (from '%s'))r&   r'   splitextnormpath
splitdriveisabs
startswithrfindbasenameabspathZsrc_extensionsr   r+   Zobj_extensionappend)r;   Zsource_filenamesZ	strip_dir
output_dirZ	obj_namessrc_namebaseextidobj_namer"   r"   r#   CCompiler_object_filenames   s&    

r|   Zobject_filenamesc	                    sr  t  }	t" tdu rt|	aW d   n1 s20    Y  |sDg S ddlm}
m}m} t	|
rg }dD ]4}t
d| }|du rqj|d|d|f  qjd|}nj}d	d|f }t| |||||\}}}|| d
d  }r|dd 7 }t|  fdd}t	|
rt| }g g  }}|D ]r}||v r`|| \}}jdkrt|}t|}||r||s||||ff n||||ff q`|}|D ]}|| qn| }t|dkrZ|	dkrZddlm} ||	}|||}W d   n1 sF0    Y  t| n|D ]}|| q^|S )a  
    Compile one or more source files.

    Please refer to the Python distutils API reference for more details.

    Parameters
    ----------
    sources : list of str
        A list of filenames
    output_dir : str, optional
        Path to the output directory.
    macros : list of tuples
        A list of macro definitions.
    include_dirs : list of str, optional
        The directories to add to the default include file search path for
        this compilation only.
    debug : bool, optional
        Whether or not to output debug symbols in or alongside the object
        file(s).
    extra_preargs, extra_postargs : ?
        Extra pre- and post-arguments.
    depends : list of str, optional
        A list of file names that all targets depend on.

    Returns
    -------
    objects : list of str
        A list of object file names, one per source file `sources`.

    Raises
    ------
    CompileError
        If compilation fails.

    Nr   )	FCompiler	is_f_filehas_f90_header)Zf77f90ZfixZ	compiler_zFortran %s compiler: %srJ   r   zC compiler: %s
zcompile options: '%s'z
extra options: '%s'c                    s  | \}\}}t | sd S t0 |tvrDt| W d    qnW d    n1 sX0    Y  td qzjt$ |||  W d    n1 s0    Y  W t t| W d    n1 s0    Y  n0t t| W d    n1 s0    Y  0 d S )Ng?)	r9   _global_lock_processing_filesaddtimesleep_job_semaphore_compileremove)r<   r1   srcrx   r2   r3   r4   r;   r"   r#   single_compile1  s    
,4*z)CCompiler_compile.<locals>.single_compileZabsoftrj   )ThreadPoolExecutor)r   r   r   	threading	SemaphoreZnumpy.distutils.fcompilerr}   r~   r   
isinstancegetattrrt   r+   compiler_sor   rR   Z_setup_compileZ_get_cc_argsrQ   keyscompiler_typer   itemslenconcurrent.futuresr   map)r;   sourcesru   macrosinclude_dirsdebugextra_preargsr3   dependsjobsr}   r~   r   rb   ZfcZfcompZccompobjectsbuildr   Zobjects_to_buildZf77_objectsZother_objectsr1   r   rx   Zbuild_itemsrd   r   poolresr"   r   r#   CCompiler_compile   sh    ((





,
r   compiler"   c                    s  t d| jj jjf  t| drJd| jd v rJ| jd | jd  fdd}|drl|  j	 |d	r j
D ]\}}| || qz|d
r jD ]}| | q|dr| | j j  |dr| | j j  |dr|  j |dr|  j dS )a  
    Customize compiler using distutils command.

    Parameters
    ----------
    cmd : class instance
        An instance inheriting from `distutils.cmd.Command`.
    ignore : sequence of str, optional
        List of `CCompiler` commands (without ``'set_'``) that should not be
        altered. Strings that are checked for are:
        ``('include_dirs', 'define', 'undef', 'libraries', 'library_dirs',
        'rpath', 'link_objects')``.

    Returns
    -------
    None

    zcustomize %s using %scompilerclangr   z-ftrapping-mathc                    s   t  | d d uo| vS r:   )r   )attrra   ignorer"   r#   allow  s    z&CCompiler_customize_cmd.<locals>.allowr   defineundef	librarieslibrary_dirsrpathlink_objectsN)r   rR   	__class____name__hasattrr   rt   r   Zset_include_dirsr   r   Zdefine_macror   Zundefine_macroZset_librariesr   Zset_library_dirsr   Zset_runtime_library_dirsr   Zset_link_objectsr   )r;   ra   r   r   namevaluemacror"   r   r#   CCompiler_customize_cmdt  s.    

r   Zcustomize_cmdc                    s   g }d}t | j }dD ]}||vr|| q|D ]8}t| |r6t| |}t|t|}||t|f q6dt|d  d   fdd|D }d	|S )	Nr   )
versionr   r   Zobject_switchZcompile_switchr   r   r   r   r   z%-rj   zs = %sc                    s   g | ]} | qS r"   r"   )r    propfmtr"   r#   r$     rA   z'_compiler_to_string.<locals>.<listcomp>r   )
rQ   Zexecutablesr   rt   r   r   maxr   reprr+   )r   propsZmxr   keyvr6   r"   r   r#   _compiler_to_string  s    

r   c                 C   sV   z|    W n ty   Y n0 tjjdk rRtd t| j tt|  td dS )z
    Print the compiler customizations to stdout.

    Parameters
    ----------
    None

    Returns
    -------
    None

    Notes
    -----
    Printing is only done if the distutils log threshold is < 2.

    rk   zP********************************************************************************N)get_version	Exceptionr   _global_log	thresholdprintr   r   rG   r"   r"   r#   CCompiler_show_customization  s    
r   Zshow_customizationc           
   	   C   s  t d| jj  t|  |rz| jd W n ttfyD   Y n0 t	| drd| j
d v r| js| j
d dr~d\}}nd\}}| j
d ||g| j
d	d
  | _n8t	| drt d| j
f  t	| dst d| jj  t	| dr(d| j
d v s d| j
d v s d| j
d v r(d| _ntjdkrdd
l}dd
l}| }zzjtj|d}t|d}	|	d W d
   n1 s0    Y  | j|g|dd|d gd d| _W n ty   d| _Y n0 W || n|| 0 d
S )am  
    Do any platform-specific customization of a compiler instance.

    This method calls `distutils.sysconfig.customize_compiler` for
    platform-specific customization, as well as optionally remove a flag
    to suppress spurious warnings in case C++ code is being compiled.

    Parameters
    ----------
    dist : object
        This parameter is not used for anything.
    need_cxx : bool, optional
        Whether or not C++ has to be compiled. If so (True), the
        ``"-Wstrict-prototypes"`` option is removed to prevent spurious
        warnings. Default is False.

    Returns
    -------
    None

    Notes
    -----
    All the default options used by distutils can be extracted with::

      from distutils import sysconfig
      sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS',
                                'CCSHARED', 'LDSHARED', 'SO')

    zcustomize %sz-Wstrict-prototypesr   ccr   gcc)r   g++)r   zc++rj   Nz#### %s #######compiler_cxxzMissing compiler_cxx fix for r   r   Tr%   zfile.cwzint a;
z-MMDz-MFr   )ru   r   F)r   rR   r   r   r   r   r   r^   
ValueErrorr   r   r   rp   replacewarnZ_auto_dependsr&   r   tempfileshutilmkdtempr'   r+   r)   writer   r   rmtree)
r;   distZneed_cxxabr   r   Ztmpdirfnr5   r"   r"   r#   CCompiler_customize  sR    


*

r   Z	customize[-.\d]+c                    s    fdd}|S )aU  
    Simple matching of version numbers, for use in CCompiler and FCompiler.

    Parameters
    ----------
    pat : str, optional
        A regular expression matching version numbers.
        Default is ``r'[-.\d]+'``.
    ignore : str, optional
        A regular expression matching patterns to skip.
        Default is ``''``, in which case nothing is skipped.
    start : str, optional
        A regular expression matching the start of where to start looking
        for version numbers.
        Default is ``''``, in which case searching is started at the
        beginning of the version string given to `matcher`.

    Returns
    -------
    matcher : callable
        A function that is appropriate to use as the ``.version_match``
        attribute of a `CCompiler` class. `matcher` takes a single parameter,
        a version string.

    c                    sz   | dd}d}r0t|}|s(d S | }t||d  }|sLd S  rpt |drp| }q0qpq0|dS )Nr   rJ   r   )r   r_   matchendr`   group)r;   version_stringposrE   r   patstartr"   r#   matcherB  s    z%simple_version_match.<locals>.matcherr"   )r   r   r   r   r"   r   r#   simple_version_match(  s    r   Fc           	         s2  |st | dr| jS |   z
| j}W n ty:   Y dS 0 |rH|d sLdS z
| j}W n@ ty   z
| j W n ty   Y Y dS 0  fdd}Y n0 ztj|tj	d}W nJ tj
y } z|j}|j}W Y d}~n.d}~0  ty   d}d}Y n0 t|}d}d}||v r(||}|r(t|}|| _|S )	a  
    Return compiler version, or None if compiler is not available.

    Parameters
    ----------
    force : bool, optional
        If True, force a new determination of the version, even if the
        compiler already has a version attribute. Default is False.
    ok_status : list of int, optional
        The list of status values returned by the version look-up process
        for which a version string is returned. If the status value is not
        in `ok_status`, None is returned. Default is ``[0]``.

    Returns
    -------
    version : str or None
        Version string, in the format of `distutils.version.LooseVersion`.

    r   Nr   c                    s"   t  | }|sd S |d}|S )Nr   )r_   r   r   )r   rE   r   r   r"   r#   r   {  s
    
z&CCompiler_get_version.<locals>.matcher)rL   rN   rA   )r   r   rI   version_cmdr^   Zversion_matchZversion_patternrT   rU   rV   rW   rX   rY   r0   r   r   )	r;   forceZ	ok_statusr   r   rX   rc   statusr   r"   r   r#   CCompiler_get_versionW  sD    





r   r   c                 C   s   | j dv r| S t| }|j|_|jd gt|jdd  |_tjdr|d|jd v r||jd |jd g|jdd  |_tjdr|j	d	 |j	d
 |j	d
 n|jd g|jdd  |_|S )z
    Return the C++ compiler.

    Parameters
    ----------
    None

    Returns
    -------
    cxx : class instance
        The C++ compiler, as a `CCompiler` instance.

    )msvcintelwintelemwr   rj   N)aixos400Z	ld_so_aixrk   r   z-D__STDC_FORMAT_MACROSz-fno-extern-tls-init)
r   r   r   r   r   r[   platformrp   	linker_sort   )r;   cxxr"   r"   r#   CCompiler_cxx_compiler  s&    

r   Zcxx_compiler)intelccompilerZIntelCCompilerz(Intel C Compiler for 32-bit applicationsintel)r   ZIntelItaniumCCompilerz7Intel C Itanium Compiler for Itanium-based applicationsintele)r   ZIntelEM64TCCompilerz(Intel C Compiler for 64-bit applicationsintelem)r   ZIntelCCompilerWz3Intel C Compiler for 32-bit applications on Windowsr   )r   ZIntelEM64TCCompilerWz3Intel C Compiler for 64-bit applications on Windowsr   )ZpathccompilerZPathScaleCCompilerz2PathScale Compiler for SiCortex-based applicationspathcc)ZarmccompilerZArmCCompilerzArm C Compilerarm))linux.*r   )r   r   )r   r   )r   r   )ntr   )r   r   win32)Zmingw32ccompilerZMingw32CCompilerz>Mingw32 port of GNU C Compiler for Win32(for MSC built Python)r   z+Setting mingw32 as default compiler for nt.))r   r   c                 C   s  |d u rt  t jk}| d u r$tj} z"|d u r6t| }t| \}}}W n6 ty|   d|  }|d urp|d|  }t|Y n0 d| }zt	| W n t
y$ }	 zxt|	}t dt| |dd  }zt	| W n: t
y }	 z t|	}td| W Y d }	~	n
d }	~	0 0 W Y d }	~	n
d }	~	0 0 ztj| }
t|
| }W n$ tyd   td||f Y n0 |d ||}||_t d|  |S )	Nz5don't know how to compile C/C++ code on platform '%s'z with '%s' compilerznumpy.distutils.z,%s in numpy.distutils; trying from distutils   z4can't compile C/C++ code: unable to load module '%s'zBcan't compile C/C++ code: unable to find class '%s' in module '%s'znew_compiler returns %s)r   Zget_thresholdINFOr&   r   r   r   KeyErrorr   
__import__ImportErrorstrrR   r
   r[   modulesvarsrS   r   )platr   rS   dry_runr   module_name
class_namelong_descriptionrg   rf   modulerC   r"   r"   r#   r     sP    2
r   c                 C   sB   t | |||}g }|D ]&}t|r2|t| q|| q|S r:   )_distutils_gen_lib_optionsr   extendrQ   rt   )r   r   runtime_library_dirsr   r   Zlib_optsry   r"   r"   r#   r     s    r   )Zmsvc9r   Z_msvcZbcppZcygwincZemxcZunixcz
distutils.r   )NN)r   r   )NNNr   NNN)r"   )r   )r   r   r   )NNNr   r   )Br&   r_   r[   r,   r   rT   r   	distutilsr   distutils.ccompilerr   r   r   r   r   distutils.errorsr	   r
   r   r   r   distutils.sysconfigr   Zdistutils.versionr   Znumpy.distutilsr   Znumpy.distutils.exec_commandr   r   Znumpy.distutils.misc_utilr   r   r   r   r   r   r   r   Lockr   setr   r9   rF   rH   rh   r|   r   r   r   r   r   r   r   r   Z_default_compilersr   rR   Z_distutils_new_compilerr
  Z_ccr  get_mrB   r"   r"   r"   r#   <module>   s    2

G
-   
 
0
Q
/C$
     
,
