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
))
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
:
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
51 def runonce(function
):
53 if args
in runonce_ret
:
54 return runonce_ret
[args
]
57 runonce_ret
[args
] = ret
62 def install_rpath(bld
):
63 '''the rpath value for installation'''
65 bld
.env
['RPATH_ST'] = []
66 if bld
.env
.RPATH_ON_INSTALL
:
67 return ['-Wl,-rpath=%s/lib' % bld
.env
.PREFIX
]
72 '''the rpath value for build'''
73 rpath
= os
.path
.normpath('%s/%s' % (bld
.env
['BUILD_DIRECTORY'], LIB_PATH
))
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
82 #############################################################
83 # return a named build cache dictionary, used to store
84 # state inside the following functions
86 def LOCAL_CACHE(ctx
, name
):
93 #############################################################
94 # set a value in a local cache
96 def LOCAL_CACHE_SET(ctx
, cachename
, key
, value
):
97 cache
= LOCAL_CACHE(ctx
, cachename
)
100 #############################################################
101 # a build assert call
103 def ASSERT(ctx
, expression
, msg
):
105 sys
.stderr
.write("ERROR: %s\n" % msg
)
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):
113 for l
in TO_LIST(list):
114 ret
= ret
+ os
.path
.normpath(os
.path
.join(subdir
, l
)) + ' '
116 Build
.BuildContext
.SUBDIR
= SUBDIR
118 #######################################################
120 def dict_concat(d1
, d2
):
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
):
132 if isinstance(cmd
, list):
134 debug('runner: %s' % _cmd
)
136 self
.log
.write('%s\n' % cmd
)
139 if not kw
.get('cwd', None):
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
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
)
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
))
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)
179 # y = self.bld.name_to_obj(x, self.env)
183 #import TaskGen, Task
185 #old_post_run = Task.Task.post_run
186 #def new_post_run(self):
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"
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
:]
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
222 @after('apply_core', 'apply_obj_vars_cc')
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'''
232 if item
in seen
: continue
238 '''Split a list, preserving quoted strings and existing lists'''
239 if isinstance(str, list):
242 # the string may have had quotes in it, now we
243 # check if we did have quotes, and use the slower shlex
247 return shlex
.split(str)
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
:
263 def force_previous_groups(self
):
268 for g
in bld
.task_manager
.groups
:
269 for t
in g
.tasks_gen
:
276 for g
in bld
.task_manager
.groups
:
279 for t
in g
.tasks_gen
:
281 Build
.BuildContext
.ENFORCE_GROUP_ORDERING
= ENFORCE_GROUP_ORDERING
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)
289 # y = self.name_to_obj(x)
291 # raise Utils.WafError('object %r was not found in uselib_local (required by add_objects %r)' % (x, self.name))
293 # self.env.append_unique('INC_PATHS', y.env.INC_PATHS)
296 def recursive_dirlist(dir, relbase
):
297 '''recursive directory list'''
299 for f
in os
.listdir(dir):
301 if os
.path
.isdir(f2
):
302 ret
.extend(recursive_dirlist(f2
, relbase
))
304 ret
.append(os_path_relpath(f2
, relbase
))
310 if os
.path
.isdir(dir):
312 mkdir_p(os
.path
.dirname(dir))
316 def SUBST_VARS_RECURSIVE(string
, env
):
317 '''recursively expand variables'''
321 while (string
.find('${') != -1 and limit
> 0):
322 string
= Utils
.subst_vars(string
, env
)
330 '''run a external command, return exit code or signal'''
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
)