summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Node/__init__.py
blob: 03e347e38b2fa907bb6c247fb4bc1028af175d4a (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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""SCons.Node

The Node package for the SCons software construction utility.

"""

#
# 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__"



from SCons.Errors import BuildError
import string
import types
import copy
import sys

# Node states
#
# These are in "priority" order, so that the maximum value for any
# child/dependency of a node represents the state of that node if
# it has no builder of its own.  The canonical example is a file
# system directory, which is only up to date if all of its children
# were up to date.
pending = 1
executing = 2
up_to_date = 3
executed = 4
failed = 5

class Node:
    """The base Node class, for entities that we know how to
    build, or use to build other Nodes.
    """

    def __init__(self):
        self.sources = []       # source files used to build node
        self.depends = []       # explicit dependencies (from Depends)
        self.implicit = {}	# implicit (scanned) dependencies
        self.ignore = []	# dependencies to ignore
        self.parents = []
        self.wkids = None       # Kids yet to walk, when it's an array
	self.builder = None
        self.scanners = []
        self.scanned = {}
	self.env = None
        self.state = None
        self.bsig = None
        self.csig = None
        self.use_signature = 1
        self.precious = None

    def build(self):
        """Actually build the node.   Return the status from the build."""
	if not self.builder:
	    return None
        try:
            # If this Builder instance has already been called,
            # there will already be an associated status.
            stat = self.builder.status
        except AttributeError:
            if not self.precious:
                self.remove()
            try:
                stat = self.builder.execute(env = self.env.Dictionary(),
                                            target = self,
                                            source = self.sources)
            except:
                raise BuildError(self, "Exception",
                                 sys.exc_type,
                                 sys.exc_value,
                                 sys.exc_traceback)
        if stat:
            raise BuildError(node = self, errstr = "Error %d" % stat)

        # If we succesfully build a node, then we need to rescan for
        # implicit dependencies, since it might have changed on us.

        # XXX Modify this so we only rescan using the scanner(s) relevant
        # to this build.
        for scn in self.scanners:
            try:
                del self.scanned[scn]
            except KeyError:
                pass
        
        self.scan()

        for scn in self.scanners:
            try:
                for dep in self.implicit[scn]:
                    w=Walker(dep)
                    while not w.is_done():
                        w.next().scan()
            except KeyError:
                pass
	return stat

    def builder_set(self, builder):
	self.builder = builder

    def builder_sig_adapter(self):
        """Create an adapter for calculating a builder's signature.

        The underlying signature class will call get_contents()
        to fetch the signature of a builder, but the actual
        content of that signature depends on the node and the
        environment (for construction variable substitution),
        so this adapter provides the right glue between the two.
        """
        class Adapter:
            def __init__(self, node):
                self.node = node
            def get_contents(self):
                env = self.node.env.Dictionary()
                try:
                    dir = self.node.getcwd()
                except AttributeError:
                    dir = None
                return self.node.builder.get_contents(env = env, dir = dir)
        return Adapter(self)

    def scanner_set(self, scanner):
        if not scanner in self.scanners:
            self.scanners.append(scanner)

    def scan(self):
        for scn in self.scanners:
            self.scanned[scn] = 1

    def env_set(self, env, safe=0):
        if safe and self.env:
            return
	self.env = env

    def get_bsig(self):
        """Get the node's build signature (based on the signatures
        of its dependency files and build information)."""
        return self.bsig

    def set_bsig(self, bsig):
        """Set the node's build signature (based on the signatures
        of its dependency files and build information)."""
        self.bsig = bsig

    def get_csig(self):
        """Get the signature of the node's content."""
        return self.csig

    def set_csig(self, csig):
        """Set the signature of the node's content."""
        self.csig = csig

    def store_sigs(self):
        """Make the signatures permanent (that is, store them in the
        .sconsign file or equivalent)."""
        pass

    def set_precious(self, precious = 1):
        """Set the Node's precious value."""
        self.precious = precious

    def remove(self):
        """Remove this Node's external object:  no-op by default."""
        pass

    def add_dependency(self, depend):
	"""Adds dependencies. The depend argument must be a list."""
        self._add_child(self.depends, depend)

    def add_ignore(self, depend):
        """Adds dependencies to ignore. The depend argument must be a list."""
        self._add_child(self.ignore, depend)

    def add_source(self, source):
	"""Adds sources. The source argument must be a list."""
        self._add_child(self.sources, source)

    def add_implicit(self, implicit, key):
        """Adds implicit (scanned) dependencies. The implicit
        argument must be a list."""
        if not self.implicit.has_key(key):
             self.implicit[key] = []
        self._add_child(self.implicit[key], implicit)

    def _add_child(self, collection, child):
        """Adds 'child' to 'collection'. The 'child' argument must be a list"""
        if type(child) is not type([]):
            raise TypeError("child must be a list")
	child = filter(lambda x, s=collection: x not in s, child)
	if child:
	    collection.extend(child)

        for c in child:
            c._add_parent(self)

    def _add_parent(self, parent):
        """Adds 'parent' to the list of parents of this node"""

        if parent not in self.parents: self.parents.append(parent)

    def add_wkid(self, wkid):
        """Add a node to the list of kids waiting to be evaluated"""
        if self.wkids != None:
            self.wkids.append(wkid)

    def children(self):
        #XXX Need to remove duplicates from this
        return filter(lambda x, i=self.ignore: x not in i,
                      self.sources \
                      + self.depends \
                      + reduce(lambda x, y: x + y, self.implicit.values(), []))

    def get_parents(self):
        return self.parents

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

    def get_state(self):
        return self.state

    def current(self):
        return None

    def children_are_executed(self):
        return reduce(lambda x,y: ((y.get_state() == executed
                                   or y.get_state() == up_to_date)
                                   and x),
                      self.children(),
                      1)

def get_children(node): return node.children()
def ignore_cycle(node, stack): pass

class Walker:
    """An iterator for walking a Node tree.

    This is depth-first, children are visited before the parent.
    The Walker object can be initialized with any node, and
    returns the next node on the descent with each next() call.
    'kids_func' is an optional function that will be called to
    get the children of a node instead of calling 'children'. 
    'cycle_func' is an optional function that will be called
    when a cycle is detected.
    
    This class does not get caught in node cycles caused, for example,
    by C header file include loops.
    """
    def __init__(self, node, kids_func=get_children, cycle_func=ignore_cycle):
        self.kids_func = kids_func
        self.cycle_func = cycle_func
        node.wkids = copy.copy(kids_func(node))
        self.stack = [node]
        self.history = {} # used to efficiently detect and avoid cycles
        self.history[node] = None

    def next(self):
	"""Return the next node for this walk of the tree.

	This function is intentionally iterative, not recursive,
	to sidestep any issues of stack size limitations.
	"""

	while self.stack:
            if self.stack[-1].wkids:
                node = self.stack[-1].wkids.pop(0)
                if not self.stack[-1].wkids:
                    self.stack[-1].wkids = None
                if self.history.has_key(node):
                    self.cycle_func(node, self.stack)
                else:
                    node.wkids = copy.copy(self.kids_func(node))
                    self.stack.append(node)
                    self.history[node] = None
            else:
                node = self.stack.pop()
                del self.history[node]
                return node

    def is_done(self):
        return not self.stack