slow_qt4.py 2.7 KB

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