o
     JAf:                     @   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                   @   sb  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 drd0d1Z!d2d3 Z"d4d5 Z#d6d7 Z$d8d9 Z%		dsd:d;Z&			dtd<d=Z'd>d? Z(		dud@dAZ)dBZ*dCZ+dDZ,										dvdEdFZ-										dvdGdHZ.										dvdIdJZ/								dwdKdLZ0dMdN Z1dOdP Z2dQdR Z3		dxdSdTZ4dydUdVZ5dzdXdYZ6dzdZd[Z7dzd\d]Z8	^	Wd{d_d`Z9d|dbdcZ:ddde Z;dfdg Z<d}dhdiZ=djdk Z>dldm Z?d~dodp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
|  q#d 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.10/distutils/ccompiler.py__init__U   s   zCCompiler.__init__c                 K   s>   	 |D ]}|| 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| d S 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   sb   	 |D ]+}t |tr$t|dv r$t |d ts|d d u r$t |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   s0   	 |  |}|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   s2   	 |  |}|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      	 | j | d S r   )r   r6   r   dirr   r   r    add_include_dir      zCCompiler.add_include_dirc                 C      	 |d d  | _ d S r   r   r   dirsr   r   r    set_include_dirs   s   zCCompiler.set_include_dirsc                 C   r9   r   )r   r6   )r   libnamer   r   r    add_library   s   zCCompiler.add_libraryc                 C   r>   r   )r   )r   Zlibnamesr   r   r    set_libraries      zCCompiler.set_librariesc                 C   r9   r   )r   r6   r:   r   r   r    add_library_dir  r=   zCCompiler.add_library_dirc                 C   r>   r   )r   r@   r   r   r    set_library_dirs  s   zCCompiler.set_library_dirsc                 C   r9   r   )r   r6   r:   r   r   r    add_runtime_library_dir  s   z!CCompiler.add_runtime_library_dirc                 C   r>   r   )r   r@   r   r   r    set_runtime_library_dirs  rF   z"CCompiler.set_runtime_library_dirsc                 C   r9   r   )r   r6   )r   objectr   r   r    add_link_object   r=   zCCompiler.add_link_objectc                 C   r>   r   )r   )r   r   r   r   r    set_link_objects(  rF   zCCompiler.set_link_objectsc                 C   s  	 |d u r	| j }n	t|tstd|d u r| j}nt|tr'|| jp$g  }ntd|d u r3| j}nt|ttfrDt|| jpAg  }ntd|d u rNg }| j|d|d}t	||}i }	t
t|D ]!}
||
 }||
 }tj|d }| tj| ||f|	|< qc|||||	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   r]   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 r| j}nt|tr'|| jp$g  }ntd|d u r3| j}nt|ttfrDt|| jpAg  }ntd|||fS )NrN   rO   rP   )r   r'   r(   r3   r   rR   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   )rS   )r   rZ   r   r[   r   r   r   r    _prep_compile  s   zCCompiler._prep_compilec                 C   sP   	 t |ttfstdt|}|d u r| j}||fS t |ts$td||fS )Nz,'objects' must be a list or tuple of stringsrN   )r'   rR   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rt|| j pg  }ntd|d u r&| j}nt|ttfr7t|| jp4g  }ntd|d u rC| j}nt|ttfrTt|| jpQg  }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'   rR   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}|S t||}|S )NTnewer)missing)r   r   r   )r   r   output_filerk   r   r   r    
_need_link  s   
zCCompiler._need_linkc           	   	   C   s|   	 t |ts	|g}d }t| j}|D ])}tj|\}}| j|}z| j	|}||k r1|}|}W q t
y;   Y qw |S r   )r'   rR   r2   language_orderrV   rW   rX   language_mapgetindexr"   )	r   rZ   langrr   sourcebasera   ZextlangZextindexr   r   r    detect_language  s$   

zCCompiler.detect_languagec                 C      	 d S r   r   )r   rt   rm   r   r   extra_preargsextra_postargsr   r   r    
preprocess  s   	zCCompiler.preprocessc	              	   C   st   	 |  ||||||\}}	}}
}| |
||}|	D ]}z|| \}}W n	 ty,   Y qw | ||||||
 q|	S r   )rb   rf   KeyError_compile)r   rZ   r   r   r   rc   rx   ry   r[   r   r]   r^   re   r`   r_   ra   r   r   r    compile  s   3zCCompiler.compilec                 C   rw   r   r   )r   r`   r_   ra   re   ry   r]   r   r   r    r|   C     zCCompiler._compilec                 C   rw   r   r   )r   r   output_libnamer   rc   target_langr   r   r    create_static_libI  s   zCCompiler.create_static_libZshared_objectZshared_library
executablec                 C      	 t r   NotImplementedError)r   Ztarget_descr   output_filenamer   r   r   r   export_symbolsrc   rx   ry   
build_tempr   r   r   r    linki  s   +zCCompiler.linkc                 C   s2   |  tj|| j|dd|||||||	|
|| d S )Nshared)lib_type)r   r
   SHARED_LIBRARYlibrary_filename)r   r   r   r   r   r   r   r   rc   rx   ry   r   r   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   rc   rx   ry   r   r   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   rc   rx   ry   r   r   r   r    link_executable  s
   


zCCompiler.link_executablec                 C   r   r   r   r:   r   r   r    library_dir_option  r~   zCCompiler.library_dir_optionc                 C   r   r   r   r:   r   r   r    runtime_library_dir_option  r~   z$CCompiler.runtime_library_dir_optionc                 C   r   r   r   )r   libr   r   r    library_option  r~   zCCompiler.library_optionc              	   C   s   	 dd l }|d u rg }|d u rg }|d u rg }|d u rg }|jd|dd\}}t|d}	z|D ]	}
|	d|
  q0|	d|  W |	  n|	  w z
| j|g|d}W n
 ty`   Y d	S w z| j|d
||d W dS  t	t
fyy   Y d	S w )Nr   r   T)textwz#include "%s"
z=int main (int argc, char **argv) {
    %s();
    return 0;
}
r?   Fza.out)r   r   )tempfilemkstemprV   fdopenwritecloser}   ZCompileErrorr   Z	LinkErrorr3   )r   funcnameZincludesr   r   r   r   fdZfnamefZinclr   r   r   r    has_function  sB   zCCompiler.has_functionc                 C   r   r   r   )r   rA   r   rc   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r3td||f |r;t j|}|t j	||| j
  q
|S )Nr   r+   z"unknown file type '%s' (from '%s'))rV   rW   rX   
splitdriveisabssrc_extensionsZUnknownFileErrorbasenamer6   joinobj_extension)r   Zsource_filenamesrQ   r   Z	obj_namesZsrc_nameru   ra   r   r   r    rS   O  s"   

zCCompiler.object_filenamesc                 C   s$   |rt j|}t j||| j S r   )rV   rW   r   r   shared_lib_extensionr   r   rQ   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   )rV   rW   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 }	|r(d}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"   getattrrV   rW   splitr   )
r   rC   r   rQ   r   fmtra   r;   ru   filenamer   r   r    r   l  s   zCCompiler.library_filenamer+   c                 C   s   t | d S r   )r	   rc   )r   msglevelr   r   r    announce  s   zCCompiler.announcec                 C   s    ddl m} |rt| d S 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     zCCompiler.warnc                 C   s   t |||| j d S r   )r   r   )r   funcargsr   r   r   r   r    r     r   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     r   zCCompiler.mkpath)r   r   r   r   )NNNNN)NNNr   NNN)Nr   N)
NNNNNr   NNNN)NNNNr   NNN)NNNN)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   rp   ro   r!   r&   r   r/   r4   r7   r8   r<   rB   rD   rE   rG   rH   rI   rJ   rL   rM   rb   rf   rg   rh   ri   rj   rn   rv   rz   r}   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   rS   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   sX   	 | d u rt j} |d u rtj}tD ]\}}t||d us%t|| d ur)|  S qdS )Nr   )rV   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   sZ   	 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 n ty2   d|  }|d ur.|d|  }t|w zd| }t| tj| }	t	|	| }
W n t
yU   td|  tyc   td||f w |
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')rV   r,   r   r   r{   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 ]G}t |trdt|  krdksn td| t|dkr/|d|d   qt|dkrL|d d u rE|d|d   q|d|  q|D ]	}|d|  qO|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   r]   Zmacror;   r   r   r    rT     s&   $rT   c                 C   s   	 g }|D ]
}| | | q|D ]}| |}t|tr#|| }q| | q|D ]+}tj|\}}	|rN| |g|	}
|
rF| |
 q+| 	d|  q+| | 
| q+|S )Nz6no library file corresponding to '%s' found (skipping))r6   r   r   r'   rR   rV   rW   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   rV   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   rT   r   r   r   r   r    <module>   s:           

--