summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Taskmaster.py
blob: 9e8e105497ad91a45f1289d4482f55b52d801bae (plain)
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
"""SCons.Taskmaster

Generic Taskmaster.

"""

#
# Copyright (c) 2001 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



class Task:
    """Default SCons build engine task."""
    def __init__(self, tm, target, top):
        self.tm = tm
        self.target = target
        self.sig = None
        self.top = top

    def execute(self):
        self.target.build()

    def get_target(self):
        return self.target

    def set_sig(self, sig):
        self.sig = sig

    def set_state(self, state):
        self.target.set_state(state)

    def up_to_date(self):
        self.set_state(SCons.Node.up_to_date)

    def executed(self):
        self.set_state(SCons.Node.executed)
        self.tm.add_pending(self.target)
        self.target.set_signature(self.sig)

    def failed(self):
        self.fail_stop()

    def fail_stop(self):
        self.set_state(SCons.Node.failed)
        self.tm.stop()

    def fail_continue(self):
        def get_parents(node): return node.get_parents()
        walker = SCons.Node.Walker(self.target, get_parents)
        while 1:
            node = walker.next()
            if node == None: break
            self.tm.remove_pending(node)
            node.set_state(SCons.Node.failed)
        


class Calc:
    def get_signature(self, node):
        """
        """
        return None

    def set_signature(self, node):
        """
        """
        pass

    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()):
        self.walkers = map(SCons.Node.Walker, targets)
        self.tasker = tasker
        self.calc = calc
        self.targets = targets
        self.ready = []
        self.pending = 0

        self._find_next_ready_node()

    def next_task(self):
        if self.ready:
            task = self.ready.pop()
            task.set_state(SCons.Node.executing)
            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)
                continue
            task = self.tasker(self, n, self.walkers[0].is_done())
            if not n.children_are_executed():
                n.set_state(SCons.Node.pending)
                n.task = task
                self.pending = self.pending + 1
                continue
            sig = self.calc.get_signature(n)
            task.set_sig(sig)
            if self.calc.current(n, sig):
                task.up_to_date()
            else:
                self.ready.append(task)
                return None
 
    def is_blocked(self):
        return not self.ready and self.pending

    def stop(self):
        self.walkers = []
        self.pending = 0
        self.ready = []

    def add_pending(self, node):
        # add all the pending parents that are now executable to the 'ready'
        # queue:
        ready = filter(lambda x: (x.get_state() == SCons.Node.pending
                                  and x.children_are_executed()),
                       node.get_parents())
        for n in ready:
            task = n.task
            delattr(n, "task")
            sig = self.calc.get_signature(n)
            task.set_sig(sig)
            if self.calc.current(n, sig):
                task.up_to_date()
            else:
                self.ready.append(task)
        self.pending = self.pending - len(ready)

    def remove_pending(self, node):
        if node.get_state() == SCons.Node.pending:
            self.pending = self.pending - 1