1 ## @file DecPomAlignment.py
2 # This file contained the adapter for convert INF parser object to POM Object
4 # Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 from __future__
import print_function
28 import Logger
.Log
as Logger
29 from Logger
import StringTable
as ST
30 from Logger
.ToolError
import UPT_MUL_DEC_ERROR
31 from Logger
.ToolError
import FORMAT_INVALID
33 from Library
.Parsing
import NormPath
34 from Library
.DataType
import ARCH_LIST
35 from Library
.DataType
import TAB_GUIDS
36 from Library
.DataType
import TAB_PROTOCOLS
37 from Library
.DataType
import TAB_PPIS
38 from Library
.DataType
import TAB_DEC_DEFINES_PACKAGE_NAME
39 from Library
.DataType
import TAB_DEC_DEFINES_PACKAGE_GUID
40 from Library
.DataType
import TAB_DEC_DEFINES_PACKAGE_VERSION
41 from Library
.DataType
import TAB_DEC_DEFINES_DEC_SPECIFICATION
42 from Library
.DataType
import TAB_DEC_DEFINES_PKG_UNI_FILE
43 from Library
.DataType
import TAB_ARCH_COMMON
44 from Library
.DataType
import TAB_INCLUDES
45 from Library
.DataType
import TAB_LIBRARY_CLASSES
46 from Library
.DataType
import TAB_PCDS
47 from Library
.DataType
import TAB_PCDS_FIXED_AT_BUILD_NULL
48 from Library
.DataType
import TAB_PCDS_PATCHABLE_IN_MODULE_NULL
49 from Library
.DataType
import TAB_PCDS_FEATURE_FLAG_NULL
50 from Library
.DataType
import TAB_PCDS_DYNAMIC_EX_NULL
51 from Library
.DataType
import TAB_PCDS_DYNAMIC_NULL
52 from Library
.DataType
import TAB_PTR_TYPE_PCD
53 from Library
.DataType
import ITEM_UNDEFINED
54 from Library
.DataType
import TAB_DEC_BINARY_ABSTRACT
55 from Library
.DataType
import TAB_DEC_BINARY_DESCRIPTION
56 from Library
.DataType
import TAB_LANGUAGE_EN_US
57 from Library
.DataType
import TAB_BINARY_HEADER_IDENTIFIER
58 from Library
.DataType
import TAB_BINARY_HEADER_USERID
59 from Library
.DataType
import TAB_LANGUAGE_EN_X
60 from Library
.DataType
import TAB_LANGUAGE_EN
61 from Library
.DataType
import TAB_STR_TOKENCNAME
62 from Library
.DataType
import TAB_STR_TOKENPROMPT
63 from Library
.DataType
import TAB_STR_TOKENHELP
64 from Library
.DataType
import TAB_STR_TOKENERR
65 from Library
.DataType
import TAB_HEX_START
66 from Library
.DataType
import TAB_SPLIT
67 import Library
.DataType
as DT
68 from Library
.CommentParsing
import ParseHeaderCommentSection
69 from Library
.CommentParsing
import ParseGenericComment
70 from Library
.CommentParsing
import ParseDecPcdGenericComment
71 from Library
.CommentParsing
import ParseDecPcdTailComment
72 from Library
.Misc
import GetFiles
73 from Library
.Misc
import Sdict
74 from Library
.Misc
import GetRelativePath
75 from Library
.Misc
import PathClass
76 from Library
.Misc
import ValidateUNIFilePath
77 from Library
.UniClassObject
import UniFileClassObject
78 from Library
.UniClassObject
import ConvertSpecialUnicodes
79 from Library
.UniClassObject
import GetLanguageCode1766
80 from Library
.ParserValidate
import IsValidPath
81 from Parser
.DecParser
import Dec
82 from Object
.POM
.PackageObject
import PackageObject
83 from Object
.POM
.CommonObject
import UserExtensionObject
84 from Object
.POM
.CommonObject
import IncludeObject
85 from Object
.POM
.CommonObject
import GuidObject
86 from Object
.POM
.CommonObject
import ProtocolObject
87 from Object
.POM
.CommonObject
import PpiObject
88 from Object
.POM
.CommonObject
import LibraryClassObject
89 from Object
.POM
.CommonObject
import PcdObject
90 from Object
.POM
.CommonObject
import TextObject
91 from Object
.POM
.CommonObject
import MiscFileObject
92 from Object
.POM
.CommonObject
import FileObject
97 # Inherited from PackageObject
99 class DecPomAlignment(PackageObject
):
100 def __init__(self
, Filename
, WorkspaceDir
= None, CheckMulDec
= False):
101 PackageObject
.__init
__(self
)
102 self
.UserExtensions
= ''
103 self
.WorkspaceDir
= WorkspaceDir
104 self
.SupArchList
= ARCH_LIST
105 self
.CheckMulDec
= CheckMulDec
106 self
.DecParser
= None
107 self
.UniFileClassObject
= None
108 self
.PcdDefaultValueDict
= {}
113 self
.LoadDecFile(Filename
)
116 # Transfer to Package Object if IsToPackage is True
122 # Load the file if it exists
124 # @param Filename: Input value for filename of Dec file
126 def LoadDecFile(self
, Filename
):
128 # Insert a record for file
130 Filename
= NormPath(Filename
)
131 (Path
, Name
) = os
.path
.split(Filename
)
132 self
.SetFullPath(Filename
)
133 self
.SetRelaPath(Path
)
134 self
.SetFileName(Name
)
135 self
.SetPackagePath(GetRelativePath(Path
, self
.WorkspaceDir
))
136 self
.SetCombinePath(GetRelativePath(Filename
, self
.WorkspaceDir
))
138 self
.DecParser
= Dec(Filename
)
140 ## Transfer to Package Object
142 # Transfer all contents of a Dec file to a standard Package Object
144 def DecToPackage(self
):
146 # Init global information for the file
148 ContainerFile
= self
.GetFullPath()
151 # Generate Package Header
153 self
.GenPackageHeader(ContainerFile
)
158 self
.GenIncludes(ContainerFile
)
163 self
.GenGuidProtocolPpis(TAB_GUIDS
, ContainerFile
)
168 self
.GenGuidProtocolPpis(TAB_PROTOCOLS
, ContainerFile
)
173 self
.GenGuidProtocolPpis(TAB_PPIS
, ContainerFile
)
176 # Generate LibraryClasses
178 self
.GenLibraryClasses(ContainerFile
)
183 self
.GenPcds(ContainerFile
)
186 # Generate Module File list, will be used later on to generate
189 self
.GenModuleFileList(ContainerFile
)
192 # Generate user extensions
194 self
.GenUserExtensions()
196 ## Generate user extension
199 def GenUserExtensions(self
):
200 UEObj
= self
.DecParser
.GetUserExtensionSectionObject()
201 UEList
= UEObj
.GetAllUserExtensions()
203 if not Item
.UserString
:
205 UserExtension
= UserExtensionObject()
207 if UserId
.startswith('"') and UserId
.endswith('"'):
208 UserId
= UserId
[1:-1]
209 UserExtension
.SetUserID(UserId
)
210 Identifier
= Item
.IdString
211 if Identifier
.startswith('"') and Identifier
.endswith('"'):
212 Identifier
= Identifier
[1:-1]
214 # Generate miscellaneous files of DEC file
216 if UserId
== 'TianoCore' and Identifier
== 'ExtraFiles':
217 self
.GenMiscFiles(Item
.UserString
)
218 UserExtension
.SetIdentifier(Identifier
)
219 UserExtension
.SetStatement(Item
.UserString
)
220 UserExtension
.SetSupArchList(
221 Item
.ArchAndModuleType
223 self
.SetUserExtensionList(
224 self
.GetUserExtensionList() + [UserExtension
]
227 # Add Private sections to UserExtension
228 if self
.DecParser
.GetPrivateSections():
229 PrivateUserExtension
= UserExtensionObject()
230 PrivateUserExtension
.SetStatement(self
.DecParser
.GetPrivateSections())
231 PrivateUserExtension
.SetIdentifier(DT
.TAB_PRIVATE
)
232 PrivateUserExtension
.SetUserID(DT
.TAB_INTEL
)
233 self
.SetUserExtensionList(self
.GetUserExtensionList() + [PrivateUserExtension
])
235 ## Generate miscellaneous files on DEC file
238 def GenMiscFiles(self
, Content
):
239 MiscFileObj
= MiscFileObject()
240 for Line
in Content
.splitlines():
243 FileName
= Line
[:Line
.find('#')]
247 if IsValidPath(FileName
, self
.GetRelaPath()):
248 FileObj
= FileObject()
249 FileObj
.SetURI(FileName
)
250 MiscFileObj
.SetFileList(MiscFileObj
.GetFileList()+[FileObj
])
252 Logger
.Error("InfParser",
254 ST
.ERR_INF_PARSER_FILE_NOT_EXIST_OR_NAME_INVALID
%(Line),
255 File
=self
.GetFileName(),
257 self
.SetMiscFileList(self
.GetMiscFileList()+[MiscFileObj
])
259 ## Generate Package Header
261 # Gen Package Header of Dec as <Key> = <Value>
263 # @param ContainerFile: The Dec file full path
265 def GenPackageHeader(self
, ContainerFile
):
266 Logger
.Debug(2, "Generate PackageHeader ...")
270 # Update all defines item in database
272 DefObj
= self
.DecParser
.GetDefineSectionObject()
273 for Item
in DefObj
.GetDefines():
275 # put items into Dict except for PackageName, Guid, Version, DEC_SPECIFICATION
277 SkipItemList
= [TAB_DEC_DEFINES_PACKAGE_NAME
, \
278 TAB_DEC_DEFINES_PACKAGE_GUID
, TAB_DEC_DEFINES_PACKAGE_VERSION
, \
279 TAB_DEC_DEFINES_DEC_SPECIFICATION
, TAB_DEC_DEFINES_PKG_UNI_FILE
]
280 if Item
.Key
in SkipItemList
:
282 DefinesDict
['%s = %s' % (Item
.Key
, Item
.Value
)] = TAB_ARCH_COMMON
284 self
.SetBaseName(DefObj
.GetPackageName())
285 self
.SetVersion(DefObj
.GetPackageVersion())
286 # self.SetName(DefObj.GetPackageName() + ' Version ' + \
287 # DefObj.GetPackageVersion())
288 self
.SetName(os
.path
.splitext(self
.GetFileName())[0])
289 self
.SetGuid(DefObj
.GetPackageGuid())
290 if DefObj
.GetPackageUniFile():
291 ValidateUNIFilePath(DefObj
.GetPackageUniFile())
292 self
.UniFileClassObject
= \
293 UniFileClassObject([PathClass(os
.path
.join(DefObj
.GetPackagePath(), DefObj
.GetPackageUniFile()))])
295 self
.UniFileClassObject
= None
298 UserExtension
= UserExtensionObject()
299 UserExtension
.SetDefinesDict(DefinesDict
)
300 UserExtension
.SetIdentifier('DefineModifiers')
301 UserExtension
.SetUserID('EDK2')
302 self
.SetUserExtensionList(
303 self
.GetUserExtensionList() + [UserExtension
]
307 # Get File header information
309 if self
.UniFileClassObject
:
310 Lang
= TAB_LANGUAGE_EN_X
312 Lang
= TAB_LANGUAGE_EN_US
313 Abstract
, Description
, Copyright
, License
= \
314 ParseHeaderCommentSection(self
.DecParser
.GetHeadComment(),
317 self
.SetAbstract((Lang
, Abstract
))
319 self
.SetDescription((Lang
, Description
))
321 self
.SetCopyright(('', Copyright
))
323 self
.SetLicense(('', License
))
326 # Get Binary header information
328 if self
.DecParser
.BinaryHeadComment
:
329 Abstract
, Description
, Copyright
, License
= \
330 ParseHeaderCommentSection(self
.DecParser
.BinaryHeadComment
,
333 if not Abstract
or not Description
or not Copyright
or not License
:
334 Logger
.Error('MkPkg',
336 ST
.ERR_INVALID_BINARYHEADER_FORMAT
,
339 self
.SetBinaryHeaderAbstract((Lang
, Abstract
))
340 self
.SetBinaryHeaderDescription((Lang
, Description
))
341 self
.SetBinaryHeaderCopyright(('', Copyright
))
342 self
.SetBinaryHeaderLicense(('', License
))
344 BinaryAbstractList
= []
345 BinaryDescriptionList
= []
347 #Get Binary header from UNI file
348 # Initialize the UniStrDict dictionary, top keys are language codes
350 if self
.UniFileClassObject
:
351 UniStrDict
= self
.UniFileClassObject
.OrderedStringList
352 for Lang
in UniStrDict
:
353 for StringDefClassObject
in UniStrDict
[Lang
]:
354 Lang
= GetLanguageCode1766(Lang
)
355 if StringDefClassObject
.StringName
== TAB_DEC_BINARY_ABSTRACT
:
356 if (Lang
, ConvertSpecialUnicodes(StringDefClassObject
.StringValue
)) \
357 not in self
.GetBinaryHeaderAbstract():
358 BinaryAbstractList
.append((Lang
, ConvertSpecialUnicodes(StringDefClassObject
.StringValue
)))
359 if StringDefClassObject
.StringName
== TAB_DEC_BINARY_DESCRIPTION
:
360 if (Lang
, ConvertSpecialUnicodes(StringDefClassObject
.StringValue
)) \
361 not in self
.GetBinaryHeaderDescription():
362 BinaryDescriptionList
.append((Lang
,
363 ConvertSpecialUnicodes(StringDefClassObject
.StringValue
)))
364 #Combine Binary header from DEC file and UNI file
365 BinaryAbstractList
= self
.GetBinaryHeaderAbstract() + BinaryAbstractList
366 BinaryDescriptionList
= self
.GetBinaryHeaderDescription() + BinaryDescriptionList
367 BinaryCopyrightList
= self
.GetBinaryHeaderCopyright()
368 BinaryLicenseList
= self
.GetBinaryHeaderLicense()
369 #Generate the UserExtensionObject for TianoCore."BinaryHeader"
370 if BinaryAbstractList
or BinaryDescriptionList
or BinaryCopyrightList
or BinaryLicenseList
:
371 BinaryUserExtension
= UserExtensionObject()
372 BinaryUserExtension
.SetBinaryAbstract(BinaryAbstractList
)
373 BinaryUserExtension
.SetBinaryDescription(BinaryDescriptionList
)
374 BinaryUserExtension
.SetBinaryCopyright(BinaryCopyrightList
)
375 BinaryUserExtension
.SetBinaryLicense(BinaryLicenseList
)
376 BinaryUserExtension
.SetIdentifier(TAB_BINARY_HEADER_IDENTIFIER
)
377 BinaryUserExtension
.SetUserID(TAB_BINARY_HEADER_USERID
)
378 self
.SetUserExtensionList(self
.GetUserExtensionList() + [BinaryUserExtension
])
383 # Gen Includes of Dec
385 # @param ContainerFile: The Dec file full path
387 def GenIncludes(self
, ContainerFile
):
390 Logger
.Debug(2, "Generate %s ..." % TAB_INCLUDES
)
391 IncludesDict
= Sdict()
393 IncObj
= self
.DecParser
.GetIncludeSectionObject()
394 for Item
in IncObj
.GetAllIncludes():
395 IncludePath
= os
.path
.normpath(Item
.File
)
396 if platform
.system() != 'Windows' and platform
.system() != 'Microsoft':
397 IncludePath
= IncludePath
.replace('\\', '/')
398 if IncludePath
in IncludesDict
:
399 if Item
.GetArchList() == [TAB_ARCH_COMMON
] or IncludesDict
[IncludePath
] == [TAB_ARCH_COMMON
]:
400 IncludesDict
[IncludePath
] = [TAB_ARCH_COMMON
]
402 IncludesDict
[IncludePath
] = IncludesDict
[IncludePath
] + Item
.GetArchList()
404 IncludesDict
[IncludePath
] = Item
.GetArchList()
407 # get the standardIncludeFileList(industry), packageIncludeFileList
408 # (others) for PackageObject
410 PackagePath
= os
.path
.split(self
.GetFullPath())[0]
412 [os
.path
.normpath(Path
) + sep
for Path
in IncludesDict
.keys()]
413 IncludePathList
.sort()
416 # get a non-overlap set of include path, IncludePathList should be
417 # sorted, and path should be end with path seperator '\'
420 for Path1
in IncludePathList
:
421 for Path2
in NonOverLapList
:
422 if Path1
.startswith(Path2
):
425 NonOverLapList
.append(Path1
)
427 # revert the list so the longest path shown first in list, also need
428 # to remove the extra path seperator '\'
429 # as this list is used to search the supported Arch info
431 for IndexN
in range (0, len(IncludePathList
)):
432 IncludePathList
[IndexN
] = os
.path
.normpath(IncludePathList
[IndexN
])
433 IncludePathList
.sort()
434 IncludePathList
.reverse()
436 # save the include path list for later usage
438 self
.SetIncludePathList(IncludePathList
)
439 StandardIncludeFileList
= []
440 PackageIncludeFileList
= []
443 for Path
in NonOverLapList
:
444 FileList
= GetFiles(os
.path
.join(PackagePath
, Path
), ['CVS', '.svn'], False)
445 IncludeFileList
+= [os
.path
.normpath(os
.path
.join(Path
, File
)) for File
in FileList
]
446 for Includefile
in IncludeFileList
:
447 ExtName
= os
.path
.splitext(Includefile
)[1]
448 if ExtName
.upper() == '.DEC' and self
.CheckMulDec
:
449 Logger
.Error('MkPkg',
451 ST
.ERR_MUL_DEC_ERROR
%(os
.path
.dirname(ContainerFile
),
452 os
.path
.basename(ContainerFile
),
455 FileCombinePath
= os
.path
.dirname(Includefile
)
456 Include
= IncludeObject()
457 for Path
in IncludePathList
:
458 if FileCombinePath
.startswith(Path
):
459 SupArchList
= IncludesDict
[Path
]
461 Include
.SetFilePath(Includefile
)
462 Include
.SetSupArchList(SupArchList
)
463 if Includefile
.find('IndustryStandard') != -1:
464 StandardIncludeFileList
.append(Include
)
466 PackageIncludeFileList
.append(Include
)
468 self
.SetStandardIncludeFileList(StandardIncludeFileList
)
471 # put include path into the PackageIncludeFileList
474 IncObj
= self
.DecParser
.GetIncludeSectionObject()
475 for Item
in IncObj
.GetAllIncludes():
476 IncludePath
= Item
.File
477 Include
= IncludeObject()
478 Include
.SetFilePath(IncludePath
)
479 Include
.SetSupArchList(Item
.GetArchList())
480 PackagePathList
.append(Include
)
481 self
.SetPackageIncludeFileList(PackagePathList
+ PackageIncludeFileList
)
486 # <CName>=<GuidValue>
488 # @param ContainerFile: The Dec file full path
490 def GenGuidProtocolPpis(self
, Type
, ContainerFile
):
493 Logger
.Debug(2, "Generate %s ..." % Type
)
497 if Type
== TAB_GUIDS
:
498 Obj
= self
.DecParser
.GetGuidSectionObject()
499 def CreateGuidObject():
500 Object
= GuidObject()
501 Object
.SetGuidTypeList([])
502 Object
.SetUsage(None)
505 Factory
= CreateGuidObject
506 elif Type
== TAB_PROTOCOLS
:
507 Obj
= self
.DecParser
.GetProtocolSectionObject()
509 def CreateProtocolObject():
510 return ProtocolObject()
511 Factory
= CreateProtocolObject
512 elif Type
== TAB_PPIS
:
513 Obj
= self
.DecParser
.GetPpiSectionObject()
515 def CreatePpiObject():
517 Factory
= CreatePpiObject
524 DeclarationsList
= []
527 # Go through each arch
529 for Item
in Obj
.GetGuidStyleAllItems():
530 Name
= Item
.GuidCName
531 Value
= Item
.GuidString
532 HelpTxt
= ParseGenericComment(Item
.GetHeadComment() + \
533 Item
.GetTailComment())
535 ListObject
= Factory()
536 ListObject
.SetCName(Name
)
537 ListObject
.SetGuid(Value
)
538 ListObject
.SetSupArchList(Item
.GetArchList())
540 if self
.UniFileClassObject
:
541 HelpTxt
.SetLang(TAB_LANGUAGE_EN_X
)
542 ListObject
.SetHelpTextList([HelpTxt
])
544 DeclarationsList
.append(ListObject
)
547 #GuidTypeList is abstracted from help
549 if Type
== TAB_GUIDS
:
550 self
.SetGuidList(self
.GetGuidList() + DeclarationsList
)
551 elif Type
== TAB_PROTOCOLS
:
552 self
.SetProtocolList(self
.GetProtocolList() + DeclarationsList
)
553 elif Type
== TAB_PPIS
:
554 self
.SetPpiList(self
.GetPpiList() + DeclarationsList
)
558 # Gen LibraryClasses of Dec
559 # <CName>=<GuidValue>
561 # @param ContainerFile: The Dec file full path
563 def GenLibraryClasses(self
, ContainerFile
):
566 Logger
.Debug(2, "Generate %s ..." % TAB_LIBRARY_CLASSES
)
567 LibraryClassDeclarations
= []
569 LibObj
= self
.DecParser
.GetLibraryClassSectionObject()
570 for Item
in LibObj
.GetAllLibraryclasses():
571 LibraryClass
= LibraryClassObject()
572 LibraryClass
.SetLibraryClass(Item
.Libraryclass
)
573 LibraryClass
.SetSupArchList(Item
.GetArchList())
574 LibraryClass
.SetIncludeHeader(Item
.File
)
575 HelpTxt
= ParseGenericComment(Item
.GetHeadComment() + \
576 Item
.GetTailComment(), None, '@libraryclass')
578 if self
.UniFileClassObject
:
579 HelpTxt
.SetLang(TAB_LANGUAGE_EN_X
)
580 LibraryClass
.SetHelpTextList([HelpTxt
])
581 LibraryClassDeclarations
.append(LibraryClass
)
583 self
.SetLibraryClassList(self
.GetLibraryClassList() + \
584 LibraryClassDeclarations
)
589 # <TokenSpcCName>.<TokenCName>|<Value>|<DatumType>|<Token>
591 # @param ContainerFile: The Dec file full path
593 def GenPcds(self
, ContainerFile
):
594 Logger
.Debug(2, "Generate %s ..." % TAB_PCDS
)
595 PcdObj
= self
.DecParser
.GetPcdSectionObject()
601 (TAB_PCDS_FIXED_AT_BUILD_NULL
, 'FixedPcd'),
602 (TAB_PCDS_PATCHABLE_IN_MODULE_NULL
, 'PatchPcd'),
603 (TAB_PCDS_FEATURE_FLAG_NULL
, 'FeaturePcd'),
604 (TAB_PCDS_DYNAMIC_EX_NULL
, 'PcdEx'),
605 (TAB_PCDS_DYNAMIC_NULL
, 'Pcd')]
610 # Initialize UniStrDict dictionary, top keys are language codes
615 if self
.UniFileClassObject
:
616 Language
= TAB_LANGUAGE_EN_X
618 Language
= TAB_LANGUAGE_EN_US
620 if self
.UniFileClassObject
:
621 UniStrDict
= self
.UniFileClassObject
.OrderedStringList
622 for Lang
in UniStrDict
:
623 for StringDefClassObject
in UniStrDict
[Lang
]:
624 StrList
= StringDefClassObject
.StringName
.split('_')
625 # StringName format is STR_<TOKENSPACECNAME>_<PCDCNAME>_PROMPT
626 if len(StrList
) == 4 and StrList
[0] == TAB_STR_TOKENCNAME
and StrList
[3] == TAB_STR_TOKENPROMPT
:
627 PromptStrList
.append((GetLanguageCode1766(Lang
), StringDefClassObject
.StringName
, \
628 StringDefClassObject
.StringValue
))
629 # StringName format is STR_<TOKENSPACECNAME>_<PCDCNAME>_HELP
630 if len(StrList
) == 4 and StrList
[0] == TAB_STR_TOKENCNAME
and StrList
[3] == TAB_STR_TOKENHELP
:
631 HelpStrList
.append((GetLanguageCode1766(Lang
), StringDefClassObject
.StringName
, \
632 StringDefClassObject
.StringValue
))
633 # StringName format is STR_<TOKENSPACECNAME>_ERR_##
634 if len(StrList
) == 4 and StrList
[0] == TAB_STR_TOKENCNAME
and StrList
[2] == TAB_STR_TOKENERR
:
635 PcdErrStrList
.append((GetLanguageCode1766(Lang
), StringDefClassObject
.StringName
, \
636 StringDefClassObject
.StringValue
))
640 for PcdType
, Type
in IterList
:
642 # Go through all archs
644 # for Arch in self.SupArchList + [TAB_ARCH_COMMON]:
646 for Item
in PcdObj
.GetPcdsByType(PcdType
.upper()):
647 PcdDeclaration
= GenPcdDeclaration(
649 (Item
.TokenSpaceGuidCName
, Item
.TokenCName
,
650 Item
.DefaultValue
, Item
.DatumType
, Item
.TokenValue
,
651 Type
, Item
.GetHeadComment(), Item
.GetTailComment(), ''),
653 self
.DecParser
.GetDefineSectionMacro()
655 PcdDeclaration
.SetSupArchList(Item
.GetArchListOfType(PcdType
))
658 # Get PCD error message from PCD error comment section in DEC file
660 for PcdErr
in PcdDeclaration
.GetPcdErrorsList():
661 if (PcdDeclaration
.GetTokenSpaceGuidCName(), PcdErr
.GetErrorNumber()) \
662 in self
.DecParser
.PcdErrorCommentDict
:
663 Key
= (PcdDeclaration
.GetTokenSpaceGuidCName(), PcdErr
.GetErrorNumber())
664 PcdErr
.SetErrorMessageList(PcdErr
.GetErrorMessageList() + \
665 [(Language
, self
.DecParser
.PcdErrorCommentDict
[Key
])])
667 for Index
in range(0, len(PromptStrList
)):
668 StrNameList
= PromptStrList
[Index
][1].split('_')
669 if StrNameList
[1].lower() == Item
.TokenSpaceGuidCName
.lower() and \
670 StrNameList
[2].lower() == Item
.TokenCName
.lower():
671 TxtObj
= TextObject()
672 TxtObj
.SetLang(PromptStrList
[Index
][0])
673 TxtObj
.SetString(PromptStrList
[Index
][2])
674 for Prompt
in PcdDeclaration
.GetPromptList():
675 if Prompt
.GetLang() == TxtObj
.GetLang() and \
676 Prompt
.GetString() == TxtObj
.GetString():
679 PcdDeclaration
.SetPromptList(PcdDeclaration
.GetPromptList() + [TxtObj
])
681 for Index
in range(0, len(HelpStrList
)):
682 StrNameList
= HelpStrList
[Index
][1].split('_')
683 if StrNameList
[1].lower() == Item
.TokenSpaceGuidCName
.lower() and \
684 StrNameList
[2].lower() == Item
.TokenCName
.lower():
685 TxtObj
= TextObject()
686 TxtObj
.SetLang(HelpStrList
[Index
][0])
687 TxtObj
.SetString(HelpStrList
[Index
][2])
688 for HelpStrObj
in PcdDeclaration
.GetHelpTextList():
689 if HelpStrObj
.GetLang() == TxtObj
.GetLang() and \
690 HelpStrObj
.GetString() == TxtObj
.GetString():
693 PcdDeclaration
.SetHelpTextList(PcdDeclaration
.GetHelpTextList() + [TxtObj
])
696 # Get PCD error message from UNI file
698 for Index
in range(0, len(PcdErrStrList
)):
699 StrNameList
= PcdErrStrList
[Index
][1].split('_')
700 if StrNameList
[1].lower() == Item
.TokenSpaceGuidCName
.lower() and \
701 StrNameList
[2].lower() == TAB_STR_TOKENERR
.lower():
702 for PcdErr
in PcdDeclaration
.GetPcdErrorsList():
703 if PcdErr
.GetErrorNumber().lower() == (TAB_HEX_START
+ StrNameList
[3]).lower() and \
704 (PcdErrStrList
[Index
][0], PcdErrStrList
[Index
][2]) not in PcdErr
.GetErrorMessageList():
705 PcdErr
.SetErrorMessageList(PcdErr
.GetErrorMessageList() + \
706 [(PcdErrStrList
[Index
][0], PcdErrStrList
[Index
][2])])
709 # Check to prevent missing error message if a Pcd has the error code.
711 for PcdErr
in PcdDeclaration
.GetPcdErrorsList():
712 if PcdErr
.GetErrorNumber().strip():
713 if not PcdErr
.GetErrorMessageList():
716 ST
.ERR_DECPARSE_PCD_UNMATCHED_ERRORCODE
% PcdErr
.GetErrorNumber(),
719 PcdErr
.GetFileLine())
721 PcdDeclarations
.append(PcdDeclaration
)
722 self
.SetPcdList(self
.GetPcdList() + PcdDeclarations
)
726 # Get error message via language
727 # @param ErrorMessageList: Error message tuple list the language and its message
728 # @param Lang: the language of setting
729 # @return: the error message described in the related UNI file
730 def GetEnErrorMessage(self
, ErrorMessageList
):
733 Lang
= TAB_LANGUAGE_EN_US
734 for (Language
, Message
) in ErrorMessageList
:
737 for (Language
, Message
) in ErrorMessageList
:
738 if Language
.find(TAB_LANGUAGE_EN
) >= 0:
742 return ErrorMessageList
[0][1]
748 # Replace the strings for Python eval function.
749 # @param ReplaceValue: The string that needs to be replaced.
750 # @return: The string was replaced, then eval function is always making out it.
751 def ReplaceForEval(self
, ReplaceValue
, IsRange
=False, IsExpr
=False):
755 # deal with "NOT EQ", "NOT LT", "NOT GT", "NOT LE", "NOT GE", "NOT NOT"
757 NOTNOT_Pattern
= '[\t\s]*NOT[\t\s]+NOT[\t\s]*'
758 NOTGE_Pattern
= '[\t\s]*NOT[\t\s]+GE[\t\s]*'
759 NOTLE_Pattern
= '[\t\s]*NOT[\t\s]+LE[\t\s]*'
760 NOTGT_Pattern
= '[\t\s]*NOT[\t\s]+GT[\t\s]*'
761 NOTLT_Pattern
= '[\t\s]*NOT[\t\s]+LT[\t\s]*'
762 NOTEQ_Pattern
= '[\t\s]*NOT[\t\s]+EQ[\t\s]*'
763 ReplaceValue
= re
.compile(NOTNOT_Pattern
).sub('', ReplaceValue
)
764 ReplaceValue
= re
.compile(NOTLT_Pattern
).sub('x >= ', ReplaceValue
)
765 ReplaceValue
= re
.compile(NOTGT_Pattern
).sub('x <= ', ReplaceValue
)
766 ReplaceValue
= re
.compile(NOTLE_Pattern
).sub('x > ', ReplaceValue
)
767 ReplaceValue
= re
.compile(NOTGE_Pattern
).sub('x < ', ReplaceValue
)
768 ReplaceValue
= re
.compile(NOTEQ_Pattern
).sub('x != ', ReplaceValue
)
771 ReplaceValue
= ReplaceValue
.replace('EQ', 'x ==')
772 ReplaceValue
= ReplaceValue
.replace('LT', 'x <')
773 ReplaceValue
= ReplaceValue
.replace('LE', 'x <=')
774 ReplaceValue
= ReplaceValue
.replace('GT', 'x >')
775 ReplaceValue
= ReplaceValue
.replace('GE', 'x >=')
776 ReplaceValue
= ReplaceValue
.replace('XOR', 'x ^')
778 ReplaceValue
= ReplaceValue
.replace('EQ', '==')
779 ReplaceValue
= ReplaceValue
.replace('NE', '!=')
780 ReplaceValue
= ReplaceValue
.replace('LT', '<')
781 ReplaceValue
= ReplaceValue
.replace('LE', '<=')
782 ReplaceValue
= ReplaceValue
.replace('GT', '>')
783 ReplaceValue
= ReplaceValue
.replace('GE', '>=')
784 ReplaceValue
= ReplaceValue
.replace('XOR', '^')
786 ReplaceValue
= ReplaceValue
.replace('AND', 'and')
787 ReplaceValue
= ReplaceValue
.replace('&&', ' and ')
788 ReplaceValue
= ReplaceValue
.replace('xor', '^')
789 ReplaceValue
= ReplaceValue
.replace('OR', 'or')
790 ReplaceValue
= ReplaceValue
.replace('||', ' or ')
791 ReplaceValue
= ReplaceValue
.replace('NOT', 'not')
792 if ReplaceValue
.find('!') >= 0 and ReplaceValue
[ReplaceValue
.index('!') + 1] != '=':
793 ReplaceValue
= ReplaceValue
.replace('!', ' not ')
794 if '.' in ReplaceValue
:
795 Pattern
= '[a-zA-Z0-9]{1,}\.[a-zA-Z0-9]{1,}'
796 MatchedList
= re
.findall(Pattern
, ReplaceValue
)
797 for MatchedItem
in MatchedList
:
798 if MatchedItem
not in self
.PcdDefaultValueDict
:
799 Logger
.Error("Dec File Parser", FORMAT_INVALID
, Message
=ST
.ERR_DECPARSE_PCD_NODEFINED
% MatchedItem
,
802 ReplaceValue
= ReplaceValue
.replace(MatchedItem
, self
.PcdDefaultValueDict
[MatchedItem
])
807 # Check pcd's default value according to the pcd's description
809 def CheckPcdValue(self
):
810 for Pcd
in self
.GetPcdList():
811 self
.PcdDefaultValueDict
[TAB_SPLIT
.join((Pcd
.GetTokenSpaceGuidCName(), Pcd
.GetCName())).strip()] = \
812 Pcd
.GetDefaultValue()
814 for Pcd
in self
.GetPcdList():
815 ValidationExpressions
= []
816 PcdGuidName
= TAB_SPLIT
.join((Pcd
.GetTokenSpaceGuidCName(), Pcd
.GetCName()))
817 Valids
= Pcd
.GetPcdErrorsList()
819 Expression
= Valid
.GetExpression()
822 # Delete the 'L' prefix of a quoted string, this operation is for eval()
824 QUOTED_PATTERN
= '[\t\s]*L?"[^"]*"'
825 QuotedMatchedObj
= re
.search(QUOTED_PATTERN
, Expression
)
827 MatchedStr
= QuotedMatchedObj
.group().strip()
828 if MatchedStr
.startswith('L'):
829 Expression
= Expression
.replace(MatchedStr
, MatchedStr
[1:].strip())
831 Expression
= self
.ReplaceForEval(Expression
, IsExpr
=True)
832 Expression
= Expression
.replace(PcdGuidName
, 'x')
833 Message
= self
.GetEnErrorMessage(Valid
.GetErrorMessageList())
834 ValidationExpressions
.append((Expression
, Message
))
836 ValidList
= Valid
.GetValidValue()
838 ValidValue
= 'x in %s' % [eval(v
) for v
in ValidList
.split(' ') if v
]
839 Message
= self
.GetEnErrorMessage(Valid
.GetErrorMessageList())
840 ValidationExpressions
.append((ValidValue
, Message
))
842 ValidValueRange
= Valid
.GetValidValueRange()
844 ValidValueRange
= self
.ReplaceForEval(ValidValueRange
, IsRange
=True)
845 if ValidValueRange
.find('-') >= 0:
846 ValidValueRange
= ValidValueRange
.replace('-', '<= x <=')
847 elif not ValidValueRange
.startswith('x ') and not ValidValueRange
.startswith('not ') \
848 and not ValidValueRange
.startswith('not(') and not ValidValueRange
.startswith('('):
849 ValidValueRange
= 'x %s' % ValidValueRange
850 Message
= self
.GetEnErrorMessage(Valid
.GetErrorMessageList())
851 ValidationExpressions
.append((ValidValueRange
, Message
))
853 DefaultValue
= self
.PcdDefaultValueDict
[PcdGuidName
.strip()]
855 # Delete the 'L' prefix of a quoted string, this operation is for eval()
857 QUOTED_PATTERN
= '[\t\s]*L?"[^"]*"'
858 QuotedMatchedObj
= re
.search(QUOTED_PATTERN
, DefaultValue
)
860 MatchedStr
= QuotedMatchedObj
.group().strip()
861 if MatchedStr
.startswith('L'):
862 DefaultValue
= DefaultValue
.replace(MatchedStr
, MatchedStr
[1:].strip())
865 DefaultValue
= eval(DefaultValue
.replace('TRUE', 'True').replace('true', 'True')
866 .replace('FALSE', 'False').replace('false', 'False'))
867 except BaseException
:
870 for (Expression
, Msg
) in ValidationExpressions
:
872 if not eval(Expression
, {'x':DefaultValue
}):
873 Logger
.Error("Dec File Parser", FORMAT_INVALID
, ExtraData
='%s, value = %s' %\
874 (PcdGuidName
, DefaultValue
), Message
=Msg
, File
=self
.FullPath
)
876 Logger
.Error("Dec File Parser", FORMAT_INVALID
, ExtraData
=PcdGuidName
, \
877 Message
=Msg
, File
=self
.FullPath
)
881 def GenModuleFileList(self
, ContainerFile
):
883 ContainerFileName
= os
.path
.basename(ContainerFile
)
884 ContainerFilePath
= os
.path
.dirname(ContainerFile
)
885 for Item
in GetFiles(ContainerFilePath
,
886 ['CVS', '.svn'] + self
.GetIncludePathList(), False):
887 ExtName
= os
.path
.splitext(Item
)[1]
888 if ExtName
.lower() == '.inf':
889 ModuleFileList
.append(Item
)
890 elif ExtName
.upper() == '.DEC' and self
.CheckMulDec
:
891 if Item
== ContainerFileName
:
893 Logger
.Error('MkPkg',
895 ST
.ERR_MUL_DEC_ERROR
%(ContainerFilePath
,
899 self
.SetModuleFileList(ModuleFileList
)
901 ## Show detailed information of Package
903 # Print all members and their values of Package class
905 def ShowPackage(self
):
906 print('\nName =', self
.GetName())
907 print('\nBaseName =', self
.GetBaseName())
908 print('\nVersion =', self
.GetVersion())
909 print('\nGuid =', self
.GetGuid())
911 print('\nStandardIncludes = %d ' \
912 % len(self
.GetStandardIncludeFileList()), end
=' ')
913 for Item
in self
.GetStandardIncludeFileList():
914 print(Item
.GetFilePath(), ' ', Item
.GetSupArchList())
915 print('\nPackageIncludes = %d \n' \
916 % len(self
.GetPackageIncludeFileList()), end
=' ')
917 for Item
in self
.GetPackageIncludeFileList():
918 print(Item
.GetFilePath(), ' ', Item
.GetSupArchList())
920 print('\nGuids =', self
.GetGuidList())
921 for Item
in self
.GetGuidList():
922 print(Item
.GetCName(), Item
.GetGuid(), Item
.GetSupArchList())
923 print('\nProtocols =', self
.GetProtocolList())
924 for Item
in self
.GetProtocolList():
925 print(Item
.GetCName(), Item
.GetGuid(), Item
.GetSupArchList())
926 print('\nPpis =', self
.GetPpiList())
927 for Item
in self
.GetPpiList():
928 print(Item
.GetCName(), Item
.GetGuid(), Item
.GetSupArchList())
929 print('\nLibraryClasses =', self
.GetLibraryClassList())
930 for Item
in self
.GetLibraryClassList():
931 print(Item
.GetLibraryClass(), Item
.GetRecommendedInstance(), \
932 Item
.GetSupArchList())
933 print('\nPcds =', self
.GetPcdList())
934 for Item
in self
.GetPcdList():
935 print('CName=', Item
.GetCName(), 'TokenSpaceGuidCName=', \
936 Item
.GetTokenSpaceGuidCName(), \
937 'DefaultValue=', Item
.GetDefaultValue(), \
938 'ValidUsage=', Item
.GetValidUsage(), \
939 'SupArchList', Item
.GetSupArchList(), \
940 'Token=', Item
.GetToken(), 'DatumType=', Item
.GetDatumType())
942 for Item
in self
.GetMiscFileList():
943 print(Item
.GetName())
944 for FileObjectItem
in Item
.GetFileList():
945 print(FileObjectItem
.GetURI())
946 print('****************\n')
950 # @param ContainerFile: File name of the DEC file
951 # @param PcdInfo: Pcd information, of format (TokenGuidCName,
952 # TokenName, Value, DatumType, Token, Type,
953 # GenericComment, TailComment, Arch)
954 # @param Language: The language of HelpText, Prompt
956 def GenPcdDeclaration(ContainerFile
, PcdInfo
, Language
, MacroReplaceDict
):
960 TokenGuidCName
, TokenName
, Value
, DatumType
, Token
, Type
, \
961 GenericComment
, TailComment
, Arch
= PcdInfo
963 Pcd
.SetCName(TokenName
)
965 Pcd
.SetTokenSpaceGuidCName(TokenGuidCName
)
966 Pcd
.SetDatumType(DatumType
)
967 Pcd
.SetDefaultValue(Value
)
968 Pcd
.SetValidUsage(Type
)
970 # MaxDatumSize is required field for 'VOID*' PCD
972 if DatumType
== TAB_PTR_TYPE_PCD
:
973 Pcd
.SetMaxDatumSize(ITEM_UNDEFINED
)
976 Pcd
.SetSupArchList(SupArchList
)
979 HelpStr
, PcdErrList
, PromptStr
= ParseDecPcdGenericComment(GenericComment
,
985 Pcd
.SetPcdErrorsList(PcdErrList
)
988 SupModuleList
, TailHelpStr
= ParseDecPcdTailComment(TailComment
,
991 Pcd
.SetSupModuleList(SupModuleList
)
993 if HelpStr
and (not HelpStr
.endswith('\n')) and TailHelpStr
:
995 HelpStr
+= TailHelpStr
997 HelpTxtObj
= TextObject()
998 HelpTxtObj
.SetLang(Language
)
999 HelpTxtObj
.SetString(HelpStr
)
1000 Pcd
.SetHelpTextList([HelpTxtObj
])
1002 TxtObj
= TextObject()
1003 TxtObj
.SetLang(Language
)
1004 TxtObj
.SetString(PromptStr
)
1005 Pcd
.SetPromptList([TxtObj
])