#! /usr/bin/env python
# encoding: utf-8
# WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file

import os,re,threading
from waflib import Task,Logs,Utils,Errors
from waflib.Tools import c_preproc
from waflib.TaskGen import before_method,feature
lock=threading.Lock()
preprocessor_flag='-MD'
supported_compilers=['gcc','icc']
@feature('c')
@before_method('process_source')
def add_mmd_cc(self):
	if self.env.CC_NAME in supported_compilers and self.env.get_flat('CFLAGS').find(preprocessor_flag)<0:
		self.env.append_value('CFLAGS',[preprocessor_flag])
@feature('cxx')
@before_method('process_source')
def add_mmd_cxx(self):
	if self.env.CXX_NAME in supported_compilers and self.env.get_flat('CXXFLAGS').find(preprocessor_flag)<0:
		self.env.append_value('CXXFLAGS',[preprocessor_flag])
def scan(self):
	if self.env.CC_NAME not in supported_compilers:
		return self.no_gccdeps_scan()
	nodes=self.generator.bld.node_deps.get(self.uid(),[])
	names=[]
	return(nodes,names)
re_o=re.compile("\.o$")
re_splitter=re.compile(r'(?<!\\)\s+')
def remove_makefile_rule_lhs(line):
	rulesep=': '
	sep_idx=line.find(rulesep)
	if sep_idx>=0:
		return line[sep_idx+2:]
	else:
		return line
def path_to_node(base_node,path,cached_nodes):
	if getattr(path,'__hash__'):
		node_lookup_key=(base_node,path)
	else:
		node_lookup_key=(base_node,os.path.sep.join(path))
	try:
		lock.acquire()
		node=cached_nodes[node_lookup_key]
	except KeyError:
		node=base_node.find_resource(path)
		cached_nodes[node_lookup_key]=node
	finally:
		lock.release()
	return node
def post_run(self):
	if self.env.CC_NAME not in supported_compilers:
		return self.no_gccdeps_post_run()
	if getattr(self,'cached',None):
		return Task.Task.post_run(self)
	name=self.outputs[0].abspath()
	name=re_o.sub('.d',name)
	txt=Utils.readf(name)
	txt='\n'.join([remove_makefile_rule_lhs(line)for line in txt.splitlines()])
	txt=txt.replace('\\\n','')
	val=txt.strip()
	lst=val.split(':')
	val=[x.replace('\\ ',' ')for x in re_splitter.split(val)if x]
	nodes=[]
	bld=self.generator.bld
	try:
		cached_nodes=bld.cached_nodes
	except AttributeError:
		cached_nodes=bld.cached_nodes={}
	for x in val:
		node=None
		if os.path.isabs(x):
			node=path_to_node(bld.root,x,cached_nodes)
		else:
			path=bld.bldnode
			x=[k for k in Utils.split_path(x)if k and k!='.']
			while lst and x[0]=='..':
				x=x[1:]
				path=path.parent
			node=path_to_node(path,x,cached_nodes)
		if not node:
			raise ValueError('could not find %r for %r'%(x,self))
		else:
			if not c_preproc.go_absolute:
				if not(node.is_child_of(bld.srcnode)or node.is_child_of(bld.bldnode)):
					continue
			if id(node)==id(self.inputs[0]):
				continue
			nodes.append(node)
	Logs.debug('deps: real scanner for %s returned %s'%(str(self),str(nodes)))
	bld.node_deps[self.uid()]=nodes
	bld.raw_deps[self.uid()]=[]
	try:
		del self.cache_sig
	except:
		pass
	Task.Task.post_run(self)
def sig_implicit_deps(self):
	if self.env.CC_NAME not in supported_compilers:
		return self.no_gccdeps_sig_implicit_deps()
	try:
		return Task.Task.sig_implicit_deps(self)
	except Errors.WafError:
		return Utils.SIG_NIL
for name in'c cxx'.split():
	try:
		cls=Task.classes[name]
	except KeyError:
		pass
	else:
		cls.no_gccdeps_post_run=cls.post_run
		cls.no_gccdeps_scan=cls.scan
		cls.no_gccdeps_sig_implicit_deps=cls.sig_implicit_deps
		cls.post_run=post_run
		cls.scan=scan
		cls.sig_implicit_deps=sig_implicit_deps
