Fixed HDS tracker 200760: Gendepex tool should handle merging of GUID in the final...
authorjwang36 <jwang36@7335b38e-4728-0410-8992-fb3ffe349368>
Thu, 28 Feb 2008 06:50:15 +0000 (06:50 +0000)
committerjwang36 <jwang36@7335b38e-4728-0410-8992-fb3ffe349368>
Thu, 28 Feb 2008 06:50:15 +0000 (06:50 +0000)
git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@1025 7335b38e-4728-0410-8992-fb3ffe349368

Source/Python/AutoGen/GenDepex.py

index e372788..d5e899a 100644 (file)
@@ -97,7 +97,7 @@ class DependencyExpression:
     #   @param  Expression  The list or string of dependency expression\r
     #   @param  ModuleType  The type of the module using the dependency expression\r
     # \r
-    def __init__(self, Expression, ModuleType):\r
+    def __init__(self, Expression, ModuleType, Optimize=False):\r
         self.Phase = gType2Phase[ModuleType]\r
         if type(Expression) == type([]):\r
             self.ExpressionString = " ".join(Expression)\r
@@ -111,6 +111,8 @@ class DependencyExpression:
 \r
         self.GetPostfixNotation()\r
         self.ValidateOpcode()\r
+        if Optimize:\r
+            self.Optimize()\r
 \r
     def __str__(self):\r
         return " ".join(self.TokenList)\r
@@ -139,9 +141,15 @@ class DependencyExpression:
                         break\r
                     self.PostfixNotation.append(Stack.pop())\r
             elif Token in self.OpcodePriority:\r
-                if Token == "NOT" and LastToken not in self.SupportedOpcode:\r
-                    EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: missing operator before NOT",\r
-                                    ExtraData=str(self))\r
+                if Token == "NOT":\r
+                    if LastToken not in self.SupportedOpcode:\r
+                        EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: missing operator before NOT",\r
+                                        ExtraData=str(self))\r
+                else:\r
+                    if LastToken in self.SupportedOpcode:\r
+                        EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: missing operand before " + Token,\r
+                                        ExtraData=str(self))\r
+\r
                 while len(Stack) > 0:\r
                     if Stack[-1] == "(" or self.OpcodePriority[Token] >= self.OpcodePriority[Stack[-1]]:\r
                         break\r
@@ -204,6 +212,23 @@ class DependencyExpression:
             EdkLogger.error("GenDepex", PARSER_ERROR, "Extra expressions after END", \r
                             ExtraData=str(self))\r
 \r
+    ## Simply optimize the dependency expression by removing duplicated operands\r
+    def Optimize(self):\r
+        ValidOpcode = list(set(self.OpcodeList))\r
+        if len(ValidOpcode) != 1 or ValidOpcode[0] not in ['AND', 'OR']:\r
+            return\r
+        Op = ValidOpcode[0]\r
+        NewOperand = []\r
+        for Token in self.PostfixNotation:\r
+            if Token in self.SupportedOpcode or Token in NewOperand:\r
+                continue\r
+            NewOperand.append(Token)\r
+        Op = " " + Op + " "\r
+        self.TokenList = Op.join(NewOperand).split()\r
+        self.PostfixNotation = []\r
+        self.GetPostfixNotation()\r
+\r
+\r
     ## Convert a GUID value in C structure format into its binary form\r
     #\r
     #   @param  Guid    The GUID value in C structure format\r
@@ -263,6 +288,8 @@ def GetOptions():
                       help="The type of module for which the dependency expression serves")\r
     Parser.add_option("-e", "--dependency-expression", dest="Expression", default="",\r
                       help="The string of dependency expression. If this option presents, the input file will be ignored.")\r
+    Parser.add_option("-m", "--optimize", dest="Optimize", default=False, action="store_true",\r
+                      help="Do some simple optimization on the expression.")\r
     Parser.add_option("-v", "--verbose", dest="verbose", default=False, action="store_true",\r
                       help="build with verbose information")\r
     Parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true",\r
@@ -299,7 +326,7 @@ def Main():
             print "No expression string or file given"\r
             return 1\r
 \r
-        Dpx = DependencyExpression(DxsString, Option.ModuleType)\r
+        Dpx = DependencyExpression(DxsString, Option.ModuleType, Option.Optimize)\r
 \r
         if Option.OutputFile != None:\r
             Dpx.Generate(Option.OutputFile)\r