1#! /usr/bin/env python
2# Thomas Nagy, 2011 (ita)
3
4"""
5Create _moc.cpp files
6
7The builds are 30-40% faster when .moc files are included,
8you should NOT use this tool. If you really
9really want it:
10
11def configure(conf):
12	conf.load('compiler_cxx qt4')
13	conf.load('slow_qt4')
14
15See playground/slow_qt/wscript for a complete example.
16"""
17
18from waflib.TaskGen import extension
19from waflib import Task
20import waflib.Tools.qt4
21import waflib.Tools.cxx
22
23@extension(*waflib.Tools.qt4.EXT_QT4)
24def cxx_hook(self, node):
25	return self.create_compiled_task('cxx_qt', node)
26
27class cxx_qt(Task.classes['cxx']):
28	def runnable_status(self):
29		ret = Task.classes['cxx'].runnable_status(self)
30		if ret != Task.ASK_LATER and not getattr(self, 'moc_done', None):
31
32			try:
33				cache = self.generator.moc_cache
34			except AttributeError:
35				cache = self.generator.moc_cache = {}
36
37			deps = self.generator.bld.node_deps[self.uid()]
38			for x in [self.inputs[0]] + deps:
39				if x.read().find('Q_OBJECT') > 0:
40
41					# process "foo.h -> foo.moc" only if "foo.cpp" is in the sources for the current task generator
42					# this code will work because it is in the main thread (runnable_status)
43					if x.name.rfind('.') > -1: # a .h file...
44						name = x.name[:x.name.rfind('.')]
45						for tsk in self.generator.compiled_tasks:
46							if tsk.inputs and tsk.inputs[0].name.startswith(name):
47								break
48						else:
49							# no corresponding file, continue
50							continue
51
52					# the file foo.cpp could be compiled for a static and a shared library - hence the %number in the name
53					cxx_node = x.parent.get_bld().make_node(x.name.replace('.', '_') + '_%d_moc.cpp' % self.generator.idx)
54					if cxx_node in cache:
55						continue
56					cache[cxx_node] = self
57
58					tsk = Task.classes['moc'](env=self.env, generator=self.generator)
59					tsk.set_inputs(x)
60					tsk.set_outputs(cxx_node)
61
62					if x.name.endswith('.cpp'):
63						# moc is trying to be too smart but it is too dumb:
64						# why forcing the #include when Q_OBJECT is in the cpp file?
65						gen = self.generator.bld.producer
66						gen.outstanding.append(tsk)
67						gen.total += 1
68						self.set_run_after(tsk)
69					else:
70						cxxtsk = Task.classes['cxx'](env=self.env, generator=self.generator)
71						cxxtsk.set_inputs(tsk.outputs)
72						cxxtsk.set_outputs(cxx_node.change_ext('.o'))
73						cxxtsk.set_run_after(tsk)
74
75						try:
76							self.more_tasks.extend([tsk, cxxtsk])
77						except AttributeError:
78							self.more_tasks = [tsk, cxxtsk]
79
80						try:
81							link = self.generator.link_task
82						except AttributeError:
83							pass
84						else:
85							link.set_run_after(cxxtsk)
86							link.inputs.extend(cxxtsk.outputs)
87							link.inputs.sort(key=lambda x: x.abspath())
88
89			self.moc_done = True
90
91		for t in self.run_after:
92			if not t.hasrun:
93				return Task.ASK_LATER
94
95		return ret
96
97