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
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
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
]
67 #############################################################
68 # return a named build cache dictionary, used to store
69 # state inside the following functions
71 def LOCAL_CACHE(ctx
, name
):
78 #############################################################
79 # set a value in a local cache
81 def LOCAL_CACHE_SET(ctx
, cachename
, key
, value
):
82 cache
= LOCAL_CACHE(ctx
, cachename
)
85 #############################################################
88 def ASSERT(ctx
, expression
, msg
):
90 sys
.stderr
.write("ERROR: %s\n" % msg
)
92 Build
.BuildContext
.ASSERT
= ASSERT
94 ################################################################
95 # create a list of files by pre-pending each with a subdir name
96 def SUBDIR(bld
, subdir
, list):
98 for l
in TO_LIST(list):
99 ret
= ret
+ os
.path
.normpath(os
.path
.join(subdir
, l
)) + ' '
101 Build
.BuildContext
.SUBDIR
= SUBDIR
103 #######################################################
105 def dict_concat(d1
, d2
):
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
):
117 if isinstance(cmd
, list):
119 debug('runner: %s' % _cmd
)
121 self
.log
.write('%s\n' % cmd
)
124 if not kw
.get('cwd', None):
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
132 ##########################################################
133 # add a new top level command to waf
134 def ADD_COMMAND(opt
, name
, function
):
135 Utils
.g_module
.__dict
__[name
] = function
137 Options
.Handler
.ADD_COMMAND
= ADD_COMMAND
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
)
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
))
152 if getattr(y
, 'more_includes', None):
153 self
.includes
+= " " + y
.more_includes
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)
164 # y = self.bld.name_to_obj(x, self.env)
168 #import TaskGen, Task
170 #old_post_run = Task.Task.post_run
171 #def new_post_run(self):
173 # return old_post_run(self)
175 #for y in ['cc', 'cxx']:
176 # TaskGen.classes[y].post_run = new_post_run
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"
183 Build
.BuildContext
.ENABLE_MAGIC_ORDERING
= ENABLE_MAGIC_ORDERING
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("/")
195 # Work out how much of the filepath is shared by start and path.
196 i
= len(os
.path
.commonprefix([start_list
, path_list
]))
198 rel_list
= ['..'] * (len(start_list
)-i
) + path_list
[i
:]
201 return os
.path
.join(*rel_list
)
204 # this is a useful way of debugging some of the rules in waf
205 from TaskGen
import feature
, after
207 @after('apply_core', 'apply_obj_vars_cc')
209 if self
.target
== 'HEIMDAL_HEIM_ASN1':
210 print "@@@@@@@@@@@@@@2", self
.includes
, self
.env
._CCINCFLAGS
212 def unique_list(seq
):
213 '''return a uniquified list in the same order as the existing list'''
217 if item
in seen
: continue
223 '''Split a list, preserving quoted strings and existing lists'''
224 if isinstance(str, list):
227 # the string may have had quotes in it, now we
228 # check if we did have quotes, and use the slower shlex
232 return shlex
.split(str)
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
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
:
248 def force_previous_groups(self
):
253 for g
in bld
.task_manager
.groups
:
254 for t
in g
.tasks_gen
:
261 for g
in bld
.task_manager
.groups
:
264 for t
in g
.tasks_gen
:
266 Build
.BuildContext
.ENFORCE_GROUP_ORDERING
= ENFORCE_GROUP_ORDERING
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)
274 # y = self.name_to_obj(x)
276 # raise Utils.WafError('object %r was not found in uselib_local (required by add_objects %r)' % (x, self.name))
278 # self.env.append_unique('INC_PATHS', y.env.INC_PATHS)
281 def recursive_dirlist(dir, relbase
):
282 '''recursive directory list'''
284 for f
in os
.listdir(dir):
286 if os
.path
.isdir(f2
):
287 ret
.extend(recursive_dirlist(f2
, relbase
))
289 ret
.append(os_path_relpath(f2
, relbase
))
295 if os
.path
.isdir(dir):
297 mkdir_p(os
.path
.dirname(dir))
304 '''run a external command, return exit code or signal'''
305 # recursively expand variables
307 while cmd
.find('${') != -1:
308 cmd
= Utils
.subst_vars(cmd
, env
)
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
)