U
    «Ì0e«T  ã                   @   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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r¤dZn&ejdkr´d	Zne d
 ej¡¡ dZd-dd„Zd.dd„Z d/dd„Z!d0dd„Z"d1d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$d2dd„Z%d3dd„Z&dd„ Z'd d!„ Z(d"d#„ Z)d4d%d&„Z*d'd(„ Z+d5d)d*„Z,d6d+d,„Z-dS )7é    N)Ú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   sF  i }|dk	rr|  ¡ D ]\\}}	t|tƒrBt |j¡D ]}
|	||
< q2qt|tƒrht|j|ƒD ]}
|	||
< qXq|	||< q|pxd}tj	 
|¡sªtj	 |¡r¢td |¡ƒ‚nt|ƒ |dkrÐd}| D ]}t||ddd qºg }| D ]h}|ròtj	 |¡\}}ntj	 tj	 |¡¡\}}| ¡ }| | |i ¡¡ | t||fd|i|—Ž¡ qØ|S )aL   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``.

    NÚ.z{} is not a directoryT)Zonly_updateZ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   Zkeep_dir_structZper_file_kwargsÚkwargsZ_per_file_kwargsÚkÚvr   ÚfZdstpathsÚnameÚextZfile_kwargs© r2   úL/tmp/pip-unpacked-wheel-_6tpq7m6/sympy/utilities/_compilation/compilation.pyÚcompile_sources   s8    


r4   c                 C   s~   | pt j dd¡} |  ¡ dkrF|r4tdddgi| fS tddgi| fS n4|  ¡ dksR|rftdd	gi| fS ti | fS ntd
ƒ‚d S )NZSYMPY_COMPILER_VENDORZgnuÚintelÚflagsz-nofor_mainz-cxxlibZllvmZlib_optionsZfortranzNo vendor found.)r   Úenvironr&   Úlowerr   r   Ú
ValueError©ÚvendorÚcplusr   r2   r2   r3   Úget_mixed_fort_c_linker]   s,    
 ÿ ÿ ÿ ÿr=   c                 K   s  |dkr4t j t j | d ¡¡\}}|r4|tdƒ7 }|sš|rŒt| dd¡||d\}}	}
|	 ¡ D ](\}}||kr€||  |¡ q`|||< q`n|r–t	}nt
}| dg ¡}|r¼d|kr¼| d¡ | dd	¡}|sÔtd
ƒ‚t||d}|| ||fd|i|—Ž}| ¡  |S )aK   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``.
    \*\*kwargs: dict
        Keyword arguments passed to ``Runner``.

    Returns
    =======

    The absolute path to the generated shared object / executable.

    NéÿÿÿÿÚ
EXT_SUFFIXr;   r:   r6   z-sharedÚ
run_linkerTz*run_linker was set to False (nonsensical).©r   r   )r   r   r#   r$   r   r=   r&   r   Úexpandr   r   Úpopr'   r9   r	   Úrun)Ú	obj_filesÚout_fileÚsharedr*   r   r<   Úfortr,   r1   Úextra_kwargsr;   r-   r.   r6   r@   Úrunnerr2   r2   r3   Úlinkr   s<     
ýÿ
rK   c                 K   sÞ  |pg }|  dg ¡}|  dg ¡}tjdkr8t d¡ nTtjdkr
tƒ }	| dg ¡|	d g |d< ||	d g7 }d	}
|	d
  ¡ D ]R}|
r¨| dg ¡d|g |d< d	}
q‚| d¡rÆ| 	|dd… ¡ q‚| d¡r‚d}
q‚|	d }d 
| d¡dd… ¡dd… }| 	|¡ n‚tjdd… dkr nltdƒrŒtƒ }	| dg ¡|	d g |d< ||	d g7 }|	d  ¡ D ]$}| d¡rd||dd… g7 }qdn |  dg ¡}d}|D ]}||k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``.
    kwargs**: dict
        Keyword arguments passed to ``link(...)``.

    Returns
    =======

    Absolute path to the generate shared object.
    Úinclude_dirsÚlibrary_dirsÚwin32zWindows not yet supported.ÚdarwinZlinklineÚLDFLAGSÚLIBDIRFÚLIBSz
-frameworkz-lé   NTÚLIBRARYr   r>   é   ÚaixÚPy_ENABLE_SHAREDÚ
BLDLIBRARYr6   )ú-pthread)rG   r6   r   r<   rH   rL   Ú	librariesrM   )rC   ÚsysÚplatformÚwarningsÚwarnr   r&   ÚsplitÚ
startswithr'   Újoinr   rK   )rE   Zso_filer   rZ   r<   rH   r,   rL   rM   ZcfgDictZis_frameworkÚoptZlibfileZlibnamer6   Úneeded_flagsÚflagr2   r2   r3   Ú
link_py_so¸   sX    


 

   þþre   c              	   K   sX  ddl m}m} ddl m} |  ¡  d¡s<|  ¡  d¡s<t‚|pBd}|pJd}| dd¡r\d	nd
}tj	 
tj	 | ¡¡d | }tj	 ||¡}	|r˜t ¡ }
nd}
t |¡ z ||ƒ}|j |¡ d|krÎd|jd< || g|ƒ}|jdkrìtdƒ‚tj	 tj	 | ¡¡tj	 |¡krDtj	 |	¡r&t |	¡ t tj	 tj	 | ¡|¡|¡ W 5 t |
¡ X |	S )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   r<   Fú.cppú.cZlanguage_levelrU   zCython compilation failed.)ZCython.Compiler.Mainrf   rg   rh   r8   ÚendswithÚAssertionErrorr&   r   r   r#   r$   ra   ÚgetcwdÚchdirÚ__dict__r%   Z
num_errorsr9   ÚrealpathÚdirnamer    ÚunlinkÚshutilÚmove)Úsrcr+   r   Ú	cy_kwargsrf   rg   Z
cy_compiler1   Zc_nameZdstfileZori_dirZ
cy_optionsZ	cy_resultr2   r2   r3   Úsimple_cythonize  s4     



"
"rx   Zf95Zf2003Zf2008)
rk   rj   z.cxxz.fz.forz.ftnz.f90z.f95z.f03z.f08c                 K   sJ  t j t j | ¡¡\}}|dkrFt j | ¡r2d}nt j | ¡}|pDd}t j |¡rdt j ||t ¡}| 	dg ¡}|rŽt
dƒ}	|	|krŽ| |	¡ | ¡ dkr²t| f|||dœ|—ŽS |dkrÚt| ¡  \}}
d|krÚ|
|d< | 	dg ¡}d	}|D ]}||krî| |¡ qî| 	d
d¡}|r t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   rL   Úincluderi   )ÚobjpathrL   r   Ústdr6   )ú-fPICr@   Fz#src2obj called with run_linker=True)rL   r@   r   r6   )r   r   r#   r$   Úisabsrr   r   ra   ÚobjextrC   r   r'   r8   Úpyx2objÚextension_mappingr   rD   )Zsrcpathr*   rz   r   Úinc_pyr,   r0   r1   rL   Z
py_inc_dirr{   r6   rc   rd   r@   rJ   r2   r2   r3   r(   U  sL    
ÿ  ÿÿr(   c                 K   sJ  |   d¡st‚|pd}|pd}|p,tj |¡}t||d}tj |¡rttj | ¡}	tj |	¡\}
}tj 	||
t
 ¡}|pzi }||d< |dkr”t| ƒ}||d< t| f||dœ|—Ž}|p¸g }| dg ¡}d	}|D ]}||krÎ| |¡ qÎ| 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.

    ri   r   rA   Ú
output_dirNr<   ©r+   r   r6   )z-fwrapvrY   r|   ÚoptionsÚstrict_aliasingFz/Cython requires strict aliasing to be disabled.r{   zc++98Zc99T)rz   r   rL   r6   r{   r„   r   r…   )rl   rm   r   r   rr   r	   r   r$   r#   ra   r~   r   rx   rC   r'   r   r(   )Zpyxpathrz   r+   r   rL   rw   r<   r,   Zabs_objpathZ	pyx_fnamer0   r1   Zinterm_c_filer6   rc   rd   r„   r{   r2   r2   r3   r   —  sJ    %
    þýr   c                 C   sD   | D ]:}t j |¡\}}| ¡ }|tkrt| d |kr dS qdS )Nr   TF)r   r   r#   r8   r€   )ÚsrcsÚclsrv   r0   r1   Úkeyr2   r2   r3   Ú_any_Xè  s    r‰   c                 C   s
   t | tƒS ©N)r‰   r   ©r†   r2   r2   r3   Úany_fortran_srcò  s    rŒ   c                 C   s
   t | tƒS rŠ   )r‰   r   r‹   r2   r2   r3   Úany_cplus_srcö  s    r   r   c              	   C   s®   |dkr$t j t j | d ¡¡d }|p*i }|p2i }ztt j ||¡| ƒ}W n\ tk
r¨   ttt	t
| ƒƒf||dœ|—Ž}t|f|t| ƒt| ƒdœ|—Ž}t|ƒ}Y nX |S )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 : string
        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``

    Returns
    =======

    The imported module from of the Python extension.
    Nr>   r   rƒ   ©r   rH   r<   )r   r   r#   r$   r   ra   ÚImportErrorr4   ÚlistÚmapr	   re   rŒ   r   )ÚsourcesZextnameÚ	build_dirÚcompile_kwargsÚlink_kwargsÚmodÚobjsÚsor2   r2   r3   Úcompile_link_import_py_extú  s&    ÿÿÿÿr™   c           
   
   C   s  |p
t  ¡ }tj |¡s"td|ƒ‚g }| D ]Ð\}}tj ||¡}d}t| d¡ƒ 	¡ }tj 
|¡r¨tj 
|d ¡r”t|d ƒ}| ¡ }	W 5 Q R X nt|ƒ 	¡ }	|	|k}|rðt|dƒ}| |¡ W 5 Q R X t|d dƒ}| |¡ W 5 Q R X | |¡ q*||fS )NzNon-existent directory: Túutf-8z.sha256Úwt)ÚtempfileÚmkdtempr   r   r   r!   ra   r   ÚencodeÚ	hexdigestr    ÚopenÚreadr   Úwriter'   )
r’   r“   Úsource_filesr0   rv   ÚdestZdiffersZsha256_in_memÚfhZsha256_on_diskr2   r2   r3   Ú_write_sources_to_build_dir&  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“   r,   r£   r–   Úinfor2   r2   r3   Úcompile_link_import_stringsA  s    r¨   c              	   C   sÚ   |r|dk	rt dƒ‚zŽt| |ƒ\}}ttt	t
|ƒƒf||dœ|pBi —Ž}t|f|t|ƒt|ƒdœ|pfi —Ž}tj|gtjtjd}| ¡ }	dd„ | ¡ D ƒ\}
}W 5 |rÂtj |¡rÂt |¡ d}X |	|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Ž   )ÚstdoutÚstderrc                 S   s   g | ]}|  d ¡‘qS )rš   )Údecode)Ú.0Útxtr2   r2   r3   Ú
<listcomp>‚  s     z'compile_run_strings.<locals>.<listcomp>)Úexit_statusr“   )r9   r   r   r   rt   Úrmtreer¦   r4   r   r‘   r	   rK   rŒ   r   Ú
subprocessÚPopenÚPIPEÚwaitÚcommunicate)r’   r“   Úcleanr”   r•   r£   r—   ÚprogÚpr¯   r©   rª   r§   r2   r2   r3   Úcompile_run_strings\  s.    ÿÿþþ

r¹   )NNNFN)NFN)NFNNFF)NNNFF)NN)NNNF)NNNNNN)Nr   NN)N)NFNN).r   r   rt   r±   r[   rœ   r]   Ú	sysconfigr   r   r   Zrunnersr   r   r   Úutilr	   r
   r   r   r   r   r   r   r   r   r   r0   r~   r^   r"   r4   r=   rK   re   rx   r€   r(   r   r‰   rŒ   r   r™   r¦   r¨   r¹   r2   r2   r2   r3   Ú<module>   sn   4

  ÿ
?
      ÿ
F    ÿ
U
;ö
B      ÿ
Q
  ÿ
,
