B
    v9a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dddddddd	d
g	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d0dd „Zd1d!d"„Zd2d#d$„Zd3d%d&„Zd4d'd(„Zd5d*d+„Zdddg fd,d-„Zd6d.d/„ZdS )7Ú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.7/config.pyÚinitialize_options3   s    zconfig.initialize_optionsc             C   s   | j d kr| jj pg | _ nt| j tƒr6| j  tj¡| _ | jd krHg | _nt| jtƒr^| jg| _| jd k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 )Nr   )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   sl   dt |  }t|dƒ}|r@x|D ]}| d| ¡ q W | d¡ | |¡ |d dkr`| d¡ | ¡  |S )NZ_configtestÚwz#include <%s>
Ú
éÿÿÿÿ)ÚLANG_EXTÚopenÚwriteÚclose)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   )r3   r   Úextendr   Z
preprocess)r   r-   r.   r   r/   ÚsrcÚoutr   r   r   Ú_preprocessx   s
    zconfig._preprocessc             C   s\   |   |||¡}| jr"t|d| ƒ | j |g¡\}| j ||g¡ | jj|g|d ||fS )Nzcompiling '%s':)r   )r3   r   Ú	dump_filer   Zobject_filenamesr   r4   Úcompile)r   r-   r.   r   r/   r5   Úobjr   r   r   Ú_compile   s    zconfig._compilec       
      C   sr   |   ||||¡\}}tj tj |¡¡d }	| jj|g|	|||d | jjd k	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   r/   r5   r:   Úprogr   r   r   Ú_linkˆ   s    zconfig._linkc          	   G   sX   |s| j }g | _ t dd |¡¡ x0|D ](}yt |¡ W q( tk
rN   Y q(X q(W d S )Nzremoving: %sú )r   r   ÚinfoÚjoinr   ÚremoveÚOSError)r   Ú	filenamesr0   r   r   r   Ú_clean—   s    
zconfig._cleanNr   c             C   sR   ddl m} |  ¡  d}y|  ||||¡ W n |k
rD   d}Y nX |  ¡  |S )Nr   )ÚCompileErrorTF)r$   rI   r%   r7   rH   )r   r-   r.   r   r/   rI   Úokr   r   r   Útry_cpp­   s    
zconfig.try_cppc             C   sx   |   ¡  |  ||||¡\}}t|tƒr0t |¡}t|ƒ}d}	x&| ¡ }
|
dkrPP | |
¡r>d}	P q>W | 	¡  |  
¡  |	S )NFÚ T)r%   r7   r   r   Úrer9   r*   ÚreadlineÚsearchr,   rH   )r   Úpatternr-   r.   r   r/   r5   r6   r1   ÚmatchÚliner   r   r   Ú
search_cpp¿   s     	


zconfig.search_cppc             C   sd   ddl m} |  ¡  y|  ||||¡ d}W n |k
rD   d}Y nX t |rRdpTd¡ |  ¡  |S )Nr   )rI   TFzsuccess!zfailure.)r$   rI   r%   r;   r   rC   rH   )r   r-   r.   r   r/   rI   rJ   r   r   r   Útry_compileÜ   s    
zconfig.try_compilec       
   	   C   sp   ddl m}m} |  ¡  y|  ||||||¡ d}	W n ||fk
rP   d}	Y nX t |	r^dp`d¡ |  ¡  |	S )Nr   )rI   Ú	LinkErrorTFzsuccess!zfailure.)r$   rI   rU   r%   rA   r   rC   rH   )
r   r-   r.   r   r   r   r/   rI   rU   rJ   r   r   r   Útry_linkì   s    


zconfig.try_linkc          
   C   s„   ddl m}m} |  ¡  y.|  ||||||¡\}	}
}|  |g¡ d}W n ||tfk
rd   d}Y nX t |rrdptd¡ |  	¡  |S )Nr   )rI   rU   TFzsuccess!zfailure.)
r$   rI   rU   r%   rA   Zspawnr   r   rC   rH   )r   r-   r.   r   r   r   r/   rI   rU   r5   r:   ZexerJ   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%   r?   rD   rV   )	r   Úfuncr.   r   r   r   ZdeclZcallr-   r   r   r   Ú
check_func  s    


zconfig.check_funcc             C   s    |   ¡  |  d|||g| |¡S )Nzint main (void) { })r%   rV   )r   Zlibraryr   r.   r   Zother_librariesr   r   r   Ú	check_lib6  s    

zconfig.check_libc             C   s   | j d|g|dS )Nz/* No body */)r-   r.   r   )rK   )r   r2   r   r   r/   r   r   r   Úcheck_headerD  s    
zconfig.check_header)NNNr   )NNNr   )NNr   )NNNNr   )NNNNr   )NNNNr   r   )NNr   )Ú__name__Ú
__module__Ú__qualname__ÚdescriptionZuser_optionsr   r   r   r%   r3   r7   r;   rA   rH   rK   rS   rT   rV   rW   rZ   r[   r\   r   r   r   r   r      sB   	
 

 
 
 
 r   c             C   sJ   |d krt  d| ¡ n
t  |¡ t| ƒ}zt  | ¡ ¡ W d | ¡  X d S )Nz%s)r   rC   r*   Úreadr,   )r0   Úheadr1   r   r   r   r8   N  s    
r8   )N)r   rM   Zdistutils.corer   Zdistutils.errorsr   Zdistutils.sysconfigr   Z	distutilsr   r)   r   r8   r   r   r   r   Ú<module>   s   
  ;