o
    8Va]#                     @   s   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
Z
d dlZd dlZddlmZmZ G dd dZG d	d
 d
eZdd ZG dd deZG dd deZdS )    )CallableDictOptionalTupleUnion)OrderedDict)CompileErrorN   )find_binary_of_commandunique_listc                   @   sN   e Zd ZdZdZdZdZdZ			dddZe	dddZ
d	d
 Zdd ZdS )CompilerRunnera   CompilerRunner base class.

    Parameters
    ==========

    sources : list of str
        Paths to sources.
    out : str
    flags : iterable of str
        Compiler flags.
    run_linker : bool
    compiler_name_exe : (str, str) tuple
        Tuple of compiler name &  command to call.
    cwd : str
        Path of root of relative paths.
    include_dirs : list of str
        Include directories.
    libraries : list of str
        Libraries to link against.
    library_dirs : list of str
        Paths to search for shared libraries.
    std : str
        Standard string, e.g. ``'c++11'``, ``'c99'``, ``'f2003'``.
    define: iterable of strings
        macros to define
    undef : iterable of strings
        macros to undefine
    preferred_vendor : string
        name of preferred vendor e.g. 'gnu' or 'intel'

    Methods
    =======

    run():
        Invoke compilation as a subprocess.

    NT.c                 K   s  t |tr	tdt|| _|| _|pg | _|| _|r"|\| _| _	n(|d u r-t
jdd }| |\| _| _	| _| j	d u rJtdd| j |pMg | _|pRg | _|pWg | _|p\g | _|	pag | _|
pi| jd | _|| _| jr}ttdd | j| _nd| jvr| jd | jr| j| j| j | j |pg | _|d urt d	}t d
}|du rt!t"|j#|rt$dt!t"|j#|rd S |d d S |du rt!t"|j#|rd S t!t"|j#|rt$d|d d S d}t||d S )Nz2Expected argument sources to be a list of strings.ZSYMPY_COMPILER_VENDORz No compiler found (searched: {})z, r   c                 S   s   | dkS )N-c xr   r   F/usr/lib/python3/dist-packages/sympy/utilities/_compilation/runners.py<lambda>]   s    z)CompilerRunner.__init__.<locals>.<lambda>r   zno-strict-aliasing$zstrict-aliasing$Tz4Strict aliasing cannot be both enforced and disabledz-fstrict-aliasingFz-fno-strict-aliasingz:Expected argument strict_aliasing to be True/False, got {})%
isinstancestr
ValueErrorlistsourcesoutflagscwdZcompiler_namecompiler_binaryosenvirongetfind_compilerZcompiler_vendorformatjoincompiler_dictvaluesdefineundefinclude_dirs	librarieslibrary_dirs	standardsstd
run_linkerfilterappendstd_formaterlinklinerecompileanymapmatchr   )selfr   r   r   r,   Zcompilerr   r'   r(   r)   r+   r%   r&   Zstrict_aliasingpreferred_vendorr0   kwargsZnsa_reZsa_remsgr   r   r   __init__B   sb   













zCompilerRunner.__init__c                    s^   t  j }|r||v r|g| }ntd|t fdd|D \}}|| j| fS )z/ Identify a suitable C/fortran/other compiler. zUnknown vendor {}c                    s   g | ]} j | qS r   )r#   .0r   clsr   r   
<listcomp>   s    z0CompilerRunner.find_compiler.<locals>.<listcomp>)r   r#   keysr   r!   r
   compiler_name_vendor_mapping)r>   r7   Z
candidatesnamepathr   r=   r   r    ~   s   zCompilerRunner.find_compilerc                 C   s   | j g| j dd | jD  dd | jD  dd | jD  | j }| jr:|dd | jD dd | jD  | j	 7 }g }t
dd|D ]}t|d	u r`||vr`|| d
|}t|qE|S )zD List of arguments (str) to be passed to e.g. ``subprocess.Popen``. c                 S      g | ]}d | qS )z-Ur   r;   r   r   r   r?          z&CompilerRunner.cmd.<locals>.<listcomp>c                 S   rD   )z-Dr   r;   r   r   r   r?      rE   c                 S   rD   )z-Ir   r;   r   r   r   r?      rE   c                 S   rD   )z-Lr   r;   r   r   r   r?      rE   c                 S   rD   )z-lr   r;   r   r   r   r?      rE   z\$\{(\w+)\} Nz$Environment variable '{}' undefined.)r   r   r&   r%   r'   r   r,   r)   r(   r0   r1   findallr"   r   getenvr.   r!   r   )r6   cmdZcountedZenvvarr9   r   r   r   rI      s6   

zCompilerRunner.cmdc              	   C   s   t | j| _| jd| jg tj }| j|d< tj	d
|  d| jtjtjtj|d}| }z
|d d| _W n tyM   |d d| _Y nw |j| _| jdkrmd	d
|  | jt| j| j}t|| j| jfS )
Nz-oZPWDrF   T)shellr   stdinstdoutstderrenvr   zutf-8z
iso-8859-1z3Error executing '{}' in {} (exited status {}):
 {}
)r   r   extendr   r   r   copyr   
subprocessPopenr"   rI   PIPEZSTDOUTZcommunicatedecodeZ
cmd_outerrUnicodeDecodeError
returncodeZcmd_returncoder!   r   r   )r6   rN   pZcommr9   r   r   r   run   s2   


zCompilerRunner.run)NTNr   NNNNNNNNN)N)__name__
__module____qualname____doc__r#   r*   r/   rA   r:   classmethodr    rI   rX   r   r   r   r   r      s    '
<r   c                   @   :   e Zd Zeg dZdZdjdjdjdZddddZdS )	CCompilerRunner))gnugcc)intelicc)llvmclang)Zc89Zc90Zc99Zc11-std={})ra   rc   re   r`   rb   rd   N	rY   rZ   r[   r   r#   r*   r!   r/   rA   r   r   r   r   r_      s    
r_   c                    s.   ddi v r fdd}|S dd }|S )Ng++)zWimplicit-interfacec                    s      D ]	}|| v r dS qdS )NFTr   )r   Znw
cmplr_nameZnot_welcomer   r   fltr   s
   z_mk_flag_filter.<locals>.fltrc                 S   s   dS )NTr   r   r   r   r   rk      s   r   )rj   rk   r   ri   r   _mk_flag_filter   s   rl   c                   @   r^   )	CppCompilerRunner))r`   rh   )rb   icpc)rd   clang++)zc++98zc++0xrf   )rh   rn   ro   r`   rb   rd   Nrg   r   r   r   r   rm      s    
rm   c                   @   s8   e Zd ZdZdd dd dZeddgZdd	dZd
S )FortranCompilerRunner)Nf77Zf95Zf2003Zf2008c                 C   s"   | d u rdS | dkrdS d | S )Nz-std=gnurq   z-std=legacyrf   r!   r   r   r   r   r     s   " zFortranCompilerRunner.<lambda>c                 C   s   | d u rdS d | dd  S )Nz
-stand f08z
-stand f{}rr   r   r   r   r   r     s    )gfortranifort)r`   rt   )rb   ru   r`   rb   N)rY   rZ   r[   r*   r/   r   r#   rA   r   r   r   r   rp      s    
rp   )typingr   r   r   r   r   collectionsr   Zdistutils.errorsr   r   r1   rQ   utilr
   r   r   r_   rl   rm   rp   r   r   r   r   <module>   s     5