]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/MetaFileTable.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileTable.py
1 ## @file
2 # This file is used to create/update/query/erase a meta file table
3 #
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 ##
9 # Import Modules
10 #
11 from __future__ import absolute_import
12 import uuid
13
14 import Common.EdkLogger as EdkLogger
15 from Common.BuildToolError import FORMAT_INVALID
16
17 from CommonDataClass.DataClass import MODEL_FILE_DSC, MODEL_FILE_DEC, MODEL_FILE_INF, \
18 MODEL_FILE_OTHERS
19 from Common.DataType import *
20
21 class MetaFileTable():
22 # TRICK: use file ID as the part before '.'
23 _ID_STEP_ = 1
24 _ID_MAX_ = 99999999
25
26 ## Constructor
27 def __init__(self, DB, MetaFile, FileType, Temporary, FromItem=None):
28 self.MetaFile = MetaFile
29 self.TableName = ""
30 self.DB = DB
31 self._NumpyTab = None
32
33 self.CurrentContent = []
34 DB.TblFile.append([MetaFile.Name,
35 MetaFile.Ext,
36 MetaFile.Dir,
37 MetaFile.Path,
38 FileType,
39 MetaFile.TimeStamp,
40 FromItem])
41 self.FileId = len(DB.TblFile)
42 self.ID = self.FileId * 10**8
43 if Temporary:
44 self.TableName = "_%s_%s_%s" % (FileType, len(DB.TblFile), uuid.uuid4().hex)
45 else:
46 self.TableName = "_%s_%s" % (FileType, len(DB.TblFile))
47
48 def IsIntegrity(self):
49 Result = False
50 try:
51 TimeStamp = self.MetaFile.TimeStamp
52 if not self.CurrentContent:
53 Result = False
54 else:
55 Result = self.CurrentContent[-1][0] < 0
56 except Exception as Exc:
57 EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc))
58 return False
59 return Result
60
61 def SetEndFlag(self):
62 self.CurrentContent.append(self._DUMMY_)
63
64 def GetAll(self):
65 return [item for item in self.CurrentContent if item[0] >= 0 and item[-1]>=0]
66
67 ## Python class representation of table storing module data
68 class ModuleTable(MetaFileTable):
69 _COLUMN_ = '''
70 ID REAL PRIMARY KEY,
71 Model INTEGER NOT NULL,
72 Value1 TEXT NOT NULL,
73 Value2 TEXT,
74 Value3 TEXT,
75 Scope1 TEXT,
76 Scope2 TEXT,
77 BelongsToItem REAL NOT NULL,
78 StartLine INTEGER NOT NULL,
79 StartColumn INTEGER NOT NULL,
80 EndLine INTEGER NOT NULL,
81 EndColumn INTEGER NOT NULL,
82 Enabled INTEGER DEFAULT 0
83 '''
84 # used as table end flag, in case the changes to database is not committed to db file
85 _DUMMY_ = [-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1]
86
87 ## Constructor
88 def __init__(self, Db, MetaFile, Temporary):
89 MetaFileTable.__init__(self, Db, MetaFile, MODEL_FILE_INF, Temporary)
90
91 ## Insert a record into table Inf
92 #
93 # @param Model: Model of a Inf item
94 # @param Value1: Value1 of a Inf item
95 # @param Value2: Value2 of a Inf item
96 # @param Value3: Value3 of a Inf item
97 # @param Scope1: Arch of a Inf item
98 # @param Scope2 Platform os a Inf item
99 # @param BelongsToItem: The item belongs to which another item
100 # @param StartLine: StartLine of a Inf item
101 # @param StartColumn: StartColumn of a Inf item
102 # @param EndLine: EndLine of a Inf item
103 # @param EndColumn: EndColumn of a Inf item
104 # @param Enabled: If this item enabled
105 #
106 def Insert(self, Model, Value1, Value2, Value3, Scope1=TAB_ARCH_COMMON, Scope2=TAB_COMMON,
107 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):
108
109 (Value1, Value2, Value3, Scope1, Scope2) = (Value1.strip(), Value2.strip(), Value3.strip(), Scope1.strip(), Scope2.strip())
110 self.ID = self.ID + self._ID_STEP_
111 if self.ID >= (MODEL_FILE_INF + self._ID_MAX_):
112 self.ID = MODEL_FILE_INF + self._ID_STEP_
113
114 row = [ self.ID,
115 Model,
116 Value1,
117 Value2,
118 Value3,
119 Scope1,
120 Scope2,
121 BelongsToItem,
122 StartLine,
123 StartColumn,
124 EndLine,
125 EndColumn,
126 Enabled
127 ]
128 self.CurrentContent.append(row)
129 return self.ID
130
131 ## Query table
132 #
133 # @param Model: The Model of Record
134 # @param Arch: The Arch attribute of Record
135 # @param Platform The Platform attribute of Record
136 #
137 # @retval: A recordSet of all found records
138 #
139 def Query(self, Model, Arch=None, Platform=None, BelongsToItem=None):
140
141 QueryTab = self.CurrentContent
142 result = [item for item in QueryTab if item[1] == Model and item[-1]>=0 ]
143
144 if Arch is not None and Arch != TAB_ARCH_COMMON:
145 ArchList = set(['COMMON'])
146 ArchList.add(Arch)
147 result = [item for item in result if item[5] in ArchList]
148
149 if Platform is not None and Platform != TAB_COMMON:
150 Platformlist = set( ['COMMON','DEFAULT'])
151 Platformlist.add(Platform)
152 result = [item for item in result if item[6] in Platformlist]
153
154 if BelongsToItem is not None:
155 result = [item for item in result if item[7] == BelongsToItem]
156
157 result = [ [r[2],r[3],r[4],r[5],r[6],r[0],r[8]] for r in result ]
158 return result
159
160 ## Python class representation of table storing package data
161 class PackageTable(MetaFileTable):
162 _COLUMN_ = '''
163 ID REAL PRIMARY KEY,
164 Model INTEGER NOT NULL,
165 Value1 TEXT NOT NULL,
166 Value2 TEXT,
167 Value3 TEXT,
168 Scope1 TEXT,
169 Scope2 TEXT,
170 BelongsToItem REAL NOT NULL,
171 StartLine INTEGER NOT NULL,
172 StartColumn INTEGER NOT NULL,
173 EndLine INTEGER NOT NULL,
174 EndColumn INTEGER NOT NULL,
175 Enabled INTEGER DEFAULT 0
176 '''
177 # used as table end flag, in case the changes to database is not committed to db file
178 _DUMMY_ = [-1, -1, '====', '====', '====', '====', '====', -1, -1, -1, -1, -1, -1]
179
180 ## Constructor
181 def __init__(self, Cursor, MetaFile, Temporary):
182 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DEC, Temporary)
183
184 ## Insert table
185 #
186 # Insert a record into table Dec
187 #
188 # @param Model: Model of a Dec item
189 # @param Value1: Value1 of a Dec item
190 # @param Value2: Value2 of a Dec item
191 # @param Value3: Value3 of a Dec item
192 # @param Scope1: Arch of a Dec item
193 # @param Scope2: Module type of a Dec item
194 # @param BelongsToItem: The item belongs to which another item
195 # @param StartLine: StartLine of a Dec item
196 # @param StartColumn: StartColumn of a Dec item
197 # @param EndLine: EndLine of a Dec item
198 # @param EndColumn: EndColumn of a Dec item
199 # @param Enabled: If this item enabled
200 #
201 def Insert(self, Model, Value1, Value2, Value3, Scope1=TAB_ARCH_COMMON, Scope2=TAB_COMMON,
202 BelongsToItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=0):
203 (Value1, Value2, Value3, Scope1, Scope2) = (Value1.strip(), Value2.strip(), Value3.strip(), Scope1.strip(), Scope2.strip())
204 self.ID = self.ID + self._ID_STEP_
205
206 row = [ self.ID,
207 Model,
208 Value1,
209 Value2,
210 Value3,
211 Scope1,
212 Scope2,
213 BelongsToItem,
214 StartLine,
215 StartColumn,
216 EndLine,
217 EndColumn,
218 Enabled
219 ]
220 self.CurrentContent.append(row)
221 return self.ID
222
223 ## Query table
224 #
225 # @param Model: The Model of Record
226 # @param Arch: The Arch attribute of Record
227 #
228 # @retval: A recordSet of all found records
229 #
230 def Query(self, Model, Arch=None):
231
232 QueryTab = self.CurrentContent
233 result = [item for item in QueryTab if item[1] == Model and item[-1]>=0 ]
234
235 if Arch is not None and Arch != TAB_ARCH_COMMON:
236 ArchList = set(['COMMON'])
237 ArchList.add(Arch)
238 result = [item for item in result if item[5] in ArchList]
239
240 return [[r[2], r[3], r[4], r[5], r[6], r[0], r[8]] for r in result]
241
242 def GetValidExpression(self, TokenSpaceGuid, PcdCName):
243
244 QueryTab = self.CurrentContent
245 result = [[item[2], item[8]] for item in QueryTab if item[3] == TokenSpaceGuid and item[4] == PcdCName]
246 validateranges = []
247 validlists = []
248 expressions = []
249 try:
250 for row in result:
251 comment = row[0]
252
253 LineNum = row[1]
254 comment = comment.strip("#")
255 comment = comment.strip()
256 oricomment = comment
257 if comment.startswith("@ValidRange"):
258 comment = comment.replace("@ValidRange", "", 1)
259 validateranges.append(comment.split("|")[1].strip())
260 if comment.startswith("@ValidList"):
261 comment = comment.replace("@ValidList", "", 1)
262 validlists.append(comment.split("|")[1].strip())
263 if comment.startswith("@Expression"):
264 comment = comment.replace("@Expression", "", 1)
265 expressions.append(comment.split("|")[1].strip())
266 except Exception as Exc:
267 ValidType = ""
268 if oricomment.startswith("@ValidRange"):
269 ValidType = "@ValidRange"
270 if oricomment.startswith("@ValidList"):
271 ValidType = "@ValidList"
272 if oricomment.startswith("@Expression"):
273 ValidType = "@Expression"
274 EdkLogger.error('Parser', FORMAT_INVALID, "The syntax for %s of PCD %s.%s is incorrect" % (ValidType, TokenSpaceGuid, PcdCName),
275 ExtraData=oricomment, File=self.MetaFile, Line=LineNum)
276 return set(), set(), set()
277 return set(validateranges), set(validlists), set(expressions)
278
279 ## Python class representation of table storing platform data
280 class PlatformTable(MetaFileTable):
281 _COLUMN_ = '''
282 ID REAL PRIMARY KEY,
283 Model INTEGER NOT NULL,
284 Value1 TEXT NOT NULL,
285 Value2 TEXT,
286 Value3 TEXT,
287 Scope1 TEXT,
288 Scope2 TEXT,
289 Scope3 TEXT,
290 BelongsToItem REAL NOT NULL,
291 FromItem REAL NOT NULL,
292 StartLine INTEGER NOT NULL,
293 StartColumn INTEGER NOT NULL,
294 EndLine INTEGER NOT NULL,
295 EndColumn INTEGER NOT NULL,
296 Enabled INTEGER DEFAULT 0
297 '''
298 # used as table end flag, in case the changes to database is not committed to db file
299 _DUMMY_ = [-1, -1, '====', '====', '====', '====', '====','====', -1, -1, -1, -1, -1, -1, -1]
300
301 ## Constructor
302 def __init__(self, Cursor, MetaFile, Temporary, FromItem=0):
303 MetaFileTable.__init__(self, Cursor, MetaFile, MODEL_FILE_DSC, Temporary, FromItem)
304
305 ## Insert table
306 #
307 # Insert a record into table Dsc
308 #
309 # @param Model: Model of a Dsc item
310 # @param Value1: Value1 of a Dsc item
311 # @param Value2: Value2 of a Dsc item
312 # @param Value3: Value3 of a Dsc item
313 # @param Scope1: Arch of a Dsc item
314 # @param Scope2: Module type of a Dsc item
315 # @param BelongsToItem: The item belongs to which another item
316 # @param FromItem: The item belongs to which dsc file
317 # @param StartLine: StartLine of a Dsc item
318 # @param StartColumn: StartColumn of a Dsc item
319 # @param EndLine: EndLine of a Dsc item
320 # @param EndColumn: EndColumn of a Dsc item
321 # @param Enabled: If this item enabled
322 #
323 def Insert(self, Model, Value1, Value2, Value3, Scope1=TAB_ARCH_COMMON, Scope2=TAB_COMMON, Scope3=TAB_DEFAULT_STORES_DEFAULT,BelongsToItem=-1,
324 FromItem=-1, StartLine=-1, StartColumn=-1, EndLine=-1, EndColumn=-1, Enabled=1):
325 (Value1, Value2, Value3, Scope1, Scope2, Scope3) = (Value1.strip(), Value2.strip(), Value3.strip(), Scope1.strip(), Scope2.strip(), Scope3.strip())
326 self.ID = self.ID + self._ID_STEP_
327
328 row = [ self.ID,
329 Model,
330 Value1,
331 Value2,
332 Value3,
333 Scope1,
334 Scope2,
335 Scope3,
336 BelongsToItem,
337 FromItem,
338 StartLine,
339 StartColumn,
340 EndLine,
341 EndColumn,
342 Enabled
343 ]
344 self.CurrentContent.append(row)
345 return self.ID
346
347
348 ## Query table
349 #
350 # @param Model: The Model of Record
351 # @param Scope1: Arch of a Dsc item
352 # @param Scope2: Module type of a Dsc item
353 # @param BelongsToItem: The item belongs to which another item
354 # @param FromItem: The item belongs to which dsc file
355 #
356 # @retval: A recordSet of all found records
357 #
358 def Query(self, Model, Scope1=None, Scope2=None, BelongsToItem=None, FromItem=None):
359
360 QueryTab = self.CurrentContent
361 result = [item for item in QueryTab if item[1] == Model and item[-1]>0 ]
362 if Scope1 is not None and Scope1 != TAB_ARCH_COMMON:
363 Sc1 = set(['COMMON'])
364 Sc1.add(Scope1)
365 result = [item for item in result if item[5] in Sc1]
366 Sc2 = set( ['COMMON','DEFAULT'])
367 if Scope2 and Scope2 != TAB_COMMON:
368 if '.' in Scope2:
369 Index = Scope2.index('.')
370 NewScope = TAB_COMMON + Scope2[Index:]
371 Sc2.add(NewScope)
372 Sc2.add(Scope2)
373 result = [item for item in result if item[6] in Sc2]
374
375 if BelongsToItem is not None:
376 result = [item for item in result if item[8] == BelongsToItem]
377 else:
378 result = [item for item in result if item[8] < 0]
379 if FromItem is not None:
380 result = [item for item in result if item[9] == FromItem]
381
382 result = [ [r[2],r[3],r[4],r[5],r[6],r[7],r[0],r[10]] for r in result ]
383 return result
384
385 def DisableComponent(self,comp_id):
386 for item in self.CurrentContent:
387 if item[0] == comp_id or item[8] == comp_id:
388 item[-1] = -1
389
390 ## Factory class to produce different storage for different type of meta-file
391 class MetaFileStorage(object):
392 _FILE_TABLE_ = {
393 MODEL_FILE_INF : ModuleTable,
394 MODEL_FILE_DEC : PackageTable,
395 MODEL_FILE_DSC : PlatformTable,
396 MODEL_FILE_OTHERS : MetaFileTable,
397 }
398
399 _FILE_TYPE_ = {
400 ".inf" : MODEL_FILE_INF,
401 ".dec" : MODEL_FILE_DEC,
402 ".dsc" : MODEL_FILE_DSC,
403 }
404 _ObjectCache = {}
405 ## Constructor
406 def __new__(Class, Cursor, MetaFile, FileType=None, Temporary=False, FromItem=None):
407 # no type given, try to find one
408 key = (MetaFile.Path, FileType,Temporary,FromItem)
409 if key in Class._ObjectCache:
410 return Class._ObjectCache[key]
411 if not FileType:
412 if MetaFile.Type in self._FILE_TYPE_:
413 FileType = Class._FILE_TYPE_[MetaFile.Type]
414 else:
415 FileType = MODEL_FILE_OTHERS
416
417 # don't pass the type around if it's well known
418 if FileType == MODEL_FILE_OTHERS:
419 Args = (Cursor, MetaFile, FileType, Temporary)
420 else:
421 Args = (Cursor, MetaFile, Temporary)
422 if FromItem:
423 Args = Args + (FromItem,)
424
425 # create the storage object and return it to caller
426 reval = Class._FILE_TABLE_[FileType](*Args)
427 if not Temporary:
428 Class._ObjectCache[key] = reval
429 return reval
430