B
    ‘Í‚^ X  ã               @   s€  d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
mZmZmZmZ ddlmZ dZdZdgZed	ƒed
ƒdd„ ƒƒZedƒdd„ ƒZG dd„ dejƒZG dd„ dejƒZedƒeddƒedƒdd„ ƒƒƒZedƒeddƒdd„ ƒƒZedƒedƒdd„ ƒƒZed6dd „ƒZed7d"d#„ƒZed8d%d&„ƒZed9d(d)„ƒZed:d*d+„ƒZed;d,d-„ƒZ d.Z!ed<d0d1„ƒZ"d2d3„ Z#d4d5„ Z$dS )=a´  
Support for Python, detect the headers and libraries and provide
*use* variables to link C/C++ programs against them::

	def options(opt):
		opt.load('compiler_c python')
	def configure(conf):
		conf.load('compiler_c python')
		conf.check_python_version((2,4,2))
		conf.check_python_headers()
	def build(bld):
		bld.program(features='pyembed', source='a.c', target='myprog')
		bld.shlib(features='pyext', source='b.c', target='mylib')
é    N)ÚErrorsÚLogsÚNodeÚOptionsÚTaskÚUtils)Ú	extensionÚbefore_methodÚafter_methodÚfeature)Úconfzþ
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
	void Py_Initialize(void);
	void Py_Finalize(void);
#ifdef __cplusplus
}
#endif
int main(int argc, char **argv)
{
   (void)argc; (void)argv;
   Py_Initialize();
   Py_Finalize();
   return 0;
}
zX
import sys, py_compile
py_compile.compile(sys.argv[1], sys.argv[2], sys.argv[3], True)
z>from distutils.sysconfig import get_config_var, get_python_libÚprocess_sourceÚpyc             C   sr   t | ddƒ| _t | ddƒ}|r6t|tjƒs6| j |¡}|| _| jj}|sT| j	 
d¡ t| dd¡ƒdkrnd	| _dS )
zI
	Create tasks to byte-compile .py files and install them, if requested
	Úinstall_pathz${PYTHONDIR}Úinstall_fromNzNInstalling python files requires PYTHON_VERSION, try conf.check_python_versionÚ.Ú é   T)Úgetattrr   Ú
isinstancer   ÚpathÚfind_dirr   ÚenvÚPYTHON_VERSIONÚbldÚfatalÚintÚreplaceZ
install_32)Úselfr   Úver© r    ú./wcdo/src/waf-tools/waf/waflib/Tools/python.pyÚ
feature_py:   s    r"   z.pyc       
      C   sø  t | dƒs"td| j| j ¡ f ƒ‚t| ddƒ| _t| ddƒ}| jrpt| jtjƒsptd| j| j ¡ t	| jƒf ƒ‚| j
r¦| jr”| j| j
|| j|d� n| j| j
||d	� g }| jjr¼| d
¡ | jjrÎ| d¡ | j
�r@| j�r|rî| | j¡n|j}t d| j
|f | j¡}n0|�r | | j¡n|j}t d| j
|f | j¡}n| ¡ }xª|D ]¢}| jj�r¤| jj�s¤|jdd… }|j ¡  d¡ d|| jj|f ¡}|j ¡  n| d| ¡}|  |||¡}	||	_| j
�rN| jtj |¡||j ¡ |d� �qNW dS )zI
	Add signature of .py file, so it will be byte-compiled when necessary
	r   z2add features="py" for target "%s" in "%s/wscript".r   NÚrelative_trickTz7add features="py" for target "%s" in "%s/wscript" (%s).)Ú
install_tor   Úcwdr#   )r$   r   r#   ÚpycÚpyoz%s/%séýÿÿÿÚ__pycache__z%s.%s.%sz.%s) ÚhasattrÚAssertionErrorÚtargetr   Z	nice_pathr   r   r   r   Útyper   Úadd_install_filesr   ÚPYCÚappendÚPYOÚ	path_fromÚnamer   Ú
subst_varsÚabspathÚPYTAGÚ	NOPYCACHEÚparentÚget_bldÚ	make_nodeÚmkdirÚ
change_extÚcreate_taskÚpydÚosÚdirname)
r   Únoder#   ÚlstZ
target_dirr>   Úextr3   ZpyobjÚtskr    r    r!   Ú
process_pyM   s@    "


$rE   c               @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )r&   z
	Byte-compiling python files
	ÚPINKc             C   s   | j d }| |j ¡ ¡S )Nr   )Úoutputsr2   ÚctxÚlaunch_node)r   rA   r    r    r!   Ú__str__„   s    
zpyc.__str__c             C   sB   t  d| j¡dt| jd  ¡ | jd  ¡ | jg}| jj	 
|¡}|S )Nz	${PYTHON}z-cr   )r   r4   r   ÚINSTÚinputsr5   rG   r>   Ú	generatorr   Úexec_command)r   ÚcmdÚretr    r    r!   Úrun‡   s    0zpyc.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚcolorrJ   rQ   r    r    r    r!   r&      s   r&   c               @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )r'   z
	Byte-compiling python files
	rF   c             C   s   | j d }| |j ¡ ¡S )Nr   )rG   r2   rH   rI   )r   rA   r    r    r!   rJ   ‘   s    
zpyo.__str__c             C   sN   t  d| j¡t  d| j¡dt| jd  ¡ | jd  ¡ | jg}| jj	 
|¡}|S )Nz	${PYTHON}z${PYFLAGS_OPT}z-cr   )r   r4   r   rK   rL   r5   rG   r>   rM   r   rN   )r   rO   rP   r    r    r!   rQ   ”   s    <zpyo.runN)rR   rS   rT   rU   rV   rJ   rQ   r    r    r    r!   r'   Œ   s   r'   ÚpyextZpropagate_uselib_varsZ
apply_linkZapply_bundlec             C   sˆ   |   t| dg ƒ¡| _d| jkr*| j d¡ | jj | j_ | j_| j_| jj | j_	| j_
y| jsfdS W n tk
r‚   d| _Y nX dS )zp
	Change the values of *cshlib_PATTERN* and *cxxshlib_PATTERN* to remove the
	*lib* prefix from library names.
	ÚuselibÚPYEXTNz${PYTHONARCHDIR})Úto_listr   rX   r0   r   Úpyext_PATTERNZcshlib_PATTERNZcxxshlib_PATTERNZmacbundle_PATTERNZfcshlib_PATTERNZdshlib_PATTERNr   ÚAttributeError)r   r    r    r!   Ú
init_pyext™   s    
r]   c             C   s   t  ¡ dkrd| _dS )z?Mac-specific pyext extension that enables bundles from c_osx.pyÚdarwinTN)r   Úunversioned_sys_platformZ
mac_bundle)r   r    r    r!   Ú
set_bundle®   s    r`   Úpyembedc             C   s.   |   t| dg ƒ¡| _d| jkr*| j d¡ dS )z
	Add the PYEMBED variable.
	rX   ÚPYEMBEDN)rZ   r   rX   r0   )r   r    r    r!   Úinit_pyembedµ   s    
rc   c       	      C   sv  |s(y
| j }W n tk
r&   t}Y nX t|ƒ}| d¡ x|D ]}| d| ¡ q@W ttjƒ}y
|d= W n tk
r~   Y nX y$| j	| j
jdd |¡g |d�}W n  tjk
rÄ   |  d¡ Y nX |  |¡ g }xœ| ¡ D ]�}| ¡ }|sðqÞ|dk�r| d	¡ qÞ|d
 dk�r"|d dk�s>|d
 dk�rN|d dk�rN| t|ƒ¡ qÞ|d
  ¡ �rl| t|ƒ¡ qÞP qÞW |S )a  
	Spawn a new python process to dump configuration variables

	:param variables: variables to print
	:type variables: list of string
	:param imports: one import by element
	:type imports: list of string
	:return: the variable values
	:rtype: list of string
	r   zprint(repr(%s))ÚMACOSX_DEPLOYMENT_TARGETz-cÚ
)r   z9The distutils module is unusable: install "python-devel"?ÚNoneNr   ú'éÿÿÿÿú")Zpython_importsr\   ÚDISTUTILS_IMPÚlistr0   Údictr?   ÚenvironÚKeyErrorÚcmd_and_logr   ÚPYTHONÚjoinr   ÚWafErrorr   Úto_logÚ
splitlinesÚstripÚevalÚisdigitr   )	r   Z	variablesZimportsÚprogramÚvZos_envÚoutZreturn_valuesÚsr    r    r!   Úget_python_variables¿   s@    





$

8r|   úTesting pyembed configurationc          	   C   s"   | j dd|tdd||f d� d S )NzPython.hZHAVE_PYEMBEDz-Could not build a python embedded interpreterz%s %sprogram pyembed)Úheader_nameÚdefine_nameÚmsgÚfragmentÚerrmsgÚfeatures)ÚcheckÚFRAG)r   Úmoder€   r    r    r!   Útest_pyembedî   s    
r‡   úTesting pyext configurationc          	   C   s"   | j dd|tdd||f d� d S )NzPython.hZ
HAVE_PYEXTz!Could not build python extensionsz%s %sshlib pyext)r~   r   r€   r�   r‚   rƒ   )r„   r…   )r   r†   r€   r    r    r!   Ú
test_pyextô   s    
r‰   úpyembed pyextc             C   s  t  |¡}d| jks,d| jks,d| jks,dS x:d ¡ D ].}|| jkrT|  d| ¡ q6| j| | j|< q6W | jjrtdpvd}d	|krÀ| j d| j d¡¡}|d
krª|  d¡ n|  |d¡ |  	|¡ d|k�r
| j d| j d¡¡}|d
krô|  d¡ n|  |d¡ |  
|¡ dS )a   
	For cross-compilation purposes, it is possible to bypass the normal detection and set the flags that you want:
	PYTHON_VERSION='3.4' PYTAG='cpython34' pyext_PATTERN="%s.so" PYTHON_LDFLAGS='-lpthread -ldl' waf configure

	The following variables are used:
	PYTHON_VERSION    required
	PYTAG             required
	PYTHON_LDFLAGS    required
	pyext_PATTERN     required
	PYTHON_PYEXT_LDFLAGS
	PYTHON_PYEMBED_LDFLAGS
	ZPYTHON_LDFLAGSZPYTHON_PYEXT_LDFLAGSZPYTHON_PYEMBED_LDFLAGSFz"PYTHON_VERSION PYTAG pyext_PATTERNz#Please set %s in the os environmentÚcxxÚcrW   Nz:No flags provided through PYTHON_PYEXT_LDFLAGS as requiredrY   ra   z<No flags provided through PYTHON_PYEMBED_LDFLAGS as requiredrb   T)r   rZ   rm   Úsplitr   r   ÚCXX_NAMEÚgetÚparse_flagsr‰   r‡   )r   rƒ   ÚxÚxxÚflagsr    r    r!   Úpython_cross_compileú   s*    




r”   c             C   s  t  |¡}d|ks"d|ks"tdƒ‚| j}|js>|js>|  d¡ |  |¡rLdS |jsZ|  	¡  |j
}|sn|  d¡ d ¡ }y|  dd	„ |D ƒ¡}W n tk
r¬   |  d
¡ Y nX dd	„ t||ƒD ƒ}|  d|d |¡f ¡ tt||ƒƒ}d}|| �r||  ||< | j|< d|d  |_d |j d¡dd… ¡}	| jd |¡d d|	 d|	 d|	 gdddd� |j�r@| jj�rzdS dddgg}
tjdk �r¤d d	„ |
d! D ƒ}
|j�r²d"�p´d#}d|k�r˜xx|
D ]p}|d$g }y$| jd%d& |¡ |jdd'|d(� W n: | jjk
�r2   | jd%d& |¡ |jdd'|d(� Y nX �qÆW y|  |¡ W nN | jjk
�r–   |d) �r�|d* �r�| d+|d* g¡ |  |¡ n‚ Y nX d|k�r0x.|
D ]&}| jd,d& |¡ |jdd-|d(� �q¨W y|  |¡ W nN | jjk
�r.   |d) �r(|d* �r(| d.|d* g¡ |  |¡ n‚ Y nX |  d/d0¡ dS |d1 d& |d2  }
|   |
d'¡ |d1 d& |d3  d& |d2  }
|   |
d-¡ d}|d4 �s¤|j|d4< �x0d5|d4  d5|j d6 d5|j !dd¡ fD �] }|�s|j"�r|j"}|  d7| ¡ | j#|d'|dd8| d9�}|�sR|d* �rR|d* g}|  d:| ¡ | j#|d'|dd;| d9�}|�sŽ|d< �rŽ|d< g}|  d=¡ | j#|d'|dd>| d9�}|�sÊt$j% |d? d@¡g}|  dA¡ | j#|d'|ddB| d9�}|�rÒP �qÒW |�rô||_"| &dC|g¡ n
|  dD¡ t j'�s|d) �r |j"|_(|j)|_*|  dE|dF f ¡ |dF g|_+|dF g|_,|jdGk�rt| dHdIg¡ | dJdIg¡ |jdGk�rœ| dKdIg¡ | dLdIg¡ |jdMk�rìd!dNl-m.} |ƒ }| /¡  | &dJ|j0¡ | &dL|j0¡ | &dO|j1¡ | j#dPd/d't2dQdR� dS )Sa  
	Check for headers and libraries necessary to extend or embed python by using the module *distutils*.
	On success the environment variables xxx_PYEXT and xxx_PYEMBED are added:

	* PYEXT: for compiling python extensions
	* PYEMBED: for embedding a python interpreter
	ra   rW   zCcheck_python_headers features must include 'pyembed' and/or 'pyext'z$load a compiler first (gcc, g++, ..)Nz$Could not find the python executablezlprefix SO LDFLAGS LIBDIR LIBPL INCLUDEPY Py_ENABLE_SHARED MACOSX_DEPLOYMENT_TARGET LDSHARED CFLAGS LDVERSIONc             S   s   g | ]}d | ‘qS )zget_config_var('%s') or ''r    )Ú.0r‘   r    r    r!   ú
<listcomp>@  s    z(check_python_headers.<locals>.<listcomp>z6Python development headers not found (-v for details).c             S   s   g | ]\}}d ||f ‘qS )z%s = %rr    )r•   r‘   Úyr    r    r!   r–   D  s    z#Configuration returned from %r:
%s
re   rd   z%sZSOr   é   r   z-configzpython%s-configzpython-config-%szpython%sm-configÚPYTHON_CONFIGzpython-configF)Úvarr€   Ú	mandatoryz--cflagsz--libsz	--ldflagsi  c             S   s   g | ]
}|g‘qS r    r    )r•   Úkr    r    r!   r–   Z  s    r   r‹   rŒ   z--embedz)Asking python-config for pyembed %r flagsú rb   )r€   r   ÚpackageZuselib_storeÚargsZPy_ENABLE_SHAREDÚLIBDIRÚLIBPATH_PYEMBEDz'Asking python-config for pyext %r flagsrY   ÚLIBPATH_PYEXTÚHAVE_PYTHON_Hé   ZLDFLAGSZCFLAGSZLDSHAREDZ	LDVERSIONÚpythonÚmz'

# Trying default LIBPATH_PYEMBED: %r
z*Checking for library %s in LIBPATH_PYEMBED)ÚlibrX   Zlibpathr›   r€   z(

# try again with -L$python_LIBDIR: %r
z!Checking for library %s in LIBDIRZLIBPLzb

# try again with -L$python_LIBPL (some systems don't install the python library in $prefix/lib)
z'Checking for library %s in python_LIBPLÚprefixZlibszS

# try again with -L$prefix/libs, and pythonXY name rather than pythonX.Y (win32)
z'Checking for library %s in $prefix/libsÚLIB_PYEMBEDz

### LIB NOT FOUND
zDInclude path for Python extensions (found via distutils module): %r
Z	INCLUDEPYZgccZCFLAGS_PYEMBEDz-fno-strict-aliasingZCFLAGS_PYEXTZCXXFLAGS_PYEMBEDZCXXFLAGS_PYEXTZmsvc)ÚMSVCCompilerZLINKFLAGS_PYEXTzPython.hzKDistutils not installed? Broken python installation? Get python-config now!)r~   r   rX   r�   r‚   )3r   rZ   r+   r   ZCC_NAMErŽ   r   r”   r   Úcheck_python_versionrp   r�   r|   ÚRuntimeErrorÚziprs   rq   rl   rm   r[   Úfind_programr™   r£   ÚsysÚ
hexversionZ	check_cfgÚerrorsÚConfigurationErrorr‡   Úappend_uniquer‰   Údefiner�   r   r¡   r„   r?   r   Úappend_valueÚis_win32r¢   r©   Z	LIB_PYEXTZINCLUDES_PYEXTZINCLUDES_PYEMBEDZdistutils.msvccompilerrª   Z
initializeZcompile_optionsZldflags_sharedr…   )r   rƒ   r   Úpybinry   rB   ZvalsÚdctr‘   ÚnumZ	all_flagsr’   r“   Z
embedflagsÚresultr3   r   rª   Zdist_compilerr    r    r!   Úcheck_python_headers#  sÚ    	




0



$,

& 

4




r»   c             C   s‚  |dkst |tƒst‚| jj}|s,|  d¡ |ddg }t d|¡ |  |¡ 	¡ }t
|ƒdksrtdt
|ƒ|f ƒ‚t|d ƒt|d	 ƒt|d
 ƒ|d t|d ƒf}|dkp²||k}|�rd dd„ |dd
… D ƒ¡}|| j_d| jkrò| jj}n’d| jk�r
| jd }nztj�r&|  ddg¡\}	}nd}	|  d| jj g¡\}|	dk�r„| jj�rltj | jjd| ¡}	ntj | jjdd| ¡}	d| jk�rš| jj}
n8d| jk�r²| jd }
n |  d| jj g¡\}
|
�sÒ|}
t| dƒ�rö|  d|¡ |  d|
¡ || j_|
| j_d tt|dd… ƒ¡}|dk�r6|  d|¡ n2d tt|ƒ¡}| jd|f ||�r`d�pbdd� |�s~|  d|f ¡ dS )a  
	Check if the python interpreter is found matching a given minimum version.
	minver should be a tuple, eg. to check for python >= 2.4.2 pass (2,4,2) as minver.

	If successful, PYTHON_VERSION is defined as 'MAJOR.MINOR' (eg. '2.4')
	of the actual python version found, and PYTHONDIR and PYTHONARCHDIR
	are defined, pointing to the site-packages directories appropriate for
	this python version, where modules/packages/extensions should be
	installed.

	:param minver: minimum version
	:type minver: tuple of int
	Nz$could not find the python executablez-cz3import sys
for x in sys.version_info: print(str(x))z!python: Running python command %ré   zfound %r lines, expected 5: %rr   r¤   r˜   é   é   r   c             S   s   g | ]}t |ƒ‘qS r    )Ústr)r•   r‘   r    r    r!   r–   ì  s    z(check_python_version.<locals>.<listcomp>Ú	PYTHONDIRzget_config_var('LIBDEST') or ''z$get_python_lib(standard_lib=0) or ''z/get_python_lib(standard_lib=0, prefix=%r) or ''r¥   r§   ÚPYTHONARCHDIRz@get_python_lib(plat_specific=1, standard_lib=0, prefix=%r) or ''r´   zChecking for python versionz!Checking for python version >= %sÚGREENÚYELLOW)rV   z+The python version is too old, expecting %r)r   Útupler+   r   rp   r   r   Údebugro   r�   Úlenr   rq   r   rÀ   rm   r   r¶   r|   ÚPREFIXr    r?   r   rÁ   r*   r´   Úmapr¿   r€   )r   Zminverr·   rO   ÚlinesZpyver_tuplerº   ÚpyverZpydirZpython_LIBDESTZ	pyarchdirZ
pyver_fullZ
minver_strr    r    r!   r«   Ì  s\    
 2





"r«   z¡
import %s as current_module
version = getattr(current_module, '__version__', None)
if version is not None:
	print(str(version))
else:
	print('unknown version')
r   c                s  d| }|rd||f }|   |¡ y|  | jjdt| g ¡}W n. tjk
rn   |  d¡ |  d| ¡ Y nX | 	¡ }|râ|  |¡ |dkrœ|  d| ¡ dd	l
m‰  ‡ fd
d„}|ˆ |ƒdœ}t|i |ƒ}|sà|  d| ¡ n|dkrö|  d¡ n
|  |¡ dS )a#  
	Check if the selected python interpreter can import the given python module::

		def configure(conf):
			conf.check_python_module('pygccxml')
			conf.check_python_module('re', condition="ver > num(2, 0, 4) and ver <= num(3, 0, 0)")

	:param module_name: module
	:type module_name: string
	zChecking for python module %rz%s (%s)z-cFz#Could not find the python module %rzunknown versionzCould not check the %s versionr   )ÚLooseVersionc                 s6   t | d tƒr&ˆ d dd„ | D ƒ¡ƒS ˆ | d ƒS d S )Nr   r   c             S   s   g | ]}t |ƒ‘qS r    )r¿   )r•   r‘   r    r    r!   r–   J  s    z4check_python_module.<locals>.num.<locals>.<listcomp>)r   r   rq   )rœ   )rË   r    r!   r¹   H  s    z check_python_module.<locals>.num)r¹   r   z0The %s version does not satisfy the requirementsTN)Ú	start_msgro   r   rp   ÚPYTHON_MODULE_TEMPLATEr   rr   Úend_msgr   ru   Zdistutils.versionrË   rv   )r   Úmodule_nameZ	conditionr€   rP   r¹   ÚdZevr    )rË   r!   Úcheck_python_module+  s.    


rÑ   c             C   sð   | j }ttjddƒrtjj|_ttjddƒr6tjj|_ttjddƒrNtjj|_	|j
slttjddƒpftjg|_
t |j
¡|_
| jddd� d|_d	|_ttjd
dƒ|_ttjddƒ|_y |  | j j
ddg ¡ ¡ |_W n tjk
rê   Y nX dS )z!
	Detect the python interpreter
	Ú	pythondirNÚpythonarchdirÚ	nopycacher¥   rp   )rš   r   z-Or&   r¤   r'   z-cznimport sys
try:
 print(sys.implementation.cache_tag)
except AttributeError:
 import imp
 print(imp.get_tag())
)r   r   r   ÚoptionsrÒ   rÀ   rÓ   rÁ   rÔ   r7   rp   r¯   Ú
executabler   rZ   r®   ZPYFLAGSZPYFLAGS_OPTr/   r1   ro   ru   r6   r   rr   )r   ry   r    r    r!   Ú	configureW  s&    


 r×   c             C   s~   |   d¡}|jdddddd� |jdd	ddd
d� |jddddd� |jdddtj d� |jdddd� |jdddd� dS )z
	Add python-specific options
	zPython Optionsz--nopycr&   Ústore_falser¤   zMDo not install bytecode compiled .pyc files (configuration) [Default:install])ÚdestÚactionÚdefaultÚhelpz--nopyor'   zNDo not install optimised compiled .pyo files (configuration) [Default:install]z--nopycacherÔ   Ú
store_truezBDo not use __pycache__ directory to install objects [Default:auto])rÙ   rÚ   rÜ   z--pythonr¥   z&python binary to be used [Default: %s])rÙ   rÜ   z--pythondirrÒ   zRInstallation path for python modules (py, platform-independent .py and .pyc files)z--pythonarchdirrÓ   zVInstallation path for python extension (pyext, platform-dependent .so or .dylib files)N)Úadd_option_groupÚ
add_optionr¯   rÖ   )ÚoptZpyoptr    r    r!   rÕ   s  s    

rÕ   )N)r}   )rˆ   )rŠ   )rŠ   )N)r   )%rU   r?   r¯   Úwaflibr   r   r   r   r   r   Zwaflib.TaskGenr   r	   r
   r   Zwaflib.Configurer   r…   rK   rj   r"   rE   r&   r'   r]   r`   rc   r|   r‡   r‰   r”   r»   r«   rÍ   rÑ   r×   rÕ   r    r    r    r!   Ú<module>   sJ    2	.( )\+