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