]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/PackageDocumentTools/plugins/EdkPlugins/edk2/model/dec.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Scripts / PackageDocumentTools / plugins / EdkPlugins / edk2 / model / dec.py
1 ## @file
2 #
3 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
4 #
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 from plugins.EdkPlugins.basemodel import ini
9 import re, os
10 from plugins.EdkPlugins.basemodel.message import *
11
12 class DECFile(ini.BaseINIFile):
13
14 def GetSectionInstance(self, parent, name, isCombined=False):
15 return DECSection(parent, name, isCombined)
16
17 def GetComponents(self):
18 return self.GetSectionByName('Components')
19
20 def GetPackageRootPath(self):
21 return os.path.dirname(self.GetFilename()).strip()
22
23 def GetBaseName(self):
24 return self.GetDefine("PACKAGE_NAME").strip()
25
26 def GetVersion(self):
27 return self.GetDefine("PACKAGE_VERSION").strip()
28
29 def GetSectionObjectsByName(self, name, arch=None):
30 arr = []
31 sects = self.GetSectionByName(name)
32 for sect in sects:
33 # skip unmatched archtecture content
34 if not sect.IsArchMatch(arch):
35 continue
36
37 for obj in sect.GetObjects():
38 arr.append(obj)
39
40 return arr
41
42 class DECSection(ini.BaseINISection):
43 def GetSectionINIObject(self, parent):
44 type = self.GetType()
45
46 if type.lower().find('defines') != -1:
47 return DECDefineSectionObject(self)
48 if type.lower().find('includes') != -1:
49 return DECIncludeObject(self)
50 if type.lower().find('pcd') != -1:
51 return DECPcdObject(self)
52 if type.lower() == 'libraryclasses':
53 return DECLibraryClassObject(self)
54 if type.lower() == 'guids':
55 return DECGuidObject(self)
56 if type.lower() == 'ppis':
57 return DECPpiObject(self)
58 if type.lower() == 'protocols':
59 return DECProtocolObject(self)
60
61 return DECSectionObject(self)
62
63 def GetType(self):
64 arr = self._name.split('.')
65 return arr[0].strip()
66
67 def GetArch(self):
68 arr = self._name.split('.')
69 if len(arr) == 1:
70 return 'common'
71 return arr[1]
72
73 def IsArchMatch(self, arch):
74 if arch is None or self.GetArch() == 'common':
75 return True
76
77 if self.GetArch().lower() != arch.lower():
78 return False
79
80 return True
81
82 class DECSectionObject(ini.BaseINISectionObject):
83 def GetArch(self):
84 return self.GetParent().GetArch()
85
86 class DECDefineSectionObject(DECSectionObject):
87 def __init__(self, parent):
88 DECSectionObject.__init__(self, parent)
89 self._key = None
90 self._value = None
91
92 def Parse(self):
93 assert (self._start == self._end), 'The object in define section must be in single line'
94
95 line = self.GetLineByOffset(self._start).strip()
96
97 line = line.split('#')[0]
98 arr = line.split('=')
99 if len(arr) != 2:
100 ErrorMsg('Invalid define section object',
101 self.GetFilename(),
102 self.GetParent().GetName()
103 )
104 return False
105
106 self._key = arr[0].strip()
107 self._value = arr[1].strip()
108
109 return True
110
111 def GetKey(self):
112 return self._key
113
114 def GetValue(self):
115 return self._value
116
117 class DECGuidObject(DECSectionObject):
118 _objs = {}
119
120 def __init__(self, parent):
121 DECSectionObject.__init__(self, parent)
122 self._name = None
123
124 def Parse(self):
125 line = self.GetLineByOffset(self._start).strip().split('#')[0]
126 self._name = line.split('=')[0].strip()
127 self._guid = line.split('=')[1].strip()
128 objdict = DECGuidObject._objs
129 if self._name not in objdict.keys():
130 objdict[self._name] = [self]
131 else:
132 objdict[self._name].append(self)
133
134 return True
135
136 def GetName(self):
137 return self._name
138
139 def GetGuid(self):
140 return self._guid
141
142 def Destroy(self):
143 objdict = DECGuidObject._objs
144 objdict[self._name].remove(self)
145 if len(objdict[self._name]) == 0:
146 del objdict[self._name]
147
148 @staticmethod
149 def GetObjectDict():
150 return DECGuidObject._objs
151
152 class DECPpiObject(DECSectionObject):
153 _objs = {}
154 def __init__(self, parent):
155 DECSectionObject.__init__(self, parent)
156 self._name = None
157
158 def Parse(self):
159 line = self.GetLineByOffset(self._start).strip().split('#')[0]
160 self._name = line.split('=')[0].strip()
161 self._guid = line.split('=')[1].strip()
162 objdict = DECPpiObject._objs
163 if self._name not in objdict.keys():
164 objdict[self._name] = [self]
165 else:
166 objdict[self._name].append(self)
167
168 return True
169
170 def GetName(self):
171 return self._name
172
173 def GetGuid(self):
174 return self._guid
175
176 def Destroy(self):
177 objdict = DECPpiObject._objs
178 objdict[self._name].remove(self)
179 if len(objdict[self._name]) == 0:
180 del objdict[self._name]
181
182 @staticmethod
183 def GetObjectDict():
184 return DECPpiObject._objs
185
186 class DECProtocolObject(DECSectionObject):
187 _objs = {}
188
189 def __init__(self, parent):
190 DECSectionObject.__init__(self, parent)
191 self._name = None
192
193 def Parse(self):
194 line = self.GetLineByOffset(self._start).strip().split('#')[0]
195 self._name = line.split('=')[0].strip()
196 self._guid = line.split('=')[1].strip()
197 objdict = DECProtocolObject._objs
198 if self._name not in objdict.keys():
199 objdict[self._name] = [self]
200 else:
201 objdict[self._name].append(self)
202
203 return True
204
205 def GetName(self):
206 return self._name
207
208 def GetGuid(self):
209 return self._guid
210
211 def Destroy(self):
212 objdict = DECProtocolObject._objs
213 objdict[self._name].remove(self)
214 if len(objdict[self._name]) == 0:
215 del objdict[self._name]
216
217
218 @staticmethod
219 def GetObjectDict():
220 return DECProtocolObject._objs
221
222 class DECLibraryClassObject(DECSectionObject):
223 _objs = {}
224
225 def __init__(self, parent):
226 DECSectionObject.__init__(self, parent)
227 self.mClassName = None
228 self.mHeaderFile = None
229
230 def Parse(self):
231 line = self.GetLineByOffset(self._start).strip().split('#')[0]
232 self.mClassName, self.mHeaderFile = line.split('|')
233 objdict = DECLibraryClassObject._objs
234 if self.mClassName not in objdict.keys():
235 objdict[self.mClassName] = [self]
236 else:
237 objdict[self.mClassName].append(self)
238 return True
239
240 def GetClassName(self):
241 return self.mClassName
242
243 def GetName(self):
244 return self.mClassName
245
246 def GetHeaderFile(self):
247 return self.mHeaderFile
248
249 def Destroy(self):
250 objdict = DECLibraryClassObject._objs
251 objdict[self.mClassName].remove(self)
252 if len(objdict[self.mClassName]) == 0:
253 del objdict[self.mClassName]
254
255 @staticmethod
256 def GetObjectDict():
257 return DECLibraryClassObject._objs
258
259 class DECIncludeObject(DECSectionObject):
260 def __init__(self, parent):
261 DECSectionObject.__init__(self, parent)
262
263 def GetPath(self):
264 return self.GetLineByOffset(self._start).split('#')[0].strip()
265
266 class DECPcdObject(DECSectionObject):
267 _objs = {}
268
269 def __init__(self, parent):
270 DECSectionObject.__init__(self, parent)
271 self.mPcdName = None
272 self.mPcdDefaultValue = None
273 self.mPcdDataType = None
274 self.mPcdToken = None
275
276 def Parse(self):
277 line = self.GetLineByOffset(self._start).strip().split('#')[0]
278 (self.mPcdName, self.mPcdDefaultValue, self.mPcdDataType, self.mPcdToken) = line.split('|')
279 objdict = DECPcdObject._objs
280 if self.mPcdName not in objdict.keys():
281 objdict[self.mPcdName] = [self]
282 else:
283 objdict[self.mPcdName].append(self)
284
285 return True
286
287 def Destroy(self):
288 objdict = DECPcdObject._objs
289 objdict[self.mPcdName].remove(self)
290 if len(objdict[self.mPcdName]) == 0:
291 del objdict[self.mPcdName]
292
293 def GetPcdType(self):
294 return self.GetParent().GetType()
295
296 def GetPcdName(self):
297 return self.mPcdName
298
299 def GetPcdValue(self):
300 return self.mPcdDefaultValue
301
302 def GetPcdDataType(self):
303 return self.mPcdDataType
304
305 def GetPcdToken(self):
306 return self.mPcdToken
307
308 def GetName(self):
309 return self.GetPcdName().split('.')[1]
310
311 @staticmethod
312 def GetObjectDict():
313 return DECPcdObject._objs