Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / tools / scons_to_gn / conditions.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 """Conditions for scons to gn
6
7 Contains all conditions we iterate over (OS, CPU), as well as helper
8 convertion functions.
9 """
10
11 FULLARCH = {
12   'arm' : 'arm',
13   'x86' : 'x86-32',
14   'x64' : 'x86-64'
15 }
16
17 SUBARCH = {
18   'arm' : '32',
19   'x86' : '32',
20   'x64' : '64'
21 }
22
23 class Conditions(object):
24   def __init__(self, seta, setb):
25     self._set_a = seta
26     self._set_b = setb
27     self._all = ['%s_%s' % (a, b) for a in seta for b in setb]
28     self._active_condition = self._all[0]
29
30   def get(self, key, default=False):
31     os, arch = self._active_condition.split('_')
32     if key in ["TARGET_FULLARCH", "TARGET_ARCHITECTURE"]:
33       return FULLARCH[arch]
34     if key == "TARGET_SUBARCH":
35       return SUBARCH[arch]
36
37   def All(self):
38     return self._all
39
40   def Bit(self, name):
41     _, arch = self._active_condition.split('_')
42
43     if name == 'coverage_enabled':
44       return False
45
46     if name == 'build_x86':
47       return arch == 'x86' or arch == 'x64'
48
49     if name == 'build_arm':
50       return arch == 'arm'
51
52     if name == 'build_x86_32':
53       return arch == 'x86'
54
55     if name == 'build_x86_64':
56       return arch == 'x64'
57
58     if name == 'build_mips32':
59       return arch == 'mips32'
60
61     if name == 'target_arm':
62       return arch == 'arm'
63
64     if name == 'target_x86':
65       return arch == 'x86' or arch == 'x64'
66
67     if name == 'target_x86_32':
68       return arch == 'x86'
69
70     if name == 'target_x86_64':
71       return arch == 'x64'
72
73     print 'Unknown bit: ' + name
74     return False
75
76   def SetA(self):
77     return self._set_a
78
79   def SetB(self):
80     return self._set_b
81
82   def ActiveCondition(self):
83     return self._active_condition
84
85   def SetActiveCondition(self, cond):
86     if cond not in self._all:
87       raise RuntimeError('Unknown condition: ' + cond)
88     self._active_condition = cond
89
90   def WriteImports(self, fileobj):
91     if self.imports:
92       fileobj.write("\n")
93       for imp in self.imports:
94         fileobj.write('import("%s")\n' % imp)
95       fileobj.write("\n")
96
97
98 class TrustedConditions(Conditions):
99   def __init__(self):
100     OSES = ['AND', 'CHR', 'IOS',  'LIN', 'MAC', 'WIN']
101     ARCH = ['arm', 'x86', 'x64']
102     Conditions.__init__(self, OSES, ARCH)
103     self.imports = []
104
105   def Bit(self, name):
106     os, arch = self._active_condition.split('_')
107     osname = name[:3].upper()
108
109     if osname in self.SetA():
110       return osname == os
111
112     return Conditions.Bit(self, name)
113
114
115 class UntrustedConditions(Conditions):
116   def __init__(self):
117     LIBS = ['newlib', 'glibc', 'bionic']
118     ARCH = ['arm', 'x86', 'x64', 'pnacl']
119     Conditions.__init__(self, LIBS, ARCH)
120     self.imports = [
121       "//native_client/build/toolchain/nacl/nacl_sdk.gni"
122     ]
123
124   def get(self, key, default=False):
125     os, arch = self._active_condition.split('_')
126     if key == "TARGET_FULLARCH":
127       return FULLARCH[arch]
128     return Conditions.get(self, key, default)
129
130   def Bit(self, name):
131     libc, arch = self._active_condition.split('_')
132
133     if name == 'bitcode':
134       return arch == 'pnacl'
135
136     if name[:5] == 'nacl_':
137       return name[5:] == libc
138
139     return Conditions.Bit(self, name)
140
141
142 BOGUS = """
143 ALL = ['%s_%s' % (os, cpu) for os in OSES for cpu in CPUS]
144
145 CPU_TO_BIT_MAP = {}
146 BIT_TO_CPU_MAP = {}
147
148 for idx, cpu in enumerate(CPUS):
149   CPU_TO_BIT_MAP[cpu] = 1 << idx
150   BIT_TO_CPU_MAP[1 << idx] = cpu
151
152 def CPUsToBits(cpus):
153   out = 0;
154   for cpu in cpus:
155     out += CPU_TO_BIT_MAP[cpu]
156   return out
157
158
159 def BitsToCPUs(cpus):
160   out = []
161   for i in [1, 2, 4]:
162     if cpus & i:
163       out.append(BIT_TO_CPU_MAP[i])
164   return out
165 """