U
    HZûaÿJ  ã                   @   sþ   d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZmZmZ dZdZdZG dd„ dejƒZd	d
„ Zeddd„ƒZedd„ ƒZed dd„ƒZeg fdd„ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdS )!a$  
Configuration system

A :py:class:`waflib.Configure.ConfigurationContext` instance is created when ``waf configure`` is called, it is used to:

* create data dictionaries (ConfigSet instances)
* store the list of modules to import
* hold configuration routines such as ``find_program``, etc
é    N)Ú	ConfigSetÚUtilsÚOptionsÚLogsÚContextÚBuildÚErrorsz
config.logFz}# project %(app)s configured on %(now)s by
# waf %(wafver)s (abi %(abi)s, python %(pyver)x on %(systype)s)
# using %(args)s
#c                       sŽ   e Zd ZdZdZg Z‡ fdd„Zddd„Zdd	„ Zd
d„ Z	e
ee	ƒZdd„ Z‡ fdd„Zdd„ Zdd„ Zddd„Z‡ fdd„Zdd„ Z‡  ZS )ÚConfigurationContextzconfigures the projectÚ	configurec                    sV   t t| ƒjf |Ž ttjƒ| _i | _d | _d | _g | _	d| _
g | _g | _|  d¡ d S )Nr   Ú )Úsuperr	   Ú__init__ÚdictÚosÚenvironÚall_envsÚtop_dirÚout_dirÚtoolsÚhashÚfilesÚ
tool_cacheÚsetenv)ÚselfÚkw©Ú	__class__© ú+/wcdo/src/waf-tools/waf/waflib/Configure.pyr   '   s    zConfigurationContext.__init__Nc                 C   sB   || j ks|r8|s&t ¡ }|  |¡ n| ¡ }|| j |< || _dS )a–  
		Set a new config set for conf.env. If a config set of that name already exists,
		recall it without modification.

		The name is the filename prefix to save to ``c4che/NAME_cache.py``, and it
		is also used as *variants* by the build commands.
		Though related to variants, whatever kind of data may be stored in the config set::

			def configure(cfg):
				cfg.env.ONE = 1
				cfg.setenv('foo')
				cfg.env.ONE = 2

			def build(bld):
				2 == bld.env_of_name('foo').ONE

		:param name: name of the configuration set
		:type name: string
		:param env: ConfigSet to copy, or an empty ConfigSet is created
		:type env: :py:class:`waflib.ConfigSet.ConfigSet`
		N)r   r   Úprepare_envÚderiveÚvariant)r   ÚnameÚenvr   r   r   r   8   s    
zConfigurationContext.setenvc                 C   s   | j | j S )zGetter for the env property©r   r!   )r   r   r   r   Úget_envW   s    zConfigurationContext.get_envc                 C   s   || j | j< dS )zSetter for the env propertyNr$   )r   Úvalr   r   r   Úset_envZ   s    zConfigurationContext.set_envc                 C   s  | j }|stjj}|s&ttjtjdƒ}|s4| j 	¡ }t
j 	|¡}t
j |¡rR| jpV| j |¡| _| jsjt‚| j}|s|tjj}|s�ttjtjdƒ}|s°tj dtj d¡ dd¡}t
j |¡}t
j |¡rÎ| jpÒ| j |¡| _| j ¡  t
j | j 	¡ ¡�s|  d| j 	¡  ¡ dS )z>
		Initialize the project directory and the build directory
		Nz.lock-waf_%s_r   z	.lock-wafz'Could not create the build directory %s)r   r   ÚoptionsÚtopÚgetattrr   Úg_moduleZTOPÚpathÚabspathr   ÚisabsÚrootZfind_dirÚsrcnodeÚAssertionErrorr   ÚoutZOUTÚlockfileÚreplaceÚsysÚplatformÚrealpathÚ	make_nodeÚbldnodeÚmkdirÚisdirÚfatal)r   r)   r2   r   r   r   Ú	init_dirs`   s,    
 
 
zConfigurationContext.init_dirsc                    sh  |   ¡  | j tj¡| _| j ¡  tj 	| j 
¡ t¡}t |d¡| _ttjddƒ}|rtttjddƒ}|rtd||f }t ¡ tjtjd 	tj¡tjtj|dœ}|  t| ¡ |  d| j 
¡ ¡ |  d	| j 
¡ ¡ t| jƒt| jƒkrît d
¡ n0t| jƒt| jƒk�r| j  | j¡�rt d¡ t!t"| ƒ #¡  |  $¡  | j 
¡ t_%| j 
¡ t_&t' '¡ }tj|_t(j)j*|_)| j+|_,tj-|_-tj%|_%tj&|_&| j.|_.| j/|_/t0| j1ƒ|_1tj2|_2| j3j4�sè|j1 5d¡�sètt(j)dƒ�sè| $tj 	tj-t(j6¡¡ | j3j7�s&|j1 5d¡�s&tt(j)dƒ�s&| $tj 	tj%t(j6¡¡ | j3j8�sd|j1 5d¡�sdtt(j)dƒ�sd| $tj 	tj&t(j6¡¡ dS )z3
		See :py:func:`waflib.Context.Context.execute`
		ÚcfgÚAPPNAMEr   ÚVERSIONz%s (%s)ú )ÚnowZpyverZsystypeÚargsZwafverZabiÚappzSetting top tozSetting out tozSetting top == outz5Are you certain that you do not want to set top="." ?ÚNO_LOCK_IN_RUNÚno_lock_in_runÚNO_LOCK_IN_TOPÚno_lock_in_topÚNO_LOCK_IN_OUTZno_lock_in_outN)9r=   r9   r8   r   Ú	CACHE_DIRÚcachedirr:   r   r,   Újoinr-   ÚWAF_CONFIG_LOGr   Zmake_loggerÚloggerr*   r   r+   ÚtimeÚctimer5   Ú
hexversionr6   ÚargvÚ
WAFVERSIONZABIÚto_logÚconf_templateÚmsgr0   ÚidÚwarnZis_child_ofr   r	   ÚexecuteÚstorer   r   r   r   r(   Ú__dict__ÚcmdÚ
config_cmdÚrun_dirr   r   r   r   Ú
launch_dirr#   rE   Úgetr3   rG   rI   )r   r,   rD   ÚverÚparamsr#   r   r   r   rY   ‚   sP    
(

&&&zConfigurationContext.executec                 C   s‚   |j s&tjjstjr tjj|_ nd|_ |jsNtjjr@tjj|_nt d|¡|_|j	s~tjj
rhtjj
|_	nt dt ¡  |¡|_	dS )z¥
		Insert *PREFIX*, *BINDIR* and *LIBDIR* values into ``env``

		:type env: :py:class:`waflib.ConfigSet.ConfigSet`
		:param env: a ConfigSet, usually ``conf.env``
		ú/z${PREFIX}/binz${PREFIX}/lib%sN)ZPREFIXr   r(   Úprefixr   Úis_win32ZBINDIRZbindirÚ
subst_varsZLIBDIRZlibdirÚlib64)r   r#   r   r   r   r   À   s    z ConfigurationContext.prepare_envc                 C   sl   | j  d¡}| dtj| jf ¡ | js2|  d¡ | jD ].}| j| }| t	j
 | j  ¡ |tj ¡¡ q8dS )z+Save the config results into the cache filezbuild.config.pyzversion = 0x%x
tools = %r
z.nothing to store in the configuration context!N)rK   r8   Úwriter   Ú
HEXVERSIONr   r   r<   rZ   r   r,   rL   r-   r   ZCACHE_SUFFIX)r   ÚnÚkeyZtmpenvr   r   r   rZ   ×   s    


zConfigurationContext.storeTFc                 C   sh  t  |¡}|rt  |¡}|D �]D}|r^|t| jƒ||f}|| jkrR|  d| ¡ q| j |¡ d}	ztj||| |d�}	W n„ t	k
r¸ }
 z"|  
d|t|
dtjƒ|
f ¡ W 5 d}
~
X Y nF tk
rü }
 z(|  d|||f ¡ |  t ¡ ¡ ‚ W 5 d}
~
X Y nX |dk	�r|  |¡ n:t|	ddƒ}|�rNt|ƒtt jƒk�rD|| ƒ n
|  |¡ | j |||dœ¡ qdS )	aƒ  
		Load Waf tools, which will be imported whenever a build is started.

		:param tool_list: waf tools to import
		:type tool_list: list of string
		:param tooldir: paths for the imports
		:type tooldir: list of string
		:param funs: functions to execute from the waf tools
		:type funs: list of string
		:param cache: whether to prevent the tool from running twice
		:type cache: bool
		z%(tool %s is already loaded, skipping)N)ÚctxÚwith_sys_pathz)Could not load the Waf tool %r from %r
%sZwaf_sys_pathzimp %r (%r & %r)r
   )ÚtoolÚtooldirÚfuns)r   Úto_listrW   r#   r   rT   Úappendr   Z	load_toolÚImportErrorr<   r*   r5   r,   Ú	ExceptionÚ	tracebackÚ
format_excÚ
eval_rulesÚtypeÚreadfr   )r   Z	tool_listro   rp   rm   Úcacher   rn   ZmagÚmoduleÚeÚfuncr   r   r   Úloadã   s6    



.


zConfigurationContext.loadc                    s<   t t| ƒ |¡ t | j| d¡f¡| _| j | 	¡ ¡ dS )z®
		Records the path and a hash of the scripts visited, see :py:meth:`waflib.Context.Context.post_recurse`

		:param node: script
		:type node: :py:class:`waflib.Node.Node`
		ÚrbN)
r   r	   Úpost_recurser   Úh_listr   Úreadr   rr   r-   )r   Únoder   r   r   r€     s    z!ConfigurationContext.post_recursec                 C   s>   t  |¡| _| jD ]&}t| |ƒ}|s2|  d| ¡ |ƒ  qdS )z˜
		Execute configuration tests provided as list of functions to run

		:param rules: list of configuration method names
		:type rules: list of string
		z!No such configuration function %rN)r   rq   Úrulesr*   r<   )r   r„   ÚxÚfr   r   r   rw      s    

zConfigurationContext.eval_rules)N)NNTF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r\   Zerror_handlersr   r   r%   r'   Úpropertyr#   r=   rY   r   rZ   r~   r€   rw   Ú__classcell__r   r   r   r   r	      s   

">
2r	   c                    s6   ‡ fdd„}ˆ j |_ ttˆ j |ƒ ttjˆ j |ƒ ˆ S )ah  
	Decorator: attach new configuration functions to :py:class:`waflib.Build.BuildContext` and
	:py:class:`waflib.Configure.ConfigurationContext`. The methods bound will accept a parameter
	named 'mandatory' to disable the configuration errors::

		def configure(conf):
			conf.find_program('abc', mandatory=False)

	:param f: method to bind
	:type f: function
	c                     s:   |  dd¡}zˆ | |ŽW S  tjk
r4   |r0‚ Y nX d S )NÚ	mandatoryT)Úpopr   ÚConfigurationError)Úkr   r�   ©r†   r   r   Úfun:  s    zconf.<locals>.fun)r‡   Úsetattrr	   r   ÚBuildContext)r†   r’   r   r‘   r   Úconf.  s
    r•   c                 C   sj   zt  | j| ¡}W n tk
r*   Y dS X |sTd |¡d t | j|pJ| ¡¡krf| j |p`||¡ dS )a@  
	Import operating system environment values into ``conf.env`` dict::

		def configure(conf):
			conf.add_os_flags('CFLAGS')

	:param var: variable to use
	:type var: string
	:param dest: destination variable, by default the same as var
	:type dest: string
	:param dup: add the same set of flags again
	:type dup: bool
	Nr   )	ÚshlexÚsplitr   ÚKeyErrorrL   r   rq   r#   Zappend_value)r   ÚvarÚdestÚdupÚflagsr   r   r   Úadd_os_flagsG  s    (r�   c                 C   sd   t |tƒr`tj |¡r|gS tjdkr0t |¡S ztj|dd�W S  tk
r^   t |¡ Y S X |S )z˜
	Detect if a command is written in pseudo shell like ``ccache g++`` and return a list.

	:param cmd: command
	:type cmd: a string or a list of string
	rc   F)Úposix)	Ú
isinstanceÚstrr   r,   ÚisfileÚsepr–   r—   Ú	TypeError)r   r\   r   r   r   Úcmd_to_list]  s    


r¤   ú1.9.99ú2.1.0c                 K   s€   | j dt|ƒt|ƒf f|Ž tj}t |¡|krJ|  dt |¡|f ¡ t |¡|k rp|  dt |¡|f ¡ | jd|Ž dS )a.  
	Raise a Configuration error if the Waf version does not strictly match the given bounds::

		conf.check_waf_version(mini='1.9.99', maxi='2.1.0')

	:type  mini: number, tuple or string
	:param mini: Minimum required version
	:type  maxi: number, tuple or string
	:param maxi: Maximum allowed version
	z!Checking for waf version in %s-%sz,waf version should be at least %r (%r found)z+waf version should be at most %r (%r found)ÚokN)r§   )Ú	start_msgr    r   ri   r   Únum2verr<   Úend_msg)r   ZminiZmaxir   ra   r   r   r   Úcheck_waf_versions  s    r«   c                 C   s`   t  |¡D ]B}t  |¡D ]2}tj tj ||¡¡}tj |¡r|    S qq
|  d| ¡ dS )zÕ
	Find a file in a list of paths

	:param filename: name of the file to search for
	:param path_list: list of directories to search
	:return: the first matching filename; else a configuration exception is raised
	zCould not find %rN)r   rq   r   r,   Ú
expanduserrL   Úexistsr<   )r   ÚfilenameÚ	path_listrj   ÚdÚpr   r   r   Ú	find_file‡  s    	r²   c                 K   s,  |  dtjrdpd¡}|  dt| dtjƒ¡}d}t |¡}|  dd |¡¡}|  dd¡}|srt 	d	d
|d  
¡ ¡}|  dd¡}|rŽt |¡}n|  dd¡ tj¡}|  d¡r¼|  |d ¡}n–|  |¡rÖ|  || ¡}n|| j| rò|  | j| ¡}n`|�s|  || d¡|¡}|�s*tj�r*t tjj|¡}|�sHtj�rHt tjj|¡}|  |¡}|�rvt|ƒdk�rp|d }	n|}	nd}	| jd| |	f|Ž |  d¡�s°|  d||||f ¡ |�sÒ|  |  dd¡�pÎd| ¡ |  d¡}
|
dk�rtj|d |d��s|  d| ¡ || j|< n| j|
 | | j|< |S )a…  
	Search for a program on the operating system

	When var is used, you may set os.environ[var] to help find a specific program version, for example::

		$ CC='ccache gcc' waf configure

	:param path_list: paths to use for searching
	:type param_list: list of string
	:param var: store the result to conf.env[var] where var defaults to filename.upper() if not provided; the result is stored as a list of strings
	:type var: string
	:param value: obtain the program from the value passed exclusively
	:type value: list or string (list is preferred)
	:param exts: list of extensions for the binary (do not add an extension for portability)
	:type exts: list of string
	:param msg: name to display in the log, by default filename is used
	:type msg: string
	:param interpreter: interpreter for the program
	:type interpreter: ConfigSet variable key
	:raises: :py:class:`waflib.Errors.ConfigurationError`
	Úextsz.exe,.com,.bat,.cmdz,.sh,.pl,.pyr   r   rV   z, r™   z[-.]Ú_r   r¯   ÚPATHÚvalueú,é   FzChecking for program %rÚquietz%find program=%r paths=%r var=%r -> %rÚerrmsgzCould not find the program %rÚinterpreterN)r#   zProgram %r is not executable)r`   r   re   r*   r   r   rq   rL   ÚreÚsubÚupperr—   Úpathsepr¤   r#   Úfind_binaryÚwinregÚget_registry_app_pathÚHKEY_CURRENT_USERÚHKEY_LOCAL_MACHINEÚlenrV   rT   r<   Ú	check_exe)r   r®   r   r³   r   ÚretrV   r™   r¯   Zretmsgr»   r   r   r   Úfind_program—  sT    







rÈ   c           	   	   C   s‚   |D ]x}|D ]n}|| }t j |¡r>t j |¡rz|    S q|D ]6}t j t j ||¡¡}t j |¡rB|      S qBqqd S ©N)r   r,   r.   r¡   r¬   rL   )	r   Ú	filenamesr³   Úpathsr†   ÚextZexe_namer,   r…   r   r   r   rÀ   í  s    rÀ   c                 O   s®  g }t | ¡ ƒD ]F}|| }t|tjƒr,qqt|dƒrH| t |¡¡ q| t|ƒ¡ qt 	|¡}| j
 ¡ tj tj r|dp~d d t |¡ }| dttjddƒ¡}|sÀtj |¡rÀt |¡ zt |¡ W n tk
râ   Y nX zt |¡ W n$ tk
�r   |  d| ¡ Y nX |dk�r€zt tj |d	¡¡}	W n tk
�rP   Y n0X |	d	 }
t|
tƒ�r||
 d
¡�r||  |
¡ |
S tj |d¡}tj |¡�s¦t |¡ | d¡�p¼t| ddƒ}tj |||d� | _!}| "¡  d|_#d|_$| j%|_%|j& '| j&¡ |d |_(||_)| |_*|d |ƒ d}
zLz| ,¡  W n. t-j.k
�r`   dt/ 0¡  }
|  |
¡ Y nX t|ddƒ}
W 5 |�ržt ¡ }	|
|	d	< |	 +tj |d	¡¡ n
t |¡ X |
S )aq  
	Create a temporary build context to execute a build. A temporary reference to that build
	context is kept on self.test_bld for debugging purposes.
	The arguments to this function are passed to a single task generator for that build.
	Only three parameters are mandatory:

	:param features: features to pass to a task generator created in the build
	:type features: list of string
	:param compile_filename: file to create for the compilation (default: *test.c*)
	:type compile_filename: string
	:param code: input file contents
	:type code: string

	Though this function returns *0* by default, the build may bind attribute named *retval* on the
	build context object to return a particular value. See :py:func:`waflib.Tools.c_config.test_exec_fun` for example.

	The temporary builds creates a temporary folder; the name of that folder is calculated
	by hashing input arguments to this function, with the exception of :py:class:`waflib.ConfigSet.ConfigSet`
	objects which are used for both reading and writing values.

	This function also features a cache which is disabled by default; that cache relies
	on the hash value calculated as indicated above::

		def options(opt):
			opt.add_option('--confcache', dest='confcache', default=0,
				action='count', help='Use a configuration cache')

	And execute the configuration with the following command-line::

		$ waf configure --confcache

	Ú__call__Ú.r   Zconf_check_Z	confcacheNz+cannot use the configuration test folder %rr¸   Zcache_run_buildzTest does not buildZ	testbuildZrun_build_clsÚbuild)r   r   r   Ú*r#   Z	build_funéÿÿÿÿzTest does not build: %sZretval)1ÚsortedÚkeysrŸ   r   Úhasattrrr   r   Úh_funr    r�   r9   r-   r   r¢   re   Úto_hexr`   r*   r   r(   r,   r­   ÚshutilÚrmtreeÚmakedirsÚOSErrorÚstatr<   rL   ÚEnvironmentErrorÚ
startswithr   Úcreate_contextZtest_bldr=   Zprogress_barZtargetsrN   r   Úupdater#   r   r•   rZ   Úcompiler   ÚWafErrorru   rv   )r   r�   r   Úbufrk   ÚvÚhÚdirZ	cachemodeZprojrÇ   ZbdirZcls_nameZbldr   r   r   Ú	run_buildü  st    "

.




ræ   c                 C   s   t |tƒr|S ||ƒS rÉ   )rŸ   r    )r   rV   rC   r   r   r   Úret_msgj  s    
rç   c                 O   sü   d|kr| j  ¡ |d< | d¡r,|d |ƒ | j|d f|Ž d }z| j||Ž}W nD | jjk
r–   | j|d df|Ž tj	dkrˆ‚ n
|  
d¡ Y n
X ||d< | d	¡r¶|d	 |ƒ}|rÞ| j|d df|Ž |  
d
| ¡ n| j|  |d |¡f|Ž |S )Nr#   ÚvalidaterV   rº   ZYELLOWr¸   zThe configuration failedZsuccessZ
post_checkzThe configuration failed %rZokmsg)r#   r    r`   r¨   ræ   Úerrorsr�   rª   r   Úverboser<   rç   )r   r�   r   rÇ   r   r   r   Útestp  s*    


rë   )NF)r¥   r¦   )rŠ   r   r¼   r–   r×   r5   rO   ru   Úwaflibr   r   r   r   r   r   r   rM   Ú
autoconfigrU   r	   r•   r�   r¤   r«   r²   rÈ   rÀ   ræ   rç   rë   r   r   r   r   Ú<module>   s6   
8$  

U

m
