o
     JAf=3                     @   sj   	 d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 dddZ
G d	d
 d
eZdddZdS )    N)Command)DistutilsExecError)customize_compiler)logz.cz.cxx)czc++c                   @   s   e Zd ZdZg 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dZ		d(ddZd)ddZ		d*ddZ		d*dd Z		!d+d"d#Zdddg fd$d%Z		d)d&d'ZdS ),configzprepare to build)	)z	compiler=Nzspecify the compiler type)zcc=Nzspecify the compiler executable)zinclude-dirs=Iz.list of directories to search for header files)zdefine=DzC preprocessor macros to define)zundef=Uz!C preprocessor macros to undefine)z
libraries=lz!external C libraries to link with)zlibrary-dirs=Lz.directories to search for external C libraries)noisyNz1show every action (compile, link, run, ...) taken)zdump-sourceNz=dump generated source files before attempting to compile themc                 C   s4   d | _ d | _d | _d | _d | _d| _d| _g | _d S )N   )compilerZccinclude_dirs	librarieslibrary_dirsr   dump_source
temp_filesself r   //usr/lib/python3.10/distutils/command/config.pyinitialize_options3   s   
zconfig.initialize_optionsc                 C   s   | j d u r| jj p
g | _ nt| j tr| j tj| _ | jd u r$g | _nt| jtr/| jg| _| jd u r9g | _d S t| jtrI| jtj| _d S d S N)	r   Zdistribution
isinstancestrsplitospathsepr   r   r   r   r   r   finalize_optionsB   s   




zconfig.finalize_optionsc                 C   s   d S r   r   r   r   r   r   runR   s   z
config.runc                 C   s   	 ddl m}m} t| j|s>|| j| jdd| _t| j | jr(| j| j | j	r2| j
| j	 | jr@| j| j d S d S d S )Nr   )	CCompilernew_compilerr   )r   dry_runforce)distutils.ccompilerr"   r#   r   r   r$   r   r   Zset_include_dirsr   Zset_librariesr   Zset_library_dirs)r   r"   r#   r   r   r   _check_compilerY   s   
zconfig._check_compilerc                 C   s   dt |  }t|d4}|r|D ]	}|d|  q|d || |d dkr7|d W d    |S W d    |S 1 sBw   Y  |S )NZ_configtestwz#include <%s>

)LANG_EXTopenwrite)r   bodyheaderslangfilenamefileheaderr   r   r   _gen_temp_sourcefilek   s    



zconfig._gen_temp_sourcefilec                 C   s<   |  |||}d}| j||g | jj|||d ||fS )Nz_configtest.ir   )r4   r   extendr   Z
preprocess)r   r.   r/   r   r0   srcoutr   r   r   _preprocessw   s
   zconfig._preprocessc                 C   s\   |  |||}| jrt|d|  | j|g\}| j||g | jj|g|d ||fS )Nzcompiling '%s':r5   )r4   r   	dump_filer   Zobject_filenamesr   r6   compile)r   r.   r/   r   r0   r7   objr   r   r   _compile~   s   zconfig._compilec           
      C   sr   |  ||||\}}tjtj|d }	| jj|g|	|||d | jjd ur.|	| jj }	| j	|	 |||	fS )Nr   )r   r   Ztarget_lang)
r=   r   pathsplitextbasenamer   Zlink_executableZexe_extensionr   append)
r   r.   r/   r   r   r   r0   r7   r<   progr   r   r   _link   s   
zconfig._linkc              	   G   sP   |s| j }g | _ tdd| |D ]}zt| W q ty%   Y qw d S )Nzremoving: %s )r   r   infojoinr   removeOSError)r   	filenamesr1   r   r   r   _clean   s   zconfig._cleanNr   c                 C   sR   	 ddl m} |   d}z
| |||| W n |y"   d}Y nw |   |S )Nr   CompileErrorTF)r&   rL   r'   r9   rJ   r   r.   r/   r   r0   rL   okr   r   r   try_cpp   s   zconfig.try_cppc                 C   s   	 |    | ||||\}}t|trt|}t|}d}		 | }
|
dkr*n	||
r2d}	nq!W d    n1 s=w   Y  | 	  |	S )NFT )
r'   r9   r   r   rer;   r,   readlinesearchrJ   )r   patternr.   r/   r   r0   r7   r8   r2   matchliner   r   r   
search_cpp   s&   




zconfig.search_cppc                 C   sd   	 ddl m} |   z| |||| d}W n |y"   d}Y nw t|r)dp*d |   |S )Nr   rK   TFsuccess!failure.)r&   rL   r'   r=   r   rE   rJ   rM   r   r   r   try_compile   s   zconfig.try_compilec           
   	   C   sp   	 ddl m}m} |   z| |||||| d}	W n ||fy(   d}	Y nw t|	r/dp0d |   |	S Nr   )rL   	LinkErrorTFrX   rY   )r&   rL   r\   r'   rC   r   rE   rJ   )
r   r.   r/   r   r   r   r0   rL   r\   rN   r   r   r   try_link   s   
zconfig.try_linkc              
   C   s   	 ddl m}m} |   z| ||||||\}	}
}| |g d}W n ||tfy2   d}Y nw t|r9dp:d | 	  |S r[   )
r&   rL   r\   r'   rC   spawnr   r   rE   rJ   )r   r.   r/   r   r   r   r0   rL   r\   r7   r<   ZexerN   r   r   r   try_run   s   

zconfig.try_runr   c           	      C   sv   	 |    g }|r|d|  |d |r|d|  n|d|  |d d|d }| |||||S )Nz
int %s ();zint main () {z  %s();z  %s;}r)   )r'   rA   rF   r]   )	r   funcr/   r   r   r   Zdeclcallr.   r   r   r   
check_func  s   


zconfig.check_funcc                 C   s"   	 |    | d|||g| |S )Nzint main (void) { })r'   r]   )r   Zlibraryr   r/   r   Zother_librariesr   r   r   	check_lib4  s
   

zconfig.check_libc                 C   s   	 | j d|g|dS )Nz/* No body */)r.   r/   r   )rO   )r   r3   r   r   r0   r   r   r   check_headerB  s   
zconfig.check_header)NNNr   )NNr   )NNNNr   )NNNNr   r   )__name__
__module____qualname__descriptionZuser_optionsr   r    r!   r'   r4   r9   r=   rC   rJ   rO   rW   rZ   r]   r_   rc   rd   re   r   r   r   r   r      s@    	






r   c                 C   sR   	 |d u rt d|  nt | t| }zt |  W |  d S |  w )Nz%s)r   rE   r,   readclose)r1   headr2   r   r   r   r:   K  s   
r:   r   )r   rQ   Zdistutils.corer   Zdistutils.errorsr   Zdistutils.sysconfigr   Z	distutilsr   r+   r   r:   r   r   r   r   <module>   s    
  8