8 # Copyright (c) 2001 Steven Knight
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
41 """Default SCons build engine task.
43 This controls the interaction of the actual building of node
44 and the rest of the engine.
46 This is expected to handle all of the normally-customizable
47 aspects of controlling a build, so any given application
48 *should* be able to do what it wants by sub-classing this
49 class and overriding methods as appropriate. If an application
50 needs to customze something by sub-classing Taskmaster (or
51 some other build engine class), we should first try to migrate
52 that functionality into this class.
54 Note that it's generally a good idea for sub-classes to call
55 these methods explicitly to update state, etc., rather than
56 roll their own interaction with Taskmaster from scratch."""
57 def __init__(self, tm, targets, top):
59 self.targets = targets
63 if self.targets[0].get_state() != SCons.Node.up_to_date:
64 self.targets[0].build()
67 """Fetch the target being built or updated by this task.
69 return self.targets[0]
71 def set_tstates(self, state):
72 """Set all of the target nodes's states."""
73 for t in self.targets:
77 """Called when the task has been successfully executed.
79 This may have been a do-nothing operation (to preserve
80 build order), so check the node's state before updating
81 things. Most importantly, this calls back to the
82 Taskmaster to put any node tasks waiting on this one
83 back on the pending list."""
84 if self.targets[0].get_state() == SCons.Node.executing:
85 self.set_tstates(SCons.Node.executed)
86 for t in self.targets:
89 for p in reduce(lambda x, y: x + y.get_parents(), self.targets, []):
91 ready = filter(lambda x: (x.get_state() == SCons.Node.pending
92 and x.children_are_executed()),
95 for t in map(lambda r: r.task, ready):
97 self.tm.pending_to_ready(tasks.keys())
100 """Default action when a task fails: stop the build."""
104 """Explicit stop-the-build failure."""
105 self.set_tstates(SCons.Node.failed)
108 def fail_continue(self):
109 """Explicit continue-the-build failure.
111 This sets failure status on the target nodes and all of
112 their dependent parent nodes.
115 for t in self.targets:
116 def get_parents(node): return node.get_parents()
117 walker = SCons.Node.Walker(t, get_parents)
122 pending = filter(lambda x: x.get_state() == SCons.Node.pending,
125 for t in map(lambda r: r.task, pending):
127 self.tm.pending_remove(tasks.keys())
129 def make_ready(self):
130 """Make a task ready for execution."""
131 state = SCons.Node.up_to_date
132 for t in self.targets:
133 bsig = self.tm.calc.bsig(t)
135 if not self.tm.calc.current(t, bsig):
136 state = SCons.Node.executing
137 self.set_tstates(state)
138 self.tm.add_ready(self)
143 def bsig(self, node):
148 def current(self, node, sig):
149 """Default SCons build engine is-it-current function.
151 This returns "always out of date," so every node is always
159 """A generic Taskmaster for handling a bunch of targets.
161 Classes that override methods of this class should call
162 the base class method, so this class can do its thing.
165 def __init__(self, targets=[], tasker=Task, calc=Calc()):
167 def out_of_date(node):
168 # Scan the file before fetching its children().
170 return filter(lambda x: x.get_state() != SCons.Node.up_to_date,
173 def cycle_error(node, stack):
175 nodes = stack + [node]
177 desc = "Dependency cycle: " + string.join(map(str, nodes), " -> ")
178 raise SCons.Errors.UserError, desc
180 #XXX In Python 2.2 we can get rid of f1 and f2:
181 self.walkers = map(lambda x, f1=out_of_date, f2=cycle_error: SCons.Node.Walker(x, f1, f2),
188 self._find_next_ready_node()
191 """Return the next task to be executed."""
193 task = self.ready.pop()
195 self._find_next_ready_node()
200 def _find_next_ready_node(self):
201 """Find the next node that is ready to be built"""
203 n = self.walkers[0].next()
208 # The state is set, so someone has already been here
209 # (finished or currently executing). Find another one.
212 # It's a source file, we don't need to build it,
213 # but mark it as "up to date" so targets won't
215 n.set_state(SCons.Node.up_to_date)
216 # set the signature for non-derived files
217 # here so they don't get recalculated over
219 n.set_csig(self.calc.csig(n))
222 tlist = n.builder.targets(n)
223 except AttributeError:
225 task = self.tasker(self, tlist, self.walkers[0].is_done())
226 if not tlist[0].children_are_executed():
228 t.set_state(SCons.Node.pending)
230 self.pending = self.pending + 1
235 def is_blocked(self):
236 return not self.ready and self.pending
239 """Stop the current build completely."""
244 def add_ready(self, task):
245 """Add a task to the ready queue.
247 self.ready.append(task)
249 def pending_to_ready(self, tasks):
250 """Move the specified tasks from the pending count
251 to the 'ready' queue.
253 self.pending_remove(tasks)
257 def pending_remove(self, tasks):
258 """Remove tasks from the pending count.
260 We assume that the caller has already confirmed that
261 the nodes in this task are in pending state.
263 self.pending = self.pending - len(tasks)