3 # Thomas Nagy, 2006-2015 (ita)
6 Add a pre-build hook to remove build files (declared in the system)
7 that do not have a corresponding target
9 This can be used for example to remove the targets
10 that have changed name without performing
13 Of course, it will only work if there are no dynamically generated
14 nodes/tasks, in which case the method will have to be modified
15 to exclude some folders for example.
18 from waflib import Logs, Build
19 from waflib.Runner import Parallel
21 DYNAMIC_EXT = [] # add your non-cleanable files/extensions here
22 MOC_H_EXTS = '.cpp .cxx .hpp .hxx .h'.split()
25 """Imperfect moc cleanup which does not look for a Q_OBJECT macro in the files"""
26 if not node.name.endswith('.moc'):
29 p1 = node.parent.get_src()
30 p2 = node.parent.get_bld()
33 n = p1.search_node(h_name)
36 n = p2.search_node(h_name)
40 # foo.cpp.moc, foo.h.moc, etc.
46 # recursion over the nodes to find the stale files
47 def stale_rec(node, nodes):
48 if node.abspath() in node.ctx.env[Build.CFG_FILES]:
51 if getattr(node, 'children', []):
52 for x in node.children.values():
56 for ext in DYNAMIC_EXT:
57 if node.name.endswith(ext):
62 Logs.warn("Removing stale file -> %s" % node.abspath())
65 old = Parallel.refill_task_list
66 def refill_task_list(self):
70 # execute this operation only once
71 if getattr(self, 'stale_done', False):
73 self.stale_done = True
75 # this does not work in partial builds
76 if hasattr(bld, 'options') and bld.options.targets and bld.options.targets != '*':
79 # this does not work in dynamic builds
80 if not hasattr(bld, 'post_mode') or bld.post_mode == Build.POST_LAZY:
83 # obtain the nodes to use during the build
85 for i in range(len(bld.groups)):
86 tasks = bld.get_tasks_group(i)
89 nodes.extend(x.outputs)
93 stale_rec(bld.bldnode, nodes)
96 Parallel.refill_task_list = refill_task_list