Revert "waf: added suncc_wrap"
[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
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 import Node, Environment
94
95 def vari(self):
96     return "default"
97 Environment.Environment.variant = vari
98
99 def variant(self, env):
100     if not env: return 0
101     elif self.id & 3 == Node.FILE: return 0
102     else: return "default"
103 Node.Node.variant = variant
104
105
106 import TaskGen, Task
107
108 def create_task(self, name, src=None, tgt=None):
109     task = Task.TaskBase.classes[name](self.env, generator=self)
110     if src:
111         task.set_inputs(src)
112     if tgt:
113         task.set_outputs(tgt)
114     return task
115 TaskGen.task_gen.create_task = create_task
116
117 def hash_constraints(self):
118     a = self.attr
119     sum = hash((str(a('before', '')),
120             str(a('after', '')),
121             str(a('ext_in', '')),
122             str(a('ext_out', '')),
123             self.__class__.maxjobs))
124     return sum
125 Task.TaskBase.hash_constraints = hash_constraints
126
127
128 # import cc
129 # from TaskGen import extension
130 # import Utils
131
132 # @extension(cc.EXT_CC)
133 # def c_hook(self, node):
134 #     task = self.create_task('cc', node, node.change_ext('.o'))
135 #     try:
136 #         self.compiled_tasks.append(task)
137 #     except AttributeError:
138 #         raise Utils.WafError('Have you forgotten to set the feature "cc" on %s?' % str(self))
139
140 #     bld = self.bld
141 #     try:
142 #         dc = bld.dc
143 #     except AttributeError:
144 #         dc = bld.dc = {}
145
146 #     if task.outputs[0].id in dc:
147 #         raise Utils.WafError('Samba, you are doing it wrong %r %s %s' % (task.outputs, task.generator, dc[task.outputs[0].id].generator))
148 #     else:
149 #         dc[task.outputs[0].id] = task
150
151 #     return task
152
153
154 def hash_env_vars(self, env, vars_lst):
155     idx = str(id(env)) + str(vars_lst)
156     try:
157         return self.cache_sig_vars[idx]
158     except KeyError:
159         pass
160
161     m = Utils.md5()
162     m.update(''.join([str(env[a]) for a in vars_lst]))
163
164     ret = self.cache_sig_vars[idx] = m.digest()
165     return ret
166 Build.BuildContext.hash_env_vars = hash_env_vars
167
168
169 def store_fast(self, filename):
170     file = open(filename, 'wb')
171     data = self.get_merged_dict()
172     try:
173         Build.cPickle.dump(data, file, -1)
174     finally:
175         file.close()
176 Environment.Environment.store_fast = store_fast
177
178 def load_fast(self, filename):
179     file = open(filename, 'rb')
180     try:
181         data = Build.cPickle.load(file)
182     finally:
183         file.close()
184     self.table.update(data)
185 Environment.Environment.load_fast = load_fast
186
187 def is_this_a_static_lib(self, name):
188     try:
189         cache = self.cache_is_this_a_static_lib
190     except AttributeError:
191         cache = self.cache_is_this_a_static_lib = {}
192     try:
193         return cache[name]
194     except KeyError:
195         ret = cache[name] = 'cstaticlib' in self.bld.name_to_obj(name, self.env).features
196         return ret
197 TaskGen.task_gen.is_this_a_static_lib = is_this_a_static_lib
198
199 def shared_ancestors(self):
200     try:
201         cache = self.cache_is_this_a_static_lib
202     except AttributeError:
203         cache = self.cache_is_this_a_static_lib = {}
204     try:
205         return cache[id(self)]
206     except KeyError:
207
208         ret = []
209         if 'cshlib' in self.features: # or 'cprogram' in self.features:
210             if getattr(self, 'uselib_local', None):
211                 lst = self.to_list(self.uselib_local)
212                 ret = [x for x in lst if not self.is_this_a_static_lib(x)]
213         cache[id(self)] = ret
214         return ret
215 TaskGen.task_gen.shared_ancestors = shared_ancestors
216
217 @feature('cc', 'cxx')
218 @after('apply_link', 'init_cc', 'init_cxx', 'apply_core')
219 def apply_lib_vars(self):
220     """after apply_link because of 'link_task'
221     after default_cc because of the attribute 'uselib'"""
222
223     # after 'apply_core' in case if 'cc' if there is no link
224
225     env = self.env
226     app = env.append_value
227     seen_libpaths = set([])
228
229     # OPTIMIZATION 1: skip uselib variables already added (700ms)
230     seen_uselib = set([])
231
232     # 1. the case of the libs defined in the project (visit ancestors first)
233     # the ancestors external libraries (uselib) will be prepended
234     self.uselib = self.to_list(self.uselib)
235     names = self.to_list(self.uselib_local)
236
237     seen = set([])
238     tmp = Utils.deque(names) # consume a copy of the list of names
239     while tmp:
240         lib_name = tmp.popleft()
241         # visit dependencies only once
242         if lib_name in seen:
243             continue
244
245         y = self.name_to_obj(lib_name)
246         if not y:
247             raise Utils.WafError('object %r was not found in uselib_local (required by %r)' % (lib_name, self.name))
248         y.post()
249         seen.add(lib_name)
250
251         # OPTIMIZATION 2: pre-compute ancestors shared libraries (100ms)
252         tmp.extend(y.shared_ancestors())
253
254         # link task and flags
255         if getattr(y, 'link_task', None):
256
257             link_name = y.target[y.target.rfind('/') + 1:]
258             if 'cstaticlib' in y.features:
259                 app('STATICLIB', link_name)
260             elif 'cshlib' in y.features or 'cprogram' in y.features:
261                 # WARNING some linkers can link against programs
262                 app('LIB', link_name)
263
264             # the order
265             self.link_task.set_run_after(y.link_task)
266
267             # for the recompilation
268             dep_nodes = getattr(self.link_task, 'dep_nodes', [])
269             self.link_task.dep_nodes = dep_nodes + y.link_task.outputs
270
271             # OPTIMIZATION 3: reduce the amount of function calls
272             # add the link path too
273             par = y.link_task.outputs[0].parent
274             if id(par) not in seen_libpaths:
275                 seen_libpaths.add(id(par))
276                 tmp_path = par.bldpath(self.env)
277                 if not tmp_path in env['LIBPATH']:
278                     env.prepend_value('LIBPATH', tmp_path)
279
280
281         # add ancestors uselib too - but only propagate those that have no staticlib
282         for v in self.to_list(y.uselib):
283             if v not in seen_uselib:
284                 seen_uselib.add(v)
285                 if not env['STATICLIB_' + v]:
286                     if not v in self.uselib:
287                         self.uselib.insert(0, v)
288
289     # 2. the case of the libs defined outside
290     for x in self.uselib:
291         for v in self.p_flag_vars:
292             val = self.env[v + '_' + x]
293             if val:
294                 self.env.append_value(v, val)
295
296 @feature('cprogram', 'cshlib', 'cstaticlib')
297 @after('apply_lib_vars')
298 @before('apply_obj_vars')
299 def samba_before_apply_obj_vars(self):
300     """before apply_obj_vars for uselib, this removes the standard pathes"""
301
302     def is_standard_libpath(env, path):
303         for _path in env.STANDARD_LIBPATH:
304             if _path == os.path.normpath(path):
305                 return True
306         return False
307
308     v = self.env
309
310     for i in v['RPATH']:
311         if is_standard_libpath(v, i):
312             v['RPATH'].remove(i)
313
314     for i in v['LIBPATH']:
315         if is_standard_libpath(v, i):
316             v['LIBPATH'].remove(i)
317
318 @feature('cc')
319 @before('apply_incpaths', 'apply_obj_vars_cc')
320 def samba_stash_cppflags(self):
321     """Fix broken waf ordering of CPPFLAGS"""
322
323     self.env.SAVED_CPPFLAGS = self.env.CPPFLAGS
324     self.env.CPPFLAGS = []
325
326 @feature('cc')
327 @after('apply_incpaths', 'apply_obj_vars_cc')
328 def samba_pop_cppflags(self):
329     """append stashed user CPPFLAGS after our internally computed flags"""
330
331     self.env.append_value('_CCINCFLAGS', self.env.SAVED_CPPFLAGS)
332     self.env.SAVED_CPPFLAGS = []
333
334 @feature('cprogram', 'cshlib', 'cstaticlib')
335 @before('apply_obj_vars', 'add_extra_flags')
336 def samba_stash_linkflags(self):
337     """stash away LINKFLAGS in order to fix waf's broken ordering wrt or
338     user LDFLAGS"""
339
340     self.env.SAVE_LINKFLAGS = self.env.LINKFLAGS
341     self.env.LINKFLAGS = []
342
343 @feature('cprogram', 'cshlib', 'cstaticlib')
344 @after('apply_obj_vars', 'add_extra_flags')
345 def samba_pop_linkflags(self):
346     """after apply_obj_vars append saved LDFLAGS"""
347
348     self.env.append_value('LINKFLAGS', self.env.SAVE_LINKFLAGS)
349     self.env.SAVE_LINKFLAGS = []