a
    åze=3  ã                   @   sh   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.9/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ƒr6| j  tj¡| _ | jd u rHg | _nt| jtƒr^| jg| _| jd u rpg | _nt| jtƒrŒ| j tj¡| _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   sz   ddl m}m} t| j|ƒsv|| j| jdd| _t| jƒ | jrN| j | j¡ | j	rb| j 
| j	¡ | jrv| j | j¡ 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ƒV}|r>|D ]}| d| ¡ q | d¡ | |¡ |d dkr^| d¡ W d   ƒ n1 sr0    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   sR   |s| j }g | _ t dd |¡¡ |D ]&}zt |¡ W q& tyJ   Y q&0 q&d S )Nzremoving: %sú )r   r   ÚinfoÚjoinr   ÚremoveÚOSError)r   Ú	filenamesr1   r   r   r   Ú_clean–   s    zconfig._cleanNr   c                 C   sP   ddl m} |  ¡  d}z|  ||||¡ W n |yB   d}Y n0 |  ¡  |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ƒr0t |¡}t|ƒ8}d}	| ¡ }
|
dkrPqb| |
¡r>d}	qbq>W d   ƒ n1 sv0    Y  |  	¡  |	S )NFÚ T)
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   sb   ddl m} |  ¡  z|  ||||¡ d}W n |yB   d}Y n0 t |rPdpRd¡ |  ¡  |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   sn   ddl m}m} |  ¡  z|  ||||||¡ d}	W n ||fyN   d}	Y n0 t |	r\dp^d¡ |  ¡  |	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yb   d}Y n0 t |rpdprd¡ |  	¡  |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   st   |   ¡  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   )NNNr   )NNr   )NNNNr   )NNNNr   )NNNNr   r   )NNr   )Ú__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 | ¡  n
| ¡  0 d S )Nz%s)r   rE   r,   ÚreadÚclose)r1   Úheadr2   r   r   r   r:   K  s    
r:   )N)r   rQ   Zdistutils.corer   Zdistutils.errorsr   Zdistutils.sysconfigr   Z	distutilsr   r+   r   r:   r   r   r   r   Ú<module>   s   
  8