a
    ze:                     @   s   d dl Z d dlZd dlZd dlT d dl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 G d	d
 d
ZdZdddZddddddZdd ZdddZdd Zdd ZdS )    N)*)spawn)	move_file)mkpath)newer_group)split_quotedexecute)logc                
   @   s  e Zd ZdZdZdZdZdZdZdZ	dZ
ddddddZg dZdqdd	Zd
d Zdd Zdd Zdd ZdrddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Z dsd0d1Z!d2d3 Z"d4d5 Z#d6d7 Z$d8d9 Z%dtd:d;Z&dud<d=Z'd>d? Z(dvd@dAZ)dBZ*dCZ+dDZ,dwdEdFZ-dxdGdHZ.dydIdJZ/dzdKdLZ0dMdN Z1dOdP Z2dQdR Z3d{dSdTZ4d|dUdVZ5d}dXdYZ6d~dZd[Z7dd\d]Z8dd_d`Z9ddbdcZ:ddde Z;dfdg Z<ddhdiZ=djdk Z>dldm Z?ddodpZ@dS )	CCompilerNcc++objc).cz.ccz.cppz.cxxz.m)r   r   r   r   c                 C   sb   || _ || _|| _d | _g | _g | _g | _g | _g | _g | _	| j
 D ]}| || j
|  qFd S N)dry_runforceverbose
output_dirmacrosinclude_dirs	librarieslibrary_dirsruntime_library_dirsobjectsexecutableskeysset_executable)selfr   r   r   key r   )/usr/lib/python3.9/distutils/ccompiler.py__init__U   s    zCCompiler.__init__c                 K   s<   |D ]2}|| j vr&td|| jjf | |||  qd S )Nz$unknown executable '%s' for class %s)r   
ValueError	__class____name__r   )r   kwargsr   r   r   r    set_executablesy   s    

zCCompiler.set_executablesc                 C   s,   t |trt| |t| nt| || d S r   )
isinstancestrsetattrr   )r   r   valuer   r   r    r      s    
zCCompiler.set_executablec                 C   s0   d}| j D ] }|d |kr"|  S |d7 }q
d S )Nr      )r   )r   nameidefnr   r   r    _find_macro   s    

zCCompiler._find_macroc                 C   s`   |D ]V}t |trFt|dv rFt |d ts8|d d u rFt |d tstd| d d qd S )N)r+      r+   r   zinvalid macro definition '%s': z.must be tuple (string,), (string, string), or z(string, None))r'   tuplelenr(   	TypeError)r   Zdefinitionsr.   r   r   r    _check_macro_definitions   s    


z"CCompiler._check_macro_definitionsc                 C   s.   |  |}|d ur| j|= | j||f d S r   r/   r   append)r   r,   r*   r-   r   r   r    define_macro   s    	
zCCompiler.define_macroc                 C   s0   |  |}|d ur| j|= |f}| j| d S r   r5   )r   r,   r-   Zundefnr   r   r    undefine_macro   s
    
zCCompiler.undefine_macroc                 C   s   | j | d S r   )r   r6   r   dirr   r   r    add_include_dir   s    zCCompiler.add_include_dirc                 C   s   |d d  | _ d S r   r   r   dirsr   r   r    set_include_dirs   s    zCCompiler.set_include_dirsc                 C   s   | j | d S r   )r   r6   )r   libnamer   r   r    add_library   s    zCCompiler.add_libraryc                 C   s   |d d  | _ d S r   )r   )r   Zlibnamesr   r   r    set_libraries   s    zCCompiler.set_librariesc                 C   s   | j | d S r   )r   r6   r9   r   r   r    add_library_dir  s    zCCompiler.add_library_dirc                 C   s   |d d  | _ d S r   )r   r=   r   r   r    set_library_dirs  s    zCCompiler.set_library_dirsc                 C   s   | j | d S r   )r   r6   r9   r   r   r    add_runtime_library_dir  s    z!CCompiler.add_runtime_library_dirc                 C   s   |d d  | _ d S r   )r   r=   r   r   r    set_runtime_library_dirs  s    z"CCompiler.set_runtime_library_dirsc                 C   s   | j | d S r   )r   r6   )r   objectr   r   r    add_link_object   s    zCCompiler.add_link_objectc                 C   s   |d d  | _ d S r   )r   )r   r   r   r   r    set_link_objects(  s    zCCompiler.set_link_objectsc                 C   s  |d u r| j }nt|ts"td|d u r2| j}n"t|trL|| jpFg  }ntd|d u rd| j}n*t|ttfrt|| jpg  }ntd|d u rg }| j|d|d}t	||}i }	t
t|D ]B}
||
 }||
 }tj|d }| tj| ||f|	|< q|||||	fS )N%'output_dir' must be a string or None/'macros' (if supplied) must be a list of tuples6'include_dirs' (if supplied) must be a list of stringsr   )	strip_dirr   r+   )r   r'   r(   r3   r   listr   r1   object_filenamesgen_preprocess_optionsranger2   ospathsplitextr   dirname)r   Zoutdirr   Zincdirssourcesdependsextrar   pp_optsbuildr-   srcobjextr   r   r    _setup_compile6  s<    


zCCompiler._setup_compilec                 C   s0   |dg }|rdg|d d< |r,||d d< |S )Nz-cz-gr   r   )r   rY   debugbeforecc_argsr   r   r    _get_cc_argsa  s    
zCCompiler._get_cc_argsc                 C   s   |d u r| j }nt|ts"td|d u r2| j}n"t|trL|| jpFg  }ntd|d u rd| j}n*t|ttfrt|| jpg  }ntd|||fS )NrJ   rK   rL   )r   r'   r(   r3   r   rN   r   r1   )r   r   r   r   r   r   r    _fix_compile_argsj  s"    


zCCompiler._fix_compile_argsc                 C   s   | j ||d}|i fS )N)r   )rO   )r   rV   r   rW   r   r   r   r    _prep_compile  s    	zCCompiler._prep_compilec                 C   sH   t |ttfstdt|}|d u r.| j}nt |ts@td||fS )Nz,'objects' must be a list or tuple of stringsrJ   )r'   rN   r1   r3   r   r(   )r   r   r   r   r   r    _fix_object_args  s    
zCCompiler._fix_object_argsc                 C   s   |d u r| j }n*t|ttfr2t|| j p,g  }ntd|d u rJ| j}n*t|ttfrlt|| jpfg  }ntd|d u r| j}n*t|ttfrt|| jpg  }ntd|||fS )Nz3'libraries' (if supplied) must be a list of stringsz6'library_dirs' (if supplied) must be a list of stringsz>'runtime_library_dirs' (if supplied) must be a list of strings)r   r'   rN   r1   r3   r   r   )r   r   r   r   r   r   r    _fix_lib_args  s,    zCCompiler._fix_lib_argsc                 C   s2   | j r
dS | jr t||dd}n
t||}|S d S )NTnewer)missing)r   r   r   )r   r   output_filerg   r   r   r    
_need_link  s    
zCCompiler._need_linkc           	   	   C   s|   t |ts|g}d }t| j}|D ]T}tj|\}}| j|}z | j	|}||k r`|}|}W q" t
yt   Y q"0 q"|S r   )r'   rN   r2   language_orderrR   rS   rT   language_mapgetindexr"   )	r   rV   langrn   sourcebaser]   ZextlangZextindexr   r   r    detect_language  s    

zCCompiler.detect_languagec                 C   s   d S r   r   )r   rp   ri   r   r   extra_preargsextra_postargsr   r   r    
preprocess  s    zCCompiler.preprocessc	              	   C   sv   |  ||||||\}}	}}
}| |
||}|	D ]@}z|| \}}W n tyZ   Y q0Y n0 | ||||||
 q0|	S r   )r^   rb   KeyError_compile)r   rV   r   r   r   r_   rs   rt   rW   r   rY   rZ   ra   r\   r[   r]   r   r   r    compile  s    6
zCCompiler.compilec                 C   s   d S r   r   )r   r\   r[   r]   ra   rt   rY   r   r   r    rw   C  s    zCCompiler._compilec                 C   s   d S r   r   )r   r   output_libnamer   r_   target_langr   r   r    create_static_libI  s    zCCompiler.create_static_libZshared_objectZshared_library
executablec                 C   s   t d S r   NotImplementedError)r   Ztarget_descr   output_filenamer   r   r   r   export_symbolsr_   rs   rt   
build_temprz   r   r   r    linki  s    9zCCompiler.linkc                 C   s2   |  tj|| j|dd|||||||	|
|| d S )Nshared)lib_type)r   r
   SHARED_LIBRARYlibrary_filename)r   r   ry   r   r   r   r   r   r_   rs   rt   r   rz   r   r   r    link_shared_lib  s    
zCCompiler.link_shared_libc                 C   s(   |  tj|||||||||	|
|| d S r   )r   r
   SHARED_OBJECT)r   r   r   r   r   r   r   r   r_   rs   rt   r   rz   r   r   r    link_shared_object  s    
zCCompiler.link_shared_objectc                 C   s.   |  tj|| |||||d |||	d |
 d S r   )r   r
   
EXECUTABLEexecutable_filename)r   r   Zoutput_prognamer   r   r   r   r_   rs   rt   rz   r   r   r    link_executable  s
    


zCCompiler.link_executablec                 C   s   t d S r   r}   r9   r   r   r    library_dir_option  s    zCCompiler.library_dir_optionc                 C   s   t d S r   r}   r9   r   r   r    runtime_library_dir_option  s    z$CCompiler.runtime_library_dir_optionc                 C   s   t d S r   r}   )r   libr   r   r    library_option  s    zCCompiler.library_optionc              	   C   s   dd l }|d u rg }|d u r g }|d u r,g }|d u r8g }|jd|dd\}}t|d}	z2|D ]}
|	d|
  q^|	d|  W |	  n
|	  0 z| j|g|d}W n ty   Y d	S 0 z| j|d
||d W n t	t
fy   Y d	S 0 dS )Nr   r   T)textwz#include "%s"
z=int main (int argc, char **argv) {
    %s();
    return 0;
}
r<   Fza.out)r   r   )tempfilemkstemprR   fdopenwritecloserx   ZCompileErrorr   Z	LinkErrorr3   )r   funcnameZincludesr   r   r   r   fdZfnamefZinclr   r   r   r    has_function  s<    	
zCCompiler.has_functionc                 C   s   t d S r   r}   )r   r>   r   r_   r   r   r    find_library_file$  s    zCCompiler.find_library_file c                 C   s   |d u rd}g }|D ]|}t j|\}}t j|d }|t j|d  }|| jvrftd||f |rvt j|}|t j	||| j
  q|S )Nr   r+   z"unknown file type '%s' (from '%s'))rR   rS   rT   
splitdriveisabssrc_extensionsZUnknownFileErrorbasenamer6   joinobj_extension)r   Zsource_filenamesrM   r   Z	obj_namesZsrc_namerq   r]   r   r   r    rO   O  s"    

zCCompiler.object_filenamesc                 C   s$   |rt j|}t j||| j S r   )rR   rS   r   r   shared_lib_extensionr   r   rM   r   r   r   r    shared_object_filename`  s    z CCompiler.shared_object_filenamec                 C   s(   |rt j|}t j||| jp"d S )Nr   )rR   rS   r   r   exe_extensionr   r   r   r    r   f  s    zCCompiler.executable_filenamestaticc           
      C   s`   |dvrt dt| |d }t| |d }tj|\}}|||f }	|rPd}tj|||	S )N)r   r   ZdylibZ
xcode_stubz?'lib_type' must be "static", "shared", "dylib", or "xcode_stub"Z_lib_formatZ_lib_extensionr   )r"   getattrrR   rS   splitr   )
r   r@   r   rM   r   fmtr]   r:   rq   filenamer   r   r    r   l  s    zCCompiler.library_filenamer+   c                 C   s   t | d S r   )r	   r_   )r   msglevelr   r   r    announce  s    zCCompiler.announcec                 C   s   ddl m} |rt| d S )Nr   )DEBUG)Zdistutils.debugr   print)r   r   r   r   r   r    debug_print  s    zCCompiler.debug_printc                 C   s   t jd|  d S )Nzwarning: %s
)sysstderrr   )r   r   r   r   r    warn  s    zCCompiler.warnc                 C   s   t |||| j d S r   )r   r   )r   funcargsr   r   r   r   r    r     s    zCCompiler.executec                 C   s   t || jd d S N)r   )r   r   )r   cmdr   r   r    r     s    zCCompiler.spawnc                 C   s   t ||| jdS r   )r   r   )r   r[   dstr   r   r    r     s    zCCompiler.move_file  c                 C   s   t ||| jd d S r   )r   r   )r   r,   moder   r   r    r     s    zCCompiler.mkpath)r   r   r   )N)N)NNNNN)NNNr   NNN)Nr   N)
NNNNNr   NNNN)
NNNNNr   NNNN)
NNNNNr   NNNN)NNNNr   NNN)NNNN)r   )r   r   )r   r   )r   r   )r   r   r   )r+   )Nr+   )r   )Ar$   
__module____qualname__Zcompiler_typer   r   Zstatic_lib_extensionr   Zstatic_lib_formatZshared_lib_formatr   rl   rk   r!   r&   r   r/   r4   r7   r8   r;   r?   rA   rB   rC   rD   rE   rF   rH   rI   r^   rb   rc   rd   re   rf   rj   rr   ru   rx   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r
      s   
$ 

+	 
"  
   
D  
          
A          
          
        
  
,
+


  


r
   ))zcygwin.*unix)posixr   )ntmsvcc                 C   sV   | d u rt j} |d u rtj}tD ]0\}}t||d usHt|| d ur |  S q dS )Nr   )rR   r,   r   platform_default_compilersrematch)Zosnamer   patterncompilerr   r   r    get_default_compiler  s    

r   )ZunixccompilerZUnixCCompilerzstandard UNIX-style compiler)Z_msvccompilerZMSVCCompilerzMicrosoft Visual C++)cygwinccompilerZCygwinCCompilerz'Cygwin port of GNU C Compiler for Win32)r   ZMingw32CCompilerz(Mingw32 port of GNU C Compiler for Win32)ZbcppcompilerZBCPPCompilerzBorland C++ Compiler)r   r   cygwinZmingw32Zbcppc                  C   sX   ddl m}  g }t D ] }|d| d t| d f q|  | |}|d d S )Nr   )FancyGetoptz	compiler=r0   zList of available compilers:)Zdistutils.fancy_getoptr   compiler_classr   r6   sort
print_help)r   Z	compilersr   Zpretty_printerr   r   r    show_compilers  s    
r   c                 C   s   | d u rt j} z"|d u r t| }t| \}}}W n6 tyf   d|  }|d urZ|d|  }t|Y n0 z*d| }t| tj| }	t	|	| }
W n> t
y   td| Y n" ty   td||f Y n0 |
d ||S )Nz5don't know how to compile C/C++ code on platform '%s'z with '%s' compilerz
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')rR   r,   r   r   rv   ZDistutilsPlatformError
__import__r   modulesvarsImportErrorZDistutilsModuleError)Zplatr   r   r   r   module_name
class_nameZlong_descriptionr   moduleklassr   r   r    new_compiler  s:    

r   c                 C   s   g }| D ]}t |tr0dt|  kr.dks<n td| t|dkr\|d|d   qt|dkr|d d u r|d|d   q|d|  q|D ]}|d|  q|S )	Nr+   r0   zPbad macro definition '%s': each element of 'macros' list must be a 1- or 2-tuplez-U%sr   z-D%sz-D%s=%sz-I%s)r'   r1   r2   r3   r6   )r   r   rY   Zmacror:   r   r   r    rP     s"    $rP   c                 C   s   g }|D ]}| | | q|D ],}| |}t|trD|| }q"| | q"|D ]V}tj|\}}	|r| |g|	}
|
r| |
 q| 	d|  qT| | 
| qT|S )Nz6no library file corresponding to '%s' found (skipping))r6   r   r   r'   rN   rR   rS   r   r   r   r   )r   r   r   r   Zlib_optsr:   optr   Zlib_dirZlib_nameZlib_filer   r   r    gen_lib_options8  s&    


r   )NN)NNr   r   r   )r   rR   r   Zdistutils.errorsZdistutils.spawnr   Zdistutils.file_utilr   Zdistutils.dir_utilr   Zdistutils.dep_utilr   Zdistutils.utilr   r   Z	distutilsr	   r
   r   r   r   r   r   rP   r   r   r   r   r    <module>   s6          

--