f84a24d10e53b63edfdcf32a3547accdf3e96bff
[obnox/samba/samba-obnox.git] / buildtools / wafsamba / samba_optimisation.py
1 # This file contains waf optimisations for Samba
2
3 # most of these optimisations are possible because of the restricted build environment
4 # that Samba has. For example, Samba doesn't attempt to cope with Win32 paths during the
5 # build, and Samba doesn't need build varients
6
7 # overall this makes some build tasks quite a bit faster
8
9 import os
10 import Build, Utils, Node
11 from TaskGen import feature, after, before
12 import preproc, Task
13
14 @feature('cc', 'cxx')
15 @after('apply_type_vars', 'apply_lib_vars', 'apply_core')
16 def apply_incpaths(self):
17     lst = []
18
19     try:
20         kak = self.bld.kak
21     except AttributeError:
22         kak = self.bld.kak = {}
23
24     # TODO move the uselib processing out of here
25     for lib in self.to_list(self.uselib):
26         for path in self.env['CPPPATH_' + lib]:
27             if not path in lst:
28                 lst.append(path)
29     if preproc.go_absolute:
30         for path in preproc.standard_includes:
31             if not path in lst:
32                 lst.append(path)
33
34     for path in self.to_list(self.includes):
35         if not path in lst:
36             if preproc.go_absolute or path[0] != '/':  # os.path.isabs(path):
37                 lst.append(path)
38             else:
39                 self.env.prepend_value('CPPPATH', path)
40
41     for path in lst:
42         node = None
43         if path[0] == '/': # os.path.isabs(path):
44             if preproc.go_absolute:
45                 node = self.bld.root.find_dir(path)
46         elif path[0] == '#':
47             node = self.bld.srcnode
48             if len(path) > 1:
49                 try:
50                     node = kak[path]
51                 except KeyError:
52                     kak[path] = node = node.find_dir(path[1:])
53         else:
54             try:
55                 node = kak[(self.path.id, path)]
56             except KeyError:
57                 kak[(self.path.id, path)] = node = self.path.find_dir(path)
58
59         if node:
60             self.env.append_value('INC_PATHS', node)
61
62 @feature('cc')
63 @after('apply_incpaths')
64 def apply_obj_vars_cc(self):
65     """after apply_incpaths for INC_PATHS"""
66     env = self.env
67     app = env.append_unique
68     cpppath_st = env['CPPPATH_ST']
69
70     lss = env['_CCINCFLAGS']
71
72     try:
73          cac = self.bld.cac
74     except AttributeError:
75          cac = self.bld.cac = {}
76
77     # local flags come first
78     # set the user-defined includes paths
79     for i in env['INC_PATHS']:
80
81         try:
82             lss.extend(cac[i.id])
83         except KeyError:
84
85             cac[i.id] = [cpppath_st % i.bldpath(env), cpppath_st % i.srcpath(env)]
86             lss.extend(cac[i.id])
87
88     env['_CCINCFLAGS'] = lss
89     # set the library include paths
90     for i in env['CPPPATH']:
91         app('_CCINCFLAGS', cpppath_st % i)
92
93 @feature('cc')
94 @before('apply_incpaths', 'apply_obj_vars_cc')
95 def samba_stash_cppflags(self):
96     """Fix broken waf ordering of CPPFLAGS"""
97     self.env['SAVED_CPPFLAGS'] = self.env['CPPFLAGS']
98     self.env['CPPFLAGS'] = []
99
100 @feature('cc')
101 @after('apply_incpaths', 'apply_obj_vars_cc')
102 def samba_pop_cppflags(self):
103     # append stashed user CPPFLAGS after our internally computed flags
104     app('_CCINCFLAGS', env['SAVED_CPPFLAGS'])
105     env['SAVED_CPPFLAGS'] = []
106
107 import Node, Environment
108
109 def vari(self):
110     return "default"
111 Environment.Environment.variant = vari
112
113 def variant(self, env):
114     if not env: return 0
115     elif self.id & 3 == Node.FILE: return 0
116     else: return "default"
117 Node.Node.variant = variant
118
119
120 import TaskGen, Task
121
122 def create_task(self, name, src=None, tgt=None):
123     task = Task.TaskBase.classes[name](self.env, generator=self)
124     if src:
125         task.set_inputs(src)
126     if tgt:
127         task.set_outputs(tgt)
128     return task
129 TaskGen.task_gen.create_task = create_task
130
131 def hash_constraints(self):
132     a = self.attr
133     sum = hash((str(a('before', '')),
134             str(a('after', '')),
135             str(a('ext_in', '')),
136             str(a('ext_out', '')),
137             self.__class__.maxjobs))
138     return sum
139 Task.TaskBase.hash_constraints = hash_constraints
140
141
142 # import cc
143 # from TaskGen import extension
144 # import Utils
145
146 # @extension(cc.EXT_CC)
147 # def c_hook(self, node):
148 #     task = self.create_task('cc', node, node.change_ext('.o'))
149 #     try:
150 #         self.compiled_tasks.append(task)
151 #     except AttributeError:
152 #         raise Utils.WafError('Have you forgotten to set the feature "cc" on %s?' % str(self))
153
154 #     bld = self.bld
155 #     try:
156 #         dc = bld.dc
157 #     except AttributeError:
158 #         dc = bld.dc = {}
159
160 #     if task.outputs[0].id in dc:
161 #         raise Utils.WafError('Samba, you are doing it wrong %r %s %s' % (task.outputs, task.generator, dc[task.outputs[0].id].generator))
162 #     else:
163 #         dc[task.outputs[0].id] = task
164
165 #     return task
166
167
168 def suncc_wrap(cls):
169     '''work around a problem with cc on solaris not handling module aliases
170     which have empty libs'''
171     if getattr(cls, 'solaris_wrap', False):
172         return
173     cls.solaris_wrap = True
174     oldrun = cls.run
175     def run(self):
176         if self.env.CC_NAME == "sun" and not self.inputs:
177             self.env = self.env.copy()
178             self.env.append_value('LINKFLAGS', '-')
179         return oldrun(self)
180     cls.run = run
181 suncc_wrap(Task.TaskBase.classes['cc_link'])
182
183
184
185 def hash_env_vars(self, env, vars_lst):
186     idx = str(id(env)) + str(vars_lst)
187     try:
188         return self.cache_sig_vars[idx]
189     except KeyError:
190         pass
191
192     m = Utils.md5()
193     m.update(''.join([str(env[a]) for a in vars_lst]))
194
195     ret = self.cache_sig_vars[idx] = m.digest()
196     return ret
197 Build.BuildContext.hash_env_vars = hash_env_vars
198
199
200 def store_fast(self, filename):
201     file = open(filename, 'wb')
202     data = self.get_merged_dict()
203     try:
204         Build.cPickle.dump(data, file, -1)
205     finally:
206         file.close()
207 Environment.Environment.store_fast = store_fast
208
209 def load_fast(self, filename):
210     file = open(filename, 'rb')
211     try:
212         data = Build.cPickle.load(file)
213     finally:
214         file.close()
215     self.table.update(data)
216 Environment.Environment.load_fast = load_fast
217
218 def is_this_a_static_lib(self, name):
219     try:
220         cache = self.cache_is_this_a_static_lib
221     except AttributeError:
222         cache = self.cache_is_this_a_static_lib = {}
223     try:
224         return cache[name]
225     except KeyError:
226         ret = cache[name] = 'cstaticlib' in self.bld.name_to_obj(name, self.env).features
227         return ret
228 TaskGen.task_gen.is_this_a_static_lib = is_this_a_static_lib
229
230 def shared_ancestors(self):
231     try:
232         cache = self.cache_is_this_a_static_lib
233     except AttributeError:
234         cache = self.cache_is_this_a_static_lib = {}
235     try:
236         return cache[id(self)]
237     except KeyError:
238
239         ret = []
240         if 'cshlib' in self.features: # or 'cprogram' in self.features:
241             if getattr(self, 'uselib_local', None):
242                 lst = self.to_list(self.uselib_local)
243                 ret = [x for x in lst if not self.is_this_a_static_lib(x)]
244         cache[id(self)] = ret
245         return ret
246 TaskGen.task_gen.shared_ancestors = shared_ancestors
247
248 @feature('cc', 'cxx')
249 @after('apply_link', 'init_cc', 'init_cxx', 'apply_core')
250 def apply_lib_vars(self):
251     """after apply_link because of 'link_task'
252     after default_cc because of the attribute 'uselib'"""
253
254     # after 'apply_core' in case if 'cc' if there is no link
255
256     env = self.env
257     app = env.append_value
258     seen_libpaths = set([])
259
260     # OPTIMIZATION 1: skip uselib variables already added (700ms)
261     seen_uselib = set([])
262
263     # 1. the case of the libs defined in the project (visit ancestors first)
264     # the ancestors external libraries (uselib) will be prepended
265     self.uselib = self.to_list(self.uselib)
266     names = self.to_list(self.uselib_local)
267
268     seen = set([])
269     tmp = Utils.deque(names) # consume a copy of the list of names
270     while tmp:
271         lib_name = tmp.popleft()
272         # visit dependencies only once
273         if lib_name in seen:
274             continue
275
276         y = self.name_to_obj(lib_name)
277         if not y:
278             raise Utils.WafError('object %r was not found in uselib_local (required by %r)' % (lib_name, self.name))
279         y.post()
280         seen.add(lib_name)
281
282         # OPTIMIZATION 2: pre-compute ancestors shared libraries (100ms)
283         tmp.extend(y.shared_ancestors())
284
285         # link task and flags
286         if getattr(y, 'link_task', None):
287
288             link_name = y.target[y.target.rfind('/') + 1:]
289             if 'cstaticlib' in y.features:
290                 app('STATICLIB', link_name)
291             elif 'cshlib' in y.features or 'cprogram' in y.features:
292                 # WARNING some linkers can link against programs
293                 app('LIB', link_name)
294
295             # the order
296             self.link_task.set_run_after(y.link_task)
297
298             # for the recompilation
299             dep_nodes = getattr(self.link_task, 'dep_nodes', [])
300             self.link_task.dep_nodes = dep_nodes + y.link_task.outputs
301
302             # OPTIMIZATION 3: reduce the amount of function calls
303             # add the link path too
304             par = y.link_task.outputs[0].parent
305             if id(par) not in seen_libpaths:
306                 seen_libpaths.add(id(par))
307                 tmp_path = par.bldpath(self.env)
308                 if not tmp_path in env['LIBPATH']:
309                     env.prepend_value('LIBPATH', tmp_path)
310
311
312         # add ancestors uselib too - but only propagate those that have no staticlib
313         for v in self.to_list(y.uselib):
314             if v not in seen_uselib:
315                 seen_uselib.add(v)
316                 if not env['STATICLIB_' + v]:
317                     if not v in self.uselib:
318                         self.uselib.insert(0, v)
319
320     # 2. the case of the libs defined outside
321     for x in self.uselib:
322         for v in self.p_flag_vars:
323             val = self.env[v + '_' + x]
324             if val:
325                 self.env.append_value(v, val)
326
327 @feature('cprogram', 'cshlib', 'cstaticlib')
328 @after('apply_lib_vars')
329 @before('apply_obj_vars')
330 def samba_before_apply_obj_vars(self):
331     """before apply_obj_vars for uselib, this removes the standard pathes"""
332
333     def is_standard_libpath(env, path):
334         for _path in env.STANDARD_LIBPATH:
335             if _path == os.path.normpath(path):
336                 return True
337         return False
338
339     v = self.env
340
341     for i in v['RPATH']:
342         if is_standard_libpath(v, i):
343             v['RPATH'].remove(i)
344
345     for i in v['LIBPATH']:
346         if is_standard_libpath(v, i):
347             v['LIBPATH'].remove(i)
348
349 @feature('cprogram', 'cshlib', 'cstaticlib')
350 @before('apply_obj_vars', 'add_extra_flags')
351 def samba_stash_linkflags(self):
352     """stash away LINKFLAGS in order to fix waf's broken ordering wrt or
353     user LDFLAGS"""
354
355     self.env.SAVE_LINKFLAGS = self.env['LINKFLAGS']
356     self.env['LINKFLAGS'] = []
357
358 @feature('cprogram', 'cshlib', 'cstaticlib')
359 @after('apply_obj_vars', 'add_extra_flags')
360 def samba_pop_linkflags(self):
361     """after apply_obj_vars append saved LDFLAGS"""
362
363     self.env.append_value('LINKFLAGS', self.env.SAVE_LINKFLAGS)
364     self.env.SAVE_LINKFLAGS = []