]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Workspace/MetaFileParser.py
BaseTools: Fix one bug of nest !include parser
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileParser.py
index 95ea6fb45abb5af1704c5dae9e0e316c6ab5684d..a2ded0c845ae49bfd6e880ffc42c549592df29f7 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
@@ -26,15 +26,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 +163,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
@@ -219,7 +223,7 @@ class MetaFileParser(object):
         NewRecordList = []\r
         for Record in RecordList:\r
             Arch = Record[3]\r
-            if Arch == 'COMMON' or Arch == FilterArch:\r
+            if Arch == TAB_ARCH_COMMON or Arch == FilterArch:\r
                 NewRecordList.append(Record)\r
         return NewRecordList\r
 \r
@@ -241,7 +245,7 @@ class MetaFileParser(object):
                 self.Start()\r
 \r
         # No specific ARCH or Platform given, use raw data\r
-        if self._RawTable and (len(DataInfo) == 1 or DataInfo[1] == None):\r
+        if self._RawTable and (len(DataInfo) == 1 or DataInfo[1] is None):\r
             return self._FilterRecordList(self._RawTable.Query(*DataInfo), self._Arch)\r
 \r
         # Do post-process if necessary\r
@@ -306,7 +310,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
@@ -319,25 +323,25 @@ class MetaFileParser(object):
             if len(ItemList) > 1:\r
                 S1 = ItemList[1].upper()\r
             else:\r
-                S1 = 'COMMON'\r
+                S1 = TAB_ARCH_COMMON\r
             ArchList.add(S1)\r
 \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
             else:\r
-                S2 = 'COMMON'\r
+                S2 = TAB_COMMON\r
             if len(ItemList) > 3:\r
                 S3 = ItemList[3]\r
             else:\r
-                S3 = "COMMON"\r
+                S3 = TAB_COMMON\r
             self._Scope.append([S1, S2, S3])\r
 \r
         # 'COMMON' must not be used with specific ARCHs at the same section\r
-        if 'COMMON' in ArchList and len(ArchList) > 1:\r
+        if TAB_ARCH_COMMON in ArchList and len(ArchList) > 1:\r
             EdkLogger.error('Parser', FORMAT_INVALID, "'common' ARCH must not be used with specific ARCHs",\r
                             File=self.MetaFile, Line=self._LineIndex + 1, ExtraData=self._CurrentLine)\r
         # If the section information is needed later, it should be stored in database\r
@@ -366,9 +370,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 +421,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
@@ -455,12 +458,12 @@ class MetaFileParser(object):
 \r
             for ActiveScope in self._Scope:\r
                 Scope0, Scope1,Scope2 = ActiveScope[0], ActiveScope[1],ActiveScope[2]\r
-                if(Scope0, Scope1,Scope2) not in Scope and (Scope0, "COMMON","COMMON") not in Scope and ("COMMON", Scope1,"COMMON") not in Scope:\r
+                if(Scope0, Scope1,Scope2) not in Scope and (Scope0, TAB_COMMON, TAB_COMMON) not in Scope and (TAB_COMMON, Scope1, TAB_COMMON) not in Scope:\r
                     break\r
             else:\r
                 ComSpeMacroDict.update(self._SectionsMacroDict[(SectionType, Scope)])\r
 \r
-            if ("COMMON", "COMMON","COMMON") in Scope:\r
+            if (TAB_COMMON, TAB_COMMON, TAB_COMMON) in Scope:\r
                 ComComMacroDict.update(self._SectionsMacroDict[(SectionType, Scope)])\r
 \r
         Macros.update(ComComMacroDict)\r
@@ -568,8 +571,8 @@ class InfParser(MetaFileParser):
             if Line[0] == TAB_SECTION_START and Line[-1] == TAB_SECTION_END:\r
                 if not GetHeaderComment:\r
                     for Cmt, LNo in Comments:\r
-                        self._Store(MODEL_META_DATA_HEADER_COMMENT, Cmt, '', '', 'COMMON',\r
-                                    'COMMON', self._Owner[-1], LNo, -1, LNo, -1, 0)\r
+                        self._Store(MODEL_META_DATA_HEADER_COMMENT, Cmt, '', '', TAB_COMMON,\r
+                                    TAB_COMMON, self._Owner[-1], LNo, -1, LNo, -1, 0)\r
                     GetHeaderComment = True\r
                 else:\r
                     TailComments.extend(SectionComments + Comments)\r
@@ -620,7 +623,7 @@ class InfParser(MetaFileParser):
             self._ValueList = ['', '', '']\r
             # parse current line, result will be put in self._ValueList\r
             self._SectionParser[self._SectionType](self)\r
-            if self._ValueList == None or self._ItemType == MODEL_META_DATA_DEFINE:\r
+            if self._ValueList is None or self._ItemType == MODEL_META_DATA_DEFINE:\r
                 self._ItemType = -1\r
                 Comments = []\r
                 continue\r
@@ -658,8 +661,8 @@ class InfParser(MetaFileParser):
 \r
         # If there are tail comments in INF file, save to database whatever the comments are\r
         for Comment in TailComments:\r
-            self._Store(MODEL_META_DATA_TAIL_COMMENT, Comment[0], '', '', 'COMMON',\r
-                                'COMMON', self._Owner[-1], -1, -1, -1, -1, 0)\r
+            self._Store(MODEL_META_DATA_TAIL_COMMENT, Comment[0], '', '', TAB_COMMON,\r
+                                TAB_COMMON, self._Owner[-1], -1, -1, -1, -1, 0)\r
         self._Done()\r
 \r
     ## Data parser for the format in which there's path\r
@@ -932,7 +935,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
@@ -952,7 +955,7 @@ class DscParser(MetaFileParser):
 \r
             self._ValueList = ['', '', '']\r
             self._SectionParser[SectionType](self)\r
-            if self._ValueList == None:\r
+            if self._ValueList is None:\r
                 continue\r
             #\r
             # Model, Value1, Value2, Value3, Arch, ModuleType, BelongsToItem=-1, BelongsToFile=-1,\r
@@ -1022,7 +1025,7 @@ class DscParser(MetaFileParser):
                             ExtraData=self._CurrentLine)\r
 \r
         ItemType = self.DataType[DirectiveName]\r
-        Scope = [['COMMON', 'COMMON','COMMON']]\r
+        Scope = [[TAB_COMMON, TAB_COMMON, TAB_COMMON]]\r
         if ItemType == MODEL_META_DATA_INCLUDE:\r
             Scope = self._Scope\r
         if ItemType == MODEL_META_DATA_CONDITIONAL_STATEMENT_ENDIF:\r
@@ -1243,6 +1246,13 @@ class DscParser(MetaFileParser):
         # PCD cannot be referenced in macro definition\r
         if self._ItemType not in [MODEL_META_DATA_DEFINE, MODEL_META_DATA_GLOBAL_DEFINE]:\r
             Macros.update(self._Symbols)\r
+        if GlobalData.BuildOptionPcd:\r
+            for Item in GlobalData.BuildOptionPcd:\r
+                if type(Item) is tuple:\r
+                    continue\r
+                PcdName, TmpValue = Item.split("=")\r
+                TmpValue = BuildOptionValue(TmpValue, self._GuidDict)\r
+                Macros[PcdName.strip()] = TmpValue\r
         return Macros\r
 \r
     def _PostProcess(self):\r
@@ -1285,7 +1295,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
@@ -1354,7 +1364,7 @@ class DscParser(MetaFileParser):
                                 File=self._FileWithError, ExtraData=' '.join(self._ValueList),\r
                                 Line=self._LineIndex + 1)\r
 \r
-            if self._ValueList == None:\r
+            if self._ValueList is None:\r
                 continue\r
 \r
             NewOwner = self._IdMapping.get(Owner, -1)\r
@@ -1497,12 +1507,12 @@ class DscParser(MetaFileParser):
             # Allow using system environment variables  in path after !include\r
             #\r
             __IncludeMacros['WORKSPACE'] = GlobalData.gGlobalDefines['WORKSPACE']\r
-            if "ECP_SOURCE" in GlobalData.gGlobalDefines.keys():\r
+            if "ECP_SOURCE" in GlobalData.gGlobalDefines:\r
                 __IncludeMacros['ECP_SOURCE'] = GlobalData.gGlobalDefines['ECP_SOURCE']\r
             #\r
             # During GenFds phase call DSC parser, will go into this branch.\r
             #\r
-            elif "ECP_SOURCE" in GlobalData.gCommandLineDefines.keys():\r
+            elif "ECP_SOURCE" in GlobalData.gCommandLineDefines:\r
                 __IncludeMacros['ECP_SOURCE'] = GlobalData.gCommandLineDefines['ECP_SOURCE']\r
 \r
             __IncludeMacros['EFI_SOURCE'] = GlobalData.gGlobalDefines['EFI_SOURCE']\r
@@ -1531,21 +1541,39 @@ 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
+            # todo: rework the nested include checking logic\r
+            # Current nested include checking rely on dsc file order inside build.db.\r
+            # It is not reliable and will lead to build fail in some case.\r
+            #\r
+            # When project A and B include a common dsc file C.\r
+            # Build project A. It give dsc file A = ID 1 in build.db, and C ID = 2.\r
+            # Build project B. It give dsc file B ID = 3, and C ID still = 2.\r
+            # Then, we build project B fail, unless we clean build.db.\r
+            # In oldder BaseTools, the project B ID will still be 1,\r
+            # that's why it work before.\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
+            #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
@@ -1553,7 +1581,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
@@ -1733,7 +1765,7 @@ class DecParser(MetaFileParser):
             # section content\r
             self._ValueList = ['', '', '']\r
             self._SectionParser[self._SectionType[0]](self)\r
-            if self._ValueList == None or self._ItemType == MODEL_META_DATA_DEFINE:\r
+            if self._ValueList is None or self._ItemType == MODEL_META_DATA_DEFINE:\r
                 self._ItemType = -1\r
                 self._Comments = []\r
                 continue\r
@@ -1825,7 +1857,7 @@ class DecParser(MetaFileParser):
             if len(ItemList) > 1:\r
                 S1 = ItemList[1].upper()\r
             else:\r
-                S1 = 'COMMON'\r
+                S1 = TAB_ARCH_COMMON\r
             ArchList.add(S1)\r
             # S2 may be Platform or ModuleType\r
             if len(ItemList) > 2:\r
@@ -1836,18 +1868,18 @@ class DecParser(MetaFileParser):
                         EdkLogger.error("Parser", FORMAT_INVALID, 'Please use keyword "Private" as section tag modifier.',\r
                                         File=self.MetaFile, Line=self._LineIndex + 1, ExtraData=self._CurrentLine)\r
             else:\r
-                S2 = 'COMMON'\r
+                S2 = TAB_COMMON\r
             PrivateList.add(S2)\r
             if [S1, S2, self.DataType[self._SectionName]] not in self._Scope:\r
                 self._Scope.append([S1, S2, self.DataType[self._SectionName]])\r
 \r
         # 'COMMON' must not be used with specific ARCHs at the same section\r
-        if 'COMMON' in ArchList and len(ArchList) > 1:\r
+        if TAB_ARCH_COMMON in ArchList and len(ArchList) > 1:\r
             EdkLogger.error('Parser', FORMAT_INVALID, "'common' ARCH must not be used with specific ARCHs",\r
                             File=self.MetaFile, Line=self._LineIndex + 1, ExtraData=self._CurrentLine)\r
 \r
         # It is not permissible to mix section tags without the Private attribute with section tags with the Private attribute\r
-        if 'COMMON' in PrivateList and len(PrivateList) > 1:\r
+        if TAB_COMMON in PrivateList and len(PrivateList) > 1:\r
             EdkLogger.error('Parser', FORMAT_INVALID, "Can't mix section tags without the Private attribute with section tags with the Private attribute",\r
                             File=self.MetaFile, Line=self._LineIndex + 1, ExtraData=self._CurrentLine)\r
 \r