
    Rie#                        d dl mZ d dlmZ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mZ  G d d          Z G d	 d
e          Zd Z G d de          Z G d de          ZdS )    )annotations)CallableOptional)OrderedDictN   )find_binary_of_commandunique_listCompileErrorc                  n    e Zd ZU dZded<   ded<   ded<   ded<   	 	 	 ddZedd            Zd Z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.

    zdict[str, str]compiler_dictztuple[None | str, ...]	standardsz)dict[str, Callable[[Optional[str]], str]]std_formatercompiler_name_vendor_mappingNT.c                &   t          |t                    rt          d          t          |          | _        || _        |pg | _        || _        |r|\  | _        | _	        n| t          j                            dd           }|                     |          \  | _        | _	        | _        | j	        Lt          d                    d                    | j                                                                      |pg | _        |pg | _        |pg | _        |pg | _        |	pg | _        |
p| j        d         | _        || _        | j        r)t          t7          d | j                            | _        n#d| j        vr| j                            d           | j        r8| j                             | j        | j                 | j                             |pg | _        |,t?          j         d          }t?          j         d	          }|d
u rltC          tE          |j#        |                    rtI          d          tC          tE          |j#        |                    rd S |                    d           d S |du rltC          tE          |j#        |                    rd S tC          tE          |j#        |                    rtI          d          |                    d           d S d}t          |                    |                    d S )Nz2Expected argument sources to be a list of strings.SYMPY_COMPILER_VENDORz No compiler found (searched: {})z, r   c                    | dk    S )N-c xs    Dlib/python3.11/site-packages/sympy/utilities/_compilation/runners.py<lambda>z)CompilerRunner.__init__.<locals>.<lambda>]   s
    qDy     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cwdcompiler_namecompiler_binaryosenvirongetfind_compilercompiler_vendorformatjoinr   valuesdefineundefinclude_dirs	librarieslibrary_dirsr   std
run_linkerfilterappendr   linklinerecompileanymapmatchr
   )selfr    r!   r"   r4   compilerr#   r0   r1   r2   r3   r.   r/   strict_aliasingpreferred_vendorr7   kwargsnsa_resa_remsgs                       r   __init__zCompilerRunner.__init__B   s    gs## 	SQRRRG}}[b
 	t7?4D 4 4  '#%:>>2I4#P#P MQM_M_`pMqMqJD 4d6J#+ !C!J!J499UYUgUnUnUpUpKqKq!r!rsssl[b
(.B"b(.B+$.+$? 	(f%8%8$*EEFFDJJ4:%%
!!$'''8 	/J $d/"$$(H. . / / / !B&Z 566FJ122E$&&s6<//00 6&']^^^U[%0011 6DLL!455555 E))s6<//00 =D3u{E2233 =*+abbb%;<<<<<R O!<!<===) '&r   c                
    t           j                                                  }|r-||v r|g|z   }n"t          d                    |                    t           fd|D                       \  }}|| j        |         fS )z/ Identify a suitable C/fortran/other compiler. zUnknown vendor {}c                *    g | ]}j         |         S r   )r   ).0r   clss     r   
<listcomp>z0CompilerRunner.find_compiler.<locals>.<listcomp>   s!    ,V,V,VaS->q-A,V,V,Vr   )r   r   keysr   r+   r   r   )rI   r@   
candidatesnamepaths   `    r   r)   zCompilerRunner.find_compiler~   s     #+002233
 	O:--./
:

 !4!;!;<L!M!MNNN+,V,V,V,V:,V,V,VWW
dT3;DAAAr   c                   | j         g| j        z   d | j        D             z   d | j        D             z   d | j        D             z   | j        z   }| j        r.|d | j        D             d | j        D             z   | j	        z   z  }g }t          j        dd                    |                    D ]S}t          j        |          =||vr9|                    |           d	                    |          }t#          |          T|S )
zD List of arguments (str) to be passed to e.g. ``subprocess.Popen``. c                    g | ]}d |z   S )z-Ur   rH   r   s     r   rJ   z&CompilerRunner.cmd.<locals>.<listcomp>   s    (((T!V(((r   c                    g | ]}d |z   S )z-Dr   rQ   s     r   rJ   z&CompilerRunner.cmd.<locals>.<listcomp>   s    )))T!V)))r   c                    g | ]}d |z   S )z-Ir   rQ   s     r   rJ   z&CompilerRunner.cmd.<locals>.<listcomp>   s    ///T!V///r   c                    g | ]}d |z   S )z-Lr   rQ   s     r   rJ   z&CompilerRunner.cmd.<locals>.<listcomp>   s    777T!V777r   c                    g | ]}d |z   S )z-lr   rQ   s     r   rJ   z&CompilerRunner.cmd.<locals>.<listcomp>   s    444T!V444r   z\$\{(\w+)\} Nz$Environment variable '{}' undefined.)r%   r"   r/   r.   r0   r    r4   r2   r1   r7   r8   findallr,   r&   getenvr6   r+   r
   )r=   cmdcountedenvvarrD   s        r   rY   zCompilerRunner.cmd   sA    !"J((TZ((() *)T[)))* 0/T.///	0
 L 	 ? 	#77T%677744T^4445M" #C j#?? 	, 	,Fy  (((NN6***@GGOOC&s+++
r   c           	     r   t          | j                  | _        | j                            d| j        g           t          j                                        }| j        |d<   t          j	        d
                    |                                           d| j        t          j        t          j        t          j        |          }|                                }	 |d                             d          | _        n0# t"          $ r# |d                             d          | _        Y nw xY w|j        | _        | j        dk    rhd	                    d
                    |                                           | j        t+          | j                  | j                  }t-          |          | j        | j        fS )
Nz-oPWDrV   T)shellr#   stdinstdoutstderrenvr   zutf-8z
iso-8859-1z3Error executing '{}' in {} (exited status {}):
 {}
)r	   r"   extendr!   r&   r'   copyr#   
subprocessPopenr,   rY   PIPESTDOUTcommunicatedecode
cmd_outerrUnicodeDecodeError
returncodecmd_returncoder+   r   r
   )r=   rb   pcommrD   s        r   runzCompilerRunner.run   sv    ,,
 	
4*+++jooXE
 SXXdhhjj11#'!%#-?$.O$.$5!$& & & }}	;"1gnnW55DOO! 	; 	; 	;"1gnn\::DOOO	;l !##IPP$$dhD4G0H0H$/ C s### 333s    C< <*D)(D))NTNr   NNNNNNNNN)N)
__name__
__module____qualname____doc____annotations__rE   classmethodr)   rY   rq   r   r   r   r   r      s         $ $N "!!! &%%% <;;; 1000UXX\SW:> :> :> :>x 	B 	B 	B [	B  .4 4 4 4 4r   r   c                  b    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)c89c90c99c11-std={})r{   r}   r   rz   r|   r~   N	rr   rs   rt   r   r   r   r+   r   r   r   r   r   ry   ry      so        K ! ! !  M -I ! L $ $   r   ry   c                .     ddi v r fd}nd }|S )Ng++)zWimplicit-interfacec                ,             D ]	}|| v r dS 
dS )NFTr   )r   nw
cmplr_namenot_welcomes     r   fltrz_mk_flag_filter.<locals>.fltr   s1    !*- ! !77 55 4r   c                    dS )NTr   r   s    r   r   z_mk_flag_filter.<locals>.fltr   s    4r   r   )r   r   r   s   ` @r   _mk_flag_filterr      sQ    23K[  	 	 	 	 	 	 		 	 	Kr   c                  b    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 )	CppCompilerRunner))rz   r   )r|   icpc)r~   clang++)zc++98zc++0xr   )r   r   r   rz   r|   r~   Nr   r   r   r   r   r      so        K ! ! !  M #I  # L $ $   r   r   c                  D    e Zd ZdZd d dZ eddg          ZdddZd	S )
FortranCompilerRunner)Nf77f95f2003f2008c                D    | dn| dk    rdnd                     |           S )Nz-std=gnur   z-std=legacyr   r+   r   s    r   r   zFortranCompilerRunner.<lambda>  s+    AIjjAQVJJ==\e\l\lmn\o\o r   c                D    | dnd                     | dd                    S )Nz
-stand f08z
-stand f{}r   r   s    r   r   zFortranCompilerRunner.<lambda>  s'    19<<,:M:MaPRPSPSf:U:U r   )gfortranifort)rz   r   )r|   r   rz   r|   N)rr   rs   rt   r   r   r   r   r   r   r   r   r   r      sc        6I poUU L
  K!  M $ $   r   r   )
__future__r   typingr   r   collectionsr   r&   r8   re   utilr   r	   r
   r   ry   r   r   r   r   r   r   <module>r      sY   " " " " " " % % % % % % % % # # # # # # 				 				             
q4 q4 q4 q4 q4 q4 q4 q4h    n   .         0    N     r   