build: add cflags from pkg_config results to header/function tests
[nivanova/samba-autobuild/.git] / buildtools / wafsamba / samba_utils.py
1 # a waf tool to add autoconf-like macros to the configure section
2 # and for SAMBA_ macros for building libraries, binaries etc
3
4 import Build, os, sys, Options, Utils
5 from TaskGen import feature, before
6 from Configure import conf
7 from Logs import debug
8 import shlex
9
10 # TODO: make this a --option
11 LIB_PATH="shared"
12
13
14 ##########################################################
15 # create a node with a new name, based on an existing node
16 def NEW_NODE(node, name):
17     ret = node.parent.find_or_declare([name])
18     ASSERT(node, ret is not None, "Unable to find new target with name '%s' from '%s'" % (
19             name, node.name))
20     return ret
21
22
23 #############################################################
24 # set a value in a local cache
25 # return False if it's already set
26 def SET_TARGET_TYPE(ctx, target, value):
27     cache = LOCAL_CACHE(ctx, 'TARGET_TYPE')
28     if target in cache:
29         ASSERT(ctx, cache[target] == value,
30                "Target '%s' re-defined as %s - was %s" % (target, value, cache[target]))
31         debug("task_gen: Skipping duplicate target %s (curdir=%s)" % (target, ctx.curdir))
32         return False
33     LOCAL_CACHE_SET(ctx, 'TARGET_TYPE', target, value)
34     debug("task_gen: Target '%s' created of type '%s' in %s" % (target, value, ctx.curdir))
35     return True
36
37
38 def GET_TARGET_TYPE(ctx, target):
39     '''get target type from cache'''
40     cache = LOCAL_CACHE(ctx, 'TARGET_TYPE')
41     if not target in cache:
42         return None
43     return cache[target]
44
45
46 ######################################################
47 # this is used as a decorator to make functions only
48 # run once. Based on the idea from
49 # http://stackoverflow.com/questions/815110/is-there-a-decorator-to-simply-cache-function-return-values
50 runonce_ret = {}
51 def runonce(function):
52     def wrapper(*args):
53         if args in runonce_ret:
54             return runonce_ret[args]
55         else:
56             ret = function(*args)
57             runonce_ret[args] = ret
58             return ret
59     return wrapper
60
61
62 def install_rpath(bld):
63     '''the rpath value for installation'''
64     bld.env['RPATH'] = []
65     bld.env['RPATH_ST'] = []
66     if bld.env.RPATH_ON_INSTALL:
67         return ['-Wl,-rpath=%s/lib' % bld.env.PREFIX]
68     return []
69
70
71 def build_rpath(bld):
72     '''the rpath value for build'''
73     rpath = os.path.normpath('%s/%s' % (bld.env['BUILD_DIRECTORY'], LIB_PATH))
74     bld.env['RPATH'] = []
75     bld.env['RPATH_ST'] = []
76     if bld.env.RPATH_ON_BUILD:
77         return ['-Wl,-rpath=%s' % rpath]
78     os.environ['LD_LIBRARY_PATH'] = rpath
79     return []
80
81
82 #############################################################
83 # return a named build cache dictionary, used to store
84 # state inside the following functions
85 @conf
86 def LOCAL_CACHE(ctx, name):
87     if name in ctx.env:
88         return ctx.env[name]
89     ctx.env[name] = {}
90     return ctx.env[name]
91
92
93 #############################################################
94 # set a value in a local cache
95 @conf
96 def LOCAL_CACHE_SET(ctx, cachename, key, value):
97     cache = LOCAL_CACHE(ctx, cachename)
98     cache[key] = value
99
100 #############################################################
101 # a build assert call
102 @conf
103 def ASSERT(ctx, expression, msg):
104     if not expression:
105         sys.stderr.write("ERROR: %s\n" % msg)
106         raise AssertionError
107 Build.BuildContext.ASSERT = ASSERT
108
109 ################################################################
110 # create a list of files by pre-pending each with a subdir name
111 def SUBDIR(bld, subdir, list):
112     ret = ''
113     for l in TO_LIST(list):
114         ret = ret + os.path.normpath(os.path.join(subdir, l)) + ' '
115     return ret
116 Build.BuildContext.SUBDIR = SUBDIR
117
118 #######################################################
119 # d1 += d2
120 def dict_concat(d1, d2):
121     for t in d2:
122         if t not in d1:
123             d1[t] = d2[t]
124
125 ############################################################
126 # this overrides the 'waf -v' debug output to be in a nice
127 # unix like format instead of a python list.
128 # Thanks to ita on #waf for this
129 def exec_command(self, cmd, **kw):
130     import Utils, Logs
131     _cmd = cmd
132     if isinstance(cmd, list):
133         _cmd = ' '.join(cmd)
134     debug('runner: %s' % _cmd)
135     if self.log:
136         self.log.write('%s\n' % cmd)
137         kw['log'] = self.log
138     try:
139         if not kw.get('cwd', None):
140             kw['cwd'] = self.cwd
141     except AttributeError:
142         self.cwd = kw['cwd'] = self.bldnode.abspath()
143     return Utils.exec_command(cmd, **kw)
144 Build.BuildContext.exec_command = exec_command
145
146
147 ##########################################################
148 # add a new top level command to waf
149 def ADD_COMMAND(opt, name, function):
150     Utils.g_module.__dict__[name] = function
151     opt.name = function
152 Options.Handler.ADD_COMMAND = ADD_COMMAND
153
154
155 @feature('cc', 'cshlib', 'cprogram')
156 @before('apply_core','exec_rule')
157 def process_depends_on(self):
158     '''The new depends_on attribute for build rules
159        allow us to specify a dependency on output from
160        a source generation rule'''
161     if getattr(self , 'depends_on', None):
162         lst = self.to_list(self.depends_on)
163         for x in lst:
164             y = self.bld.name_to_obj(x, self.env)
165             self.bld.ASSERT(y is not None, "Failed to find dependency %s of %s" % (x, self.name))
166             y.post()
167             if getattr(y, 'more_includes', None):
168                   self.includes += " " + y.more_includes
169
170
171 #@feature('cprogram', 'cc', 'cshlib')
172 #@before('apply_core')
173 #def process_generated_dependencies(self):
174 #    '''Ensure that any dependent source generation happens
175 #       before any task that requires the output'''
176 #    if getattr(self , 'depends_on', None):
177 #        lst = self.to_list(self.depends_on)
178 #        for x in lst:
179 #            y = self.bld.name_to_obj(x, self.env)
180 #            y.post()
181
182
183 #import TaskGen, Task
184 #
185 #old_post_run = Task.Task.post_run
186 #def new_post_run(self):
187 #    self.cached = True
188 #    return old_post_run(self)
189 #
190 #for y in ['cc', 'cxx']:
191 #    TaskGen.classes[y].post_run = new_post_run
192
193 def ENABLE_MAGIC_ORDERING(bld):
194     '''enable automatic build order constraint calculation
195        see page 35 of the waf book'''
196     print "NOT Enabling magic ordering"
197     #bld.use_the_magic()
198 Build.BuildContext.ENABLE_MAGIC_ORDERING = ENABLE_MAGIC_ORDERING
199
200
201 os_path_relpath = getattr(os.path, 'relpath', None)
202 if os_path_relpath is None:
203     # Python < 2.6 does not have os.path.relpath, provide a replacement
204     # (imported from Python2.6.5~rc2)
205     def os_path_relpath(path, start):
206         """Return a relative version of a path"""
207         start_list = os.path.abspath(start).split("/")
208         path_list = os.path.abspath(path).split("/")
209
210         # Work out how much of the filepath is shared by start and path.
211         i = len(os.path.commonprefix([start_list, path_list]))
212
213         rel_list = ['..'] * (len(start_list)-i) + path_list[i:]
214         if not rel_list:
215             return start
216         return os.path.join(*rel_list)
217
218
219 # this is a useful way of debugging some of the rules in waf
220 from TaskGen import feature, after
221 @feature('dbg')
222 @after('apply_core', 'apply_obj_vars_cc')
223 def dbg(self):
224         if self.target == 'HEIMDAL_HEIM_ASN1':
225                 print "@@@@@@@@@@@@@@2", self.includes, self.env._CCINCFLAGS
226
227 def unique_list(seq):
228     '''return a uniquified list in the same order as the existing list'''
229     seen = {}
230     result = []
231     for item in seq:
232         if item in seen: continue
233         seen[item] = True
234         result.append(item)
235     return result
236
237 def TO_LIST(str):
238     '''Split a list, preserving quoted strings and existing lists'''
239     if str is None:
240         return []
241     if isinstance(str, list):
242         return str
243     lst = str.split()
244     # the string may have had quotes in it, now we
245     # check if we did have quotes, and use the slower shlex
246     # if we need to
247     for e in lst:
248         if e[0] == '"':
249             return shlex.split(str)
250     return lst
251
252 @conf
253 def SUBST_ENV_VAR(ctx, varname):
254     '''Substitute an environment variable for any embedded variables'''
255     return Utils.subst_vars(ctx.env[varname], ctx.env)
256 Build.BuildContext.SUBST_ENV_VAR = SUBST_ENV_VAR
257
258
259 def ENFORCE_GROUP_ORDERING(bld):
260     '''enforce group ordering for the project. This
261        makes the group ordering apply only when you specify
262        a target with --target'''
263     if Options.options.compile_targets:
264         @feature('*')
265         def force_previous_groups(self):
266             my_id = id(self)
267
268             bld = self.bld
269             stop = None
270             for g in bld.task_manager.groups:
271                 for t in g.tasks_gen:
272                     if id(t) == my_id:
273                         stop = id(g)
274                         break
275                 if stop is None:
276                     return
277
278                 for g in bld.task_manager.groups:
279                     if id(g) == stop:
280                         break
281                     for t in g.tasks_gen:
282                         t.post()
283 Build.BuildContext.ENFORCE_GROUP_ORDERING = ENFORCE_GROUP_ORDERING
284
285 # @feature('cc')
286 # @before('apply_lib_vars')
287 # def process_objects(self):
288 #     if getattr(self, 'add_objects', None):
289 #         lst = self.to_list(self.add_objects)
290 #         for x in lst:
291 #             y = self.name_to_obj(x)
292 #             if not y:
293 #                 raise Utils.WafError('object %r was not found in uselib_local (required by add_objects %r)' % (x, self.name))
294 #             y.post()
295 #             self.env.append_unique('INC_PATHS', y.env.INC_PATHS)
296
297
298 def recursive_dirlist(dir, relbase):
299     '''recursive directory list'''
300     ret = []
301     for f in os.listdir(dir):
302         f2 = dir + '/' + f
303         if os.path.isdir(f2):
304             ret.extend(recursive_dirlist(f2, relbase))
305         else:
306             ret.append(os_path_relpath(f2, relbase))
307     return ret
308
309
310 def mkdir_p(dir):
311     '''like mkdir -p'''
312     if os.path.isdir(dir):
313         return
314     mkdir_p(os.path.dirname(dir))
315     os.mkdir(dir)
316
317
318 def SUBST_VARS_RECURSIVE(string, env):
319     '''recursively expand variables'''
320     if string is None:
321         return string
322     limit=100
323     while (string.find('${') != -1 and limit > 0):
324         string = Utils.subst_vars(string, env)
325         limit -= 1
326     return string
327
328
329 def RUN_COMMAND(cmd,
330                 env=None,
331                 shell=False):
332     '''run a external command, return exit code or signal'''
333     if env:
334         cmd = SUBST_VARS_RECURSIVE(cmd, env)
335
336     status = os.system(cmd)
337     if os.WIFEXITED(status):
338         return os.WEXITSTATUS(status)
339     if os.WIFSIGNALED(status):
340         return - os.WTERMSIG(status)
341     print "Unknown exit reason %d for command: %s" (status, cmd)
342     return -1