build: finer grained rpath checking for binary/install
[Samba/gebeck_regimport.git] / buildtools / wafsamba / samba_utils.py
blob8bd913f7202f90cabe4cdf12f2435b0b590f7c0e
1 # a waf tool to add autoconf-like macros to the configure section
2 # and for SAMBA_ macros for building libraries, binaries etc
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
10 # TODO: make this a --option
11 LIB_PATH="shared"
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
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
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]
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
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 []
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 []
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]
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
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
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
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]
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
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
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
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()
183 #import TaskGen, Task
185 #old_post_run = Task.Task.post_run
186 #def new_post_run(self):
187 # self.cached = True
188 # return old_post_run(self)
190 #for y in ['cc', 'cxx']:
191 # TaskGen.classes[y].post_run = new_post_run
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
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("/")
210 # Work out how much of the filepath is shared by start and path.
211 i = len(os.path.commonprefix([start_list, path_list]))
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)
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
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
237 def TO_LIST(str):
238 '''Split a list, preserving quoted strings and existing lists'''
239 if isinstance(str, list):
240 return str
241 lst = str.split()
242 # the string may have had quotes in it, now we
243 # check if we did have quotes, and use the slower shlex
244 # if we need to
245 for e in lst:
246 if e[0] == '"':
247 return shlex.split(str)
248 return lst
250 @conf
251 def SUBST_ENV_VAR(ctx, varname):
252 '''Substitute an environment variable for any embedded variables'''
253 return Utils.subst_vars(ctx.env[varname], ctx.env)
254 Build.BuildContext.SUBST_ENV_VAR = SUBST_ENV_VAR
257 def ENFORCE_GROUP_ORDERING(bld):
258 '''enforce group ordering for the project. This
259 makes the group ordering apply only when you specify
260 a target with --target'''
261 if Options.options.compile_targets:
262 @feature('*')
263 def force_previous_groups(self):
264 my_id = id(self)
266 bld = self.bld
267 stop = None
268 for g in bld.task_manager.groups:
269 for t in g.tasks_gen:
270 if id(t) == my_id:
271 stop = id(g)
272 break
273 if stop is None:
274 return
276 for g in bld.task_manager.groups:
277 if id(g) == stop:
278 break
279 for t in g.tasks_gen:
280 t.post()
281 Build.BuildContext.ENFORCE_GROUP_ORDERING = ENFORCE_GROUP_ORDERING
283 # @feature('cc')
284 # @before('apply_lib_vars')
285 # def process_objects(self):
286 # if getattr(self, 'add_objects', None):
287 # lst = self.to_list(self.add_objects)
288 # for x in lst:
289 # y = self.name_to_obj(x)
290 # if not y:
291 # raise Utils.WafError('object %r was not found in uselib_local (required by add_objects %r)' % (x, self.name))
292 # y.post()
293 # self.env.append_unique('INC_PATHS', y.env.INC_PATHS)
296 def recursive_dirlist(dir, relbase):
297 '''recursive directory list'''
298 ret = []
299 for f in os.listdir(dir):
300 f2 = dir + '/' + f
301 if os.path.isdir(f2):
302 ret.extend(recursive_dirlist(f2, relbase))
303 else:
304 ret.append(os_path_relpath(f2, relbase))
305 return ret
308 def mkdir_p(dir):
309 '''like mkdir -p'''
310 if os.path.isdir(dir):
311 return
312 mkdir_p(os.path.dirname(dir))
313 os.mkdir(dir)
316 def SUBST_VARS_RECURSIVE(string, env):
317 '''recursively expand variables'''
318 if string is None:
319 return string
320 limit=100
321 while (string.find('${') != -1 and limit > 0):
322 string = Utils.subst_vars(string, env)
323 limit -= 1
324 return string
327 def RUN_COMMAND(cmd,
328 env=None,
329 shell=False):
330 '''run a external command, return exit code or signal'''
331 if env:
332 cmd = SUBST_VARS_RECURSIVE(cmd, env)
334 status = os.system(cmd)
335 if os.WIFEXITED(status):
336 return os.WEXITSTATUS(status)
337 if os.WIFSIGNALED(status):
338 return - os.WTERMSIG(status)
339 print "Unknown exit reason %d for command: %s" (status, cmd)
340 return -1