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
10 # TODO: make this a --option
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'" % (
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')
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))
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))
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
42 def runonce(function):
44 if args in runonce_ret:
45 return runonce_ret[args]
48 runonce_ret[args] = ret
54 ################################################################
55 # magic rpath handling
57 # we want a different rpath when installing and when building
58 # Note that this should really check if rpath is available on this platform
59 # and it should also honor an --enable-rpath option
61 if Options.is_install:
62 if bld.env['RPATH_ON_INSTALL']:
63 bld.env['RPATH'] = ['-Wl,-rpath=%s/lib' % bld.env.PREFIX]
67 rpath = os.path.normpath('%s/%s' % (bld.env['BUILD_DIRECTORY'], LIB_PATH))
68 bld.env.append_value('RPATH', '-Wl,-rpath=%s' % rpath)
69 Build.BuildContext.set_rpath = set_rpath
72 #############################################################
73 # return a named build cache dictionary, used to store
74 # state inside the following functions
76 def LOCAL_CACHE(ctx, name):
83 #############################################################
84 # set a value in a local cache
86 def LOCAL_CACHE_SET(ctx, cachename, key, value):
87 cache = LOCAL_CACHE(ctx, cachename)
90 #############################################################
93 def ASSERT(ctx, expression, msg):
95 sys.stderr.write("ERROR: %s\n" % msg)
97 Build.BuildContext.ASSERT = ASSERT
99 ################################################################
100 # create a list of files by pre-pending each with a subdir name
101 def SUBDIR(bld, subdir, list):
103 for l in TO_LIST(list):
104 ret = ret + os.path.normpath(os.path.join(subdir, l)) + ' '
106 Build.BuildContext.SUBDIR = SUBDIR
108 #######################################################
110 def dict_concat(d1, d2):
115 ############################################################
116 # this overrides the 'waf -v' debug output to be in a nice
117 # unix like format instead of a python list.
118 # Thanks to ita on #waf for this
119 def exec_command(self, cmd, **kw):
122 if isinstance(cmd, list):
124 debug('runner: %s' % _cmd)
126 self.log.write('%s\n' % cmd)
129 if not kw.get('cwd', None):
131 except AttributeError:
132 self.cwd = kw['cwd'] = self.bldnode.abspath()
133 return Utils.exec_command(cmd, **kw)
134 Build.BuildContext.exec_command = exec_command
137 ##########################################################
138 # add a new top level command to waf
139 def ADD_COMMAND(opt, name, function):
140 Utils.g_module.__dict__[name] = function
142 Options.Handler.ADD_COMMAND = ADD_COMMAND
145 @feature('cc', 'cshlib', 'cprogram')
146 @before('apply_core','exec_rule')
147 def process_depends_on(self):
148 '''The new depends_on attribute for build rules
149 allow us to specify a dependency on output from
150 a source generation rule'''
151 if getattr(self , 'depends_on', None):
152 lst = self.to_list(self.depends_on)
154 y = self.bld.name_to_obj(x, self.env)
155 self.bld.ASSERT(y is not None, "Failed to find dependency %s of %s" % (x, self.name))
157 if getattr(y, 'more_includes', None):
158 self.includes += " " + y.more_includes
161 #@feature('cprogram', 'cc', 'cshlib')
162 #@before('apply_core')
163 #def process_generated_dependencies(self):
164 # '''Ensure that any dependent source generation happens
165 # before any task that requires the output'''
166 # if getattr(self , 'depends_on', None):
167 # lst = self.to_list(self.depends_on)
169 # y = self.bld.name_to_obj(x, self.env)
173 #import TaskGen, Task
175 #old_post_run = Task.Task.post_run
176 #def new_post_run(self):
178 # return old_post_run(self)
180 #for y in ['cc', 'cxx']:
181 # TaskGen.classes[y].post_run = new_post_run
183 def ENABLE_MAGIC_ORDERING(bld):
184 '''enable automatic build order constraint calculation
185 see page 35 of the waf book'''
186 print "NOT Enabling magic ordering"
188 Build.BuildContext.ENABLE_MAGIC_ORDERING = ENABLE_MAGIC_ORDERING
191 os_path_relpath = getattr(os.path, 'relpath', None)
192 if os_path_relpath is None:
193 # Python < 2.6 does not have os.path.relpath, provide a replacement
194 # (imported from Python2.6.5~rc2)
195 def os_path_relpath(path, start):
196 """Return a relative version of a path"""
197 start_list = os.path.abspath(start).split("/")
198 path_list = os.path.abspath(path).split("/")
200 # Work out how much of the filepath is shared by start and path.
201 i = len(os.path.commonprefix([start_list, path_list]))
203 rel_list = ['..'] * (len(start_list)-i) + path_list[i:]
206 return os.path.join(*rel_list)
209 # this is a useful way of debugging some of the rules in waf
210 from TaskGen import feature, after
212 @after('apply_core', 'apply_obj_vars_cc')
214 if self.target == 'HEIMDAL_HEIM_ASN1':
215 print "@@@@@@@@@@@@@@2", self.includes, self.env._CCINCFLAGS
217 def unique_list(seq):
218 '''return a uniquified list in the same order as the existing list'''
222 if item in seen: continue
228 '''Split a list, preserving quoted strings and existing lists'''
229 if isinstance(str, list):
232 # the string may have had quotes in it, now we
233 # check if we did have quotes, and use the slower shlex
237 return shlex.split(str)
241 def SUBST_ENV_VAR(ctx, varname):
242 '''Substitute an environment variable for any embedded variables'''
243 return Utils.subst_vars(ctx.env[varname], ctx.env)
244 Build.BuildContext.SUBST_ENV_VAR = SUBST_ENV_VAR
247 def ENFORCE_GROUP_ORDERING(bld):
248 '''enforce group ordering for the project. This
249 makes the group ordering apply only when you specify
250 a target with --target'''
251 if Options.options.compile_targets:
253 def force_previous_groups(self):
258 for g in bld.task_manager.groups:
259 for t in g.tasks_gen:
266 for g in bld.task_manager.groups:
269 for t in g.tasks_gen:
271 Build.BuildContext.ENFORCE_GROUP_ORDERING = ENFORCE_GROUP_ORDERING
274 # @before('apply_lib_vars')
275 # def process_objects(self):
276 # if getattr(self, 'add_objects', None):
277 # lst = self.to_list(self.add_objects)
279 # y = self.name_to_obj(x)
281 # raise Utils.WafError('object %r was not found in uselib_local (required by add_objects %r)' % (x, self.name))
283 # self.env.append_unique('INC_PATHS', y.env.INC_PATHS)
286 def recursive_dirlist(dir, relbase):
287 '''recursive directory list'''
289 for f in os.listdir(dir):
291 if os.path.isdir(f2):
292 ret.extend(recursive_dirlist(f2, relbase))
294 ret.append(os_path_relpath(f2, relbase))
300 if os.path.isdir(dir):
302 mkdir_p(os.path.dirname(dir))