o
    "gp                     @   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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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 Z0e/ede0 dPddZ1e/ede1 dQddZ2e/ede2 			dRddZ3e/ede3 dSddZ4e/ed e4 d!d" Z5d#d$ Z6e/ed%e6 dTd&d'Z7e/ed(e7 dUd*d+Z8d,d gfd-d.Z9e/ed/e9 d0d1 Z:e/ed2e: d3ed4< d5ed6< d7ed8< d9ed:< d;ed<< d=ed>< d?ed@< dAedB< e j;dC7  _;ejdDkr8dEedF< e$ r8e<dG dHej; e_;eZ=				 	 dVdIdJZee_eZ>dKdL Zee_dMD ]Z?ej@AdNe? dO ZBeBdurieCeBdLe qRdS )W    N)copy)Path)	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|}| }W d   n1 sw   Y  t|||}|d }||kr4dS d|dd }	dd tj|	ddD }
zt 	| j
}|
D ]}t 	|j
|kr` W dS qRW d	S  tym   Y dS w )
z
    Check if an objects needs to be rebuild based on its dependencies

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

    Returns
    -------
    bool
    .dTN c                 S   s"   g | ]}|d kr| ds|qS )
:)endswith).0x r"   [/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/numpy/distutils/ccompiler.py
<listcomp>G   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_optsdep_fileflinescmdlinelast_cmdlinecontentsdepst_objr"   r"   r#   _needs_build'   s.   

r=   c                    s    fdd}t | || d S )Nc                    s    | g|R i |S Nr"   )selfargskwfuncr"   r#   <lambda>[   s    z replace_method.<locals>.<lambda>)setattr)klassmethod_namerC   mr"   rB   r#   replace_methodY   s   rI   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_executablesd   s   rK   find_executablesc           	      C   sV  |dur|nt tj}|du r|}t|rdt|}t| z| jr/t	j
||d W dS t	j
|t	j|d W dS  t	jyS } z|j}|j}W Y d}~n6d}~w ty } z&d| d}z	|tjj}W n tyw   |d}Y nw d}W Y d}~nd}~ww t|rdt|}| jrt| td	|rd
}nd}td|||f )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)stderrrN   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displayrN   excosemsgr"   r"   r#   CCompiler_spawnr   sH   
rk   spawnr   c                 C   s   |du rd}g }|D ]h}t jt j|\}}t j|d }|t j|d }|drN|dd }|d| }	t jt j	|	}	|	||d  }|| j
vr[td||f |rct 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abspathsrc_extensionsr   r+   obj_extensionappend)r?   source_filenames	strip_dir
output_dir	obj_namessrc_namebaseextidobj_namer"   r"   r#   CCompiler_object_filenames   s&   

r   object_filenamesc	                    sn  t  }	t tdu rt|	aW d   n1 sw   Y  |s"g S ddlm}
m}m} t	|
rVg }dD ]}t
d| }|du rCq5|d|d|f  q5d|}nj}d	d|f }t| |||||\}}}|| d
d  }r|dd 7 }t|  fdd}t	|
rt| }g g }}|D ]9}||v r|| \}}jdkrt|}t|}t|j |v r||s||||ff q||||ff q|}|D ]}|| qn| }t|dkr+|	dkr+ddlm} ||	}|||}W d   n	1 s w   Y  t| |S |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FORTRAN_COMMON_FIXED_EXTENSIONShas_f90_header)f77f90fix	compiler_zFortran %s compiler: %srM   r   zC compiler: %s
zcompile options: '%s'z
extra options: '%s'c                    s  | \}\}}t | sd S 	 t |tvr$t| 	 W d    nW d    n1 s.w   Y  td qz7t |||  W d    n1 sQw   Y  W t t| W d    d S 1 sjw   Y  d S t t| W d    w 1 sw   Y  w )NTg?)	r=   _global_lock_processing_filesaddtimesleep_job_semaphore_compileremove)r@   r1   srcr   r2   r3   r4   r?   r"   r#   single_compile4  s.   


(z)CCompiler_compile.<locals>.single_compileabsoftrm   )ThreadPoolExecutor)r   r   r   	threading	Semaphorenumpy.distutils.fcompilerr   r   r   
isinstancegetattrry   r+   compiler_sor   rU   _setup_compile_get_cc_argsrT   keyscompiler_typer   r   suffixloweritemslenconcurrent.futuresr   map)r?   sourcesr|   macrosinclude_dirsdebugextra_preargsr3   dependsjobsr   r   r   re   fcfcompccompobjectsbuildr   objects_to_buildf77_objectsother_objectsr1   r   r   build_itemsrg   r   poolresr"   r   r#   CCompiler_compile   sx   (








r   compiler"   c                    s(  t d| jj jjf  t| dr0d| jd v r0t dkr$tjdks0| j	d | j
	d  fdd	}|d
rA|  j |drS jD ]
\}}| || qH|drb jD ]}| | qZ|dro| | j j  |dr|| | j j  |dr|  j |dr|  j dS 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 ``distutils.ccompiler.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   arm64darwinz-ftrapping-mathc                    s   t  | d d uo| vS r>   )r   )attrrd   ignorer"   r#   allow  s   z&CCompiler_customize_cmd.<locals>.allowr   defineundef	librarieslibrary_dirsrpathlink_objectsN)r   rU   	__class____name__hasattrr   platformmachiner^   ry   r   set_include_dirsr   r   define_macror   undefine_macroset_librariesr   set_library_dirsr   set_runtime_library_dirsr   set_link_objectsr   )r?   rd   r   r   namevaluemacror"   r   r#   CCompiler_customize_cmdx  s6   
r   customize_cmdc                    s   g }d}t | j }dD ]}||vr|| q|D ]}t| |r7t| |}t|t|}||t|f qdt|d  d   fdd|D }d	|S )	Nr   )
versionr   r   object_switchcompile_switchr   r   r   r   r   z%-rm   zs = %sc                    s   g | ]} | qS r"   r"   )r    propfmtr"   r#   r$     s    z'_compiler_to_string.<locals>.<listcomp>r   )
rT   executablesr   ry   r   r   maxr   reprr+   )r   propsmxr   keyvr7   r"   r   r#   _compiler_to_string  s    



r   c                 C   sZ   z|    W n	 ty   Y nw tjjdk r+td t| j tt|  td dS 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.

    rn   zP********************************************************************************N)get_version	Exceptionr   _global_log	thresholdprintr   r   rJ   r"   r"   r#   CCompiler_show_customization  s   
r   show_customizationc           
   	   C   s  t d| jj  t|  |rrz| jd W n ttfy"   Y nw t	| drVd| j
d v rV| jsU| j
d dr?d\}}nd\}}| j
d ||g| j
d	d
  | _nt	| drdt d| j
f  t	| dsrt d| jj  t	| drd| j
d v sd| j
d v sd| j
d v rd| _d
S tjdkrdd
l}dd
l}| }zQz4tj|d}t|d}	|	d W d
   n1 sw   Y  | j|g|dd|d gd d| _W n ty   d| _Y n	w W || d
S W || d
S || w d
S )ao  
    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++rm   Nz#### %s #######compiler_cxxzMissing compiler_cxx fix for r   r   Tr%   zfile.cwzint a;
z-MMDz-MFr   )r|   r   F)r   rU   r   r   r   r   r   ra   
ValueErrorr   r   r   rs   replacewarn_auto_dependsr&   r   tempfileshutilmkdtempr'   r+   r)   writer   r   rmtree)
r?   distneed_cxxabr   r   tmpdirfnr6   r"   r"   r#   CCompiler_customize  sd   








r  	customize[-.\d]+c                    s    fdd}|S )ak  
    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 ``distutils.ccompiler.CCompiler`` class. `matcher` takes a single parameter,
        a version string.

    c                    sz   | dd}d}rt|}|sd S | }	 t||d  }|s'd S  r7t |dr7| }q	 |dS )Nr   rM   r   )r   rb   matchendrc   group)r?   version_stringposrH   r   patstartr"   r#   matcherL  s    
z%simple_version_match.<locals>.matcherr"   )r  r   r  r  r"   r  r#   simple_version_match2  s   r  Fc           	         s.  |s
t | dr
| jS |   z| j}W n
 ty   Y dS w |r$|d s&dS z| j}W n  tyK   z| j W n tyB   Y Y dS w  fdd}Y nw z
tj|tj	d}W n% tj
yo } z|j}|j}W Y d}~nd}~w ty{   d}d}Y nw t|}d}d}||v r||}|rt|}|| _|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   )rb   r	  r  )r  rH   r   r  r"   r#   r    s
   
z&CCompiler_get_version.<locals>.matcher)rO   rQ       )r   r   rL   version_cmdra   version_matchversion_patternrW   rX   rY   rZ   r[   r\   r0   r   r   )	r?   force	ok_statusr  r  r[   rf   statusr   r"   r  r#   CCompiler_get_versiona  sN   


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rX|j	d	 |j	d
 |j	d
 |S |jd g|jdd  |_|S )z
    Return the C++ compiler.

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

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

    )msvcintelwintelemwr   rm   N)aixos400	ld_so_aixrn   r   z-D__STDC_FORMAT_MACROSz-fno-extern-tls-init)
r   r   r   r   r   r^   r   rs   	linker_sory   )r?   cxxr"   r"   r#   CCompiler_cxx_compiler  s&   

r$  cxx_compiler)intelccompilerIntelCCompilerz(Intel C Compiler for 32-bit applicationsintel)r&  IntelItaniumCCompilerz7Intel C Itanium Compiler for Itanium-based applicationsintele)r&  IntelEM64TCCompilerz(Intel C Compiler for 64-bit applicationsintelem)r&  IntelCCompilerWz3Intel C Compiler for 32-bit applications on Windowsr  )r&  IntelEM64TCCompilerWz3Intel C Compiler for 64-bit applications on Windowsr  )pathccompilerPathScaleCCompilerz2PathScale Compiler for SiCortex-based applicationspathcc)armccompilerArmCCompilerzArm C Compilerarm)fujitsuccompilerFujitsuCCompilerzFujitsu C Compilerfujitsu))linux.*r(  )r8  r*  )r8  r,  )r8  r1  )ntr  )r9  r  win32)mingw32ccompilerMingw32CCompilerz>Mingw32 port of GNU C Compiler for Win32(for MSC built Python)r   z+Setting mingw32 as default compiler for nt.))r9  r   c                 C   sp  |d u rt  t jk}| d u rtj} z|d u rt| }t| \}}}W n ty<   d|  }|d ur8|d|  }t|w d| }zt	| W nA t
y }	 z5t|	}t dt| |dd  }zt	| W n t
y} }	 z
t|	}td| d }	~	ww W Y d }	~	nd }	~	ww ztj| }
t|
| }W n ty   td||f w |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   get_thresholdINFOr&   r   r   r   KeyErrorr   
__import__ImportErrorstrrU   r   r^   modulesvarsrV   r   )platr   rV   dry_runr  module_name
class_namelong_descriptionrj   ri   modulerF   r"   r"   r#   r     s^   
r   c                 C   sB   t | |||}g }|D ]}t|r|t| q|| q|S r>   )_distutils_gen_lib_optionsr   extendrT   ry   )r   r   runtime_library_dirsr   rlib_optsr   r"   r"   r#   r      s   r   )msvc9r  _msvcbcppcygwincemxcunixcz
distutils.r   )NN)r   r   )NNNr   NNN)r"   )r   )r  r   r   )NNNr   r   )Dr&   rb   r^   r   r,   r   rW   r   pathlibr   	distutilsr   distutils.ccompilerr   r   r   r   r	   distutils.errorsr
   r   r   r   r   distutils.sysconfigr   distutils.versionr   numpy.distutilsr   numpy.distutils.exec_commandr   r   numpy.distutils.misc_utilr   r   r   r   r   r   r   r   Lockr   setr   r=   rI   rK   rk   r   r   r   r   r   r  r  r  r$  _default_compilersrU   _distutils_new_compilerrL  _ccrD  get_mrE   r"   r"   r"   r#   <module>   s     2

G
-
 
6
Q
/C$

,
