#! /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 itertools
from waflib import Utils,Task,Runner,Logs
class SetOfTasks(object):
	def __init__(self,owner):
		self._set=owner.run_after
		self._owner=owner
	def __iter__(self):
		for g in self._owner.run_after_groups:
			for task in g:
				yield task
		for task in self._set:
			yield task
	def add(self,obj):
		self._set.add(obj)
	def update(self,obj):
		self._set.update(obj)
def set_precedence_constraints(tasks):
	cstr_groups=Utils.defaultdict(list)
	for x in tasks:
		x.run_after=SetOfTasks(x)
		x.run_after_groups=[]
		x.waiting_sets=[]
		h=x.hash_constraints()
		cstr_groups[h].append(x)
	for k in cstr_groups.keys():
		cstr_groups[k]=set(cstr_groups[k])
	for key1,key2 in itertools.combinations(cstr_groups.keys(),2):
		group1=cstr_groups[key1]
		group2=cstr_groups[key2]
		t1=next(iter(group1))
		t2=next(iter(group2))
		if Task.is_before(t1,t2):
			for x in group2:
				x.run_after_groups.append(group1)
			for k in group1:
				k.waiting_sets.append(group1)
		elif Task.is_before(t2,t1):
			for x in group1:
				x.run_after_groups.append(group2)
			for k in group2:
				k.waiting_sets.append(group2)
Task.set_precedence_constraints=set_precedence_constraints
def get_out(self):
	tsk=self.out.get()
	if not self.stop:
		self.add_more_tasks(tsk)
	self.count-=1
	self.dirty=True
	try:
		ws=tsk.waiting_sets
	except AttributeError:
		pass
	else:
		for k in ws:
			try:
				k.remove(tsk)
			except KeyError:
				pass
	return tsk
Runner.Parallel.get_out=get_out
if 1:
	def start(self):
		self.total=self.bld.total()
		while not self.stop:
			self.refill_task_list()
			tsk=self.get_next_task()
			if not tsk:
				if self.count:
					continue
				else:
					break
			if tsk.hasrun:
				self.processed+=1
				continue
			if self.stop:
				break
			try:
				st=tsk.runnable_status()
			except Exception:
				self.processed+=1
				tsk.err_msg=Utils.ex_stack()
				if not self.stop and self.bld.keep:
					tsk.hasrun=Task.SKIPPED
					if self.bld.keep==1:
						if Logs.verbose>1 or not self.error:
							self.error.append(tsk)
						self.stop=True
					else:
						if Logs.verbose>1:
							self.error.append(tsk)
					continue
				tsk.hasrun=Task.EXCEPTION
				self.error_handler(tsk)
				continue
			if st==Task.ASK_LATER:
				self.postpone(tsk)
			elif st==Task.SKIP_ME:
				self.processed+=1
				tsk.hasrun=Task.SKIPPED
				self.add_more_tasks(tsk)
				try:
					ws=tsk.waiting_sets
				except AttributeError:
					pass
				else:
					for k in ws:
						try:
							k.remove(tsk)
						except KeyError:
							pass
			else:
				tsk.position=(self.processed,self.total)
				self.count+=1
				tsk.master=self
				self.processed+=1
				if self.numjobs==1:
					tsk.process()
				else:
					self.add_task(tsk)
		while self.error and self.count:
			self.get_out()
		assert(self.count==0 or self.stop)
		self.free_task_pool()
	Runner.Parallel.start=start
