U
    HZûa 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| ¡ }|D ]¢}| jj�r¢| jj�s¢|jdd… }|j ¡  d¡ d|| jj|f ¡}|j ¡  n| d| ¡}|  |||¡}	||	_| j
�rL| jtj |¡||j ¡ |d� �qLd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   sB    "ÿ

$rE   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )r&   ú
	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 )Nú	${PYTHON}ú-cr   ©r   r4   r   ÚINSTÚinputsr5   rJ   r>   Ú	generatorr   Úexec_command©r   ÚcmdÚretr    r    r!   Úrun‡   s    0zpyc.runN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚcolorrN   rY   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'   rF   rG   c                 C   s   | j d }| |j ¡ ¡S rH   rI   rM   r    r    r!   rN   ‘   s    
zpyo.__str__c                 C   sN   t  d| j¡t  d| j¡dt| jd  ¡ | jd  ¡ | jg}| jj	 
|¡}|S )NrO   z${PYFLAGS_OPT}rP   r   rQ   rV   r    r    r!   rY   ”   s    <zpyo.runNrZ   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_
z| jshW d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   ra   r0   r   Úpyext_PATTERNZcshlib_PATTERNZcxxshlib_PATTERNZmacbundle_PATTERNZfcshlib_PATTERNZdshlib_PATTERNr   ÚAttributeError©r   r    r    r!   Ú
init_pyext™   s    

rg   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_bundlerf   r    r    r!   Ú
set_bundle®   s    rj   Úpyembedc                 C   s.   |   t| dg ƒ¡| _d| jkr*| j d¡ dS )z
	Add the PYEMBED variable.
	ra   ÚPYEMBEDN)rc   r   ra   r0   rf   r    r    r!   Úinit_pyembedµ   s    
rm   c           	      C   sp  |s(z
| j }W n tk
r&   t}Y nX t|ƒ}| d¡ |D ]}| d| ¡ q>ttjƒ}z
|d= W n tk
rz   Y nX z$| j	| j
jdd |¡g |d�}W n  tjk
rÀ   |  d¡ Y nX |  |¡ g }| ¡ D ]’}| ¡ }|sêqØ|dkrþ| d	¡ qØ|d
 dk�r|d dk�s6|d
 dk�rF|d dk�rF| t|ƒ¡ qØ|d
  ¡ �rd| t|ƒ¡ qØ �qlqØ|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_TARGETrP   Ú
)r   z9The distutils module is unusable: install "python-devel"?ÚNoneNr   ú'éÿÿÿÿú")Zpython_importsre   Ú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 )Nú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 )Nrˆ   Z
HAVE_PYEXTz!Could not build python extensionsz%s %sshlib pyextr‰   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 d ¡ D ].}|| jkrR|  d| ¡ q4| j| | j|< q4| jjrpdprd}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Úcr`   Nz:No flags provided through PYTHON_PYEXT_LDFLAGS as requiredrb   rk   z<No flags provided through PYTHON_PYEMBED_LDFLAGS as requiredrl   T)r   rc   rw   Ú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 ¡ }z|  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�r8| 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”|
D ]p}|d$g }z$| jd%d& |¡ |jdd'|d(� W n: | jjk
�r0   | jd%d& |¡ |jdd'|d(� Y nX �qÄz|  |¡ W nN | jjk
�r’   |d) �rŒ|d* �rŒ| d+|d* g¡ |  |¡ n‚ Y nX d|k�r(|
D ]&}| jd,d& |¡ |jdd-|d(� �q¢z|  |¡ 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< d5|d4  d5|j d6 d5|j !dd¡ fD �]}|�s|j"�r|j"}|  d7| ¡ | j#|d'|dd8| d9�}|�sF|d* �rF|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Æ �qÎ�qÆ|�rê||_"| &dC|g¡ n
|  dD¡ t j'�s|d) �r|j"|_(|j)|_*|  dE|dF f ¡ |dF g|_+|dF g|_,|jdGk�rj| 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
	rk   r`   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
ro   rn   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ú rl   )rŒ   r   ÚpackageZuselib_storeÚargsZPy_ENABLE_SHAREDÚLIBDIRÚLIBPATH_PYEMBEDz'Asking python-config for pyext %r flagsrb   Ú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)Úlibra   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_PYEXTrˆ   zKDistutils not installed? Broken python installation? Get python-config now!)rŠ   r‹   ra   r�   rŽ   )3r   rc   r+   r   ZCC_NAMErœ   r   r¢   r   Úcheck_python_versionrz   r›   r†   ÚRuntimeErrorÚzipr}   r{   rv   rw   rd   Ú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   Úpybinrƒ   rB   ZvalsÚdctrŸ   ÚnumZ	all_flagsr    r¡   Z
embedflagsÚresultr3   r   r¹   Zdist_compilerr    r    r!   Úcheck_python_headers#  sÚ    	




0


$*
$ 

0





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 executablerP   z3import 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    ©Ústrr£   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)r_   z+The python version is too old, expecting %r)r   Útupler+   r   rz   r   r   Údebugry   r›   Úlenr   r{   r   rÐ   rw   r   rÅ   r†   ÚPREFIXr¯   r?   r   rÑ   r*   rÃ   ÚmaprÏ   rŒ   )r   ZminverrÆ   rW   Ú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 }|   |¡ z|  | 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)rP   Fz#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¥   J  s     z4check_python_module.<locals>.num.<locals>.<listcomp>)r   r   r{   )r«   rÛ   r    r!   rÈ   H  s    z check_python_module.<locals>.num)rÈ   r   z0The %s version does not satisfy the requirementsTN)Ú	start_msgry   r   rz   ÚPYTHON_MODULE_TEMPLATEr   r|   Úend_msgr   r   Zdistutils.versionrÜ   r€   )r   Úmodule_nameZ	conditionrŒ   rX   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ƒ|_z |  | j j
ddg ¡ ¡ |_W n tjk
rê   Y nX dS )z!
	Detect the python interpreter
	Ú	pythondirNÚpythonarchdirÚ	nopycacher´   rz   )r©   r   z-Or&   r³   r'   rP   znimport sys
try:
 print(sys.implementation.cache_tag)
except AttributeError:
 import imp
 print(imp.get_tag())
)r   r   r   Úoptionsrã   rÐ   rä   rÑ   rå   r7   rz   r¾   Ú
executabler   rc   r½   ZPYFLAGSZPYFLAGS_OPTr/   r1   ry   r   r6   r   r|   )r   rƒ   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   )%r^   r?   r¾   Úwaflibr   r   r   r   r   r   Zwaflib.TaskGenr   r	   r
   r   Zwaflib.Configurer   r’   rR   rt   r"   rE   r&   r'   rg   rj   rm   r†   r•   r—   r¢   rÊ   rº   rÞ   râ   rè   ræ   r    r    r    r!   Ú<module>   sT    
1.( )U	+