Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client / tools / scons_to_gn / nodes.py
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.
4
5 from conditions import *
6
7 """Nodes for scons to gn
8
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:
12
13   TopNode
14     Condition
15       Object
16         Condition
17           Property
18
19 """
20
21
22 def Express(use, avail, tag):
23   use = sorted([REMAP[x] for x in use])
24   avail = sorted([REMAP[x] for x in avail])
25   if use == avail:
26     return None
27
28   if len(use) == 1:
29     return '(%s == "%s")' % (tag, use[0])
30
31   luse = len(use)
32   lavail = len(avail)
33   if luse > (lavail - luse):
34     items = ['%s != "%s"' % (tag, i) for i in avail if i not in use]
35     return '(' + ' || '.join(items) + ')'
36
37   items = ['%s == "%s"' % (tag, i) for i in use]
38   return '(' + ' || '.join(items) + ')'
39
40
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')
44
45   if not o_cond and not c_cond:
46     return None
47
48   if o_cond and c_cond:
49     return 'if (%s && %s) {' % (o_cond, c_cond)
50
51   if o_cond:
52     return 'if %s {' % o_cond
53   return 'if %s {' % c_cond
54
55 #
56 # TopNode
57 #   Condition
58 #     Object
59 #       Condition
60 #         Property
61 #
62 class Node(object):
63   def __init__(self, name=''):
64     self.children = []
65     self.name = name
66     self.parent = None
67
68   def Write(self, fileobj, depth, text):
69     for line in text.split('\n'):
70       string = '  ' * depth + line + '\n'
71       fileobj.write(string)
72
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:
78         fileobj.write('\n')
79     self.DumpEnd(fileobj, depth)
80
81   def DumpStart(self, fileobj, depth):
82     self.Write(fileobj, depth, self.name)
83     return depth
84
85   def DumpEnd(self, fileobj, depth):
86     pass
87
88   def DumpChild(self, fileobj, child, depth):
89     child.Dump(fileobj, depth)
90
91   def AddChild(self, child):
92     self.children.append(child)
93     child.parent = self
94
95   def Examine(self, obj):
96     obj.Enter(self)
97     for child in self.children:
98       child.Examine(obj)
99     obj.Exit(self)
100
101
102 class TopNode(Node):
103   def __init__(self, name):
104     Node.__init__(self, name)
105
106   def DumpStart(self, fileobj, depth):
107     self.Write(fileobj, depth, "Autogenerated from %s.\n\n" % self.name)
108     return depth
109
110
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)
115
116   def Dump(self, fileobj, depth):
117     if self.name:
118       self.Write(fileobj, depth, self.name)
119       depth += 1
120     for child in self.children:
121       child.Dump(fileobj, depth)
122     if self.name:
123       self.Write(fileobj, depth - 1, '}')
124
125
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()
132
133   def DumpStart(self, fileobj, depth):
134     self.Write(fileobj, depth, '%s("%s") {' % (self.obj_type, self.name))
135     depth += 1
136
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)
141     return depth
142
143   def DumpEnd(self, fileobj, depth):
144     self.Write(fileobj, depth, '}')
145
146
147 class PropertyNode(Node):
148   def __init__(self, name):
149     Node.__init__(self, name)
150
151   def Dump(self, fileobj, depth):
152     if self.parent.name:
153       self.Write(fileobj, depth, '%s += [' % self.name)
154     else:
155       self.Write(fileobj, depth, '%s = [' % self.name)
156
157     for child in self.children:
158       child.Dump(fileobj, depth + 1)
159
160     self.Write(fileobj, depth, ']')
161
162
163 class ValueNode(Node):
164   def __init__(self, name):
165     Node.__init__(self,  name)
166
167   def Dump(self, fileobj, depth):
168     self.Write(fileobj, depth, '"%s",' % self.name)
169
170
171 class OrganizeProperties(object):
172   def __init__(self):
173     self.cond = None
174     self.obj = None
175     pass
176
177   def Enter(self, node):
178     if isinstance(node, ObjectNode):
179       self.obj = node
180
181     if isinstance(node, ConditionNode):
182       self.cond = node.name
183
184     if isinstance(node, PropertyNode):
185       if self.cond == None:
186         self.obj.unconditional |= set([node.name])
187       else:
188         self.obj.conditional |= set([node.name])
189       node.children = sorted(node.children, key=lambda x: x.name)
190
191   def Exit(self, node):
192     pass