]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Workspace/MetaFileParser.py
BaseTools: Remove the old python "not-equal"
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileParser.py
index 3d123147170932e5c220814b1d06c6bc553c84b7..d5fbf6f095bf502d3dd9b4b412209747d224db7f 100644 (file)
@@ -2,7 +2,7 @@
 # This file is used to parse meta files\r
 #\r
 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
-# (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
+# (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>\r
 # This program and the accompanying materials\r
 # are licensed and made available under the terms and conditions of the BSD License\r
 # which accompanies this distribution.  The full text of the license may be found at\r
@@ -15,6 +15,7 @@
 ##\r
 # Import Modules\r
 #\r
+from __future__ import print_function\r
 import Common.LongFilePathOs as os\r
 import re\r
 import time\r
@@ -26,15 +27,19 @@ import Common.GlobalData as GlobalData
 \r
 from CommonDataClass.DataClass import *\r
 from Common.DataType import *\r
-from Common.String import *\r
+from Common.StringUtils import *\r
 from Common.Misc import GuidStructureStringToGuidString, CheckPcdDatum, PathClass, AnalyzePcdData, AnalyzeDscPcd, AnalyzePcdExpression, ParseFieldValue\r
 from Common.Expression import *\r
 from CommonDataClass.Exceptions import *\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
-\r
+from collections import defaultdict\r
 from MetaFileTable import MetaFileStorage\r
 from MetaFileCommentParser import CheckInfComment\r
 \r
+## RegEx for finding file versions\r
+hexVersionPattern = re.compile(r'0[xX][\da-f-A-F]{5,8}')\r
+decVersionPattern = re.compile(r'\d+\.\d+')\r
+\r
 ## A decorator used to parse macro definition\r
 def ParseMacro(Parser):\r
     def MacroParser(self):\r
@@ -159,7 +164,7 @@ class MetaFileParser(object):
         self._FileDir = self.MetaFile.Dir\r
         self._Defines = {}\r
         self._FileLocalMacros = {}\r
-        self._SectionsMacroDict = {}\r
+        self._SectionsMacroDict = defaultdict(dict)\r
 \r
         # for recursive parsing\r
         self._Owner = [Owner]\r
@@ -306,7 +311,7 @@ class MetaFileParser(object):
             if self._SectionName in self.DataType:\r
                 self._SectionType = self.DataType[self._SectionName]\r
                 # Check if the section name is valid\r
-                if self._SectionName not in SECTIONS_HAVE_ITEM_AFTER_ARCH and len(ItemList) > 3:\r
+                if self._SectionName not in SECTIONS_HAVE_ITEM_AFTER_ARCH_SET and len(ItemList) > 3:\r
                     EdkLogger.error("Parser", FORMAT_UNKNOWN_ERROR, "%s is not a valid section name" % Item,\r
                                     self.MetaFile, self._LineIndex + 1, self._CurrentLine)\r
             elif self._Version >= 0x00010005:\r
@@ -324,7 +329,7 @@ class MetaFileParser(object):
 \r
             # S2 may be Platform or ModuleType\r
             if len(ItemList) > 2:\r
-                if self._SectionName.upper() in SECTIONS_HAVE_ITEM_PCD:\r
+                if self._SectionName.upper() in SECTIONS_HAVE_ITEM_PCD_SET:\r
                     S2 = ItemList[2]\r
                 else:\r
                     S2 = ItemList[2].upper()\r
@@ -366,9 +371,9 @@ class MetaFileParser(object):
                 EdkLogger.error("Parser", FORMAT_INVALID, "%s not defined" % (Macro), ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
         # Sometimes, we need to make differences between EDK and EDK2 modules \r
         if Name == 'INF_VERSION':\r
-            if re.match(r'0[xX][\da-f-A-F]{5,8}', Value):\r
+            if hexVersionPattern.match(Value):\r
                 self._Version = int(Value, 0)   \r
-            elif re.match(r'\d+\.\d+', Value):\r
+            elif decVersionPattern.match(Value):\r
                 ValueList = Value.split('.')\r
                 Major = '%04o' % int(ValueList[0], 0)\r
                 Minor = '%04o' % int(ValueList[1], 0)\r
@@ -417,17 +422,16 @@ class MetaFileParser(object):
     def _ConstructSectionMacroDict(self, Name, Value):\r
         ScopeKey = [(Scope[0], Scope[1],Scope[2]) for Scope in self._Scope]\r
         ScopeKey = tuple(ScopeKey)\r
-        SectionDictKey = self._SectionType, ScopeKey\r
         #\r
         # DecParser SectionType is a list, will contain more than one item only in Pcd Section\r
         # As Pcd section macro usage is not alllowed, so here it is safe\r
         #\r
         if type(self) == DecParser:\r
             SectionDictKey = self._SectionType[0], ScopeKey\r
-        if SectionDictKey not in self._SectionsMacroDict:\r
-            self._SectionsMacroDict[SectionDictKey] = {}\r
-        SectionLocalMacros = self._SectionsMacroDict[SectionDictKey]\r
-        SectionLocalMacros[Name] = Value\r
+        else:\r
+            SectionDictKey = self._SectionType, ScopeKey\r
+\r
+        self._SectionsMacroDict[SectionDictKey][Name] = Value\r
 \r
     ## Get section Macros that are applicable to current line, which may come from other sections \r
     ## that share the same name while scope is wider\r
@@ -835,6 +839,7 @@ class DscParser(MetaFileParser):
         TAB_ELSE.upper()                            :   MODEL_META_DATA_CONDITIONAL_STATEMENT_ELSE,\r
         TAB_END_IF.upper()                          :   MODEL_META_DATA_CONDITIONAL_STATEMENT_ENDIF,\r
         TAB_USER_EXTENSIONS.upper()                 :   MODEL_META_DATA_USER_EXTENSION,\r
+        TAB_ERROR.upper()                           :   MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR,\r
     }\r
 \r
     # Valid names in define section\r
@@ -932,7 +937,7 @@ class DscParser(MetaFileParser):
                 self._SubsectionType = MODEL_UNKNOWN\r
                 self._SubsectionName = ''\r
                 self._Owner[-1] = -1\r
-                OwnerId = {}\r
+                OwnerId.clear()\r
                 continue\r
             # subsection header\r
             elif Line[0] == TAB_OPTION_START and Line[-1] == TAB_OPTION_END:\r
@@ -1025,6 +1030,8 @@ class DscParser(MetaFileParser):
         Scope = [[TAB_COMMON, TAB_COMMON, TAB_COMMON]]\r
         if ItemType == MODEL_META_DATA_INCLUDE:\r
             Scope = self._Scope\r
+        elif ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR:\r
+            Scope = self._Scope\r
         if ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ENDIF:\r
             # Remove all directives between !if and !endif, including themselves\r
             while self._DirectiveStack:\r
@@ -1038,7 +1045,7 @@ class DscParser(MetaFileParser):
                 EdkLogger.error("Parser", FORMAT_INVALID, "Redundant '!endif'",\r
                                 File=self.MetaFile, Line=self._LineIndex + 1,\r
                                 ExtraData=self._CurrentLine)\r
-        elif ItemType != MODEL_META_DATA_INCLUDE:\r
+        elif ItemType not in {MODEL_META_DATA_INCLUDE, MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR}:\r
             # Break if there's a !else is followed by a !elseif\r
             if ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ELSEIF and \\r
                self._DirectiveStack and \\r
@@ -1284,6 +1291,7 @@ class DscParser(MetaFileParser):
             MODEL_META_DATA_BUILD_OPTION                    :   self.__ProcessBuildOption,\r
             MODEL_UNKNOWN                                   :   self._Skip,\r
             MODEL_META_DATA_USER_EXTENSION                  :   self._SkipUserExtension,\r
+            MODEL_META_DATA_CONDITIONAL_STATEMENT_ERROR     :   self._ProcessError,\r
         }\r
 \r
         self._Table = MetaFileStorage(self._RawTable.Cur, self.MetaFile, MODEL_FILE_DSC, True)\r
@@ -1292,7 +1300,7 @@ class DscParser(MetaFileParser):
         self._DirectiveEvalStack = []\r
         self._FileWithError = self.MetaFile\r
         self._FileLocalMacros = {}\r
-        self._SectionsMacroDict = {}\r
+        self._SectionsMacroDict.clear()\r
         GlobalData.gPlatformDefines = {}\r
 \r
         # Get all macro and PCD which has straitforward value\r
@@ -1334,7 +1342,7 @@ class DscParser(MetaFileParser):
                 self._InSubsection = False\r
             try:\r
                 Processer[self._ItemType]()\r
-            except EvaluationException, Excpt:\r
+            except EvaluationException as Excpt:\r
                 # \r
                 # Only catch expression evaluation error here. We need to report\r
                 # the precise number of line on which the error occurred\r
@@ -1356,7 +1364,7 @@ class DscParser(MetaFileParser):
                     EdkLogger.error('Parser', FORMAT_INVALID, "Invalid expression: %s" % str(Excpt),\r
                                     File=self._FileWithError, ExtraData=' '.join(self._ValueList),\r
                                     Line=self._LineIndex + 1)\r
-            except MacroException, Excpt:\r
+            except MacroException as Excpt:\r
                 EdkLogger.error('Parser', FORMAT_INVALID, str(Excpt),\r
                                 File=self._FileWithError, ExtraData=' '.join(self._ValueList),\r
                                 Line=self._LineIndex + 1)\r
@@ -1387,6 +1395,10 @@ class DscParser(MetaFileParser):
         GlobalData.gPlatformDefines.update(self._FileLocalMacros)\r
         self._PostProcessed = True\r
         self._Content = None\r
+    def _ProcessError(self):\r
+        if not self._Enabled:\r
+            return\r
+        EdkLogger.error('Parser', ERROR_STATEMENT, self._ValueList[1], File=self.MetaFile, Line=self._LineIndex + 1)\r
 \r
     def __ProcessSectionHeader(self):\r
         self._SectionName = self._ValueList[0]\r
@@ -1454,10 +1466,10 @@ class DscParser(MetaFileParser):
             Macros.update(GlobalData.gGlobalDefines)\r
             try:\r
                 Result = ValueExpression(self._ValueList[1], Macros)()\r
-            except SymbolNotFound, Exc:\r
+            except SymbolNotFound as Exc:\r
                 EdkLogger.debug(EdkLogger.DEBUG_5, str(Exc), self._ValueList[1])\r
                 Result = False\r
-            except WrnExpression, Excpt:\r
+            except WrnExpression as Excpt:\r
                 # \r
                 # Catch expression evaluation warning here. We need to report\r
                 # the precise number of line and return the evaluation result\r
@@ -1538,21 +1550,22 @@ class DscParser(MetaFileParser):
             self._FileWithError = IncludedFile1\r
 \r
             IncludedFileTable = MetaFileStorage(self._Table.Cur, IncludedFile1, MODEL_FILE_DSC, False)\r
-            Owner = self._Content[self._ContentIndex - 1][0]\r
+            FromItem = self._Content[self._ContentIndex - 1][0]\r
+            if self._InSubsection:\r
+                Owner = self._Content[self._ContentIndex - 1][8]\r
+            else:\r
+                Owner = self._Content[self._ContentIndex - 1][0]\r
             Parser = DscParser(IncludedFile1, self._FileType, self._Arch, IncludedFileTable,\r
-                               Owner=Owner, From=Owner)\r
+                               Owner=Owner, From=FromItem)\r
 \r
             self.IncludedFiles.add (IncludedFile1)\r
 \r
-            # Does not allow lower level included file to include upper level included file\r
-            if Parser._From != Owner and int(Owner) > int (Parser._From):\r
-                EdkLogger.error('parser', FILE_ALREADY_EXIST, File=self._FileWithError,\r
-                    Line=self._LineIndex + 1, ExtraData="{0} is already included at a higher level.".format(IncludedFile1))\r
-\r
-\r
             # set the parser status with current status\r
             Parser._SectionName = self._SectionName\r
-            Parser._SectionType = self._SectionType\r
+            if self._InSubsection:\r
+                Parser._SectionType = self._SubsectionType\r
+            else:\r
+                Parser._SectionType = self._SectionType\r
             Parser._Scope = self._Scope\r
             Parser._Enabled = self._Enabled\r
             # Parse the included file\r
@@ -1560,7 +1573,11 @@ class DscParser(MetaFileParser):
 \r
             # update current status with sub-parser's status\r
             self._SectionName = Parser._SectionName\r
-            self._SectionType = Parser._SectionType\r
+            if not self._InSubsection:\r
+                self._SectionType = Parser._SectionType\r
+            self._SubsectionType = Parser._SubsectionType\r
+            self._InSubsection = Parser._InSubsection\r
+\r
             self._Scope = Parser._Scope\r
             self._Enabled = Parser._Enabled\r
 \r
@@ -1598,7 +1615,7 @@ class DscParser(MetaFileParser):
         if PcdValue and "." not in self._ValueList[0]:\r
             try:\r
                 ValList[Index] = ValueExpression(PcdValue, self._Macros)(True)\r
-            except WrnExpression, Value:\r
+            except WrnExpression as Value:\r
                 ValList[Index] = Value.result\r
             except:\r
                 pass\r
@@ -1614,7 +1631,7 @@ class DscParser(MetaFileParser):
         try:\r
             self._ValueList[2] = '|'.join(ValList)\r
         except Exception:\r
-            print ValList\r
+            print(ValList)\r
 \r
     def __ProcessComponent(self):\r
         self._ValueList[0] = ReplaceMacro(self._ValueList[0], self._Macros)\r
@@ -2003,7 +2020,7 @@ class DecParser(MetaFileParser):
                 try:\r
                     self._GuidDict.update(self._AllPcdDict)\r
                     ValueList[0] = ValueExpressionEx(ValueList[0], ValueList[1], self._GuidDict)(True)\r
-                except BadExpression, Value:\r
+                except BadExpression as Value:\r
                     EdkLogger.error('Parser', FORMAT_INVALID, Value, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
             # check format of default value against the datum type\r
             IsValid, Cause = CheckPcdDatum(ValueList[1], ValueList[0])\r