2 # This file is used to define each component of DEC file
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
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.
17 import Common
.LongFilePathOs
as os
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
29 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
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
53 # This class defined the structure used in Dec object
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
60 # @param IsToPackage: Input value for IsToPackage
61 # True is to transfer to PackageObject automatically
62 # False is not to transfer to PackageObject automatically
64 # @param WorkspaceDir: Input value for current workspace directory, default is None
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
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
83 self
.Cur
= Database
.Cur
84 self
.TblFile
= Database
.TblFile
85 self
.TblDec
= Database
.TblDec
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
94 # Upper all KEYs to ignore case sensitive when parsing
96 self
.KeyList
= map(lambda c
: c
.upper(), self
.KeyList
)
102 for Key
in self
.KeyList
:
103 self
.RecordSet
[Section
[Key
]] = []
106 # Load Dec file if filename is not None
108 if Filename
is not None:
109 self
.LoadDecFile(Filename
)
112 # Transfer to Package Object if IsToPackage is True
119 # Load the file if it exists
121 # @param Filename: Input value for filename of Dec file
123 def LoadDecFile(self
, Filename
):
125 # Insert a record for file
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
)
135 #self.TblDec.Table = "Dec%s" % self.FileID
136 #self.TblDec.Create()
141 IfDefList
, SectionItemList
, CurrentSection
, ArchList
, ThirdList
, IncludeFiles
= \
142 [], [], TAB_UNKNOWN
, [], [], []
148 IsFindBlockComment
= False
150 for Line
in open(Filename
, 'r'):
153 # Remove comment block
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]
161 IsFindBlockComment
= False
162 if IsFindBlockComment
:
166 # Remove comments at tail and remove spaces again
168 Line
= CleanString(Line
)
173 # Find a new section tab
174 # First insert previous section items
175 # And then parse the content of the new section
177 if Line
.startswith(TAB_SECTION_START
) and Line
.endswith(TAB_SECTION_END
):
179 # Insert items data of previous section
181 Model
= Section
[CurrentSection
.upper()]
182 InsertSectionItemsIntoDatabase(self
.TblDec
, self
.FileID
, Filename
, Model
, CurrentSection
, SectionItemList
, ArchList
, ThirdList
, IfDefList
, self
.RecordSet
)
185 # Parse the new section
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]
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
)
204 if len(ItemList
) > 5:
205 RaiseParserError(Line
, CurrentSection
, Filename
, '', LineNo
)
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])
215 # Not in any defined section
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
)
224 SectionItemList
.append([Line
, LineNo
])
229 # Insert items data of last section
231 Model
= Section
[CurrentSection
.upper()]
232 InsertSectionItemsIntoDatabase(self
.TblDec
, self
.FileID
, Filename
, Model
, CurrentSection
, SectionItemList
, ArchList
, ThirdList
, IfDefList
, self
.RecordSet
)
235 # Replace all DEFINE macros with its actual values
237 ParseDefineMacro2(self
.TblDec
, self
.RecordSet
, GlobalData
.gGlobalDefines
)
239 ## Transfer to Package Object
241 # Transfer all contents of a Dec file to a standard Package Object
243 def DecToPackage(self
):
245 # Init global information for the file
247 ContainerFile
= self
.Identification
.FileFullPath
250 # Generate Package Header
252 self
.GenPackageHeader(ContainerFile
)
257 self
.GenIncludes(ContainerFile
)
262 self
.GenGuidProtocolPpis(DataType
.TAB_GUIDS
, ContainerFile
)
267 self
.GenGuidProtocolPpis(DataType
.TAB_PROTOCOLS
, ContainerFile
)
272 self
.GenGuidProtocolPpis(DataType
.TAB_PPIS
, ContainerFile
)
275 # Generate LibraryClasses
277 self
.GenLibraryClasses(ContainerFile
)
282 self
.GenPcds(ContainerFile
)
284 ## Get Package Header
286 # Gen Package Header of Dec as <Key> = <Value>
288 # @param ContainerFile: The Dec file full path
290 def GenPackageHeader(self
, ContainerFile
):
291 EdkLogger
.debug(2, "Generate PackageHeader ...")
293 # Update all defines item in database
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
)
306 # Get detailed information
308 for Arch
in self
.SupArchList
:
309 PackageHeader
= PackageHeaderClass()
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]
318 self
.Package
.Header
[Arch
] = PackageHeader
322 # Gen Includes of Dec
325 # @param ContainerFile: The Dec file full path
327 def GenIncludes(self
, ContainerFile
):
328 EdkLogger
.debug(2, "Generate %s ..." % TAB_INCLUDES
)
333 RecordSet
= self
.RecordSet
[MODEL_EFI_INCLUDE
]
336 # Go through each arch
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
)
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
)
352 # <CName>=<GuidValue>
354 # @param ContainerFile: The Dec file full path
356 def GenGuidProtocolPpis(self
, Type
, ContainerFile
):
357 EdkLogger
.debug(2, "Generate %s ..." % Type
)
362 RecordSet
= self
.RecordSet
[Section
[Type
.upper()]]
365 # Go through each arch
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
)
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
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
)
395 # Gen LibraryClasses of Dec
396 # <CName>=<GuidValue>
398 # @param ContainerFile: The Dec file full path
400 def GenLibraryClasses(self
, ContainerFile
):
401 EdkLogger
.debug(2, "Generate %s ..." % TAB_LIBRARY_CLASSES
)
406 RecordSet
= self
.RecordSet
[MODEL_EFI_LIBRARY_CLASS
]
409 # Go through each arch
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
)
416 RaiseParserError(Record
[0], 'LibraryClasses', ContainerFile
, '<LibraryClassName>|<LibraryClassInstanceFilename>', Record
[2])
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
)
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
)
437 # <TokenSpcCName>.<TokenCName>|<Value>|<DatumType>|<Token>
439 # @param ContainerFile: The Dec file full path
441 def GenPcds(self
, ContainerFile
):
442 EdkLogger
.debug(2, "Generate %s ..." % TAB_PCDS
)
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
]
455 # Go through each arch
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
)
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
)
491 for Key
in Pcds
.keys():
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
)
502 ## Show detailed information of Package
504 # Print all members and their values of Package class
506 def ShowPackage(self
):
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
537 # This acts like the main() function for the script, unless it is 'import'ed into another
540 if __name__
== '__main__':
541 EdkLogger
.Initialize()
542 EdkLogger
.SetLevel(EdkLogger
.DEBUG_0
)
544 W
= os
.getenv('WORKSPACE')
545 F
= os
.path
.join(W
, 'Nt32Pkg/Nt32Pkg.dec')
547 Db
= Database
.Database('Dec.db')
550 P
= Dec(os
.path
.normpath(F
), True, True, W
, Db
)