3
^Ã_^ 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   st   t | ddƒ| _t | ddƒ}|r8t|tjƒ r8| jj|ƒ}|| _| jj}|sV| j	j
dƒ t|jddƒƒdkrpd	| _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    ú>/home/yuhw/wc/wct-package/waf-tools/waf/waflib/Tools/python.pyÚ
feature_py:   s    r"   z.pyc       
      C   sú  t | dƒs"td| j| jjƒ f ƒ‚t| ddƒ| _t| ddƒ}| jrpt| jtjƒsptd| j| jjƒ t	| jƒf ƒ‚| j
r¦| jr”| j| j
|| j|d� n| j| j
||d	� g }| jjr¼|jd
ƒ | jjrÎ|jdƒ | j
�r@| j�r|rî|j| jƒn|j}tjd| j
|f | jƒ}n0|�r |j| jƒn|j}tjd| j
|f | jƒ}n|jƒ }x¬|D ]¤}| jj�r¦| jj �r¦|jdd… }|jjƒ jdƒjd|| jj|f ƒ}|jjƒ  n|jd| ƒ}| j|||ƒ}	||	_| j
�rN| jtjj|ƒ||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?   Úextr4   ZpyobjÚtskr    r    r!   Ú
process_pyM   s@    "


$rF   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|jjƒ ƒS )Nr   )Úoutputsr3   ÚctxÚlaunch_node)r   rB   r    r    r!   Ú__str__„   s    
zpyc.__str__c             C   sB   t jd| jƒdt| jd jƒ | jd jƒ | jg}| jj	j
|ƒ}|S )Nz	${PYTHON}z-cr   )r   r5   r   ÚINSTÚinputsr6   rH   r?   Ú	generatorr   Úexec_command)r   ÚcmdÚretr    r    r!   Úrun‡   s    0zpyc.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚcolorrK   rR   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
	rG   c             C   s   | j d }|j|jjƒ ƒS )Nr   )rH   r3   rI   rJ   )r   rB   r    r    r!   rK   ‘   s    
zpyo.__str__c             C   sN   t jd| jƒt jd| jƒdt| jd jƒ | jd jƒ | jg}| jj	j
|ƒ}|S )Nz	${PYTHON}z${PYFLAGS_OPT}z-cr   )r   r5   r   rL   rM   r6   rH   r?   rN   r   rO   )r   rP   rQ   r    r    r!   rR   ”   s    <zpyo.runN)rS   rT   rU   rV   rW   rK   rR   r    r    r    r!   r'   Œ   s   r'   ÚpyextZpropagate_uselib_varsZ
apply_linkZapply_bundlec             C   sˆ   | j t| dg ƒƒ| _d| jkr*| j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   rY   r1   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 jƒ 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    ra   Úpyembedc             C   s.   | j t| dg ƒƒ| _d| jkr*| jjdƒ dS )z
	Add the PYEMBED variable.
	rY   ÚPYEMBEDN)r[   r   rY   r1   )r   r    r    r!   Úinit_pyembedµ   s    
rd   c       	      C   sv  |s(y
| j }W n tk
r&   t}Y nX t|ƒ}|jdƒ x|D ]}|jd| ƒ q@W ttjƒ}y
|d= W n tk
r~   Y nX y$| j	| j
jddj|ƒg |d�}W n  tjk
rÄ   | jdƒ Y nX | j|ƒ g }xœ|jƒ D ]�}|jƒ }|sðqÞ|dk�r|jd	ƒ qÞ|d
 dk�r"|d dk�s>|d
 dk�rN|d dk�rN|jt|ƒƒ qÞ|d
 jƒ �rl|j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   ú'é   ú"éÿÿÿÿrk   )Zpython_importsr]   ÚDISTUTILS_IMPÚlistr1   Údictr@   ÚenvironÚKeyErrorÚcmd_and_logr   ÚPYTHONÚjoinr   ÚWafErrorr   Úto_logÚ
splitlinesÚstripÚevalÚisdigitr   )	r   Ú	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 j|ƒ}d| jkp&d| jkp&d| jks,dS x:djƒ D ].}|| jkrT| jd| ƒ q6| j| | j|< q6W | jjrtdpvd}d	|krÀ| jjd| jjdƒƒ}|d
krª| jdƒ n| j|dƒ | j	|ƒ d|k�r
| jjd| jjdƒƒ}|d
krô| jdƒ n| j|dƒ | j
|ƒ 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ÚcrX   Nz:No flags provided through PYTHON_PYEXT_LDFLAGS as requiredrZ   rb   z<No flags provided through PYTHON_PYEMBED_LDFLAGS as requiredrc   T)r   r[   ro   Úsplitr   r   ÚCXX_NAMEÚgetÚparse_flagsrŒ   rŠ   )r   r†   ÚxÚxxÚflagsr    r    r!   Úpython_cross_compileú   s*    




r—   c              C   s  t j|ƒ}d|ks"d|ks"tdƒ‚| j}|j rB|j rB| jdƒ | j|ƒrPdS |js^| j	ƒ  |j
}|sr| jdƒ djƒ }y| jdd	„ |D ƒƒ}W n tk
r°   | jd
ƒ Y nX dd	„ t||ƒD ƒ}| jd|dj|ƒf ƒ tt||ƒƒ}d}|| �r||  ||< | j|< d|d  |_dj|jjdƒdd… ƒ}	| jdj|ƒd d|	 d|	 d|	 gdddd� |j�rD| jj�r~d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|ƒ |jdd'|d(� W n: | jjk
�r6   | jd%d&j|ƒ |jdd'|d(� Y nX �qÊW y| j|ƒ W nN | jjk
�rš   |d) �r”|d* �r”|jd+|d* gƒ | j|ƒ n‚ Y nX d|k�r4x.|
D ]&}| jd,d&j|ƒ |jdd-|d(� �q¬W y| j|ƒ W nN | jjk
�r2   |d) �r,|d* �r,|jd.|d* gƒ | j|ƒ n‚ Y nX | jd/d0ƒ dS |d1 d& |d2  }
| j |
d'ƒ |d1 d& |d3  d& |d2  }
| j |
d-ƒ d}|d4 �s¨|j|d4< �x6d5|d4  d5|j d6 d5|jj!ddƒ fD �]}| �r|j"�r|j"}| jd7| ƒ | j#|d'|dd8| d9�}| �rZ|d* �rZ|d* g}| jd:| ƒ | j#|d'|dd;| d9�}| �r˜|d< �r˜|d< g}| jd=ƒ | j#|d'|dd>| d9�}|�sÔt$j%j|d? d@ƒg}| jdAƒ | j#|d'|ddB| d9�}|�rÖP �qÖW |�rþ||_"|j&dC|gƒ n
| jdDƒ t j'�s|d) �r*|j"|_(|j)|_*| jdE|dF f ƒ |dF g|_+|dF g|_,|jdGk�r~|jdHdIgƒ |jdJdIgƒ |jdGk�r¦|jdKdIgƒ |jdLdIgƒ |jdMk�röd!dNl-m.} |ƒ }|j/ƒ  |j&dJ|j0ƒ |j&dL|j0ƒ |j&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
	rb   rX   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
rf   re   z%sÚ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ú rc   )rƒ   r   ÚpackageZuselib_storeÚargsÚPy_ENABLE_SHAREDÚLIBDIRÚLIBPATH_PYEMBEDz'Asking python-config for pyext %r flagsrZ   ÚLIBPATH_PYEXTÚHAVE_PYTHON_Hri   ÚLDFLAGSÚCFLAGSÚLDSHAREDÚ	LDVERSIONÚpythonÚmz'

# Trying default LIBPATH_PYEMBED: %r
z*Checking for library %s in LIBPATH_PYEMBED)ÚlibrY   ZlibpathrŸ   rƒ   z(

# try again with -L$python_LIBDIR: %r
z!Checking for library %s in LIBDIRÚ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
Ú	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‚   rY   r„   r…   )3r   r[   r,   r   ZCC_NAMEr‘   r   r—   r   Úcheck_python_versionrr   r�   r   ÚRuntimeErrorÚzipru   rs   rn   ro   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   Úpybinr|   rC   ÚvalsÚdctr”   ÚnumZ	all_flagsr•   r–   Z
embedflagsÚresultr4   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,| jdƒ |ddg }tjd|ƒ | j|ƒj	ƒ }t
|ƒdksrtdt
|ƒ|f ƒ‚t|d ƒt|d	 ƒt|d
 ƒ|d t|d ƒf}|dkp²||k}|�rdjdd„ |dd
… D ƒƒ}|| j_d| jkrò| jj}n’d| jk�r
| jd }nztj�r&| jddgƒ\}	}nd}	| jd| jj gƒ\}|	dk�r„| jj�rltjj| jjd| ƒ}	ntjj| jjdd| ƒ}	d| jk�rš| jj}
n8d| jk�r²| jd }
n | jd| jj gƒ\}
|
�sÒ|}
t| dƒ�rö| jd|ƒ | jd|
ƒ || j_|
| j_djtt|dd… ƒƒ}|dk�r6| jd|ƒ n2djtt|ƒƒ}| jd|f ||�r`d�pbdd� |�s~| j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   ri   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)rW   z+The python version is too old, expecting %r)r   Útupler,   r   rr   r   r   Údebugrq   r�   Úlenr   rs   r   rÊ   ro   r   rÀ   r   ÚPREFIXr¥   r@   r   rË   r+   r¾   ÚmaprÉ   rƒ   )r   ZminverrÁ   rP   Ú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 }| j |ƒ y| j| jjdt| g ƒ}W n. tjk
rn   | jdƒ | jd| ƒ Y nX |j	ƒ }|râ| j|ƒ |dkrœ| jd| ƒ dd	l
m‰  ‡ fd
d„}|ˆ |ƒdœ}t|i |ƒ}|sà| jd| ƒ n|dkrö| jdƒ n
| j|ƒ 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j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   rs   )r    )rÕ   r    r!   rÄ   H  s    z check_python_module.<locals>.num)rÄ   r   z0The %s version does not satisfy the requirementsTN)Ú	start_msgrq   r   rr   ÚPYTHON_MODULE_TEMPLATEr   rt   Úend_msgr   rw   Zdistutils.versionrÕ   rx   )r   Úmodule_nameZ	conditionrƒ   rQ   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
ƒ|_
| jddd� d|_d	|_ttjd
dƒ|_ttjddƒ|_y | j| j j
ddg ƒjƒ |_W n tjk
rê   Y nX dS )z!
	Detect the python interpreter
	Ú	pythondirNÚpythonarchdirÚ	nopycacher­   rr   )rž   r   z-Or&   ri   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Þ   r8   rr   r¹   Ú
executabler   r[   r¸   ZPYFLAGSZPYFLAGS_OPTr0   r2   rq   rw   r7   r   rt   )r   r|   r    r    r!   Ú	configureW  s&    


 rá   c             C   s~   | j 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_falseri   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   )%rV   r@   r¹   Úwaflibr   r   r   r   r   r   Zwaflib.TaskGenr   r	   r
   r   Zwaflib.Configurer   rˆ   rL   rl   r"   rF   r&   r'   r^   ra   rd   r   rŠ   rŒ   r—   rÆ   rµ   r×   rÛ   rá   rß   r    r    r    r!   Ú<module>   sJ    2	.( )\+