]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/MetaFileParser.py
Sync EDKII BaseTools to BaseTools project r2065.
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileParser.py
1 ## @file
2 # This file is used to parse meta files
3 #
4 # Copyright (c) 2008 - 2010, 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 os
18 import time
19 import copy
20
21 import Common.EdkLogger as EdkLogger
22 from CommonDataClass.DataClass import *
23 from Common.DataType import *
24 from Common.String import *
25 from Common.Misc import Blist, GuidStructureStringToGuidString, CheckPcdDatum
26
27 ## Base class of parser
28 #
29 # This class is used for derivation purpose. The specific parser for one kind
30 # type file must derive this class and implement some public interfaces.
31 #
32 # @param FilePath The path of platform description file
33 # @param FileType The raw data of DSC file
34 # @param Table Database used to retrieve module/package information
35 # @param Macros Macros used for replacement in file
36 # @param Owner Owner ID (for sub-section parsing)
37 # @param From ID from which the data comes (for !INCLUDE directive)
38 #
39 class MetaFileParser(object):
40 # data type (file content) for specific file type
41 DataType = {}
42
43 # Parser objects used to implement singleton
44 MetaFiles = {}
45
46 ## Factory method
47 #
48 # One file, one parser object. This factory method makes sure that there's
49 # only one object constructed for one meta file.
50 #
51 # @param Class class object of real AutoGen class
52 # (InfParser, DecParser or DscParser)
53 # @param FilePath The path of meta file
54 # @param *args The specific class related parameters
55 # @param **kwargs The specific class related dict parameters
56 #
57 def __new__(Class, FilePath, *args, **kwargs):
58 if FilePath in Class.MetaFiles:
59 return Class.MetaFiles[FilePath]
60 else:
61 ParserObject = super(MetaFileParser, Class).__new__(Class)
62 Class.MetaFiles[FilePath] = ParserObject
63 return ParserObject
64
65 ## Constructor of MetaFileParser
66 #
67 # Initialize object of MetaFileParser
68 #
69 # @param FilePath The path of platform description file
70 # @param FileType The raw data of DSC file
71 # @param Table Database used to retrieve module/package information
72 # @param Macros Macros used for replacement in file
73 # @param Owner Owner ID (for sub-section parsing)
74 # @param From ID from which the data comes (for !INCLUDE directive)
75 #
76 def __init__(self, FilePath, FileType, Table, Macros=None, Owner=-1, From=-1):
77 # prevent re-initialization
78 if hasattr(self, "_Table"):
79 return
80 self._Table = Table
81 self._FileType = FileType
82 self.MetaFile = FilePath
83 self._FileDir = os.path.dirname(self.MetaFile)
84 self._Macros = copy.copy(Macros)
85 self._Macros["WORKSPACE"] = os.environ["WORKSPACE"]
86
87 # for recursive parsing
88 self._Owner = Owner
89 self._From = From
90
91 # parsr status for parsing
92 self._Content = None
93 self._ValueList = ['', '', '', '', '']
94 self._Scope = []
95 self._LineIndex = 0
96 self._CurrentLine = ''
97 self._SectionType = MODEL_UNKNOWN
98 self._SectionName = ''
99 self._InSubsection = False
100 self._SubsectionType = MODEL_UNKNOWN
101 self._SubsectionName = ''
102 self._LastItem = -1
103 self._Enabled = 0
104 self._Finished = False
105
106 ## Store the parsed data in table
107 def _Store(self, *Args):
108 return self._Table.Insert(*Args)
109
110 ## Virtual method for starting parse
111 def Start(self):
112 raise NotImplementedError
113
114 ## Set parsing complete flag in both class and table
115 def _Done(self):
116 self._Finished = True
117 ## Do not set end flag when processing included files
118 if self._From == -1:
119 self._Table.SetEndFlag()
120
121 ## Return the table containg parsed data
122 #
123 # If the parse complete flag is not set, this method will try to parse the
124 # file before return the table
125 #
126 def _GetTable(self):
127 if not self._Finished:
128 self.Start()
129 return self._Table
130
131 ## Get the parse complete flag
132 def _GetFinished(self):
133 return self._Finished
134
135 ## Set the complete flag
136 def _SetFinished(self, Value):
137 self._Finished = Value
138
139 ## Use [] style to query data in table, just for readability
140 #
141 # DataInfo = [data_type, scope1(arch), scope2(platform,moduletype)]
142 #
143 def __getitem__(self, DataInfo):
144 if type(DataInfo) != type(()):
145 DataInfo = (DataInfo,)
146 return self.Table.Query(*DataInfo)
147
148 ## Data parser for the common format in different type of file
149 #
150 # The common format in the meatfile is like
151 #
152 # xxx1 | xxx2 | xxx3
153 #
154 def _CommonParser(self):
155 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)
156 self._ValueList[0:len(TokenList)] = TokenList
157
158 ## Data parser for the format in which there's path
159 #
160 # Only path can have macro used. So we need to replace them before use.
161 #
162 def _PathParser(self):
163 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)
164 self._ValueList[0:len(TokenList)] = TokenList
165 if len(self._Macros) > 0:
166 for Index in range(0, len(self._ValueList)):
167 Value = self._ValueList[Index]
168 if Value == None or Value == '':
169 continue
170 self._ValueList[Index] = NormPath(Value, self._Macros)
171
172 ## Skip unsupported data
173 def _Skip(self):
174 EdkLogger.warn("Parser", "Unrecognized content", File=self.MetaFile,
175 Line=self._LineIndex+1, ExtraData=self._CurrentLine);
176 self._ValueList[0:1] = [self._CurrentLine]
177
178 ## Section header parser
179 #
180 # The section header is always in following format:
181 #
182 # [section_name.arch<.platform|module_type>]
183 #
184 def _SectionHeaderParser(self):
185 self._Scope = []
186 self._SectionName = ''
187 ArchList = set()
188 for Item in GetSplitValueList(self._CurrentLine[1:-1], TAB_COMMA_SPLIT):
189 if Item == '':
190 continue
191 ItemList = GetSplitValueList(Item, TAB_SPLIT)
192 # different section should not mix in one section
193 if self._SectionName != '' and self._SectionName != ItemList[0].upper():
194 EdkLogger.error('Parser', FORMAT_INVALID, "Different section names in the same section",
195 File=self.MetaFile, Line=self._LineIndex+1, ExtraData=self._CurrentLine)
196 self._SectionName = ItemList[0].upper()
197 if self._SectionName in self.DataType:
198 self._SectionType = self.DataType[self._SectionName]
199 else:
200 self._SectionType = MODEL_UNKNOWN
201 EdkLogger.warn("Parser", "Unrecognized section", File=self.MetaFile,
202 Line=self._LineIndex+1, ExtraData=self._CurrentLine)
203 # S1 is always Arch
204 if len(ItemList) > 1:
205 S1 = ItemList[1].upper()
206 else:
207 S1 = 'COMMON'
208 ArchList.add(S1)
209 # S2 may be Platform or ModuleType
210 if len(ItemList) > 2:
211 S2 = ItemList[2].upper()
212 else:
213 S2 = 'COMMON'
214 self._Scope.append([S1, S2])
215
216 # 'COMMON' must not be used with specific ARCHs at the same section
217 if 'COMMON' in ArchList and len(ArchList) > 1:
218 EdkLogger.error('Parser', FORMAT_INVALID, "'common' ARCH must not be used with specific ARCHs",
219 File=self.MetaFile, Line=self._LineIndex+1, ExtraData=self._CurrentLine)
220
221 ## [defines] section parser
222 def _DefineParser(self):
223 TokenList = GetSplitValueList(self._CurrentLine, TAB_EQUAL_SPLIT, 1)
224 self._ValueList[0:len(TokenList)] = TokenList
225 if self._ValueList[1] == '':
226 EdkLogger.error('Parser', FORMAT_INVALID, "No value specified",
227 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1)
228
229 ## DEFINE name=value parser
230 def _MacroParser(self):
231 TokenList = GetSplitValueList(self._CurrentLine, ' ', 1)
232 MacroType = TokenList[0]
233 if len(TokenList) < 2 or TokenList[1] == '':
234 EdkLogger.error('Parser', FORMAT_INVALID, "No macro name/value given",
235 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1)
236 TokenList = GetSplitValueList(TokenList[1], TAB_EQUAL_SPLIT, 1)
237 if TokenList[0] == '':
238 EdkLogger.error('Parser', FORMAT_INVALID, "No macro name given",
239 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1)
240
241 # Macros defined in the command line override ones defined in the meta-data file
242 if not TokenList[0] in self._Macros:
243 if len(TokenList) == 1:
244 self._Macros[TokenList[0]] = ''
245 else:
246 # keep the macro definition for later use
247 self._Macros[TokenList[0]] = ReplaceMacro(TokenList[1], self._Macros, False)
248
249 return TokenList[0], self._Macros[TokenList[0]]
250
251 ## [BuildOptions] section parser
252 def _BuildOptionParser(self):
253 TokenList = GetSplitValueList(self._CurrentLine, TAB_EQUAL_SPLIT, 1)
254 TokenList2 = GetSplitValueList(TokenList[0], ':', 1)
255 if len(TokenList2) == 2:
256 self._ValueList[0] = TokenList2[0] # toolchain family
257 self._ValueList[1] = TokenList2[1] # keys
258 else:
259 self._ValueList[1] = TokenList[0]
260 if len(TokenList) == 2: # value
261 self._ValueList[2] = ReplaceMacro(TokenList[1], self._Macros)
262
263 if self._ValueList[1].count('_') != 4:
264 EdkLogger.error(
265 'Parser',
266 FORMAT_INVALID,
267 "'%s' must be in format of <TARGET>_<TOOLCHAIN>_<ARCH>_<TOOL>_FLAGS" % self._ValueList[1],
268 ExtraData=self._CurrentLine,
269 File=self.MetaFile,
270 Line=self._LineIndex+1
271 )
272
273 _SectionParser = {}
274 Table = property(_GetTable)
275 Finished = property(_GetFinished, _SetFinished)
276
277
278 ## INF file parser class
279 #
280 # @param FilePath The path of platform description file
281 # @param FileType The raw data of DSC file
282 # @param Table Database used to retrieve module/package information
283 # @param Macros Macros used for replacement in file
284 #
285 class InfParser(MetaFileParser):
286 # INF file supported data types (one type per section)
287 DataType = {
288 TAB_UNKNOWN.upper() : MODEL_UNKNOWN,
289 TAB_INF_DEFINES.upper() : MODEL_META_DATA_HEADER,
290 TAB_BUILD_OPTIONS.upper() : MODEL_META_DATA_BUILD_OPTION,
291 TAB_INCLUDES.upper() : MODEL_EFI_INCLUDE,
292 TAB_LIBRARIES.upper() : MODEL_EFI_LIBRARY_INSTANCE,
293 TAB_LIBRARY_CLASSES.upper() : MODEL_EFI_LIBRARY_CLASS,
294 TAB_PACKAGES.upper() : MODEL_META_DATA_PACKAGE,
295 TAB_NMAKE.upper() : MODEL_META_DATA_NMAKE,
296 TAB_INF_FIXED_PCD.upper() : MODEL_PCD_FIXED_AT_BUILD,
297 TAB_INF_PATCH_PCD.upper() : MODEL_PCD_PATCHABLE_IN_MODULE,
298 TAB_INF_FEATURE_PCD.upper() : MODEL_PCD_FEATURE_FLAG,
299 TAB_INF_PCD_EX.upper() : MODEL_PCD_DYNAMIC_EX,
300 TAB_INF_PCD.upper() : MODEL_PCD_DYNAMIC,
301 TAB_SOURCES.upper() : MODEL_EFI_SOURCE_FILE,
302 TAB_GUIDS.upper() : MODEL_EFI_GUID,
303 TAB_PROTOCOLS.upper() : MODEL_EFI_PROTOCOL,
304 TAB_PPIS.upper() : MODEL_EFI_PPI,
305 TAB_DEPEX.upper() : MODEL_EFI_DEPEX,
306 TAB_BINARIES.upper() : MODEL_EFI_BINARY_FILE,
307 TAB_USER_EXTENSIONS.upper() : MODEL_META_DATA_USER_EXTENSION
308 }
309
310 ## Constructor of InfParser
311 #
312 # Initialize object of InfParser
313 #
314 # @param FilePath The path of module description file
315 # @param FileType The raw data of DSC file
316 # @param Table Database used to retrieve module/package information
317 # @param Macros Macros used for replacement in file
318 #
319 def __init__(self, FilePath, FileType, Table, Macros=None):
320 MetaFileParser.__init__(self, FilePath, FileType, Table, Macros)
321
322 ## Parser starter
323 def Start(self):
324 NmakeLine = ''
325 try:
326 self._Content = open(self.MetaFile, 'r').readlines()
327 except:
328 EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile)
329
330 # parse the file line by line
331 IsFindBlockComment = False
332
333 for Index in range(0, len(self._Content)):
334 # skip empty, commented, block commented lines
335 Line = CleanString(self._Content[Index], AllowCppStyleComment=True)
336 NextLine = ''
337 if Index + 1 < len(self._Content):
338 NextLine = CleanString(self._Content[Index + 1])
339 if Line == '':
340 continue
341 if Line.find(DataType.TAB_COMMENT_R8_START) > -1:
342 IsFindBlockComment = True
343 continue
344 if Line.find(DataType.TAB_COMMENT_R8_END) > -1:
345 IsFindBlockComment = False
346 continue
347 if IsFindBlockComment:
348 continue
349
350 self._LineIndex = Index
351 self._CurrentLine = Line
352
353 # section header
354 if Line[0] == TAB_SECTION_START and Line[-1] == TAB_SECTION_END:
355 self._SectionHeaderParser()
356 continue
357 # merge two lines specified by '\' in section NMAKE
358 elif self._SectionType == MODEL_META_DATA_NMAKE:
359 if Line[-1] == '\\':
360 if NextLine == '':
361 self._CurrentLine = NmakeLine + Line[0:-1]
362 NmakeLine = ''
363 else:
364 if NextLine[0] == TAB_SECTION_START and NextLine[-1] == TAB_SECTION_END:
365 self._CurrentLine = NmakeLine + Line[0:-1]
366 NmakeLine = ''
367 else:
368 NmakeLine = NmakeLine + ' ' + Line[0:-1]
369 continue
370 else:
371 self._CurrentLine = NmakeLine + Line
372 NmakeLine = ''
373 elif Line.upper().startswith('DEFINE '):
374 # file private macros
375 self._MacroParser()
376 continue
377
378 # section content
379 self._ValueList = ['','','']
380 # parse current line, result will be put in self._ValueList
381 self._SectionParser[self._SectionType](self)
382 if self._ValueList == None:
383 continue
384 #
385 # Model, Value1, Value2, Value3, Arch, Platform, BelongsToItem=-1,
386 # LineBegin=-1, ColumnBegin=-1, LineEnd=-1, ColumnEnd=-1, Enabled=-1
387 #
388 for Arch, Platform in self._Scope:
389 self._Store(self._SectionType,
390 self._ValueList[0],
391 self._ValueList[1],
392 self._ValueList[2],
393 Arch,
394 Platform,
395 self._Owner,
396 self._LineIndex+1,
397 -1,
398 self._LineIndex+1,
399 -1,
400 0
401 )
402 if IsFindBlockComment:
403 EdkLogger.error("Parser", FORMAT_INVALID, "Open block comments (starting with /*) are expected to end with */",
404 File=self.MetaFile)
405 self._Done()
406
407 ## Data parser for the format in which there's path
408 #
409 # Only path can have macro used. So we need to replace them before use.
410 #
411 def _IncludeParser(self):
412 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)
413 self._ValueList[0:len(TokenList)] = TokenList
414 if len(self._Macros) > 0:
415 for Index in range(0, len(self._ValueList)):
416 Value = self._ValueList[Index]
417 if Value.upper().find('$(EFI_SOURCE)\Edk'.upper()) > -1 or Value.upper().find('$(EFI_SOURCE)/Edk'.upper()) > -1:
418 Value = '$(EDK_SOURCE)' + Value[17:]
419 if Value.find('$(EFI_SOURCE)') > -1 or Value.find('$(EDK_SOURCE)') > -1:
420 pass
421 elif Value.startswith('.'):
422 pass
423 elif Value.startswith('$('):
424 pass
425 else:
426 Value = '$(EFI_SOURCE)/' + Value
427
428 if Value == None or Value == '':
429 continue
430 self._ValueList[Index] = NormPath(Value, self._Macros)
431
432 ## Parse [Sources] section
433 #
434 # Only path can have macro used. So we need to replace them before use.
435 #
436 def _SourceFileParser(self):
437 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)
438 self._ValueList[0:len(TokenList)] = TokenList
439 # For Acpi tables, remove macro like ' TABLE_NAME=Sata1'
440 if 'COMPONENT_TYPE' in self._Macros:
441 if self._Macros['COMPONENT_TYPE'].upper() == 'ACPITABLE':
442 self._ValueList[0] = GetSplitValueList(self._ValueList[0], ' ', 1)[0]
443 if self._Macros['BASE_NAME'] == 'Microcode':
444 pass
445 if len(self._Macros) > 0:
446 for Index in range(0, len(self._ValueList)):
447 Value = self._ValueList[Index]
448 if Value == None or Value == '':
449 continue
450 self._ValueList[Index] = NormPath(Value, self._Macros)
451
452 ## Parse [Binaries] section
453 #
454 # Only path can have macro used. So we need to replace them before use.
455 #
456 def _BinaryFileParser(self):
457 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT, 2)
458 if len(TokenList) < 2:
459 EdkLogger.error('Parser', FORMAT_INVALID, "No file type or path specified",
460 ExtraData=self._CurrentLine + " (<FileType> | <FilePath> [| <Target>])",
461 File=self.MetaFile, Line=self._LineIndex+1)
462 if not TokenList[0]:
463 EdkLogger.error('Parser', FORMAT_INVALID, "No file type specified",
464 ExtraData=self._CurrentLine + " (<FileType> | <FilePath> [| <Target>])",
465 File=self.MetaFile, Line=self._LineIndex+1)
466 if not TokenList[1]:
467 EdkLogger.error('Parser', FORMAT_INVALID, "No file path specified",
468 ExtraData=self._CurrentLine + " (<FileType> | <FilePath> [| <Target>])",
469 File=self.MetaFile, Line=self._LineIndex+1)
470 self._ValueList[0:len(TokenList)] = TokenList
471 self._ValueList[1] = NormPath(self._ValueList[1], self._Macros)
472
473 ## [defines] section parser
474 def _DefineParser(self):
475 TokenList = GetSplitValueList(self._CurrentLine, TAB_EQUAL_SPLIT, 1)
476 self._ValueList[0:len(TokenList)] = TokenList
477 if self._ValueList[1] == '':
478 EdkLogger.error('Parser', FORMAT_INVALID, "No value specified",
479 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1)
480 self._Macros[TokenList[0]] = ReplaceMacro(TokenList[1], self._Macros, False)
481
482 ## [nmake] section parser (R8.x style only)
483 def _NmakeParser(self):
484 TokenList = GetSplitValueList(self._CurrentLine, TAB_EQUAL_SPLIT, 1)
485 self._ValueList[0:len(TokenList)] = TokenList
486 # remove macros
487 self._ValueList[1] = ReplaceMacro(self._ValueList[1], self._Macros, False)
488 # remove self-reference in macro setting
489 #self._ValueList[1] = ReplaceMacro(self._ValueList[1], {self._ValueList[0]:''})
490
491 ## [FixedPcd], [FeaturePcd], [PatchPcd], [Pcd] and [PcdEx] sections parser
492 def _PcdParser(self):
493 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT, 1)
494 ValueList = GetSplitValueList(TokenList[0], TAB_SPLIT)
495 if len(ValueList) != 2:
496 EdkLogger.error('Parser', FORMAT_INVALID, "Illegal token space GUID and PCD name format",
497 ExtraData=self._CurrentLine + " (<TokenSpaceGuidCName>.<PcdCName>)",
498 File=self.MetaFile, Line=self._LineIndex+1)
499 self._ValueList[0:1] = ValueList
500 if len(TokenList) > 1:
501 self._ValueList[2] = TokenList[1]
502 if self._ValueList[0] == '' or self._ValueList[1] == '':
503 EdkLogger.error('Parser', FORMAT_INVALID, "No token space GUID or PCD name specified",
504 ExtraData=self._CurrentLine + " (<TokenSpaceGuidCName>.<PcdCName>)",
505 File=self.MetaFile, Line=self._LineIndex+1)
506
507 ## [depex] section parser
508 def _DepexParser(self):
509 self._ValueList[0:1] = [self._CurrentLine]
510
511 _SectionParser = {
512 MODEL_UNKNOWN : MetaFileParser._Skip,
513 MODEL_META_DATA_HEADER : _DefineParser,
514 MODEL_META_DATA_BUILD_OPTION : MetaFileParser._BuildOptionParser,
515 MODEL_EFI_INCLUDE : _IncludeParser, # for R8.x modules
516 MODEL_EFI_LIBRARY_INSTANCE : MetaFileParser._CommonParser, # for R8.x modules
517 MODEL_EFI_LIBRARY_CLASS : MetaFileParser._PathParser,
518 MODEL_META_DATA_PACKAGE : MetaFileParser._PathParser,
519 MODEL_META_DATA_NMAKE : _NmakeParser, # for R8.x modules
520 MODEL_PCD_FIXED_AT_BUILD : _PcdParser,
521 MODEL_PCD_PATCHABLE_IN_MODULE : _PcdParser,
522 MODEL_PCD_FEATURE_FLAG : _PcdParser,
523 MODEL_PCD_DYNAMIC_EX : _PcdParser,
524 MODEL_PCD_DYNAMIC : _PcdParser,
525 MODEL_EFI_SOURCE_FILE : _SourceFileParser,
526 MODEL_EFI_GUID : MetaFileParser._CommonParser,
527 MODEL_EFI_PROTOCOL : MetaFileParser._CommonParser,
528 MODEL_EFI_PPI : MetaFileParser._CommonParser,
529 MODEL_EFI_DEPEX : _DepexParser,
530 MODEL_EFI_BINARY_FILE : _BinaryFileParser,
531 MODEL_META_DATA_USER_EXTENSION : MetaFileParser._Skip,
532 }
533
534 ## DSC file parser class
535 #
536 # @param FilePath The path of platform description file
537 # @param FileType The raw data of DSC file
538 # @param Table Database used to retrieve module/package information
539 # @param Macros Macros used for replacement in file
540 # @param Owner Owner ID (for sub-section parsing)
541 # @param From ID from which the data comes (for !INCLUDE directive)
542 #
543 class DscParser(MetaFileParser):
544 # DSC file supported data types (one type per section)
545 DataType = {
546 TAB_SKUIDS.upper() : MODEL_EFI_SKU_ID,
547 TAB_LIBRARIES.upper() : MODEL_EFI_LIBRARY_INSTANCE,
548 TAB_LIBRARY_CLASSES.upper() : MODEL_EFI_LIBRARY_CLASS,
549 TAB_BUILD_OPTIONS.upper() : MODEL_META_DATA_BUILD_OPTION,
550 TAB_PCDS_FIXED_AT_BUILD_NULL.upper() : MODEL_PCD_FIXED_AT_BUILD,
551 TAB_PCDS_PATCHABLE_IN_MODULE_NULL.upper() : MODEL_PCD_PATCHABLE_IN_MODULE,
552 TAB_PCDS_FEATURE_FLAG_NULL.upper() : MODEL_PCD_FEATURE_FLAG,
553 TAB_PCDS_DYNAMIC_DEFAULT_NULL.upper() : MODEL_PCD_DYNAMIC_DEFAULT,
554 TAB_PCDS_DYNAMIC_HII_NULL.upper() : MODEL_PCD_DYNAMIC_HII,
555 TAB_PCDS_DYNAMIC_VPD_NULL.upper() : MODEL_PCD_DYNAMIC_VPD,
556 TAB_PCDS_DYNAMIC_EX_DEFAULT_NULL.upper() : MODEL_PCD_DYNAMIC_EX_DEFAULT,
557 TAB_PCDS_DYNAMIC_EX_HII_NULL.upper() : MODEL_PCD_DYNAMIC_EX_HII,
558 TAB_PCDS_DYNAMIC_EX_VPD_NULL.upper() : MODEL_PCD_DYNAMIC_EX_VPD,
559 TAB_COMPONENTS.upper() : MODEL_META_DATA_COMPONENT,
560 TAB_COMPONENTS_SOURCE_OVERRIDE_PATH.upper() : MODEL_META_DATA_COMPONENT_SOURCE_OVERRIDE_PATH,
561 TAB_DSC_DEFINES.upper() : MODEL_META_DATA_HEADER,
562 TAB_INCLUDE.upper() : MODEL_META_DATA_INCLUDE,
563 TAB_IF.upper() : MODEL_META_DATA_CONDITIONAL_STATEMENT_IF,
564 TAB_IF_DEF.upper() : MODEL_META_DATA_CONDITIONAL_STATEMENT_IFDEF,
565 TAB_IF_N_DEF.upper() : MODEL_META_DATA_CONDITIONAL_STATEMENT_IFNDEF,
566 TAB_ELSE_IF.upper() : MODEL_META_DATA_CONDITIONAL_STATEMENT_ELSEIF,
567 TAB_ELSE.upper() : MODEL_META_DATA_CONDITIONAL_STATEMENT_ELSE,
568 TAB_END_IF.upper() : MODEL_META_DATA_CONDITIONAL_STATEMENT_ENDIF,
569 }
570
571 # sections which allow "!include" directive
572 _IncludeAllowedSection = [
573 TAB_COMMON_DEFINES.upper(),
574 TAB_LIBRARIES.upper(),
575 TAB_LIBRARY_CLASSES.upper(),
576 TAB_SKUIDS.upper(),
577 TAB_COMPONENTS.upper(),
578 TAB_BUILD_OPTIONS.upper(),
579 TAB_PCDS_FIXED_AT_BUILD_NULL.upper(),
580 TAB_PCDS_PATCHABLE_IN_MODULE_NULL.upper(),
581 TAB_PCDS_FEATURE_FLAG_NULL.upper(),
582 TAB_PCDS_DYNAMIC_DEFAULT_NULL.upper(),
583 TAB_PCDS_DYNAMIC_HII_NULL.upper(),
584 TAB_PCDS_DYNAMIC_VPD_NULL.upper(),
585 TAB_PCDS_DYNAMIC_EX_DEFAULT_NULL.upper(),
586 TAB_PCDS_DYNAMIC_EX_HII_NULL.upper(),
587 TAB_PCDS_DYNAMIC_EX_VPD_NULL.upper(),
588 ]
589
590 # operators which can be used in "!if/!ifdef/!ifndef" directives
591 _OP_ = {
592 "!" : lambda a: not a,
593 "!=" : lambda a,b: a!=b,
594 "==" : lambda a,b: a==b,
595 ">" : lambda a,b: a>b,
596 "<" : lambda a,b: a<b,
597 "=>" : lambda a,b: a>=b,
598 ">=" : lambda a,b: a>=b,
599 "<=" : lambda a,b: a<=b,
600 "=<" : lambda a,b: a<=b,
601 }
602
603 ## Constructor of DscParser
604 #
605 # Initialize object of DscParser
606 #
607 # @param FilePath The path of platform description file
608 # @param FileType The raw data of DSC file
609 # @param Table Database used to retrieve module/package information
610 # @param Macros Macros used for replacement in file
611 # @param Owner Owner ID (for sub-section parsing)
612 # @param From ID from which the data comes (for !INCLUDE directive)
613 #
614 def __init__(self, FilePath, FileType, Table, Macros=None, Owner=-1, From=-1):
615 MetaFileParser.__init__(self, FilePath, FileType, Table, Macros, Owner, From)
616 # to store conditional directive evaluation result
617 self._Eval = Blist()
618
619 ## Parser starter
620 def Start(self):
621 try:
622 if self._Content == None:
623 self._Content = open(self.MetaFile, 'r').readlines()
624 except:
625 EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile)
626
627 for Index in range(0, len(self._Content)):
628 Line = CleanString(self._Content[Index])
629 # skip empty line
630 if Line == '':
631 continue
632 self._CurrentLine = Line
633 self._LineIndex = Index
634 if self._InSubsection and self._Owner == -1:
635 self._Owner = self._LastItem
636
637 # section header
638 if Line[0] == TAB_SECTION_START and Line[-1] == TAB_SECTION_END:
639 self._SectionHeaderParser()
640 continue
641 # subsection ending
642 elif Line[0] == '}':
643 self._InSubsection = False
644 self._SubsectionType = MODEL_UNKNOWN
645 self._SubsectionName = ''
646 self._Owner = -1
647 continue
648 # subsection header
649 elif Line[0] == TAB_OPTION_START and Line[-1] == TAB_OPTION_END:
650 self._SubsectionHeaderParser()
651 continue
652 # directive line
653 elif Line[0] == '!':
654 self._DirectiveParser()
655 continue
656 # file private macros
657 elif Line.upper().startswith('DEFINE '):
658 if self._Enabled < 0:
659 # Do not parse the macro and add it to self._Macros dictionary if directives
660 # statement is evaluated to false.
661 continue
662
663 (Name, Value) = self._MacroParser()
664 # Make the defined macro in DSC [Defines] section also
665 # available for FDF file.
666 if self._SectionName == TAB_COMMON_DEFINES.upper():
667 self._LastItem = self._Store(
668 MODEL_META_DATA_GLOBAL_DEFINE,
669 Name,
670 Value,
671 '',
672 'COMMON',
673 'COMMON',
674 self._Owner,
675 self._From,
676 self._LineIndex+1,
677 -1,
678 self._LineIndex+1,
679 -1,
680 self._Enabled
681 )
682 continue
683 elif Line.upper().startswith('EDK_GLOBAL '):
684 if self._Enabled < 0:
685 # Do not parse the macro and add it to self._Macros dictionary
686 # if previous directives statement is evaluated to false.
687 continue
688
689 (Name, Value) = self._MacroParser()
690 for Arch, ModuleType in self._Scope:
691 self._LastItem = self._Store(
692 MODEL_META_DATA_DEFINE,
693 Name,
694 Value,
695 '',
696 Arch,
697 'COMMON',
698 self._Owner,
699 self._From,
700 self._LineIndex+1,
701 -1,
702 self._LineIndex+1,
703 -1,
704 self._Enabled
705 )
706 continue
707
708 # section content
709 if self._InSubsection:
710 SectionType = self._SubsectionType
711 SectionName = self._SubsectionName
712 else:
713 SectionType = self._SectionType
714 SectionName = self._SectionName
715
716 self._ValueList = ['', '', '']
717 self._SectionParser[SectionType](self)
718 if self._ValueList == None:
719 continue
720
721 #
722 # Model, Value1, Value2, Value3, Arch, ModuleType, BelongsToItem=-1, BelongsToFile=-1,
723 # LineBegin=-1, ColumnBegin=-1, LineEnd=-1, ColumnEnd=-1, Enabled=-1
724 #
725 for Arch, ModuleType in self._Scope:
726 self._LastItem = self._Store(
727 SectionType,
728 self._ValueList[0],
729 self._ValueList[1],
730 self._ValueList[2],
731 Arch,
732 ModuleType,
733 self._Owner,
734 self._From,
735 self._LineIndex+1,
736 -1,
737 self._LineIndex+1,
738 -1,
739 self._Enabled
740 )
741 self._Done()
742
743 ## [defines] section parser
744 def _DefineParser(self):
745 TokenList = GetSplitValueList(self._CurrentLine, TAB_EQUAL_SPLIT, 1)
746 if len(TokenList) < 2:
747 EdkLogger.error('Parser', FORMAT_INVALID, "No value specified",
748 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex+1)
749 # 'FLASH_DEFINITION', 'OUTPUT_DIRECTORY' need special processing
750 if TokenList[0] in ['FLASH_DEFINITION', 'OUTPUT_DIRECTORY']:
751 TokenList[1] = NormPath(TokenList[1], self._Macros)
752 self._ValueList[0:len(TokenList)] = TokenList
753 # Treat elements in the [defines] section as global macros for FDF file.
754 self._LastItem = self._Store(
755 MODEL_META_DATA_GLOBAL_DEFINE,
756 TokenList[0],
757 TokenList[1],
758 '',
759 'COMMON',
760 'COMMON',
761 self._Owner,
762 self._From,
763 self._LineIndex+1,
764 -1,
765 self._LineIndex+1,
766 -1,
767 self._Enabled
768 )
769
770 ## <subsection_header> parser
771 def _SubsectionHeaderParser(self):
772 self._SubsectionName = self._CurrentLine[1:-1].upper()
773 if self._SubsectionName in self.DataType:
774 self._SubsectionType = self.DataType[self._SubsectionName]
775 else:
776 self._SubsectionType = MODEL_UNKNOWN
777 EdkLogger.warn("Parser", "Unrecognized sub-section", File=self.MetaFile,
778 Line=self._LineIndex+1, ExtraData=self._CurrentLine)
779
780 ## Directive statement parser
781 def _DirectiveParser(self):
782 self._ValueList = ['','','']
783 TokenList = GetSplitValueList(self._CurrentLine, ' ', 1)
784 self._ValueList[0:len(TokenList)] = TokenList
785 DirectiveName = self._ValueList[0].upper()
786 if DirectiveName not in self.DataType:
787 EdkLogger.error("Parser", FORMAT_INVALID, "Unknown directive [%s]" % DirectiveName,
788 File=self.MetaFile, Line=self._LineIndex+1)
789 if DirectiveName in ['!IF', '!IFDEF', '!INCLUDE', '!IFNDEF', '!ELSEIF'] and self._ValueList[1] == '':
790 EdkLogger.error("Parser", FORMAT_INVALID, "Missing expression",
791 File=self.MetaFile, Line=self._LineIndex+1,
792 ExtraData=self._CurrentLine)
793 # keep the directive in database first
794 self._LastItem = self._Store(
795 self.DataType[DirectiveName],
796 self._ValueList[0],
797 self._ValueList[1],
798 self._ValueList[2],
799 'COMMON',
800 'COMMON',
801 self._Owner,
802 self._From,
803 self._LineIndex + 1,
804 -1,
805 self._LineIndex + 1,
806 -1,
807 0
808 )
809
810 # process the directive
811 if DirectiveName == "!INCLUDE":
812 if not self._SectionName in self._IncludeAllowedSection:
813 EdkLogger.error("Parser", FORMAT_INVALID, File=self.MetaFile, Line=self._LineIndex+1,
814 ExtraData="'!include' is not allowed under section [%s]" % self._SectionName)
815 # the included file must be relative to workspace
816 IncludedFile = os.path.join(os.environ["WORKSPACE"], NormPath(self._ValueList[1], self._Macros))
817 Parser = DscParser(IncludedFile, self._FileType, self._Table, self._Macros, From=self._LastItem)
818 # set the parser status with current status
819 Parser._SectionName = self._SectionName
820 Parser._SectionType = self._SectionType
821 Parser._Scope = self._Scope
822 Parser._Enabled = self._Enabled
823 try:
824 Parser.Start()
825 except:
826 EdkLogger.error("Parser", PARSER_ERROR, File=self.MetaFile, Line=self._LineIndex+1,
827 ExtraData="Failed to parse content in file %s" % IncludedFile)
828 # insert an imaginary token in the DSC table to indicate its external dependency on another file
829 self._Store(MODEL_EXTERNAL_DEPENDENCY, IncludedFile, str(os.stat(IncludedFile)[8]), "")
830 # update current status with sub-parser's status
831 self._SectionName = Parser._SectionName
832 self._SectionType = Parser._SectionType
833 self._Scope = Parser._Scope
834 self._Enabled = Parser._Enabled
835 self._Macros.update(Parser._Macros)
836 else:
837 if DirectiveName in ["!IF", "!IFDEF", "!IFNDEF"]:
838 # evaluate the expression
839 Result = self._Evaluate(self._ValueList[1])
840 if DirectiveName == "!IFNDEF":
841 Result = not Result
842 self._Eval.append(Result)
843 elif DirectiveName in ["!ELSEIF"]:
844 # evaluate the expression
845 self._Eval[-1] = (not self._Eval[-1]) & self._Evaluate(self._ValueList[1])
846 elif DirectiveName in ["!ELSE"]:
847 self._Eval[-1] = not self._Eval[-1]
848 elif DirectiveName in ["!ENDIF"]:
849 if len(self._Eval) > 0:
850 self._Eval.pop()
851 else:
852 EdkLogger.error("Parser", FORMAT_INVALID, "!IF..[!ELSE]..!ENDIF doesn't match",
853 File=self.MetaFile, Line=self._LineIndex+1)
854 if self._Eval.Result == False:
855 self._Enabled = 0 - len(self._Eval)
856 else:
857 self._Enabled = len(self._Eval)
858
859 ## Evaluate the Token for its value; for now only macros are supported.
860 def _EvaluateToken(self, TokenName, Expression):
861 if TokenName.startswith("$(") and TokenName.endswith(")"):
862 Name = TokenName[2:-1]
863 return self._Macros.get(Name)
864 else:
865 EdkLogger.error('Parser', FORMAT_INVALID, "Unknown operand '%(Token)s', "
866 "please use '$(%(Token)s)' if '%(Token)s' is a macro" % {"Token" : TokenName},
867 File=self.MetaFile, Line=self._LineIndex+1, ExtraData=Expression)
868
869 ## Evaluate the value of expression in "if/ifdef/ifndef" directives
870 def _Evaluate(self, Expression):
871 TokenList = Expression.split()
872 TokenNumber = len(TokenList)
873 # one operand, guess it's just a macro name
874 if TokenNumber == 1:
875 TokenValue = self._EvaluateToken(TokenList[0], Expression)
876 return TokenValue != None
877 # two operands, suppose it's "!xxx" format
878 elif TokenNumber == 2:
879 Op = TokenList[0]
880 if Op not in self._OP_:
881 EdkLogger.error('Parser', FORMAT_INVALID, "Unsupported operator [%s]" % Op, File=self.MetaFile,
882 Line=self._LineIndex+1, ExtraData=Expression)
883 if TokenList[1].upper() == 'TRUE':
884 Value = True
885 else:
886 Value = False
887 return self._OP_[Op](Value)
888 # three operands
889 elif TokenNumber == 3:
890 TokenValue = self._EvaluateToken(TokenList[0], Expression)
891 if TokenValue == None:
892 return False
893 Value = TokenList[2]
894 if Value[0] in ["'", '"'] and Value[-1] in ["'", '"']:
895 Value = Value[1:-1]
896 Op = TokenList[1]
897 if Op not in self._OP_:
898 EdkLogger.error('Parser', FORMAT_INVALID, "Unsupported operator [%s]" % Op, File=self.MetaFile,
899 Line=self._LineIndex+1, ExtraData=Expression)
900 return self._OP_[Op](TokenValue, Value)
901 else:
902 EdkLogger.error('Parser', FORMAT_INVALID, File=self.MetaFile, Line=self._LineIndex+1,
903 ExtraData=Expression)
904
905 ## PCD sections parser
906 #
907 # [PcdsFixedAtBuild]
908 # [PcdsPatchableInModule]
909 # [PcdsFeatureFlag]
910 # [PcdsDynamicEx
911 # [PcdsDynamicExDefault]
912 # [PcdsDynamicExVpd]
913 # [PcdsDynamicExHii]
914 # [PcdsDynamic]
915 # [PcdsDynamicDefault]
916 # [PcdsDynamicVpd]
917 # [PcdsDynamicHii]
918 #
919 def _PcdParser(self):
920 TokenList = GetSplitValueList(ReplaceMacro(self._CurrentLine, self._Macros), TAB_VALUE_SPLIT, 1)
921 self._ValueList[0:1] = GetSplitValueList(TokenList[0], TAB_SPLIT)
922 if len(TokenList) == 2:
923 self._ValueList[2] = TokenList[1]
924 if self._ValueList[0] == '' or self._ValueList[1] == '':
925 EdkLogger.error('Parser', FORMAT_INVALID, "No token space GUID or PCD name specified",
926 ExtraData=self._CurrentLine + " (<TokenSpaceGuidCName>.<TokenCName>|<PcdValue>)",
927 File=self.MetaFile, Line=self._LineIndex+1)
928 if self._ValueList[2] == '':
929 EdkLogger.error('Parser', FORMAT_INVALID, "No PCD value given",
930 ExtraData=self._CurrentLine + " (<TokenSpaceGuidCName>.<TokenCName>|<PcdValue>)",
931 File=self.MetaFile, Line=self._LineIndex+1)
932
933 ## [components] section parser
934 def _ComponentParser(self):
935 if self._CurrentLine[-1] == '{':
936 self._ValueList[0] = self._CurrentLine[0:-1].strip()
937 self._InSubsection = True
938 else:
939 self._ValueList[0] = self._CurrentLine
940 if len(self._Macros) > 0:
941 self._ValueList[0] = NormPath(self._ValueList[0], self._Macros)
942
943 def _LibraryClassParser(self):
944 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT)
945 if len(TokenList) < 2:
946 EdkLogger.error('Parser', FORMAT_INVALID, "No library class or instance specified",
947 ExtraData=self._CurrentLine + " (<LibraryClassName>|<LibraryInstancePath>)",
948 File=self.MetaFile, Line=self._LineIndex+1)
949 if TokenList[0] == '':
950 EdkLogger.error('Parser', FORMAT_INVALID, "No library class specified",
951 ExtraData=self._CurrentLine + " (<LibraryClassName>|<LibraryInstancePath>)",
952 File=self.MetaFile, Line=self._LineIndex+1)
953 if TokenList[1] == '':
954 EdkLogger.error('Parser', FORMAT_INVALID, "No library instance specified",
955 ExtraData=self._CurrentLine + " (<LibraryClassName>|<LibraryInstancePath>)",
956 File=self.MetaFile, Line=self._LineIndex+1)
957 self._ValueList[0:len(TokenList)] = TokenList
958 if len(self._Macros) > 0:
959 self._ValueList[1] = NormPath(self._ValueList[1], self._Macros)
960
961 def _CompponentSourceOverridePathParser(self):
962 if len(self._Macros) > 0:
963 self._ValueList[0] = NormPath(self._CurrentLine, self._Macros)
964
965 _SectionParser = {
966 MODEL_META_DATA_HEADER : _DefineParser,
967 MODEL_EFI_SKU_ID : MetaFileParser._CommonParser,
968 MODEL_EFI_LIBRARY_INSTANCE : MetaFileParser._PathParser,
969 MODEL_EFI_LIBRARY_CLASS : _LibraryClassParser,
970 MODEL_PCD_FIXED_AT_BUILD : _PcdParser,
971 MODEL_PCD_PATCHABLE_IN_MODULE : _PcdParser,
972 MODEL_PCD_FEATURE_FLAG : _PcdParser,
973 MODEL_PCD_DYNAMIC_DEFAULT : _PcdParser,
974 MODEL_PCD_DYNAMIC_HII : _PcdParser,
975 MODEL_PCD_DYNAMIC_VPD : _PcdParser,
976 MODEL_PCD_DYNAMIC_EX_DEFAULT : _PcdParser,
977 MODEL_PCD_DYNAMIC_EX_HII : _PcdParser,
978 MODEL_PCD_DYNAMIC_EX_VPD : _PcdParser,
979 MODEL_META_DATA_COMPONENT : _ComponentParser,
980 MODEL_META_DATA_COMPONENT_SOURCE_OVERRIDE_PATH : _CompponentSourceOverridePathParser,
981 MODEL_META_DATA_BUILD_OPTION : MetaFileParser._BuildOptionParser,
982 MODEL_UNKNOWN : MetaFileParser._Skip,
983 MODEL_META_DATA_USER_EXTENSION : MetaFileParser._Skip,
984 }
985
986 ## DEC file parser class
987 #
988 # @param FilePath The path of platform description file
989 # @param FileType The raw data of DSC file
990 # @param Table Database used to retrieve module/package information
991 # @param Macros Macros used for replacement in file
992 #
993 class DecParser(MetaFileParser):
994 # DEC file supported data types (one type per section)
995 DataType = {
996 TAB_DEC_DEFINES.upper() : MODEL_META_DATA_HEADER,
997 TAB_INCLUDES.upper() : MODEL_EFI_INCLUDE,
998 TAB_LIBRARY_CLASSES.upper() : MODEL_EFI_LIBRARY_CLASS,
999 TAB_GUIDS.upper() : MODEL_EFI_GUID,
1000 TAB_PPIS.upper() : MODEL_EFI_PPI,
1001 TAB_PROTOCOLS.upper() : MODEL_EFI_PROTOCOL,
1002 TAB_PCDS_FIXED_AT_BUILD_NULL.upper() : MODEL_PCD_FIXED_AT_BUILD,
1003 TAB_PCDS_PATCHABLE_IN_MODULE_NULL.upper() : MODEL_PCD_PATCHABLE_IN_MODULE,
1004 TAB_PCDS_FEATURE_FLAG_NULL.upper() : MODEL_PCD_FEATURE_FLAG,
1005 TAB_PCDS_DYNAMIC_NULL.upper() : MODEL_PCD_DYNAMIC,
1006 TAB_PCDS_DYNAMIC_EX_NULL.upper() : MODEL_PCD_DYNAMIC_EX,
1007 }
1008
1009 ## Constructor of DecParser
1010 #
1011 # Initialize object of DecParser
1012 #
1013 # @param FilePath The path of platform description file
1014 # @param FileType The raw data of DSC file
1015 # @param Table Database used to retrieve module/package information
1016 # @param Macros Macros used for replacement in file
1017 #
1018 def __init__(self, FilePath, FileType, Table, Macro=None):
1019 MetaFileParser.__init__(self, FilePath, FileType, Table, Macro, -1)
1020 self._Comments = []
1021
1022 ## Parser starter
1023 def Start(self):
1024 try:
1025 if self._Content == None:
1026 self._Content = open(self.MetaFile, 'r').readlines()
1027 except:
1028 EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile)
1029
1030 for Index in range(0, len(self._Content)):
1031 Line, Comment = CleanString2(self._Content[Index])
1032 self._CurrentLine = Line
1033 self._LineIndex = Index
1034
1035 # save comment for later use
1036 if Comment:
1037 self._Comments.append((Comment, self._LineIndex+1))
1038 # skip empty line
1039 if Line == '':
1040 continue
1041
1042 # section header
1043 if Line[0] == TAB_SECTION_START and Line[-1] == TAB_SECTION_END:
1044 self._SectionHeaderParser()
1045 self._Comments = []
1046 continue
1047 elif Line.startswith('DEFINE '):
1048 self._MacroParser()
1049 continue
1050 elif len(self._SectionType) == 0:
1051 self._Comments = []
1052 continue
1053
1054 # section content
1055 self._ValueList = ['','','']
1056 self._SectionParser[self._SectionType[0]](self)
1057 if self._ValueList == None:
1058 self._Comments = []
1059 continue
1060
1061 #
1062 # Model, Value1, Value2, Value3, Arch, BelongsToItem=-1, LineBegin=-1,
1063 # ColumnBegin=-1, LineEnd=-1, ColumnEnd=-1, FeatureFlag='', Enabled=-1
1064 #
1065 for Arch, ModuleType, Type in self._Scope:
1066 self._LastItem = self._Store(
1067 Type,
1068 self._ValueList[0],
1069 self._ValueList[1],
1070 self._ValueList[2],
1071 Arch,
1072 ModuleType,
1073 self._Owner,
1074 self._LineIndex+1,
1075 -1,
1076 self._LineIndex+1,
1077 -1,
1078 0
1079 )
1080 for Comment, LineNo in self._Comments:
1081 self._Store(
1082 MODEL_META_DATA_COMMENT,
1083 Comment,
1084 self._ValueList[0],
1085 self._ValueList[1],
1086 Arch,
1087 ModuleType,
1088 self._LastItem,
1089 LineNo,
1090 -1,
1091 LineNo,
1092 -1,
1093 0
1094 )
1095 self._Comments = []
1096 self._Done()
1097
1098 ## Section header parser
1099 #
1100 # The section header is always in following format:
1101 #
1102 # [section_name.arch<.platform|module_type>]
1103 #
1104 def _SectionHeaderParser(self):
1105 self._Scope = []
1106 self._SectionName = ''
1107 self._SectionType = []
1108 ArchList = set()
1109 for Item in GetSplitValueList(self._CurrentLine[1:-1], TAB_COMMA_SPLIT):
1110 if Item == '':
1111 continue
1112 ItemList = GetSplitValueList(Item, TAB_SPLIT)
1113
1114 # different types of PCD are permissible in one section
1115 self._SectionName = ItemList[0].upper()
1116 if self._SectionName in self.DataType:
1117 if self.DataType[self._SectionName] not in self._SectionType:
1118 self._SectionType.append(self.DataType[self._SectionName])
1119 else:
1120 EdkLogger.warn("Parser", "Unrecognized section", File=self.MetaFile,
1121 Line=self._LineIndex+1, ExtraData=self._CurrentLine)
1122 continue
1123
1124 if MODEL_PCD_FEATURE_FLAG in self._SectionType and len(self._SectionType) > 1:
1125 EdkLogger.error(
1126 'Parser',
1127 FORMAT_INVALID,
1128 "%s must not be in the same section of other types of PCD" % TAB_PCDS_FEATURE_FLAG_NULL,
1129 File=self.MetaFile,
1130 Line=self._LineIndex+1,
1131 ExtraData=self._CurrentLine
1132 )
1133 # S1 is always Arch
1134 if len(ItemList) > 1:
1135 S1 = ItemList[1].upper()
1136 else:
1137 S1 = 'COMMON'
1138 ArchList.add(S1)
1139 # S2 may be Platform or ModuleType
1140 if len(ItemList) > 2:
1141 S2 = ItemList[2].upper()
1142 else:
1143 S2 = 'COMMON'
1144 if [S1, S2, self.DataType[self._SectionName]] not in self._Scope:
1145 self._Scope.append([S1, S2, self.DataType[self._SectionName]])
1146
1147 # 'COMMON' must not be used with specific ARCHs at the same section
1148 if 'COMMON' in ArchList and len(ArchList) > 1:
1149 EdkLogger.error('Parser', FORMAT_INVALID, "'common' ARCH must not be used with specific ARCHs",
1150 File=self.MetaFile, Line=self._LineIndex+1, ExtraData=self._CurrentLine)
1151
1152 ## [guids], [ppis] and [protocols] section parser
1153 def _GuidParser(self):
1154 TokenList = GetSplitValueList(self._CurrentLine, TAB_EQUAL_SPLIT, 1)
1155 if len(TokenList) < 2:
1156 EdkLogger.error('Parser', FORMAT_INVALID, "No GUID name or value specified",
1157 ExtraData=self._CurrentLine + " (<CName> = <GuidValueInCFormat>)",
1158 File=self.MetaFile, Line=self._LineIndex+1)
1159 if TokenList[0] == '':
1160 EdkLogger.error('Parser', FORMAT_INVALID, "No GUID name specified",
1161 ExtraData=self._CurrentLine + " (<CName> = <GuidValueInCFormat>)",
1162 File=self.MetaFile, Line=self._LineIndex+1)
1163 if TokenList[1] == '':
1164 EdkLogger.error('Parser', FORMAT_INVALID, "No GUID value specified",
1165 ExtraData=self._CurrentLine + " (<CName> = <GuidValueInCFormat>)",
1166 File=self.MetaFile, Line=self._LineIndex+1)
1167 if TokenList[1][0] != '{' or TokenList[1][-1] != '}' or GuidStructureStringToGuidString(TokenList[1]) == '':
1168 EdkLogger.error('Parser', FORMAT_INVALID, "Invalid GUID value format",
1169 ExtraData=self._CurrentLine + \
1170 " (<CName> = <GuidValueInCFormat:{8,4,4,{2,2,2,2,2,2,2,2}}>)",
1171 File=self.MetaFile, Line=self._LineIndex+1)
1172 self._ValueList[0] = TokenList[0]
1173 self._ValueList[1] = TokenList[1]
1174
1175 ## PCD sections parser
1176 #
1177 # [PcdsFixedAtBuild]
1178 # [PcdsPatchableInModule]
1179 # [PcdsFeatureFlag]
1180 # [PcdsDynamicEx
1181 # [PcdsDynamic]
1182 #
1183 def _PcdParser(self):
1184 TokenList = GetSplitValueList(self._CurrentLine, TAB_VALUE_SPLIT, 1)
1185 self._ValueList[0:1] = GetSplitValueList(TokenList[0], TAB_SPLIT)
1186 # check PCD information
1187 if self._ValueList[0] == '' or self._ValueList[1] == '':
1188 EdkLogger.error('Parser', FORMAT_INVALID, "No token space GUID or PCD name specified",
1189 ExtraData=self._CurrentLine + \
1190 " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",
1191 File=self.MetaFile, Line=self._LineIndex+1)
1192 # check PCD datum information
1193 if len(TokenList) < 2 or TokenList[1] == '':
1194 EdkLogger.error('Parser', FORMAT_INVALID, "No PCD Datum information given",
1195 ExtraData=self._CurrentLine + \
1196 " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",
1197 File=self.MetaFile, Line=self._LineIndex+1)
1198
1199 ValueList = GetSplitValueList(TokenList[1])
1200 # check if there's enough datum information given
1201 if len(ValueList) != 3:
1202 EdkLogger.error('Parser', FORMAT_INVALID, "Invalid PCD Datum information given",
1203 ExtraData=self._CurrentLine + \
1204 " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",
1205 File=self.MetaFile, Line=self._LineIndex+1)
1206 # check default value
1207 if ValueList[0] == '':
1208 EdkLogger.error('Parser', FORMAT_INVALID, "Missing DefaultValue in PCD Datum information",
1209 ExtraData=self._CurrentLine + \
1210 " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",
1211 File=self.MetaFile, Line=self._LineIndex+1)
1212 # check datum type
1213 if ValueList[1] == '':
1214 EdkLogger.error('Parser', FORMAT_INVALID, "Missing DatumType in PCD Datum information",
1215 ExtraData=self._CurrentLine + \
1216 " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",
1217 File=self.MetaFile, Line=self._LineIndex+1)
1218 # check token of the PCD
1219 if ValueList[2] == '':
1220 EdkLogger.error('Parser', FORMAT_INVALID, "Missing Token in PCD Datum information",
1221 ExtraData=self._CurrentLine + \
1222 " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",
1223 File=self.MetaFile, Line=self._LineIndex+1)
1224 # check format of default value against the datum type
1225 IsValid, Cause = CheckPcdDatum(ValueList[1], ValueList[0])
1226 if not IsValid:
1227 EdkLogger.error('Parser', FORMAT_INVALID, Cause, ExtraData=self._CurrentLine,
1228 File=self.MetaFile, Line=self._LineIndex+1)
1229
1230 self._ValueList[2] = ValueList[0].strip() + '|' + ValueList[1].strip() + '|' + ValueList[2].strip()
1231
1232 _SectionParser = {
1233 MODEL_META_DATA_HEADER : MetaFileParser._DefineParser,
1234 MODEL_EFI_INCLUDE : MetaFileParser._PathParser,
1235 MODEL_EFI_LIBRARY_CLASS : MetaFileParser._PathParser,
1236 MODEL_EFI_GUID : _GuidParser,
1237 MODEL_EFI_PPI : _GuidParser,
1238 MODEL_EFI_PROTOCOL : _GuidParser,
1239 MODEL_PCD_FIXED_AT_BUILD : _PcdParser,
1240 MODEL_PCD_PATCHABLE_IN_MODULE : _PcdParser,
1241 MODEL_PCD_FEATURE_FLAG : _PcdParser,
1242 MODEL_PCD_DYNAMIC : _PcdParser,
1243 MODEL_PCD_DYNAMIC_EX : _PcdParser,
1244 MODEL_UNKNOWN : MetaFileParser._Skip,
1245 MODEL_META_DATA_USER_EXTENSION : MetaFileParser._Skip,
1246 }
1247
1248 ##
1249 #
1250 # This acts like the main() function for the script, unless it is 'import'ed into another
1251 # script.
1252 #
1253 if __name__ == '__main__':
1254 pass
1255