1 # Copyright (c) 2014 The Native Client Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 from conditions import *
7 """Nodes for scons to gn
9 Nodes class provides a means to store, explore and write out a tree
10 describing the table generated by interating over the scons file. The
11 tree is created in the form of:
22 def Express(use, avail, tag):
23 use = sorted([REMAP[x] for x in use])
24 avail = sorted([REMAP[x] for x in avail])
29 return '(%s == "%s")' % (tag, use[0])
33 if luse > (lavail - luse):
34 items = ['%s != "%s"' % (tag, i) for i in avail if i not in use]
35 return '(' + ' || '.join(items) + ')'
37 items = ['%s == "%s"' % (tag, i) for i in use]
38 return '(' + ' || '.join(items) + ')'
41 def Condition(os_use, os_avail, cpu_use, cpu_avail):
42 o_cond = Express(os_use, os_avail, 'os')
43 c_cond = Express(cpu_use, cpu_avail, 'cpu_arch')
45 if not o_cond and not c_cond:
49 return 'if (%s && %s) {' % (o_cond, c_cond)
52 return 'if %s {' % o_cond
53 return 'if %s {' % c_cond
63 def __init__(self, name=''):
68 def Write(self, fileobj, depth, text):
69 for line in text.split('\n'):
70 string = ' ' * depth + line + '\n'
73 def Dump(self, fileobj, depth):
74 adjust = self.DumpStart(fileobj, depth)
75 for idx, child in enumerate(self.children):
76 self.DumpChild(fileobj, child, adjust)
77 if idx != len(self.children) - 1:
79 self.DumpEnd(fileobj, depth)
81 def DumpStart(self, fileobj, depth):
82 self.Write(fileobj, depth, self.name)
85 def DumpEnd(self, fileobj, depth):
88 def DumpChild(self, fileobj, child, depth):
89 child.Dump(fileobj, depth)
91 def AddChild(self, child):
92 self.children.append(child)
95 def Examine(self, obj):
97 for child in self.children:
103 def __init__(self, name):
104 Node.__init__(self, name)
106 def DumpStart(self, fileobj, depth):
107 self.Write(fileobj, depth, "Autogenerated from %s.\n\n" % self.name)
111 class ConditionNode(Node):
112 def __init__(self, os_use, os_avail, cpu_use, cpu_avail):
113 name = Condition(os_use, os_avail, cpu_use, cpu_avail)
114 Node.__init__(self, name)
116 def Dump(self, fileobj, depth):
118 self.Write(fileobj, depth, self.name)
120 for child in self.children:
121 child.Dump(fileobj, depth)
123 self.Write(fileobj, depth - 1, '}')
126 class ObjectNode(Node):
127 def __init__(self, name, obj_type):
128 Node.__init__(self, name)
129 self.obj_type = obj_type
130 self.conditional = set()
131 self.unconditional = set()
133 def DumpStart(self, fileobj, depth):
134 self.Write(fileobj, depth, '%s("%s") {' % (self.obj_type, self.name))
137 # For every conditional only property, set and empty array
138 for cond in self.conditional:
139 if cond not in self.unconditional:
140 self.Write(fileobj, depth, '%s = []' % cond)
143 def DumpEnd(self, fileobj, depth):
144 self.Write(fileobj, depth, '}')
147 class PropertyNode(Node):
148 def __init__(self, name):
149 Node.__init__(self, name)
151 def Dump(self, fileobj, depth):
153 self.Write(fileobj, depth, '%s += [' % self.name)
155 self.Write(fileobj, depth, '%s = [' % self.name)
157 for child in self.children:
158 child.Dump(fileobj, depth + 1)
160 self.Write(fileobj, depth, ']')
163 class ValueNode(Node):
164 def __init__(self, name):
165 Node.__init__(self, name)
167 def Dump(self, fileobj, depth):
168 self.Write(fileobj, depth, '"%s",' % self.name)
171 class OrganizeProperties(object):
177 def Enter(self, node):
178 if isinstance(node, ObjectNode):
181 if isinstance(node, ConditionNode):
182 self.cond = node.name
184 if isinstance(node, PropertyNode):
185 if self.cond == None:
186 self.obj.unconditional |= set([node.name])
188 self.obj.conditional |= set([node.name])
189 node.children = sorted(node.children, key=lambda x: x.name)
191 def Exit(self, node):