BaseTools: cleanup class heirarchy
[mirror_edk2.git] / BaseTools / Source / Python / Common / DecClassObject.py
1 ## @file
2 # This file is used to define each component of DEC file
3 #
4 # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ##
15 # Import Modules
16 #
17 import Common.LongFilePathOs as os
18 from String import *
19 from DataType import *
20 from Identification import *
21 from Dictionary import *
22 from CommonDataClass.PackageClass import *
23 from CommonDataClass.CommonClass import PcdClass
24 from BuildToolError import *
25 from Table.TableDec import TableDec
26 import Database
27 from Parsing import *
28 import GlobalData
29 from Common.LongFilePathSupport import OpenLongFilePath as open
30
31 #
32 # Global variable
33 #
34 Section = {TAB_UNKNOWN.upper() : MODEL_UNKNOWN,
35 TAB_DEC_DEFINES.upper() : MODEL_META_DATA_HEADER,
36 TAB_INCLUDES.upper() : MODEL_EFI_INCLUDE,
37 TAB_LIBRARY_CLASSES.upper() : MODEL_EFI_LIBRARY_CLASS,
38 TAB_COMPONENTS.upper() : MODEL_META_DATA_COMPONENT,
39 TAB_GUIDS.upper() : MODEL_EFI_GUID,
40 TAB_PROTOCOLS.upper() : MODEL_EFI_PROTOCOL,
41 TAB_PPIS.upper() : MODEL_EFI_PPI,
42 TAB_PCDS_FIXED_AT_BUILD_NULL.upper() : MODEL_PCD_FIXED_AT_BUILD,
43 TAB_PCDS_PATCHABLE_IN_MODULE_NULL.upper() : MODEL_PCD_PATCHABLE_IN_MODULE,
44 TAB_PCDS_FEATURE_FLAG_NULL.upper() : MODEL_PCD_FEATURE_FLAG,
45 TAB_PCDS_DYNAMIC_EX_NULL.upper() : MODEL_PCD_DYNAMIC_EX,
46 TAB_PCDS_DYNAMIC_NULL.upper() : MODEL_PCD_DYNAMIC,
47 TAB_USER_EXTENSIONS.upper() : MODEL_META_DATA_USER_EXTENSION
48 }
49
50
51 ## Dec
52 #
53 # This class defined the structure used in Dec object
54 #
55 # @param Filename: Input value for Filename of Dec file, default is None
56 # @param IsMergeAllArches: Input value for IsMergeAllArches
57 # True is to merge all arches
58 # Fales is not to merge all arches
59 # default is False
60 # @param IsToPackage: Input value for IsToPackage
61 # True is to transfer to PackageObject automatically
62 # False is not to transfer to PackageObject automatically
63 # default is False
64 # @param WorkspaceDir: Input value for current workspace directory, default is None
65 #
66 # @var Identification: To store value for Identification, it is a structure as Identification
67 # @var Defines: To store value for Defines, it is a structure as DecDefines
68 # @var UserExtensions: To store value for UserExtensions
69 # @var Package: To store value for Package, it is a structure as PackageClass
70 # @var WorkspaceDir: To store value for WorkspaceDir
71 # @var Contents: To store value for Contents, it is a structure as DecContents
72 # @var KeyList: To store value for KeyList, a list for all Keys used in Dec
73 #
74 class Dec(object):
75 def __init__(self, Filename=None, IsToDatabase=False, IsToPackage=False, WorkspaceDir=None, Database=None, SupArchList=DataType.ARCH_LIST):
76 self.Identification = Identification()
77 self.Package = PackageClass()
78 self.UserExtensions = ''
79 self.WorkspaceDir = WorkspaceDir
80 self.SupArchList = SupArchList
81 self.IsToDatabase = IsToDatabase
82
83 self.Cur = Database.Cur
84 self.TblFile = Database.TblFile
85 self.TblDec = Database.TblDec
86 self.FileID = -1
87
88 self.KeyList = [
89 TAB_INCLUDES, TAB_GUIDS, TAB_PROTOCOLS, TAB_PPIS, TAB_LIBRARY_CLASSES, \
90 TAB_PCDS_FIXED_AT_BUILD_NULL, TAB_PCDS_PATCHABLE_IN_MODULE_NULL, TAB_PCDS_FEATURE_FLAG_NULL, \
91 TAB_PCDS_DYNAMIC_NULL, TAB_PCDS_DYNAMIC_EX_NULL, TAB_DEC_DEFINES
92 ]
93 #
94 # Upper all KEYs to ignore case sensitive when parsing
95 #
96 self.KeyList = map(lambda c: c.upper(), self.KeyList)
97
98 #
99 # Init RecordSet
100 #
101 self.RecordSet = {}
102 for Key in self.KeyList:
103 self.RecordSet[Section[Key]] = []
104
105 #
106 # Load Dec file if filename is not None
107 #
108 if Filename is not None:
109 self.LoadDecFile(Filename)
110
111 #
112 # Transfer to Package Object if IsToPackage is True
113 #
114 if IsToPackage:
115 self.DecToPackage()
116
117 ## Load Dec file
118 #
119 # Load the file if it exists
120 #
121 # @param Filename: Input value for filename of Dec file
122 #
123 def LoadDecFile(self, Filename):
124 #
125 # Insert a record for file
126 #
127 Filename = NormPath(Filename)
128 self.Identification.FileFullPath = Filename
129 (self.Identification.FileRelativePath, self.Identification.FileName) = os.path.split(Filename)
130 self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_DEC)
131
132 #
133 # Init DecTable
134 #
135 #self.TblDec.Table = "Dec%s" % self.FileID
136 #self.TblDec.Create()
137
138 #
139 # Init common datas
140 #
141 IfDefList, SectionItemList, CurrentSection, ArchList, ThirdList, IncludeFiles = \
142 [], [], TAB_UNKNOWN, [], [], []
143 LineNo = 0
144
145 #
146 # Parse file content
147 #
148 IsFindBlockComment = False
149 ReservedLine = ''
150 for Line in open(Filename, 'r'):
151 LineNo = LineNo + 1
152 #
153 # Remove comment block
154 #
155 if Line.find(TAB_COMMENT_EDK_START) > -1:
156 ReservedLine = GetSplitList(Line, TAB_COMMENT_EDK_START, 1)[0]
157 IsFindBlockComment = True
158 if Line.find(TAB_COMMENT_EDK_END) > -1:
159 Line = ReservedLine + GetSplitList(Line, TAB_COMMENT_EDK_END, 1)[1]
160 ReservedLine = ''
161 IsFindBlockComment = False
162 if IsFindBlockComment:
163 continue
164
165 #
166 # Remove comments at tail and remove spaces again
167 #
168 Line = CleanString(Line)
169 if Line == '':
170 continue
171
172 #
173 # Find a new section tab
174 # First insert previous section items
175 # And then parse the content of the new section
176 #
177 if Line.startswith(TAB_SECTION_START) and Line.endswith(TAB_SECTION_END):
178 #
179 # Insert items data of previous section
180 #
181 Model = Section[CurrentSection.upper()]
182 InsertSectionItemsIntoDatabase(self.TblDec, self.FileID, Filename, Model, CurrentSection, SectionItemList, ArchList, ThirdList, IfDefList, self.RecordSet)
183
184 #
185 # Parse the new section
186 #
187 SectionItemList = []
188 ArchList = []
189 ThirdList = []
190
191 CurrentSection = ''
192 LineList = GetSplitValueList(Line[len(TAB_SECTION_START):len(Line) - len(TAB_SECTION_END)], TAB_COMMA_SPLIT)
193 for Item in LineList:
194 ItemList = GetSplitValueList(Item, TAB_SPLIT)
195 if CurrentSection == '':
196 CurrentSection = ItemList[0]
197 else:
198 if CurrentSection != ItemList[0]:
199 EdkLogger.error("Parser", PARSER_ERROR, "Different section names '%s' and '%s' are found in one section definition, this is not allowed." % (CurrentSection, ItemList[0]), File=Filename, Line=LineNo, RaiseError=EdkLogger.IsRaiseError)
200 if CurrentSection.upper() not in self.KeyList:
201 RaiseParserError(Line, CurrentSection, Filename, '', LineNo)
202 ItemList.append('')
203 ItemList.append('')
204 if len(ItemList) > 5:
205 RaiseParserError(Line, CurrentSection, Filename, '', LineNo)
206 else:
207 if ItemList[1] != '' and ItemList[1].upper() not in ARCH_LIST_FULL:
208 EdkLogger.error("Parser", PARSER_ERROR, "Invalid Arch definition '%s' found" % ItemList[1], File=Filename, Line=LineNo, RaiseError=EdkLogger.IsRaiseError)
209 ArchList.append(ItemList[1].upper())
210 ThirdList.append(ItemList[2])
211
212 continue
213
214 #
215 # Not in any defined section
216 #
217 if CurrentSection == TAB_UNKNOWN:
218 ErrorMsg = "%s is not in any defined section" % Line
219 EdkLogger.error("Parser", PARSER_ERROR, ErrorMsg, File=Filename, Line=LineNo, RaiseError=EdkLogger.IsRaiseError)
220
221 #
222 # Add a section item
223 #
224 SectionItemList.append([Line, LineNo])
225 # End of parse
226 #End of For
227
228 #
229 # Insert items data of last section
230 #
231 Model = Section[CurrentSection.upper()]
232 InsertSectionItemsIntoDatabase(self.TblDec, self.FileID, Filename, Model, CurrentSection, SectionItemList, ArchList, ThirdList, IfDefList, self.RecordSet)
233
234 #
235 # Replace all DEFINE macros with its actual values
236 #
237 ParseDefineMacro2(self.TblDec, self.RecordSet, GlobalData.gGlobalDefines)
238
239 ## Transfer to Package Object
240 #
241 # Transfer all contents of a Dec file to a standard Package Object
242 #
243 def DecToPackage(self):
244 #
245 # Init global information for the file
246 #
247 ContainerFile = self.Identification.FileFullPath
248
249 #
250 # Generate Package Header
251 #
252 self.GenPackageHeader(ContainerFile)
253
254 #
255 # Generate Includes
256 #
257 self.GenIncludes(ContainerFile)
258
259 #
260 # Generate Guids
261 #
262 self.GenGuidProtocolPpis(DataType.TAB_GUIDS, ContainerFile)
263
264 #
265 # Generate Protocols
266 #
267 self.GenGuidProtocolPpis(DataType.TAB_PROTOCOLS, ContainerFile)
268
269 #
270 # Generate Ppis
271 #
272 self.GenGuidProtocolPpis(DataType.TAB_PPIS, ContainerFile)
273
274 #
275 # Generate LibraryClasses
276 #
277 self.GenLibraryClasses(ContainerFile)
278
279 #
280 # Generate Pcds
281 #
282 self.GenPcds(ContainerFile)
283
284 ## Get Package Header
285 #
286 # Gen Package Header of Dec as <Key> = <Value>
287 #
288 # @param ContainerFile: The Dec file full path
289 #
290 def GenPackageHeader(self, ContainerFile):
291 EdkLogger.debug(2, "Generate PackageHeader ...")
292 #
293 # Update all defines item in database
294 #
295 RecordSet = self.RecordSet[MODEL_META_DATA_HEADER]
296 for Record in RecordSet:
297 ValueList = GetSplitValueList(Record[0], TAB_EQUAL_SPLIT)
298 if len(ValueList) != 2:
299 RaiseParserError(Record[0], 'Defines', ContainerFile, '<Key> = <Value>', Record[2])
300 ID, Value1, Value2, Arch, LineNo = Record[3], ValueList[0], ValueList[1], Record[1], Record[2]
301 SqlCommand = """update %s set Value1 = '%s', Value2 = '%s'
302 where ID = %s""" % (self.TblDec.Table, ConvertToSqlString2(Value1), ConvertToSqlString2(Value2), ID)
303 self.TblDec.Exec(SqlCommand)
304
305 #
306 # Get detailed information
307 #
308 for Arch in self.SupArchList:
309 PackageHeader = PackageHeaderClass()
310
311 PackageHeader.Name = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_NAME, Arch, self.FileID)[0]
312 PackageHeader.Guid = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_GUID, Arch, self.FileID)[0]
313 PackageHeader.Version = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_PACKAGE_VERSION, Arch, self.FileID)[0]
314 PackageHeader.FileName = self.Identification.FileName
315 PackageHeader.FullPath = self.Identification.FileFullPath
316 PackageHeader.DecSpecification = QueryDefinesItem(self.TblDec, TAB_DEC_DEFINES_DEC_SPECIFICATION, Arch, self.FileID)[0]
317
318 self.Package.Header[Arch] = PackageHeader
319
320 ## GenIncludes
321 #
322 # Gen Includes of Dec
323 #
324 #
325 # @param ContainerFile: The Dec file full path
326 #
327 def GenIncludes(self, ContainerFile):
328 EdkLogger.debug(2, "Generate %s ..." % TAB_INCLUDES)
329 Includes = {}
330 #
331 # Get all Includes
332 #
333 RecordSet = self.RecordSet[MODEL_EFI_INCLUDE]
334
335 #
336 # Go through each arch
337 #
338 for Arch in self.SupArchList:
339 for Record in RecordSet:
340 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
341 MergeArches(Includes, Record[0], Arch)
342
343 for Key in Includes.keys():
344 Include = IncludeClass()
345 Include.FilePath = NormPath(Key)
346 Include.SupArchList = Includes[Key]
347 self.Package.Includes.append(Include)
348
349 ## GenPpis
350 #
351 # Gen Ppis of Dec
352 # <CName>=<GuidValue>
353 #
354 # @param ContainerFile: The Dec file full path
355 #
356 def GenGuidProtocolPpis(self, Type, ContainerFile):
357 EdkLogger.debug(2, "Generate %s ..." % Type)
358 Lists = {}
359 #
360 # Get all Items
361 #
362 RecordSet = self.RecordSet[Section[Type.upper()]]
363
364 #
365 # Go through each arch
366 #
367 for Arch in self.SupArchList:
368 for Record in RecordSet:
369 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
370 (Name, Value) = GetGuidsProtocolsPpisOfDec(Record[0], Type, ContainerFile, Record[2])
371 MergeArches(Lists, (Name, Value), Arch)
372 if self.IsToDatabase:
373 SqlCommand = """update %s set Value1 = '%s', Value2 = '%s'
374 where ID = %s""" % (self.TblDec.Table, ConvertToSqlString2(Name), ConvertToSqlString2(Value), Record[3])
375 self.TblDec.Exec(SqlCommand)
376
377 ListMember = None
378 if Type == TAB_GUIDS:
379 ListMember = self.Package.GuidDeclarations
380 elif Type == TAB_PROTOCOLS:
381 ListMember = self.Package.ProtocolDeclarations
382 elif Type == TAB_PPIS:
383 ListMember = self.Package.PpiDeclarations
384
385 for Key in Lists.keys():
386 ListClass = GuidProtocolPpiCommonClass()
387 ListClass.CName = Key[0]
388 ListClass.Guid = Key[1]
389 ListClass.SupArchList = Lists[Key]
390 ListMember.append(ListClass)
391
392
393 ## GenLibraryClasses
394 #
395 # Gen LibraryClasses of Dec
396 # <CName>=<GuidValue>
397 #
398 # @param ContainerFile: The Dec file full path
399 #
400 def GenLibraryClasses(self, ContainerFile):
401 EdkLogger.debug(2, "Generate %s ..." % TAB_LIBRARY_CLASSES)
402 LibraryClasses = {}
403 #
404 # Get all Guids
405 #
406 RecordSet = self.RecordSet[MODEL_EFI_LIBRARY_CLASS]
407
408 #
409 # Go through each arch
410 #
411 for Arch in self.SupArchList:
412 for Record in RecordSet:
413 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
414 List = GetSplitValueList(Record[0], DataType.TAB_VALUE_SPLIT)
415 if len(List) != 2:
416 RaiseParserError(Record[0], 'LibraryClasses', ContainerFile, '<LibraryClassName>|<LibraryClassInstanceFilename>', Record[2])
417 else:
418 CheckFileExist(self.Identification.FileRelativePath, List[1], ContainerFile, 'LibraryClasses', Record[0])
419 MergeArches(LibraryClasses, (List[0], List[1]), Arch)
420 if self.IsToDatabase:
421 SqlCommand = """update %s set Value1 = '%s', Value2 = '%s', Value3 = '%s'
422 where ID = %s""" % (self.TblDec.Table, ConvertToSqlString2(List[0]), ConvertToSqlString2(List[1]), SUP_MODULE_LIST_STRING, Record[3])
423 self.TblDec.Exec(SqlCommand)
424
425
426 for Key in LibraryClasses.keys():
427 LibraryClass = LibraryClassClass()
428 LibraryClass.LibraryClass = Key[0]
429 LibraryClass.RecommendedInstance = NormPath(Key[1])
430 LibraryClass.SupModuleList = SUP_MODULE_LIST
431 LibraryClass.SupArchList = LibraryClasses[Key]
432 self.Package.LibraryClassDeclarations.append(LibraryClass)
433
434 ## GenPcds
435 #
436 # Gen Pcds of Dec
437 # <TokenSpcCName>.<TokenCName>|<Value>|<DatumType>|<Token>
438 #
439 # @param ContainerFile: The Dec file full path
440 #
441 def GenPcds(self, ContainerFile):
442 EdkLogger.debug(2, "Generate %s ..." % TAB_PCDS)
443 Pcds = {}
444 PcdToken = {}
445 #
446 # Get all Guids
447 #
448 RecordSet1 = self.RecordSet[MODEL_PCD_FIXED_AT_BUILD]
449 RecordSet2 = self.RecordSet[MODEL_PCD_PATCHABLE_IN_MODULE]
450 RecordSet3 = self.RecordSet[MODEL_PCD_FEATURE_FLAG]
451 RecordSet4 = self.RecordSet[MODEL_PCD_DYNAMIC_EX]
452 RecordSet5 = self.RecordSet[MODEL_PCD_DYNAMIC]
453
454 #
455 # Go through each arch
456 #
457 for Arch in self.SupArchList:
458 for Record in RecordSet1:
459 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
460 (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_FIXED_AT_BUILD, ContainerFile, Record[2])
461 MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)
462 PcdToken[Record[3]] = (TokenGuidCName, TokenName)
463 for Record in RecordSet2:
464 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
465 (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_PATCHABLE_IN_MODULE, ContainerFile, Record[2])
466 MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)
467 PcdToken[Record[3]] = (TokenGuidCName, TokenName)
468 for Record in RecordSet3:
469 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
470 (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_FEATURE_FLAG, ContainerFile, Record[2])
471 MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)
472 PcdToken[Record[3]] = (TokenGuidCName, TokenName)
473 for Record in RecordSet4:
474 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
475 (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_DYNAMIC_EX, ContainerFile, Record[2])
476 MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)
477 PcdToken[Record[3]] = (TokenGuidCName, TokenName)
478 for Record in RecordSet5:
479 if Record[1] == Arch or Record[1] == TAB_ARCH_COMMON:
480 (TokenGuidCName, TokenName, Value, DatumType, Token, Type) = GetPcdOfDec(Record[0], TAB_PCDS_DYNAMIC, ContainerFile, Record[2])
481 MergeArches(Pcds, (TokenGuidCName, TokenName, Value, DatumType, Token, Type), Arch)
482 PcdToken[Record[3]] = (TokenGuidCName, TokenName)
483 #
484 # Update to database
485 #
486 if self.IsToDatabase:
487 for Key in PcdToken.keys():
488 SqlCommand = """update %s set Value2 = '%s' where ID = %s""" % (self.TblDec.Table, ".".join((PcdToken[Key][0], PcdToken[Key][1])), Key)
489 self.TblDec.Exec(SqlCommand)
490
491 for Key in Pcds.keys():
492 Pcd = PcdClass()
493 Pcd.CName = Key[1]
494 Pcd.Token = Key[4]
495 Pcd.TokenSpaceGuidCName = Key[0]
496 Pcd.DatumType = Key[3]
497 Pcd.DefaultValue = Key[2]
498 Pcd.ItemType = Key[5]
499 Pcd.SupArchList = Pcds[Key]
500 self.Package.PcdDeclarations.append(Pcd)
501
502 ## Show detailed information of Package
503 #
504 # Print all members and their values of Package class
505 #
506 def ShowPackage(self):
507 M = self.Package
508 for Arch in M.Header.keys():
509 print '\nArch =', Arch
510 print 'Filename =', M.Header[Arch].FileName
511 print 'FullPath =', M.Header[Arch].FullPath
512 print 'BaseName =', M.Header[Arch].Name
513 print 'Guid =', M.Header[Arch].Guid
514 print 'Version =', M.Header[Arch].Version
515 print 'DecSpecification =', M.Header[Arch].DecSpecification
516 print '\nIncludes =', M.Includes
517 for Item in M.Includes:
518 print Item.FilePath, Item.SupArchList
519 print '\nGuids =', M.GuidDeclarations
520 for Item in M.GuidDeclarations:
521 print Item.CName, Item.Guid, Item.SupArchList
522 print '\nProtocols =', M.ProtocolDeclarations
523 for Item in M.ProtocolDeclarations:
524 print Item.CName, Item.Guid, Item.SupArchList
525 print '\nPpis =', M.PpiDeclarations
526 for Item in M.PpiDeclarations:
527 print Item.CName, Item.Guid, Item.SupArchList
528 print '\nLibraryClasses =', M.LibraryClassDeclarations
529 for Item in M.LibraryClassDeclarations:
530 print Item.LibraryClass, Item.RecommendedInstance, Item.SupModuleList, Item.SupArchList
531 print '\nPcds =', M.PcdDeclarations
532 for Item in M.PcdDeclarations:
533 print 'CName=', Item.CName, 'TokenSpaceGuidCName=', Item.TokenSpaceGuidCName, 'DefaultValue=', Item.DefaultValue, 'ItemType=', Item.ItemType, 'Token=', Item.Token, 'DatumType=', Item.DatumType, Item.SupArchList
534
535 ##
536 #
537 # This acts like the main() function for the script, unless it is 'import'ed into another
538 # script.
539 #
540 if __name__ == '__main__':
541 EdkLogger.Initialize()
542 EdkLogger.SetLevel(EdkLogger.DEBUG_0)
543
544 W = os.getenv('WORKSPACE')
545 F = os.path.join(W, 'Nt32Pkg/Nt32Pkg.dec')
546
547 Db = Database.Database('Dec.db')
548 Db.InitDatabase()
549
550 P = Dec(os.path.normpath(F), True, True, W, Db)
551 P.ShowPackage()
552
553 Db.Close()