]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Workspace/MetaFileParser.py
BaseTools: Support TabSpace between section tag in DEC file
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / MetaFileParser.py
index f96c73c1db5d3ec257316b98db225f0308b16862..b756361afd7aca6d72a36487ff8d90384a86ae31 100644 (file)
@@ -1,7 +1,8 @@
 ## @file\r
 # This file is used to parse meta files\r
 #\r
-# Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>\r
+# (C) Copyright 2015-2016 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
@@ -25,7 +26,7 @@ import Common.GlobalData as GlobalData
 from CommonDataClass.DataClass import *\r
 from Common.DataType import *\r
 from Common.String import *\r
-from Common.Misc import GuidStructureStringToGuidString, CheckPcdDatum, PathClass, AnalyzePcdData, AnalyzeDscPcd\r
+from Common.Misc import GuidStructureStringToGuidString, CheckPcdDatum, PathClass, AnalyzePcdData, AnalyzeDscPcd, AnalyzePcdExpression\r
 from Common.Expression import *\r
 from CommonDataClass.Exceptions import *\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
@@ -143,14 +144,15 @@ class MetaFileParser(object):
     #\r
     #   @param      FilePath        The path of platform description file\r
     #   @param      FileType        The raw data of DSC file\r
+    #   @param      Arch            Default Arch value for filtering sections\r
     #   @param      Table           Database used to retrieve module/package information\r
-    #   @param      Macros          Macros used for replacement in file\r
     #   @param      Owner           Owner ID (for sub-section parsing)\r
     #   @param      From            ID from which the data comes (for !INCLUDE directive)\r
     #\r
-    def __init__(self, FilePath, FileType, Table, Owner= -1, From= -1):\r
+    def __init__(self, FilePath, FileType, Arch, Table, Owner= -1, From= -1):\r
         self._Table = Table\r
         self._RawTable = Table\r
+        self._Arch = Arch\r
         self._FileType = FileType\r
         self.MetaFile = FilePath\r
         self._FileDir = self.MetaFile.Dir\r
@@ -210,6 +212,15 @@ class MetaFileParser(object):
     def _SetFinished(self, Value):\r
         self._Finished = Value\r
 \r
+    ## Remove records that do not match given Filter Arch\r
+    def _FilterRecordList(self, RecordList, FilterArch):\r
+        NewRecordList = []\r
+        for Record in RecordList:\r
+            Arch = Record[3]\r
+            if Arch == 'COMMON' or Arch == FilterArch:\r
+                NewRecordList.append(Record)\r
+        return NewRecordList\r
+\r
     ## Use [] style to query data in table, just for readability\r
     #\r
     #   DataInfo = [data_type, scope1(arch), scope2(platform/moduletype)]\r
@@ -229,13 +240,13 @@ class MetaFileParser(object):
 \r
         # No specific ARCH or Platform given, use raw data\r
         if self._RawTable and (len(DataInfo) == 1 or DataInfo[1] == None):\r
-            return self._RawTable.Query(*DataInfo)\r
+            return self._FilterRecordList(self._RawTable.Query(*DataInfo), self._Arch)\r
 \r
         # Do post-process if necessary\r
         if not self._PostProcessed:\r
             self._PostProcess()\r
 \r
-        return self._Table.Query(*DataInfo)\r
+        return self._FilterRecordList(self._Table.Query(*DataInfo), DataInfo[1])\r
 \r
     ## Data parser for the common format in different type of file\r
     #\r
@@ -340,11 +351,23 @@ class MetaFileParser(object):
 \r
         self._ValueList = [ReplaceMacro(Value, self._Macros) for Value in self._ValueList]\r
         Name, Value = self._ValueList[1], self._ValueList[2]\r
+        MacroUsed = GlobalData.gMacroRefPattern.findall(Value)\r
+        if len(MacroUsed) != 0:\r
+            for Macro in MacroUsed:\r
+                if Macro in GlobalData.gGlobalDefines:\r
+                    EdkLogger.error("Parser", FORMAT_INVALID, "Global macro %s is not permitted." % (Macro), ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
+            else:\r
+                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
-            try:\r
-                self._Version = int(Value, 0)\r
-            except:\r
+            if re.match(r'0[xX][\da-f-A-F]{5,8}', Value):\r
+                self._Version = int(Value, 0)   \r
+            elif re.match(r'\d+\.\d+', Value):\r
+                ValueList = Value.split('.')\r
+                Major = '%04o' % int(ValueList[0], 0)\r
+                Minor = '%04o' % int(ValueList[1], 0)\r
+                self._Version = int('0x' + Major + Minor, 0)\r
+            else:\r
                 EdkLogger.error('Parser', FORMAT_INVALID, "Invalid version number",\r
                                 ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
 \r
@@ -376,7 +399,8 @@ class MetaFileParser(object):
                 File=self.MetaFile,\r
                 Line=self._LineIndex + 1\r
                 )\r
-\r
+    def GetValidExpression(self, TokenSpaceGuid, PcdCName):\r
+        return self._Table.GetValidExpression(TokenSpaceGuid, PcdCName)\r
     def _GetMacros(self):\r
         Macros = {}\r
         Macros.update(self._FileLocalMacros)\r
@@ -483,14 +507,14 @@ class InfParser(MetaFileParser):
     #\r
     #   @param      FilePath        The path of module description file\r
     #   @param      FileType        The raw data of DSC file\r
+    #   @param      Arch            Default Arch value for filtering sections\r
     #   @param      Table           Database used to retrieve module/package information\r
-    #   @param      Macros          Macros used for replacement in file\r
     #\r
-    def __init__(self, FilePath, FileType, Table):\r
+    def __init__(self, FilePath, FileType, Arch, Table):\r
         # prevent re-initialization\r
         if hasattr(self, "_Table"):\r
             return\r
-        MetaFileParser.__init__(self, FilePath, FileType, Table)\r
+        MetaFileParser.__init__(self, FilePath, FileType, Arch, Table)\r
         self.PcdsDict = {}\r
 \r
     ## Parser starter\r
@@ -814,6 +838,7 @@ class DscParser(MetaFileParser):
         "PLATFORM_VERSION",\r
         "SKUID_IDENTIFIER",\r
         "PCD_INFO_GENERATION",\r
+        "PCD_VAR_CHECK_GENERATION",\r
         "SUPPORTED_ARCHITECTURES",\r
         "BUILD_TARGETS",\r
         "OUTPUT_DIRECTORY",\r
@@ -823,7 +848,9 @@ class DscParser(MetaFileParser):
         "ISO_LANGUAGES",\r
         "TIME_STAMP_FILE",\r
         "VPD_TOOL_GUID",\r
-        "FIX_LOAD_TOP_MEMORY_ADDRESS"\r
+        "FIX_LOAD_TOP_MEMORY_ADDRESS",\r
+        "PREBUILD",\r
+        "POSTBUILD"\r
     ]\r
 \r
     SubSectionDefineKeywords = [\r
@@ -832,22 +859,24 @@ class DscParser(MetaFileParser):
 \r
     SymbolPattern = ValueExpression.SymbolPattern\r
 \r
+    IncludedFiles = set()\r
+\r
     ## Constructor of DscParser\r
     #\r
     #  Initialize object of DscParser\r
     #\r
     #   @param      FilePath        The path of platform description file\r
     #   @param      FileType        The raw data of DSC file\r
+    #   @param      Arch            Default Arch value for filtering sections\r
     #   @param      Table           Database used to retrieve module/package information\r
-    #   @param      Macros          Macros used for replacement in file\r
     #   @param      Owner           Owner ID (for sub-section parsing)\r
     #   @param      From            ID from which the data comes (for !INCLUDE directive)\r
     #\r
-    def __init__(self, FilePath, FileType, Table, Owner= -1, From= -1):\r
+    def __init__(self, FilePath, FileType, Arch, Table, Owner= -1, From= -1):\r
         # prevent re-initialization\r
         if hasattr(self, "_Table"):\r
             return\r
-        MetaFileParser.__init__(self, FilePath, FileType, Table, Owner, From)\r
+        MetaFileParser.__init__(self, FilePath, FileType, Arch, Table, Owner, From)\r
         self._Version = 0x00010005  # Only EDK2 dsc file is supported\r
         # to store conditional directive evaluation result\r
         self._DirectiveStack = []\r
@@ -905,6 +934,8 @@ class DscParser(MetaFileParser):
             elif Line[0] == '!':\r
                 self._DirectiveParser()\r
                 continue\r
+            if Line[0] == TAB_OPTION_START and not self._InSubsection:\r
+                EdkLogger.error("Parser", FILE_READ_FAILURE, "Missing the '{' before %s in Line %s" % (Line, Index+1),ExtraData=self.MetaFile)\r
 \r
             if self._InSubsection:\r
                 SectionType = self._SubsectionType\r
@@ -1008,11 +1039,6 @@ class DscParser(MetaFileParser):
                                 File=self.MetaFile, Line=self._LineIndex + 1,\r
                                 ExtraData=self._CurrentLine)\r
             self._DirectiveStack.append((ItemType, self._LineIndex + 1, self._CurrentLine))\r
-        elif self._From > 0:\r
-            EdkLogger.error('Parser', FORMAT_INVALID,\r
-                            "No '!include' allowed in included file",\r
-                            ExtraData=self._CurrentLine, File=self.MetaFile,\r
-                            Line=self._LineIndex + 1)\r
 \r
         #\r
         # Model, Value1, Value2, Value3, Arch, ModuleType, BelongsToItem=-1, BelongsToFile=-1,\r
@@ -1113,6 +1139,13 @@ class DscParser(MetaFileParser):
             EdkLogger.error('Parser', FORMAT_INVALID, "The datum type '%s' of PCD is wrong" % ValueList[1],\r
                             ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
 \r
+        # Validate the VariableName of DynamicHii and DynamicExHii for PCD Entry must not be an empty string\r
+        if self._ItemType in [MODEL_PCD_DYNAMIC_HII, MODEL_PCD_DYNAMIC_EX_HII]:\r
+            DscPcdValueList = GetSplitValueList(TokenList[1], TAB_VALUE_SPLIT, 1)\r
+            if len(DscPcdValueList[0].replace('L','').replace('"','').strip()) == 0:\r
+                EdkLogger.error('Parser', FORMAT_INVALID, "The VariableName field in the HII format PCD entry must not be an empty string",\r
+                            ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
+\r
         # if value are 'True', 'true', 'TRUE' or 'False', 'false', 'FALSE', replace with integer 1 or 0.\r
         DscPcdValueList = GetSplitValueList(TokenList[1], TAB_VALUE_SPLIT, 1)\r
         if DscPcdValueList[0] in ['True', 'true', 'TRUE']:\r
@@ -1474,9 +1507,17 @@ class DscParser(MetaFileParser):
 \r
             IncludedFileTable = MetaFileStorage(self._Table.Cur, IncludedFile1, MODEL_FILE_DSC, False)\r
             Owner = self._Content[self._ContentIndex - 1][0]\r
-            Parser = DscParser(IncludedFile1, self._FileType, IncludedFileTable,\r
+            Parser = DscParser(IncludedFile1, self._FileType, self._Arch, IncludedFileTable,\r
                                Owner=Owner, From=Owner)\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
@@ -1516,7 +1557,7 @@ class DscParser(MetaFileParser):
 \r
         ValList, Valid, Index = AnalyzeDscPcd(self._ValueList[2], self._ItemType)\r
         if not Valid:\r
-            EdkLogger.error('build', FORMAT_INVALID, "Pcd format incorrect.", File=self._FileWithError, Line=self._LineIndex+1,\r
+            EdkLogger.error('build', FORMAT_INVALID, "Pcd format incorrect.", File=self._FileWithError, Line=self._LineIndex + 1,\r
                             ExtraData="%s.%s|%s" % (self._ValueList[0], self._ValueList[1], self._ValueList[2]))\r
         PcdValue = ValList[Index]\r
         if PcdValue:\r
@@ -1601,17 +1642,18 @@ class DecParser(MetaFileParser):
     #\r
     #   @param      FilePath        The path of platform description file\r
     #   @param      FileType        The raw data of DSC file\r
+    #   @param      Arch            Default Arch value for filtering sections\r
     #   @param      Table           Database used to retrieve module/package information\r
-    #   @param      Macros          Macros used for replacement in file\r
     #\r
-    def __init__(self, FilePath, FileType, Table):\r
+    def __init__(self, FilePath, FileType, Arch, Table):\r
         # prevent re-initialization\r
         if hasattr(self, "_Table"):\r
             return\r
-        MetaFileParser.__init__(self, FilePath, FileType, Table, -1)\r
+        MetaFileParser.__init__(self, FilePath, FileType, Arch, Table, -1)\r
         self._Comments = []\r
         self._Version = 0x00010005  # Only EDK2 dec file is supported\r
         self._AllPCDs = [] # Only for check duplicate PCD\r
+        self._AllPcdDict = {}\r
 \r
     ## Parser starter\r
     def Start(self):\r
@@ -1621,6 +1663,7 @@ class DecParser(MetaFileParser):
         except:\r
             EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile)\r
 \r
+        self._DefinesCount = 0\r
         for Index in range(0, len(Content)):\r
             Line, Comment = CleanString2(Content[Index])\r
             self._CurrentLine = Line\r
@@ -1636,8 +1679,15 @@ class DecParser(MetaFileParser):
             # section header\r
             if Line[0] == TAB_SECTION_START and Line[-1] == TAB_SECTION_END:\r
                 self._SectionHeaderParser()\r
+                if self._SectionName == TAB_DEC_DEFINES.upper():\r
+                    self._DefinesCount += 1\r
                 self._Comments = []\r
                 continue\r
+            if self._SectionType == MODEL_UNKNOWN:\r
+                EdkLogger.error("Parser", FORMAT_INVALID,\r
+                                ""\r
+                                "Not able to determine \"%s\" in which section."%self._CurrentLine,\r
+                                self.MetaFile, self._LineIndex + 1)\r
             elif len(self._SectionType) == 0:\r
                 self._Comments = []\r
                 continue\r
@@ -1685,6 +1735,10 @@ class DecParser(MetaFileParser):
                         0\r
                         )\r
             self._Comments = []\r
+        if self._DefinesCount > 1:\r
+            EdkLogger.error('Parser', FORMAT_INVALID, 'Multiple [Defines] section is exist.', self.MetaFile )\r
+        if self._DefinesCount == 0:\r
+            EdkLogger.error('Parser', FORMAT_INVALID, 'No [Defines] section exist.',self.MetaFile)\r
         self._Done()\r
 \r
 \r
@@ -1699,7 +1753,8 @@ class DecParser(MetaFileParser):
         self._SectionName = ''\r
         self._SectionType = []\r
         ArchList = set()\r
-        Line = self._CurrentLine.replace("%s%s" % (TAB_COMMA_SPLIT, TAB_SPACE_SPLIT), TAB_COMMA_SPLIT)\r
+        PrivateList = set()\r
+        Line = re.sub(',[\s]*', TAB_COMMA_SPLIT, self._CurrentLine)\r
         for Item in Line[1:-1].split(TAB_COMMA_SPLIT):\r
             if Item == '':\r
                 EdkLogger.error("Parser", FORMAT_UNKNOWN_ERROR,\r
@@ -1709,6 +1764,9 @@ class DecParser(MetaFileParser):
 \r
             # different types of PCD are permissible in one section\r
             self._SectionName = ItemList[0].upper()\r
+            if self._SectionName == TAB_DEC_DEFINES.upper() and (len(ItemList) > 1 or len(Line.split(TAB_COMMA_SPLIT)) > 1):\r
+                EdkLogger.error("Parser", FORMAT_INVALID, "Defines section format is invalid",\r
+                                self.MetaFile, self._LineIndex + 1, self._CurrentLine)\r
             if self._SectionName in self.DataType:\r
                 if self.DataType[self._SectionName] not in self._SectionType:\r
                     self._SectionType.append(self.DataType[self._SectionName])\r
@@ -1734,8 +1792,14 @@ class DecParser(MetaFileParser):
             # S2 may be Platform or ModuleType\r
             if len(ItemList) > 2:\r
                 S2 = ItemList[2].upper()\r
+                # only Includes, GUIDs, PPIs, Protocols section have Private tag\r
+                if self._SectionName in [TAB_INCLUDES.upper(), TAB_GUIDS.upper(), TAB_PROTOCOLS.upper(), TAB_PPIS.upper()]:\r
+                    if S2 != 'PRIVATE':\r
+                        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
+            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
@@ -1744,6 +1808,11 @@ class DecParser(MetaFileParser):
             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
+            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
     ## [guids], [ppis] and [protocols] section parser\r
     @ParseMacro\r
     def _GuidParser(self):\r
@@ -1813,10 +1882,10 @@ class DecParser(MetaFileParser):
         # Has VOID* type string, may contain "|" character in the string. \r
         if len(PtrValue) != 0:\r
             ptrValueList = re.sub(ValueRe, '', TokenList[1])\r
-            ValueList = GetSplitValueList(ptrValueList)\r
+            ValueList = AnalyzePcdExpression(ptrValueList)\r
             ValueList[0] = PtrValue[0]\r
         else:\r
-            ValueList = GetSplitValueList(TokenList[1])\r
+            ValueList = AnalyzePcdExpression(TokenList[1])\r
 \r
 \r
         # check if there's enough datum information given\r
@@ -1843,6 +1912,19 @@ class DecParser(MetaFileParser):
                             ExtraData=self._CurrentLine + \\r
                                       " (<TokenSpaceGuidCName>.<PcdCName>|<DefaultValue>|<DatumType>|<Token>)",\r
                             File=self.MetaFile, Line=self._LineIndex + 1)\r
+\r
+        PcdValue = ValueList[0]\r
+        if PcdValue:\r
+            try:\r
+                ValueList[0] = ValueExpression(PcdValue, self._AllPcdDict)(True)\r
+            except WrnExpression, Value:\r
+                ValueList[0] = Value.result\r
+\r
+        if ValueList[0] == 'True':\r
+            ValueList[0] = '1'\r
+        if ValueList[0] == 'False':\r
+            ValueList[0] = '0'\r
+\r
         # check format of default value against the datum type\r
         IsValid, Cause = CheckPcdDatum(ValueList[1], ValueList[0])\r
         if not IsValid:\r
@@ -1861,6 +1943,7 @@ class DecParser(MetaFileParser):
                             ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)\r
         else:\r
             self._AllPCDs.append((self._Scope[0], self._ValueList[0], self._ValueList[1]))\r
+            self._AllPcdDict[TAB_SPLIT.join(self._ValueList[0:2])] = ValueList[0]\r
 \r
         self._ValueList[2] = ValueList[0].strip() + '|' + ValueList[1].strip() + '|' + ValueList[2].strip()\r
 \r