Change node and .sconsign handling to separate build and content signatures.
[scons.git] / src / engine / SCons / Taskmaster.py
1 """SCons.Taskmaster
2
3 Generic Taskmaster.
4
5 """
6
7 #
8 # Copyright (c) 2001 Steven Knight
9 #
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:
17 #
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
20 #
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.
28 #
29
30 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
31
32
33
34
35 import SCons.Node
36
37
38
39 class Task:
40     """Default SCons build engine task.
41     
42     This controls the interaction of the actual building of node
43     and the rest of the engine.
44
45     This is expected to handle all of the normally-customizable
46     aspects of controlling a build, so any given application
47     *should* be able to do what it wants by sub-classing this
48     class and overriding methods as appropriate.  If an application
49     needs to customze something by sub-classing Taskmaster (or
50     some other build engine class), we should first try to migrate
51     that functionality into this class.
52     
53     Note that it's generally a good idea for sub-classes to call
54     these methods explicitly to update state, etc., rather than
55     roll their own interaction with Taskmaster from scratch."""
56     def __init__(self, tm, target, top):
57         self.tm = tm
58         self.target = target
59         self.top = top
60
61     def execute(self):
62         if not self.target.get_state() == SCons.Node.up_to_date:
63             self.target.build()
64
65     def get_target(self):
66         """Fetch the target being built or updated by this task.
67         """
68         return self.target
69
70     def set_bsig(self, bsig):
71         """Set the task's (*not* the target's)  build signature.
72
73         This will be used later to update the target's build
74         signature if the build succeeds."""
75         self.bsig = bsig
76
77     def set_tstate(self, state):
78         """Set the target node's state."""
79         self.target.set_state(state)
80
81     def executed(self):
82         """Called when the task has been successfully executed.
83
84         This may have been a do-nothing operation (to preserve
85         build order), so check the node's state before updating
86         things.  Most importantly, this calls back to the
87         Taskmaster to put any node tasks waiting on this one
88         back on the pending list."""
89         if self.target.get_state() == SCons.Node.executing:
90             self.set_tstate(SCons.Node.executed)
91             self.target.set_bsig(self.bsig)
92             self.tm.add_pending(self.target)
93
94     def failed(self):
95         """Default action when a task fails:  stop the build."""
96         self.fail_stop()
97
98     def fail_stop(self):
99         """Explicit stop-the-build failure."""
100         self.set_tstate(SCons.Node.failed)
101         self.tm.stop()
102
103     def fail_continue(self):
104         """Explicit continue-the-build failure.
105
106         This sets failure status on the target node and all of
107         its dependent parent nodes.
108         """
109         def get_parents(node): return node.get_parents()
110         walker = SCons.Node.Walker(self.target, get_parents)
111         while 1:
112             node = walker.next()
113             if node == None: break
114             self.tm.remove_pending(node)
115             node.set_state(SCons.Node.failed)
116         
117
118
119 class Calc:
120     def bsig(self, node):
121         """
122         """
123         return None
124
125     def current(self, node, sig):
126         """Default SCons build engine is-it-current function.
127     
128         This returns "always out of date," so every node is always
129         built/visited.
130         """
131         return 0
132
133
134
135 class Taskmaster:
136     """A generic Taskmaster for handling a bunch of targets.
137
138     Classes that override methods of this class should call
139     the base class method, so this class can do its thing.    
140     """
141
142     def __init__(self, targets=[], tasker=Task, calc=Calc()):
143         self.walkers = map(SCons.Node.Walker, targets)
144         self.tasker = tasker
145         self.calc = calc
146         self.ready = []
147         self.pending = 0
148         
149         self._find_next_ready_node()
150
151     def next_task(self):
152         """Return the next task to be executed."""
153         if self.ready:
154             task = self.ready.pop()
155             if not self.ready:
156                 self._find_next_ready_node()
157             return task
158         else:
159             return None
160
161     def _find_next_ready_node(self):
162         """Find the next node that is ready to be built"""
163         while self.walkers:
164             n = self.walkers[0].next()
165             if n == None:
166                 self.walkers.pop(0)
167                 continue
168             if n.get_state():
169                 # The state is set, so someone has already been here
170                 # (finished or currently executing).  Find another one.
171                 continue
172             if not n.builder:
173                 # It's a source file, we don't need to build it,
174                 # but mark it as "up to date" so targets won't
175                 # wait for it.
176                 n.set_state(SCons.Node.up_to_date)
177                 # set the signature for non-derived files
178                 # here so they don't get recalculated over
179                 # and over again:
180                 n.set_csig(self.calc.csig(n))
181                 continue
182             task = self.tasker(self, n, self.walkers[0].is_done())
183             if not n.children_are_executed():
184                 n.set_state(SCons.Node.pending)
185                 n.task = task
186                 self.pending = self.pending + 1
187                 continue
188             self.make_ready(task, n)
189             return
190             
191     def is_blocked(self):
192         return not self.ready and self.pending
193
194     def stop(self):
195         """Stop the current build completely."""
196         self.walkers = []
197         self.pending = 0
198         self.ready = []
199
200     def add_pending(self, node):
201         """Add all the pending parents that are now executable
202         to the 'ready' queue."""
203         ready = filter(lambda x: (x.get_state() == SCons.Node.pending
204                                   and x.children_are_executed()),
205                        node.get_parents())
206         for n in ready:
207             task = n.task
208             delattr(n, "task")
209             self.make_ready(task, n) 
210         self.pending = self.pending - len(ready)
211
212     def remove_pending(self, node):
213         """Remove a node from the 'ready' queue."""
214         if node.get_state() == SCons.Node.pending:
215             self.pending = self.pending - 1
216
217     def make_ready(self, task, node):
218         """Common routine that takes a single task+node and makes
219         them available on the 'ready' queue."""
220         bsig = self.calc.bsig(node)
221         task.set_bsig(bsig)
222         if self.calc.current(node, bsig):
223             task.set_tstate(SCons.Node.up_to_date)
224         else:
225             task.set_tstate(SCons.Node.executing)
226         self.ready.append(task)