4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Generic Taskmaster module for the SCons build engine.
27 This module contains the primary interface(s) between a wrapping user
28 interface and the SCons build engine. There are two key classes here:
31 This is the main engine for walking the dependency graph and
32 calling things to decide what does or doesn't need to be built.
35 This is the base class for allowing a wrapping interface to
36 decide what does or doesn't actually need to be done. The
37 intention is for a wrapping interface to subclass this as
38 appropriate for different types of behavior it may need.
40 The canonical example is the SCons native Python interface,
41 which has Task subclasses that handle its specific behavior,
42 like printing "`foo' is up to date" when a top-level target
43 doesn't need to be built, and handling the -c option by removing
44 targets as its "build" action. There is also a separate subclass
45 for suppressing this output when the -q option is used.
47 The Taskmaster instantiates a Task object for each (set of)
48 target(s) that it decides need to be evaluated and/or built.
51 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
63 StateString = SCons.Node.StateString
67 # A subsystem for recording stats about how different Nodes are handled by
68 # the main Taskmaster loop. There's no external control here (no need for
69 # a --debug= option); enable it by changing the value of CollectStats.
75 A simple class for holding statistics about the disposition of a
76 Node by the Taskmaster. If we're collecting statistics, each Node
77 processed by the Taskmaster gets one of these attached, in which case
78 the Taskmaster records its decision each time it processes the Node.
79 (Ideally, that's just once per Node.)
83 Instantiates a Taskmaster.Stats object, initializing all
84 appropriate counters to zero.
87 self.already_handled = 0
96 fmt = "%(considered)3d "\
97 "%(already_handled)3d " \
99 "%(child_failed)3d " \
101 "%(side_effects)3d " \
105 StatsNodes.sort(lambda a, b: cmp(str(a), str(b)))
107 print (fmt % n.stats.__dict__) + str(n)
113 Default SCons build engine task.
115 This controls the interaction of the actual building of node
116 and the rest of the engine.
118 This is expected to handle all of the normally-customizable
119 aspects of controlling a build, so any given application
120 *should* be able to do what it wants by sub-classing this
121 class and overriding methods as appropriate. If an application
122 needs to customze something by sub-classing Taskmaster (or
123 some other build engine class), we should first try to migrate
124 that functionality into this class.
126 Note that it's generally a good idea for sub-classes to call
127 these methods explicitly to update state, etc., rather than
128 roll their own interaction with Taskmaster from scratch.
130 def __init__(self, tm, targets, top, node):
132 self.targets = targets
137 def display(self, message):
139 Hook to allow the calling interface to display a message.
141 This hook gets called as part of preparing a task for execution
142 (that is, a Node to be built). As part of figuring out what Node
143 should be built next, the actually target list may be altered,
144 along with a message describing the alteration. The calling
145 interface can subclass Task and provide a concrete implementation
146 of this method to see those messages.
152 Called just before the task is executed.
154 This is mainly intended to give the target Nodes a chance to
155 unlink underlying files and make all necessary directories before
156 the Action is actually called to build the targets.
159 # Now that it's the appropriate time, give the TaskMaster a
160 # chance to raise any exceptions it encountered while preparing
162 self.exception_raise()
165 self.display(self.tm.message)
166 self.tm.message = None
168 for t in self.targets:
170 for s in t.side_effects:
173 def get_target(self):
174 """Fetch the target being built or updated by this task.
180 Called to execute the task.
182 This method is called from multiple threads in a parallel build,
183 so only do thread safe stuff here. Do thread unsafe stuff in
184 prepare(), executed() or failed().
188 everything_was_cached = 1
189 for t in self.targets:
190 if not t.retrieve_from_cache():
191 everything_was_cached = 0
193 if not everything_was_cached:
194 self.targets[0].build()
195 except KeyboardInterrupt:
198 exc_value = sys.exc_info()[1]
199 raise SCons.Errors.ExplicitExit(self.targets[0], exc_value.code)
200 except SCons.Errors.UserError:
202 except SCons.Errors.BuildError:
205 raise SCons.Errors.TaskmasterException(self.targets[0],
210 Called when the task has been successfully executed.
212 This may have been a do-nothing operation (to preserve build
213 order), so we have to check the node's state before deciding
214 whether it was "built" or just "visited."
216 for t in self.targets:
217 if t.get_state() == SCons.Node.executing:
218 t.set_state(SCons.Node.executed)
225 Default action when a task fails: stop the build.
231 Explicit stop-the-build failure.
233 for t in self.targets:
234 t.set_state(SCons.Node.failed)
237 # We're stopping because of a build failure, but give the
238 # calling Task class a chance to postprocess() the top-level
239 # target under which the build failure occurred.
240 self.targets = [self.tm.current_top]
243 def fail_continue(self):
245 Explicit continue-the-build failure.
247 This sets failure status on the target nodes and all of
248 their dependent parent nodes.
250 for t in self.targets:
251 # Set failure state on all of the parents that were dependent
252 # on this failed build.
253 def set_state(node): node.set_state(SCons.Node.failed)
254 t.call_for_all_waiting_parents(set_state)
256 def make_ready_all(self):
258 Marks all targets in a task ready for execution.
260 This is used when the interface needs every target Node to be
261 visited--the canonical example being the "scons -c" option.
263 self.out_of_date = self.targets[:]
264 for t in self.targets:
265 t.disambiguate().set_state(SCons.Node.executing)
266 for s in t.side_effects:
267 s.set_state(SCons.Node.executing)
269 def make_ready_current(self):
271 Marks all targets in a task ready for execution if any target
274 This is the default behavior for building only what's necessary.
276 self.out_of_date = []
277 for t in self.targets:
279 is_up_to_date = t.disambiguate().current()
280 except EnvironmentError, e:
281 raise SCons.Errors.BuildError(node=t, errstr=e.strerror, filename=e.filename)
283 t.set_state(SCons.Node.up_to_date)
285 self.out_of_date.append(t)
286 t.set_state(SCons.Node.executing)
287 for s in t.side_effects:
288 s.set_state(SCons.Node.executing)
290 make_ready = make_ready_current
292 def postprocess(self):
294 Post-processes a task after it's been executed.
296 This examines all the targets just built (or not, we don't care
297 if the build was successful, or even if there was no build
298 because everything was up-to-date) to see if they have any
299 waiting parent Nodes, or Nodes waiting on a common side effect,
300 that can be put back on the candidates list.
303 # We may have built multiple targets, some of which may have
304 # common parents waiting for this build. Count up how many
305 # targets each parent was waiting for so we can subtract the
306 # values later, and so we *don't* put waiting side-effect Nodes
307 # back on the candidates list if the Node is also a waiting
311 for t in self.targets:
312 for p in t.waiting_parents.keys():
313 parents[p] = parents.get(p, 0) + 1
315 for t in self.targets:
316 for s in t.side_effects:
317 if s.get_state() == SCons.Node.executing:
318 s.set_state(SCons.Node.no_state)
319 for p in s.waiting_parents.keys():
320 if not parents.has_key(p):
322 for p in s.waiting_s_e.keys():
324 self.tm.candidates.append(p)
326 for p, subtract in parents.items():
327 p.ref_count = p.ref_count - subtract
329 self.tm.candidates.append(p)
331 for t in self.targets:
334 # Exception handling subsystem.
336 # Exceptions that occur while walking the DAG or examining Nodes
337 # must be raised, but must be raised at an appropriate time and in
338 # a controlled manner so we can, if necessary, recover gracefully,
339 # possibly write out signature information for Nodes we've updated,
340 # etc. This is done by having the Taskmaster tell us about the
341 # exception, and letting
345 Returns info about a recorded exception.
347 return self.exception
351 Clears any recorded exception.
353 This also changes the "exception_raise" attribute to point
354 to the appropriate do-nothing method.
356 self.exception = (None, None, None)
357 self.exception_raise = self._no_exception_to_raise
359 def exception_set(self, exception=None):
361 Records an exception to be raised at the appropriate time.
363 This also changes the "exception_raise" attribute to point
364 to the method that will, in fact
367 exception = sys.exc_info()
368 self.exception = exception
369 self.exception_raise = self._exception_raise
371 def _no_exception_to_raise(self):
374 def _exception_raise(self):
376 Raises a pending exception that was recorded while getting a
377 Task ready for execution.
379 exc = self.exc_info()[:]
381 exc_type, exc_value, exc_traceback = exc
383 exc_type, exc_value = exc
385 raise exc_type, exc_value, exc_traceback
388 def find_cycle(stack):
389 if stack[0] == stack[-1]:
391 for n in stack[-1].waiting_parents.keys():
393 if find_cycle(stack):
401 The Taskmaster for walking the dependency DAG.
404 def __init__(self, targets=[], tasker=Task, order=None, trace=None):
405 self.top_targets = targets[:]
406 self.top_targets.reverse()
414 self.next_candidate = self.find_next_candidate
416 def find_next_candidate(self):
418 Returns the next candidate Node for (potential) evaluation.
420 The candidate list (really a stack) initially consists of all of
421 the top-level (command line) targets provided when the Taskmaster
422 was initialized. While we walk the DAG, visiting Nodes, all the
423 children that haven't finished processing get pushed on to the
424 candidate list. Each child can then be popped and examined in
425 turn for whether *their* children are all up-to-date, in which
426 case a Task will be created for their actual evaluation and
429 Here is where we also allow candidate Nodes to alter the list of
430 Nodes that should be examined. This is used, for example, when
431 invoking SCons in a source directory. A source directory Node can
432 return its corresponding build directory Node, essentially saying,
433 "Hey, you really need to build this thing over here instead."
436 return self.candidates.pop()
440 node = self.top_targets.pop()
443 self.current_top = node
444 alt, message = node.alter_targets()
446 self.message = message
447 self.candidates.append(node)
448 self.candidates.extend(self.order(alt))
449 node = self.candidates.pop()
452 def no_next_candidate(self):
454 Stops Taskmaster processing by not returning a next candidate.
458 def _find_next_ready_node(self):
460 Finds the next node that is ready to be built.
462 This is *the* main guts of the DAG walk. We loop through the
463 list of candidates, looking for something that has no un-built
464 children (i.e., that is a leaf Node or has dependencies that are
465 all leaf Nodes or up-to-date). Candidate Nodes are re-scanned
466 (both the target Node itself and its sources, which are always
467 scanned in the context of a given target) to discover implicit
468 dependencies. A Node that must wait for some children to be
469 built will be put back on the candidates list after the children
470 have finished building. A Node that has been put back on the
471 candidates list in this way may have itself (or its sources)
472 re-scanned, in order to handle generated header files (e.g.) and
473 the implicit dependencies therein.
475 Note that this method does not do any signature calculation or
476 up-to-date check itself. All of that is handled by the Task
477 class. This is purely concerned with the dependency graph walk.
480 self.ready_exc = None
485 node = self.next_candidate()
489 node = node.disambiguate()
490 state = node.get_state()
493 if not hasattr(node, 'stats'):
495 StatsNodes.append(node)
497 S.considered = S.considered + 1
501 if T: T.write('Taskmaster: %s:' % repr(str(node)))
503 # Skip this node if it has already been evaluated:
504 if state > SCons.Node.pending:
505 if S: S.already_handled = S.already_handled + 1
506 if T: T.write(' already handled (%s)\n' % StateString[state])
509 # Mark this node as being on the execution stack:
510 node.set_state(SCons.Node.pending)
513 children = node.children()
515 exc_value = sys.exc_info()[1]
516 e = SCons.Errors.ExplicitExit(node, exc_value.code)
517 self.ready_exc = (SCons.Errors.ExplicitExit, e)
518 if T: T.write(' SystemExit\n')
520 except KeyboardInterrupt:
521 if T: T.write(' KeyboardInterrupt\n')
524 # We had a problem just trying to figure out the
525 # children (like a child couldn't be linked in to a
526 # BuildDir, or a Scanner threw something). Arrange to
527 # raise the exception when the Task is "executed."
528 self.ready_exc = sys.exc_info()
529 if S: S.problem = S.problem + 1
530 if T: T.write(' exception\n')
534 c = map(str, children)
536 T.write(' children:\n %s\n ' % c)
538 childinfo = map(lambda N: (N.get_state(),
539 N.is_derived() or N.is_pseudo_derived(),
542 # Skip this node if any of its children have failed. This
543 # catches the case where we're descending a top-level target
544 # and one of our children failed while trying to be built
545 # by a *previous* descent of an earlier top-level target.
546 failed_children = filter(lambda I: I[0] == SCons.Node.failed,
549 node.set_state(SCons.Node.failed)
550 if S: S.child_failed = S.child_failed + 1
552 c = map(str, failed_children)
554 T.write(' children failed:\n %s\n' % c)
557 # Detect dependency cycles:
558 pending_nodes = filter(lambda I: I[0] == SCons.Node.pending, childinfo)
560 for p in pending_nodes:
561 cycle = find_cycle([p[2], node])
563 desc = "Dependency cycle: " + string.join(map(str, cycle), " -> ")
564 if T: T.write(' dependency cycle\n')
565 raise SCons.Errors.UserError, desc
567 # Select all of the dependencies that are derived targets
568 # (that is, children who have builders or are side effects).
569 derived_children = filter(lambda I: I[1], childinfo)
571 not_started = filter(lambda I: not I[0], derived_children)
573 not_started = map(lambda I: I[2], not_started)
575 # We're waiting on one more derived targets that have
576 # not yet started building. Add this node to the
577 # waiting_parents lists of those derived files so that
578 # when they've finished building, our implicit dependency
579 # list will get cleared and we'll re-scan the newly-built
580 # file(s) for updated implicit dependencies.
581 added = map(lambda n, P=node: n.add_to_waiting_parents(P), not_started)
582 node.ref_count = node.ref_count + reduce(operator.add, added, 0)
584 # Now we add these derived targets to the candidates
585 # list so they can be examined and built. We have to
586 # add ourselves back to the list first, though, so we get
587 # a chance to re-scan and build after the dependencies.
589 # We reverse the order in which the children are added
590 # to the candidates stack so the order in which they're
591 # popped matches the order in which they show up in our
592 # children's list. This is more logical / efficient for
593 # builders with multiple targets, since the "primary"
594 # target will be examined first.
595 self.candidates.append(node)
596 not_started.reverse()
597 self.candidates.extend(self.order(not_started))
599 if S: S.not_started = S.not_started + 1
601 c = map(str, not_started)
603 T.write(' waiting on unstarted children:\n %s\n' % c)
606 not_built = filter(lambda I: I[0] <= SCons.Node.executing, derived_children)
608 not_built = map(lambda I: I[2], not_built)
610 # We're waiting on one or more derived targets that have
611 # started building but not yet finished. Add this node
612 # to the waiting parents lists of those derived files
613 # so that when they've finished building, our implicit
614 # dependency list will get cleared and we'll re-scan the
615 # newly-built file(s) for updated implicit dependencies.
616 added = map(lambda n, P=node: n.add_to_waiting_parents(P), not_built)
617 node.ref_count = node.ref_count + reduce(operator.add, added, 0)
619 if S: S.not_built = S.not_built + 1
621 c = map(str, not_built)
623 T.write(' waiting on unfinished children:\n %s\n' % c)
626 # Skip this node if it has side-effects that are currently being
627 # built themselves or waiting for something else being built.
628 side_effects = filter(lambda N:
629 N.get_state() == SCons.Node.executing,
632 map(lambda n, P=node: n.add_to_waiting_s_e(P), side_effects)
633 if S: S.side_effects = S.side_effects + 1
635 c = map(str, side_effects)
637 T.write(' waiting on side effects:\n %s\n' % c)
640 # The default when we've gotten through all of the checks above:
641 # this node is ready to be built.
642 if S: S.build = S.build + 1
643 if T: T.write(' evaluating %s\n' % node)
650 Returns the next task to be executed.
652 This simply asks for the next Node to be evaluated, and then wraps
653 it in the specific Task subclass with which we were initialized.
655 node = self._find_next_ready_node()
660 tlist = node.get_executor().targets
662 task = self.tasker(self, tlist, node is self.current_top, node)
665 except KeyboardInterrupt:
668 # We had a problem just trying to get this task ready (like
669 # a child couldn't be linked in to a BuildDir when deciding
670 # whether this node is current). Arrange to raise the
671 # exception when the Task is "executed."
672 self.ready_exc = sys.exc_info()
675 task.exception_set(self.ready_exc)
677 self.ready_exc = None
683 Stops the current build completely.
685 self.next_candidate = self.no_next_candidate