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.
17 Make sure to set bld.post_mode = waflib.Build.POST_AT_ONCE
20 from waflib
import Logs
, Build
21 from waflib
.Runner
import Parallel
23 DYNAMIC_EXT
= [] # add your non-cleanable files/extensions here
24 MOC_H_EXTS
= '.cpp .cxx .hpp .hxx .h'.split()
27 """Imperfect moc cleanup which does not look for a Q_OBJECT macro in the files"""
28 if not node
.name
.endswith('.moc'):
31 p1
= node
.parent
.get_src()
32 p2
= node
.parent
.get_bld()
35 n
= p1
.search_node(h_name
)
38 n
= p2
.search_node(h_name
)
42 # foo.cpp.moc, foo.h.moc, etc.
48 # recursion over the nodes to find the stale files
49 def stale_rec(node
, nodes
):
50 if node
.abspath() in node
.ctx
.env
[Build
.CFG_FILES
]:
53 if getattr(node
, 'children', []):
54 for x
in node
.children
.values():
58 for ext
in DYNAMIC_EXT
:
59 if node
.name
.endswith(ext
):
64 Logs
.warn('Removing stale file -> %r', node
)
67 old
= Parallel
.refill_task_list
68 def refill_task_list(self
):
72 # execute this operation only once
73 if getattr(self
, 'stale_done', False):
75 self
.stale_done
= True
77 # this does not work in partial builds
78 if bld
.targets
!= '*':
81 # this does not work in dynamic builds
82 if getattr(bld
, 'post_mode') == Build
.POST_AT_ONCE
:
85 # obtain the nodes to use during the build
87 for tasks
in bld
.groups
:
90 nodes
.extend(x
.outputs
)
91 except AttributeError:
94 stale_rec(bld
.bldnode
, nodes
)
97 Parallel
.refill_task_list
= refill_task_list