build: added deletion of stale .so and .o files
[rusty/samba.git] / buildtools / wafsamba / stale_files.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # Thomas Nagy, 2006-2010 (ita)
4
5 """
6 Add a pre-build hook to remove all build files
7 which do not have a corresponding target
8
9 This can be used for example to remove the targets
10 that have changed name without performing
11 a full 'waf clean'
12
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.
16 """
17
18 import Logs, Build, os, samba_utils, Options, Utils
19 from Runner import Parallel
20
21 old_refill_task_list = Parallel.refill_task_list
22 def replace_refill_task_list(self):
23     '''replacement for refill_task_list() that deletes stale files'''
24
25     iit = old_refill_task_list(self)
26     bld = self.bld
27
28     if not getattr(bld, 'new_rules', False):
29         # we only need to check for stale files if the build rules changed
30         return iit
31
32     if Options.options.compile_targets:
33         # not safe when --target is used
34         return iit
35
36     # execute only once
37     if getattr(self, 'cleanup_done', False):
38         return iit
39     self.cleanup_done = True
40
41     def group_name(g):
42         tm = self.bld.task_manager
43         return [x for x in tm.groups_names if id(tm.groups_names[x]) == id(g)][0]
44
45     bin_base = bld.bldnode.abspath()
46     bin_base_len = len(bin_base)
47
48     # paranoia
49     if bin_base[-4:] != '/bin':
50         raise Utils.WafError("Invalid bin base: %s" % bin_base)
51     
52     # obtain the expected list of files
53     expected = []
54     for i in range(len(bld.task_manager.groups)):
55         g = bld.task_manager.groups[i]
56         tasks = g.tasks_gen
57         for x in tasks:
58             try:
59                 if getattr(x, 'target'):
60                     tlist = samba_utils.TO_LIST(getattr(x, 'target'))
61                     ttype = getattr(x, 'samba_type', None)
62                     task_list = getattr(x, 'compiled_tasks', [])
63                     if task_list:
64                         # this gets all of the .o files, including the task
65                         # ids, so foo.c maps to foo_3.o for idx=3
66                         for tsk in task_list:
67                             for output in tsk.outputs:
68                                 objpath = os.path.normpath(output.abspath(bld.env))
69                                 expected.append(objpath)
70                     for t in tlist:
71                         if ttype in ['LIBRARY','MODULE']:
72                             t = samba_utils.apply_pattern(t, bld.env.shlib_PATTERN)
73                         if ttype == 'PYTHON':
74                             t = samba_utils.apply_pattern(t, bld.env.pyext_PATTERN)
75                         p = os.path.join(x.path.abspath(bld.env), t)
76                         p = os.path.normpath(p)
77                         expected.append(p)
78                 for n in x.allnodes:
79                     p = n.abspath(bld.env)
80                     if p[0:bin_base_len] == bin_base:
81                         expected.append(p)
82             except:
83                 pass
84
85     for root, dirs, files in os.walk(bin_base):
86         for f in files:
87             p = root + '/' + f
88             if os.path.islink(p):
89                 link = os.readlink(p)
90                 if link[0:bin_base_len] == bin_base:
91                     p = link
92             if f in ['config.h']:
93                 continue
94             (froot, fext) = os.path.splitext(f)
95             if fext not in [ '.c', '.h', '.so', '.o' ]:
96                 continue
97             if f[-7:] == '.inst.h':
98                 continue
99             if p.find("/.conf") != -1:
100                 continue
101             if not p in expected and os.path.exists(p):
102                 Logs.warn("Removing stale file: %s" % p)
103                 os.unlink(p)
104     return iit
105
106
107 def AUTOCLEANUP_STALE_FILES(bld):
108     """automatically clean up any files in bin that shouldn't be there"""
109     old_refill_task_list = Parallel.refill_task_list
110     Parallel.refill_task_list = replace_refill_task_list
111     Parallel.bld = bld
112 Build.BuildContext.AUTOCLEANUP_STALE_FILES = AUTOCLEANUP_STALE_FILES