Followon to PR #4348: more bool fixes
[scons.git] / SCons / Node / Alias.py
blobf36a4ecd52c336c1b9d6111475eb5b83d9929f49
1 # MIT License
3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Alias nodes.
26 This creates a hash of global Aliases (dummy targets).
27 """
29 import collections
31 import SCons.Errors
32 import SCons.Node
33 import SCons.Util
34 from SCons.Util import hash_signature
36 class AliasNameSpace(collections.UserDict):
37 def Alias(self, name, **kw):
38 if isinstance(name, SCons.Node.Alias.Alias):
39 return name
40 try:
41 a = self[name]
42 except KeyError:
43 a = SCons.Node.Alias.Alias(name, **kw)
44 self[name] = a
45 return a
47 def lookup(self, name, **kw):
48 try:
49 return self[name]
50 except KeyError:
51 return None
53 class AliasNodeInfo(SCons.Node.NodeInfoBase):
54 __slots__ = ('csig',)
55 current_version_id = 2
56 field_list = ['csig']
57 def str_to_node(self, s):
58 return default_ans.Alias(s)
60 def __getstate__(self):
61 """
62 Return all fields that shall be pickled. Walk the slots in the class
63 hierarchy and add those to the state dictionary. If a '__dict__' slot is
64 available, copy all entries to the dictionary. Also include the version
65 id, which is fixed for all instances of a class.
66 """
67 state = getattr(self, '__dict__', {}).copy()
68 for obj in type(self).mro():
69 for name in getattr(obj,'__slots__',()):
70 if hasattr(self, name):
71 state[name] = getattr(self, name)
73 state['_version_id'] = self.current_version_id
74 try:
75 del state['__weakref__']
76 except KeyError:
77 pass
79 return state
81 def __setstate__(self, state) -> None:
82 """
83 Restore the attributes from a pickled state.
84 """
85 # TODO check or discard version
86 del state['_version_id']
87 for key, value in state.items():
88 if key not in ('__weakref__',):
89 setattr(self, key, value)
92 class AliasBuildInfo(SCons.Node.BuildInfoBase):
93 __slots__ = ()
94 current_version_id = 2
96 class Alias(SCons.Node.Node):
98 NodeInfo = AliasNodeInfo
99 BuildInfo = AliasBuildInfo
101 def __init__(self, name) -> None:
102 super().__init__()
103 self.name = name
104 self.changed_since_last_build = 1
105 self.store_info = 0
107 def str_for_display(self):
108 return '"' + self.__str__() + '"'
110 def __str__(self) -> str:
111 return self.name
113 def make_ready(self) -> None:
114 self.get_csig()
116 really_build = SCons.Node.Node.build
117 is_up_to_date = SCons.Node.Node.children_are_up_to_date
119 def is_under(self, dir) -> bool:
120 # Make Alias nodes get built regardless of
121 # what directory scons was run from. Alias nodes
122 # are outside the filesystem:
123 return True
125 def get_contents(self):
126 """The contents of an alias is the concatenation
127 of the content signatures of all its sources."""
128 childsigs = [n.get_csig() for n in self.children()]
129 return ''.join(childsigs)
131 def sconsign(self) -> None:
132 """An Alias is not recorded in .sconsign files"""
133 pass
139 def build(self) -> None:
140 """A "builder" for aliases."""
141 pass
143 def convert(self) -> None:
144 try: del self.builder
145 except AttributeError: pass
146 self.reset_executor()
147 self.build = self.really_build
149 def get_csig(self):
151 Generate a node's content signature, the digested signature
152 of its content.
154 node - the node
155 cache - alternate node to use for the signature cache
156 returns - the content signature
158 try:
159 return self.ninfo.csig
160 except AttributeError:
161 pass
163 contents = self.get_contents()
164 csig = hash_signature(contents)
165 self.get_ninfo().csig = csig
166 return csig
168 default_ans = AliasNameSpace()
170 SCons.Node.arg2nodes_lookups.append(default_ans.lookup)
172 # Local Variables:
173 # tab-width:4
174 # indent-tabs-mode:nil
175 # End:
176 # vim: set expandtab tabstop=4 shiftwidth=4: