o
    ª3IhŒV  ã                   @   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
mZmZ ddlmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ejdkrXdZnejd	kr`d
Zne d  ej¡¡ dZ		d.dd„Z!d/dd„Z"		d0dd„Z#		d1dd„Z$d2dd„Z%edfedfedfedfedfedfedfedfedfedfdœ
Z&d3dd„Z'		d4dd„Z(dd „ Z)d!d"„ Z*d#d$„ Z+		d5d&d'„Z,d(d)„ Z-d6d*d+„Z.d7d,d-„Z/dS )8é    N)ÚPath)Úget_config_varÚget_config_varsÚget_pathé   )ÚCCompilerRunnerÚCppCompilerRunnerÚFortranCompilerRunner)Úget_abspathÚ	make_dirsÚcopyÚGlobÚArbitraryDepthGlobÚglob_at_depthÚimport_module_from_fileÚpyx_is_cplusÚsha256_of_stringÚsha256_of_fileÚCompileErrorÚposixz.oÚntz.objzUnknown os.name: {}Fc                 K   sD  i }|dur9|  ¡ D ].\}}	t|tƒr!t |j¡D ]}
|	||
< qq
t|tƒr4t|j|ƒD ]}
|	||
< q,q
|	||< q
|p<d}tj	 
|¡sTtj	 |¡rPtd |¡ƒ‚t|ƒ |du rgd}| D ]
}t||ddd q\g }| D ]4}|rxtj	 |¡\}}ntj	 tj	 |¡¡\}}| ¡ }| | |i ¡¡ | t||fd|i|¤Ž¡ qk|S )a‘   Compile source code files to object files.

    Parameters
    ==========

    files : iterable of str
        Paths to source files, if ``cwd`` is given, the paths are taken as relative.
    Runner: CompilerRunner subclass (optional)
        Could be e.g. ``FortranCompilerRunner``. Will be inferred from filename
        extensions if missing.
    destdir: str
        Output directory, if cwd is given, the path is taken as relative.
    cwd: str
        Working directory. Specify to have compiler run in other directory.
        also used as root of relative paths.
    keep_dir_struct: bool
        Reproduce directory structure in `destdir`. default: ``False``
    per_file_kwargs: dict
        Dict mapping instances in ``files`` to keyword arguments.
    \*\*kwargs: dict
        Default keyword arguments to pass to ``Runner``.

    Returns
    =======
    List of strings (paths of object files).
    NÚ.z{} is not a directoryT)Úonly_updateÚdest_is_dirÚcwd)ÚitemsÚ
isinstancer   ÚglobÚpathnamer   r   ÚfilenameÚosÚpathÚisdirÚexistsÚOSErrorÚformatr   r   ÚsplitextÚbasenameÚupdateÚgetÚappendÚsrc2obj)ÚfilesÚRunnerÚdestdirr   Úkeep_dir_structÚper_file_kwargsÚkwargsÚ_per_file_kwargsÚkÚvr!   ÚfÚdstpathsÚnameÚextÚfile_kwargs© r:   úh/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sympy/utilities/_compilation/compilation.pyÚcompile_sources   s<   

ÿ

ÿ
r<   c                 C   sn   | pt j dd¡} |  ¡ dkr"|rtdddgi| fS tddgi| fS |  ¡ dks(|r2tdd	gi| fS ti | fS )
NÚSYMPY_COMPILER_VENDORÚgnuÚintelÚflagsz-nofor_mainz-cxxlibÚllvmÚlib_optionsÚfortran)r    Úenvironr)   Úlowerr	   r   Ú
ValueError©ÚvendorÚcplusr   r:   r:   r;   Úget_mixed_fort_c_linkera   s"   ÿ
ÿ
ÿÿrJ   c                 K   s  |du rt j t j | d ¡¡\}}	|r|tdƒ7 }|sM|rFt| dd¡||d\}}
}|
 ¡ D ]\}}||v r@||  |¡ q0|||< q0n|rKt	}nt
}| dg ¡}|r^d|vr^| d¡ | dd	¡}|sjtd
ƒ‚t||d}|| |pug  ||fd|i|¤Ž}| ¡  |S )a    Link object files.

    Parameters
    ==========

    obj_files: iterable of str
        Paths to object files.
    out_file: str (optional)
        Path to executable/shared library, if ``None`` it will be
        deduced from the last item in obj_files.
    shared: bool
        Generate a shared library?
    Runner: CompilerRunner subclass (optional)
        If not given the ``cplus`` and ``fort`` flags will be inspected
        (fallback is the C compiler).
    cwd: str
        Path to the root of relative paths and working directory for compiler.
    cplus: bool
        C++ objects? default: ``False``.
    fort: bool
        Fortran objects? default: ``False``.
    extra_objs: list
        List of paths to extra object files / static libraries.
    \*\*kwargs: dict
        Keyword arguments passed to ``Runner``.

    Returns
    =======

    The absolute path to the generated shared object / executable.

    NéÿÿÿÿÚ
EXT_SUFFIXrH   rG   r@   z-sharedÚ
run_linkerTz*run_linker was set to False (nonsensical).©r   r   )r    r!   r&   r'   r   rJ   r)   r   Úexpandr   r   Úpopr*   rF   r
   Úrun)Ú	obj_filesÚout_fileÚsharedr-   r   rI   ÚfortÚ
extra_objsr1   r8   Úextra_kwargsrH   r3   r4   r@   rM   Úrunnerr:   r:   r;   Úlinkv   s>   "
ýÿ
ü
 rY   c                 K   sÒ  |pg }|  dg ¡}|  dg ¡}	tjdkrt d¡ n¦tjdkrƒtƒ }
| dg ¡|
d g |d< |	|
d g7 }	d	}|
d
  ¡ D ])}|rR| dg ¡d|g |d< d	}q?| d¡ra| 	|dd… ¡ q?| d¡rhd}q?|
d }d 
| d¡dd… ¡dd… }| 	|¡ n>tjdd… dkrn4tdƒrÀtƒ }
| dg ¡|
d g |d< |	|
d g7 }	|
d  ¡ D ]}| d¡r¾||dd… g7 }q®n	 |  dg ¡}d}|D ]}||vrÖ| 	|¡ qËt| fd|||||||	|dœ	|¤ŽS )a‘   Link Python extension module (shared object) for importing

    Parameters
    ==========

    obj_files: iterable of str
        Paths to object files to be linked.
    so_file: str
        Name (path) of shared object file to create. If not specified it will
        have the basname of the last object file in `obj_files` but with the
        extension '.so' (Unix).
    cwd: path string
        Root of relative paths and working directory of linker.
    libraries: iterable of strings
        Libraries to link against, e.g. ['m'].
    cplus: bool
        Any C++ objects? default: ``False``.
    fort: bool
        Any Fortran objects? default: ``False``.
    extra_objs: list
        List of paths of extra object files / static libraries to link against.
    kwargs**: dict
        Keyword arguments passed to ``link(...)``.

    Returns
    =======

    Absolute path to the generate shared object.
    Úinclude_dirsÚlibrary_dirsÚwin32zWindows not yet supported.ÚdarwinÚlinklineÚLDFLAGSÚLIBDIRFÚLIBSz
-frameworkz-lé   NTÚLIBRARYr   rK   é   ÚaixÚPy_ENABLE_SHAREDÚ
BLDLIBRARYr@   )ú-pthread)	rT   r@   r   rI   rU   rZ   Ú	librariesr[   rV   )rP   ÚsysÚplatformÚwarningsÚwarnr   r)   ÚsplitÚ
startswithr*   Újoinr   rY   )rR   Úso_filer   ri   rI   rU   rV   r1   rZ   r[   ÚcfgDictÚis_frameworkÚoptÚlibfileÚlibnamer@   Úneeded_flagsÚflagr:   r:   r;   Ú
link_py_so¾   s\   



€ 
€þ
€þþry   c              	   K   sl  ddl m}m} ddl m} |  ¡  d¡s|  ¡  d¡sJ ‚|p!d}|p%d}| dd¡r.d	nd
}tj 	tj 
| ¡¡d | }tj ||¡}	|rLt ¡ }
nd}
t |¡ z\||ƒ}|j |¡ d|vrgd|jd< || g|ƒ}|jdkrvtdƒ‚tj tj | ¡¡tj |¡kr¨tj |	¡r‘t |	¡ t tj tj | ¡|¡|¡ W t |
¡ |	S W t |
¡ |	S t |
¡ w )a¬   Generates a C file from a Cython source file.

    Parameters
    ==========

    src: str
        Path to Cython source.
    destdir: str (optional)
        Path to output directory (default: '.').
    cwd: path string (optional)
        Root of relative paths (default: '.').
    **cy_kwargs:
        Second argument passed to cy_compile. Generates a .cpp file if ``cplus=True`` in ``cy_kwargs``,
        else a .c file.
    r   )Údefault_optionsÚCompilationOptions)Úcompileú.pyxz.pyr   rI   Fú.cppú.cÚlanguage_levelrd   zCython compilation failed.)ÚCython.Compiler.Mainrz   r{   r|   rE   Úendswithr)   r    r!   r&   r'   rp   ÚgetcwdÚchdirÚ__dict__r(   Ú
num_errorsrF   ÚrealpathÚdirnamer#   ÚunlinkÚshutilÚmove)Úsrcr.   r   Ú	cy_kwargsrz   r{   Ú
cy_compiler8   Úc_nameÚdstfileÚori_dirÚ
cy_optionsÚ	cy_resultr:   r:   r;   Úsimple_cythonize  s<    



 
 
ú
ÿr”   Úf95Úf2003Úf2008)
r   r~   z.cxxz.fz.forz.ftnz.f90z.f95z.f03z.f08c                 K   sH  t j t j | ¡¡\}}|du r#t j | ¡rd}n
t j | ¡}|p"d}t j |¡r2t j ||t ¡}| 	dg ¡}|rGt
dƒ}	|	|vrG| |	¡ | ¡ dkrYt| f|||dœ|¤ŽS |du rmt| ¡  \}}
d|vrm|
|d< | 	dg ¡}d	}|D ]}||vr‚| |¡ qw| 	d
d¡}|rtdƒ‚|| g|f||||dœ|¤Ž}| ¡  |S )a¸   Compiles a source code file to an object file.

    Files ending with '.pyx' assumed to be cython files and
    are dispatched to pyx2obj.

    Parameters
    ==========

    srcpath: str
        Path to source file.
    Runner: CompilerRunner subclass (optional)
        If ``None``: deduced from extension of srcpath.
    objpath : str (optional)
        Path to generated object. If ``None``: deduced from ``srcpath``.
    cwd: str (optional)
        Working directory and root of relative paths. If ``None``: current dir.
    inc_py: bool
        Add Python include path to kwarg "include_dirs". Default: False
    \*\*kwargs: dict
        keyword arguments passed to Runner or pyx2obj

    Nr   rZ   Úincluder}   )ÚobjpathrZ   r   Ústdr@   )ú-fPICrM   Fz#src2obj called with run_linker=True)rZ   rM   r   r@   )r    r!   r&   r'   Úisabsrˆ   r"   rp   ÚobjextrP   r   r*   rE   Úpyx2objÚextension_mappingr   rQ   )Úsrcpathr-   r™   r   Úinc_pyr1   r7   r8   rZ   Ú
py_inc_dirrš   r@   rw   rx   rM   rX   r:   r:   r;   r+   ]  sJ   
ÿ
€ÿÿr+   c                 K   sF  |   d¡sJ ‚|p
d}|pd}|ptj |¡}t||d}tj |¡r:tj | ¡}	tj |	¡\}
}tj ||
t	 ¡}|p=i }||d< |du rJt
| ƒ}||d< t| f||dœ|¤Ž}|p\g }| dg ¡}d	}|D ]}||vrr| |¡ qg| d
g ¡}| dd¡rƒtdƒ‚|rŒ| dd¡}n| dd¡}t|f||||||dddœ|¤ŽS )a9  
    Convenience function

    If cwd is specified, pyxpath and dst are taken to be relative
    If only_update is set to `True` the modification time is checked
    and compilation is only run if the source is newer than the
    destination

    Parameters
    ==========

    pyxpath: str
        Path to Cython source file.
    objpath: str (optional)
        Path to object file to generate.
    destdir: str (optional)
        Directory to put generated C file. When ``None``: directory of ``objpath``.
    cwd: str (optional)
        Working directory and root of relative paths.
    include_dirs: iterable of path strings (optional)
        Passed onto src2obj and via cy_kwargs['include_path']
        to simple_cythonize.
    cy_kwargs: dict (optional)
        Keyword arguments passed onto `simple_cythonize`
    cplus: bool (optional)
        Indicate whether C++ is used. default: auto-detect using ``.util.pyx_is_cplus``.
    compile_kwargs: dict
        keyword arguments passed onto src2obj

    Returns
    =======

    Absolute path of generated object file.

    r}   r   rN   Ú
output_dirNrI   ©r.   r   r@   )z-fwrapvrh   r›   ÚoptionsÚstrict_aliasingFz/Cython requires strict aliasing to be disabled.rš   zc++98Úc99T)r™   r   rZ   r@   rš   r¥   r¡   r¦   )r‚   r    r!   rˆ   r
   r"   r'   r&   rp   r   r   r”   rP   r*   r   r+   )Úpyxpathr™   r.   r   rZ   r   rI   r1   Úabs_objpathÚ	pyx_fnamer7   r8   Úinterm_c_filer@   rw   rx   r¥   rš   r:   r:   r;   rž   Ÿ  sD   %
€
þýrž   c                 C   sD   | D ]}t j |¡\}}| ¡ }|tv rt| d |kr dS qdS )Nr   TF)r    r!   r&   rE   rŸ   )ÚsrcsÚclsrŒ   r7   r8   Úkeyr:   r:   r;   Ú_any_Xð  s   €r¯   c                 C   ó
   t | tƒS ©N)r¯   r	   ©r¬   r:   r:   r;   Úany_fortran_srcú  ó   
r³   c                 C   r°   r±   )r¯   r   r²   r:   r:   r;   Úany_cplus_srcþ  r´   rµ   r   c           	   
   C   s®   |du rt j t j | d ¡¡d }|pi }|pi }ztt j ||¡| ƒ}W |S  tyV   ttt	t
| ƒƒf||dœ|¤Ž}t|f|t| ƒt| ƒ|dœ|¤Ž}t|ƒ}Y |S w )a~   Compiles sources to a shared object (Python extension) and imports it

    Sources in ``sources`` which is imported. If shared object is newer than the sources, they
    are not recompiled but instead it is imported.

    Parameters
    ==========

    sources : list of strings
        List of paths to sources.
    extname : string
        Name of extension (default: ``None``).
        If ``None``: taken from the last file in ``sources`` without extension.
    build_dir: str
        Path to directory in which objects files etc. are generated.
    compile_kwargs: dict
        keyword arguments passed to ``compile_sources``
    link_kwargs: dict
        keyword arguments passed to ``link_py_so``
    extra_objs: list
        List of paths to (prebuilt) object files / static libraries to link against.

    Returns
    =======

    The imported module from of the Python extension.
    NrK   r   r¤   )r   rU   rI   rV   )r    r!   r&   r'   r   rp   ÚImportErrorr<   ÚlistÚmapr
   ry   r³   rµ   )	ÚsourcesÚextnameÚ	build_dirÚcompile_kwargsÚlink_kwargsrV   ÚmodÚobjsÚsor:   r:   r;   Úcompile_link_import_py_ext  s*   úÿÿÿÿ
úrÁ   c           
   	   C   s  |pt  ¡ }tj |¡std|ƒ‚g }| D ]t\}}tj ||¡}d}t| d¡ƒ 	¡ }tj 
|¡rLtj 
|d ¡rBt|d ƒ ¡ }nt|ƒ 	¡ }||k}|r„t|dƒ}	|	 |¡ W d   ƒ n1 scw   Y  t|d dƒ}	|	 |¡ W d   ƒ n1 sw   Y  | |¡ q||fS )NzNon-existent directory: Túutf-8z.sha256Úwt)ÚtempfileÚmkdtempr    r!   r"   r$   rp   r   ÚencodeÚ	hexdigestr#   r   Ú	read_textr   ÚopenÚwriter*   )
r¹   r»   Úsource_filesr7   rŒ   ÚdestÚdiffersÚsha256_in_memÚsha256_on_diskÚfhr:   r:   r;   Ú_write_sources_to_build_dir0  s,   
ÿÿrÑ   c                 K   s2   t | |ƒ\}}t|fd|i|¤Ž}d|i}||fS )aå   Compiles, links and imports extension module from source.

    Parameters
    ==========

    sources : iterable of name/source pair tuples
    build_dir : string (default: None)
        Path. ``None`` implies use a temporary directory.
    **kwargs:
        Keyword arguments passed onto `compile_link_import_py_ext`.

    Returns
    =======

    mod : module
        The compiled and imported extension module.
    info : dict
        Containing ``build_dir`` as 'build_dir'.

    r»   )rÑ   rÁ   )r¹   r»   r1   rË   r¾   Úinfor:   r:   r;   Úcompile_link_import_stringsJ  s   rÓ   c              	   C   sø   |r
|dur
t dƒ‚zVt| |ƒ\}}tttt|ƒƒf||dœ|p!i ¤Ž}t|f|t|ƒt|ƒdœ|p3i ¤Ž}t	j
|gt	jt	jd}| ¡ }	dd„ | ¡ D ƒ\}
}W |r`tj |¡r`t |¡ d}n|rptj |¡rpt |¡ d}w |	|dœ}|
|f|fS )	ay   Compiles, links and runs a program built from sources.

    Parameters
    ==========

    sources : iterable of name/source pair tuples
    build_dir : string (default: None)
        Path. ``None`` implies use a temporary directory.
    clean : bool
        Whether to remove build_dir after use. This will only have an
        effect if ``build_dir`` is ``None`` (which creates a temporary directory).
        Passing ``clean == True`` and ``build_dir != None`` raises a ``ValueError``.
        This will also set ``build_dir`` in returned info dictionary to ``None``.
    compile_kwargs: dict
        Keyword arguments passed onto ``compile_sources``
    link_kwargs: dict
        Keyword arguments passed onto ``link``

    Returns
    =======

    (stdout, stderr): pair of strings
    info: dict
        Containing exit status as 'exit_status' and ``build_dir`` as 'build_dir'

    NzIAutomatic removal of build_dir is only available for temporary directory.r¤   )r   rU   rI   )ÚstdoutÚstderrc                 S   s   g | ]}|  d ¡‘qS )rÂ   )Údecode)Ú.0Útxtr:   r:   r;   Ú
<listcomp>‹  s    z'compile_run_strings.<locals>.<listcomp>)Úexit_statusr»   )rF   rÑ   r<   r·   r¸   r
   rY   r³   rµ   Ú
subprocessÚPopenÚPIPEÚwaitÚcommunicater    r!   r"   rŠ   Úrmtree)r¹   r»   Úcleanr¼   r½   rË   r¿   ÚprogÚprÚ   rÔ   rÕ   rÒ   r:   r:   r;   Úcompile_run_stringse  s6   ÿÿþþ
€þ

rä   )NNNFN)NFN)NFNNFFN)NNNFFN)NN)NNNF)NNNNNN)Nr   NNNr±   )NFNN)0r   r    rŠ   rÛ   rj   rÄ   rl   Úpathlibr   Ú	sysconfigr   r   r   Úrunnersr   r   r	   Úutilr
   r   r   r   r   r   r   r   r   r   r   r7   r   rm   r%   r<   rJ   rY   ry   r”   rŸ   r+   rž   r¯   r³   rµ   rÁ   rÑ   rÓ   rä   r:   r:   r:   r;   Ú<module>   sh    4


ÿ
B
ÿH
ÿ
W;ö
B
ÿQ

ÿ.
