2 # Copyright 2009, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Defer tool for SCons."""
40 # Current group name being executed by ExecuteDefer(). Set to None outside
42 _execute_defer_context
= None
46 """Named list of functions to be deferred."""
47 # If we derive DeferGroup from object, instances of it return type
48 # <class 'defer.DeferGroup'>, which prevents SCons.Util.semi_deepcopy()
49 # from calling its __semi_deepcopy__ function.
50 # TODO: Make semi_deepcopy() capable of handling classes derived from
54 """Initialize deferred function object."""
55 self
.func_env_cwd
= []
58 def __semi_deepcopy__(self
):
59 """Makes a semi-deep-copy of this object.
62 A semi-deep-copy of this object.
64 This means it copies the sets and lists contained by this object, but
65 doesn't make copies of the function pointers and environments pointed to by
68 Needed so env.Clone() makes a copy of the defer list, so that functions
69 and after-relationships subsequently added to the clone are not added to
73 c
.func_env_cwd
= self
.func_env_cwd
[:]
74 c
.after
= self
.after
.copy()
78 def SetDeferRoot(self
):
79 """Sets the current environment as the root environment for defer.
82 self: Current environment context.
84 Functions deferred by environments cloned from the root environment (that is,
85 function deferred by children of the root environment) will be executed when
86 ExecuteDefer() is called from the root environment.
88 Functions deferred by environments from which the root environment was cloned
89 (that is, functions deferred by parents of the root environment) will be
90 passed the root environment instead of the original parent environment.
91 (Otherwise, they would have no way to determine the root environment.)
93 # Set the current environment as the root for holding defer groups
94 self
['_DEFER_ROOT_ENV'] = self
96 # Deferred functions this environment got from its parents will be run in the
98 for group
in GetDeferGroups(self
).values():
99 new_list
= [(func
, self
, cwd
) for (func
, env
, cwd
) in group
.func_env_cwd
]
100 group
.func_env_cwd
= new_list
103 def GetDeferRoot(self
):
104 """Returns the root environment for defer.
107 self: Current environment context.
110 The root environment for defer. If one of this environment's parents
111 called SetDeferRoot(), returns that environment. Otherwise returns the
114 return self
.get('_DEFER_ROOT_ENV', self
)
117 def GetDeferGroups(env
):
118 """Returns the dict of defer groups from the root defer environment.
121 env: Environment context.
124 The dict of defer groups from the root defer environment.
126 return env
.GetDeferRoot()['_DEFER_GROUPS']
129 def ExecuteDefer(self
):
130 """Executes deferred functions.
133 self: Current environment context.
135 # Check for re-entrancy
136 global _execute_defer_context
137 if _execute_defer_context
:
138 raise SCons
.Errors
.UserError('Re-entrant call to ExecuteDefer().')
140 # Save directory, so SConscript functions can occur in the right subdirs
143 # If defer root is set and isn't this environment, we're being called from a
144 # sub-environment. That's not where we should be called.
145 if self
.GetDeferRoot() != self
:
146 print ('Warning: Ignoring call to ExecuteDefer() from child of the '
147 'environment passed to SetDeferRoot().')
150 # Get list of defer groups from ourselves.
151 defer_groups
= GetDeferGroups(self
)
153 # Loop through deferred functions
157 for name
, group
in defer_groups
.items():
158 if group
.after
.intersection(defer_groups
.keys()):
159 continue # Still have dependencies
162 _execute_defer_context
= name
164 # Remove this group from the list of defer groups now, in case one of
165 # the functions it calls adds back a function into that defer group.
166 del defer_groups
[name
]
168 if group
.func_env_cwd
:
169 # Run all the functions in our named group
170 for func
, env
, cwd
in group
.func_env_cwd
:
174 # The defer groups have been altered, so restart the search for
175 # functions that can be executed.
180 errmsg
= 'Error in ExecuteDefer: dependency cycle detected.\n'
181 for name
, group
in defer_groups
.items():
182 errmsg
+= ' %s after: %s\n' % (name
, group
.after
)
183 raise SCons
.Errors
.UserError(errmsg
)
185 # No longer in a defer context
186 _execute_defer_context
= None
192 def PrintDefer(self
, print_functions
=True):
193 """Prints the current defer dependency graph.
196 self: Environment in which PrintDefer() was called.
197 print_functions: Print individual functions in defer groups.
200 # Get list of defer groups from ourselves.
201 defer_groups
= GetDeferGroups(self
)
202 dgkeys
= defer_groups
.keys()
206 group
= defer_groups
[k
]
207 after
= list(group
.after
)
213 if print_functions
and group
.func_env_cwd
:
215 for func
, env
, cwd
in group
.func_env_cwd
:
216 print ' | +- %s %s' % (func
.__name
__, cwd
)
219 def Defer(self
, *args
, **kwargs
):
220 """Adds a deferred function or modifies defer dependencies.
223 self: Environment in which Defer() was called
224 args: Positional arguments
225 kwargs: Named arguments
227 The deferred function will be passed the environment used to call Defer(),
228 and will be executed in the same working directory as the calling SConscript.
229 (Exception: if this environment is cloned and the clone calls SetDeferRoot()
230 and then ExecuteDefer(), the function will be passed the root environment,
231 instead of the environment used to call Defer().)
233 All deferred functions run after all SConscripts. Additional dependencies
234 may be specified with the after= keyword.
239 # Defer func() until after all SConscripts
241 env.Defer(func, after=otherfunc)
242 # Defer func() until otherfunc() runs
244 env.Defer(func, 'bob')
245 # Defer func() until after SConscripts, put in group 'bob'
247 env.Defer(func2, after='bob')
248 # Defer func2() until after all funcs in 'bob' group have run
250 env.Defer(func3, 'sam')
251 # Defer func3() until after SConscripts, put in group 'sam'
253 env.Defer('bob', after='sam')
254 # Defer all functions in group 'bob' until after all functions in group
257 env.Defer(func4, after=['bob', 'sam'])
258 # Defer func4() until after all functions in groups 'bob' and 'sam' have
261 # Get name of group to defer and/or the a function
265 if isinstance(a
, str):
267 elif isinstance(a
, types
.FunctionType
):
269 if func
and not name
:
272 # TODO: Why not allow multiple functions? Should be ok
274 # Get list of names and/or functions this function should defer until after
276 for a
in self
.Flatten(kwargs
.get('after')):
277 if isinstance(a
, str):
278 # TODO: Should check if '$' in a, and if so, subst() it and recurse into
281 elif isinstance(a
, types
.FunctionType
):
282 after
.append(a
.__name
__)
285 raise ValueError('Defer after=%r is not a function or name' % a
)
287 # Find the deferred function
288 defer_groups
= GetDeferGroups(self
)
289 if name
not in defer_groups
:
290 defer_groups
[name
] = DeferGroup()
291 group
= defer_groups
[name
]
293 # If we were given a function, also save environment and current directory
295 group
.func_env_cwd
.append((func
, self
, os
.getcwd()))
297 # Add dependencies for the function
298 group
.after
.update(after
)
300 # If we are already inside a call to ExecuteDefer(), any functions which are
301 # deferring until after the current function must also be deferred until
302 # after this new function. In short, this means that if b() defers until
303 # after a() and a() calls Defer() to defer c(), then b() must also defer
305 if _execute_defer_context
and name
!= _execute_defer_context
:
306 for other_name
, other_group
in GetDeferGroups(self
).items():
307 if other_name
== name
:
308 continue # Don't defer after ourselves
309 if _execute_defer_context
in other_group
.after
:
310 other_group
.after
.add(name
)
314 # NOTE: SCons requires the use of this name, which fails gpylint.
315 """SCons entry point for this tool."""
316 env
.Append(_DEFER_GROUPS
={})
319 env
.AddMethod(ExecuteDefer
)
320 env
.AddMethod(GetDeferRoot
)
321 env
.AddMethod(PrintDefer
)
322 env
.AddMethod(SetDeferRoot
)