1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
"""SCons.Taskmaster
Generic Taskmaster.
"""
#
# Copyright (c) 2001, 2002 Steven Knight
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import SCons.Node
import string
import SCons.Errors
import copy
class Task:
"""Default SCons build engine task.
This controls the interaction of the actual building of node
and the rest of the engine.
This is expected to handle all of the normally-customizable
aspects of controlling a build, so any given application
*should* be able to do what it wants by sub-classing this
class and overriding methods as appropriate. If an application
needs to customze something by sub-classing Taskmaster (or
some other build engine class), we should first try to migrate
that functionality into this class.
Note that it's generally a good idea for sub-classes to call
these methods explicitly to update state, etc., rather than
roll their own interaction with Taskmaster from scratch."""
def __init__(self, tm, targets, top):
self.tm = tm
self.targets = targets
self.top = top
def execute(self):
if self.targets[0].get_state() != SCons.Node.up_to_date:
self.targets[0].build()
def get_target(self):
"""Fetch the target being built or updated by this task.
"""
return self.targets[0]
def set_tstates(self, state):
"""Set all of the target nodes's states."""
for t in self.targets:
t.set_state(state)
def executed(self):
"""Called when the task has been successfully executed.
This may have been a do-nothing operation (to preserve
build order), so check the node's state before updating
things. Most importantly, this calls back to the
Taskmaster to put any node tasks waiting on this one
back on the pending list."""
if self.targets[0].get_state() == SCons.Node.executing:
self.set_tstates(SCons.Node.executed)
for t in self.targets:
t.store_sigs()
parents = {}
for p in reduce(lambda x, y: x + y.get_parents(), self.targets, []):
parents[p] = 1
ready = filter(lambda x: (x.get_state() == SCons.Node.pending
and x.children_are_executed()),
parents.keys())
tasks = {}
for t in map(lambda r: r.task, ready):
tasks[t] = 1
self.tm.pending_to_ready(tasks.keys())
def failed(self):
"""Default action when a task fails: stop the build."""
self.fail_stop()
def fail_stop(self):
"""Explicit stop-the-build failure."""
self.set_tstates(SCons.Node.failed)
self.tm.stop()
def fail_continue(self):
"""Explicit continue-the-build failure.
This sets failure status on the target nodes and all of
their dependent parent nodes.
"""
nodes = {}
for t in self.targets:
def get_parents(node): return node.get_parents()
walker = SCons.Node.Walker(t, get_parents)
while 1:
n = walker.next()
if n == None: break
nodes[n] = 1
pending = filter(lambda x: x.get_state() == SCons.Node.pending,
nodes.keys())
tasks = {}
for t in map(lambda r: r.task, pending):
tasks[t] = 1
self.tm.pending_remove(tasks.keys())
def make_ready(self):
"""Make a task ready for execution."""
state = SCons.Node.up_to_date
for t in self.targets:
bsig = self.tm.calc.bsig(t)
t.set_bsig(bsig)
if not self.tm.calc.current(t, bsig):
state = SCons.Node.executing
self.set_tstates(state)
self.tm.add_ready(self)
class Calc:
def bsig(self, node):
"""
"""
return None
def current(self, node, sig):
"""Default SCons build engine is-it-current function.
This returns "always out of date," so every node is always
built/visited.
"""
return 0
class Taskmaster:
"""A generic Taskmaster for handling a bunch of targets.
Classes that override methods of this class should call
the base class method, so this class can do its thing.
"""
def __init__(self, targets=[], tasker=Task, calc=Calc()):
def out_of_date(node):
# Scan the file before fetching its children().
node.scan()
return filter(lambda x: x.get_state() != SCons.Node.up_to_date,
node.children())
def cycle_error(node, stack):
if node.builder:
nodes = stack + [node]
nodes.reverse()
desc = "Dependency cycle: " + string.join(map(str, nodes), " -> ")
raise SCons.Errors.UserError, desc
#XXX In Python 2.2 we can get rid of f1 and f2:
self.walkers = map(lambda x, f1=out_of_date, f2=cycle_error: SCons.Node.Walker(x, f1, f2),
targets)
self.tasker = tasker
self.calc = calc
self.ready = []
self.pending = 0
self._find_next_ready_node()
def next_task(self):
"""Return the next task to be executed."""
if self.ready:
task = self.ready.pop()
if not self.ready:
self._find_next_ready_node()
return task
else:
return None
def _find_next_ready_node(self):
"""Find the next node that is ready to be built"""
while self.walkers:
n = self.walkers[0].next()
if n == None:
self.walkers.pop(0)
continue
if n.get_state():
# The state is set, so someone has already been here
# (finished or currently executing). Find another one.
continue
if not n.builder:
# It's a source file, we don't need to build it,
# but mark it as "up to date" so targets won't
# wait for it.
n.set_state(SCons.Node.up_to_date)
# set the signature for non-derived files
# here so they don't get recalculated over
# and over again:
n.set_csig(self.calc.csig(n))
continue
try:
tlist = n.builder.targets(n)
except AttributeError:
tlist = [ n ]
task = self.tasker(self, tlist, self.walkers[0].is_done())
if not tlist[0].children_are_executed():
for t in tlist:
t.set_state(SCons.Node.pending)
t.task = task
self.pending = self.pending + 1
continue
task.make_ready()
return
def is_blocked(self):
return not self.ready and self.pending
def stop(self):
"""Stop the current build completely."""
self.walkers = []
self.pending = 0
self.ready = []
def add_ready(self, task):
"""Add a task to the ready queue.
"""
self.ready.append(task)
def pending_to_ready(self, tasks):
"""Move the specified tasks from the pending count
to the 'ready' queue.
"""
self.pending_remove(tasks)
for t in tasks:
t.make_ready()
def pending_remove(self, tasks):
"""Remove tasks from the pending count.
We assume that the caller has already confirmed that
the nodes in this task are in pending state.
"""
self.pending = self.pending - len(tasks)
|