]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: refactor to remove functions
authorJaben Carsey <jaben.carsey@intel.com>
Fri, 18 May 2018 00:06:51 +0000 (08:06 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Wed, 13 Jun 2018 01:02:26 +0000 (09:02 +0800)
refactoring almost identical functions to delete and use the other.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jaben Carsey <jaben.carsey@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools/Source/Python/Workspace/WorkspaceCommon.py

index 9a57b9abd99242598db285620a3ac37eb70973ba..0b5b3dcf465725c26398485dd8ca26ff0fbc091c 100644 (file)
@@ -42,6 +42,7 @@ from GenPatchPcdTable.GenPatchPcdTable import parsePcdInfoFromMapFile
 import Common.VpdInfoFile as VpdInfoFile\r
 from GenPcdDb import CreatePcdDatabaseCode\r
 from Workspace.MetaFileCommentParser import UsageList\r
+from Workspace.WorkspaceCommon import GetModuleLibInstances\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 import InfSectionParser\r
 import datetime\r
@@ -2174,162 +2175,14 @@ class PlatformAutoGen(AutoGen):
         if str(Module) not in self.Platform.Modules:\r
             return []\r
 \r
-        ModuleType = Module.ModuleType\r
-\r
-        # for overridding library instances with module specific setting\r
-        PlatformModule = self.Platform.Modules[str(Module)]\r
-\r
-        # add forced library instances (specified under LibraryClasses sections)\r
-        #\r
-        # If a module has a MODULE_TYPE of USER_DEFINED,\r
-        # do not link in NULL library class instances from the global [LibraryClasses.*] sections.\r
-        #\r
-        if Module.ModuleType != SUP_MODULE_USER_DEFINED:\r
-            for LibraryClass in self.Platform.LibraryClasses.GetKeys():\r
-                if LibraryClass.startswith("NULL") and self.Platform.LibraryClasses[LibraryClass, Module.ModuleType]:\r
-                    Module.LibraryClasses[LibraryClass] = self.Platform.LibraryClasses[LibraryClass, Module.ModuleType]\r
-\r
-        # add forced library instances (specified in module overrides)\r
-        for LibraryClass in PlatformModule.LibraryClasses:\r
-            if LibraryClass.startswith("NULL"):\r
-                Module.LibraryClasses[LibraryClass] = PlatformModule.LibraryClasses[LibraryClass]\r
-\r
-        # EdkII module\r
-        LibraryConsumerList = [Module]\r
-        Constructor         = []\r
-        ConsumedByList      = OrderedDict()\r
-        LibraryInstance     = OrderedDict()\r
-\r
-        EdkLogger.verbose("")\r
-        EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), self.Arch))\r
-        while len(LibraryConsumerList) > 0:\r
-            M = LibraryConsumerList.pop()\r
-            for LibraryClassName in M.LibraryClasses:\r
-                if LibraryClassName not in LibraryInstance:\r
-                    # override library instance for this module\r
-                    if LibraryClassName in PlatformModule.LibraryClasses:\r
-                        LibraryPath = PlatformModule.LibraryClasses[LibraryClassName]\r
-                    else:\r
-                        LibraryPath = self.Platform.LibraryClasses[LibraryClassName, ModuleType]\r
-                    if LibraryPath is None or LibraryPath == "":\r
-                        LibraryPath = M.LibraryClasses[LibraryClassName]\r
-                        if LibraryPath is None or LibraryPath == "":\r
-                            EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,\r
-                                            "Instance of library class [%s] is not found" % LibraryClassName,\r
-                                            File=self.MetaFile,\r
-                                            ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), self.Arch, str(Module)))\r
-\r
-                    LibraryModule = self.BuildDatabase[LibraryPath, self.Arch, self.BuildTarget, self.ToolChain]\r
-                    # for those forced library instance (NULL library), add a fake library class\r
-                    if LibraryClassName.startswith("NULL"):\r
-                        LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))\r
-                    elif LibraryModule.LibraryClass is None \\r
-                         or len(LibraryModule.LibraryClass) == 0 \\r
-                         or (ModuleType != SUP_MODULE_USER_DEFINED\r
-                             and ModuleType not in LibraryModule.LibraryClass[0].SupModList):\r
-                        # only USER_DEFINED can link against any library instance despite of its SupModList\r
-                        EdkLogger.error("build", OPTION_MISSING,\r
-                                        "Module type [%s] is not supported by library instance [%s]" \\r
-                                        % (ModuleType, LibraryPath), File=self.MetaFile,\r
-                                        ExtraData="consumed by [%s]" % str(Module))\r
-\r
-                    LibraryInstance[LibraryClassName] = LibraryModule\r
-                    LibraryConsumerList.append(LibraryModule)\r
-                    EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))\r
-                else:\r
-                    LibraryModule = LibraryInstance[LibraryClassName]\r
-\r
-                if LibraryModule is None:\r
-                    continue\r
-\r
-                if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:\r
-                    Constructor.append(LibraryModule)\r
-\r
-                if LibraryModule not in ConsumedByList:\r
-                    ConsumedByList[LibraryModule] = []\r
-                # don't add current module itself to consumer list\r
-                if M != Module:\r
-                    if M in ConsumedByList[LibraryModule]:\r
-                        continue\r
-                    ConsumedByList[LibraryModule].append(M)\r
-        #\r
-        # Initialize the sorted output list to the empty set\r
-        #\r
-        SortedLibraryList = []\r
-        #\r
-        # Q <- Set of all nodes with no incoming edges\r
-        #\r
-        LibraryList = [] #LibraryInstance.values()\r
-        Q = []\r
-        for LibraryClassName in LibraryInstance:\r
-            M = LibraryInstance[LibraryClassName]\r
-            LibraryList.append(M)\r
-            if ConsumedByList[M] == []:\r
-                Q.append(M)\r
-\r
-        #\r
-        # start the  DAG algorithm\r
-        #\r
-        while True:\r
-            EdgeRemoved = True\r
-            while Q == [] and EdgeRemoved:\r
-                EdgeRemoved = False\r
-                # for each node Item with a Constructor\r
-                for Item in LibraryList:\r
-                    if Item not in Constructor:\r
-                        continue\r
-                    # for each Node without a constructor with an edge e from Item to Node\r
-                    for Node in ConsumedByList[Item]:\r
-                        if Node in Constructor:\r
-                            continue\r
-                        # remove edge e from the graph if Node has no constructor\r
-                        ConsumedByList[Item].remove(Node)\r
-                        EdgeRemoved = True\r
-                        if ConsumedByList[Item] == []:\r
-                            # insert Item into Q\r
-                            Q.insert(0, Item)\r
-                            break\r
-                    if Q != []:\r
-                        break\r
-            # DAG is done if there's no more incoming edge for all nodes\r
-            if Q == []:\r
-                break\r
-\r
-            # remove node from Q\r
-            Node = Q.pop()\r
-            # output Node\r
-            SortedLibraryList.append(Node)\r
-\r
-            # for each node Item with an edge e from Node to Item do\r
-            for Item in LibraryList:\r
-                if Node not in ConsumedByList[Item]:\r
-                    continue\r
-                # remove edge e from the graph\r
-                ConsumedByList[Item].remove(Node)\r
-\r
-                if ConsumedByList[Item] != []:\r
-                    continue\r
-                # insert Item into Q, if Item has no other incoming edges\r
-                Q.insert(0, Item)\r
-\r
-        #\r
-        # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle\r
-        #\r
-        for Item in LibraryList:\r
-            if ConsumedByList[Item] != [] and Item in Constructor and len(Constructor) > 1:\r
-                ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join(str(L) for L in ConsumedByList[Item])\r
-                EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),\r
-                                ExtraData=ErrorMessage, File=self.MetaFile)\r
-            if Item not in SortedLibraryList:\r
-                SortedLibraryList.append(Item)\r
-\r
-        #\r
-        # Build the list of constructor and destructir names\r
-        # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order\r
-        #\r
-        SortedLibraryList.reverse()\r
-        return SortedLibraryList\r
-\r
+        return GetModuleLibInstances(Module,\r
+                                     self.Platform,\r
+                                     self.BuildDatabase,\r
+                                     self.Arch,\r
+                                     self.BuildTarget,\r
+                                     self.ToolChain,\r
+                                     self.MetaFile,\r
+                                     EdkLogger)\r
 \r
     ## Override PCD setting (type, value, ...)\r
     #\r
index 573100081815af4bdd966762ba665a47400c370b..a28fbdf030210d2fc23ff536ad471cd2a67dfc16 100644 (file)
@@ -83,16 +83,13 @@ def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain,additionalPk
 #\r
 def GetLiabraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):\r
     if Module.AutoGenVersion >= 0x00010005:\r
-        return _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)\r
+        return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)\r
     else:\r
         return _ResolveLibraryReference(Module, Platform)\r
 \r
-def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):\r
+def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):\r
     ModuleType = Module.ModuleType\r
 \r
-    # for overriding library instances with module specific setting\r
-    PlatformModule = Platform.Modules[str(Module)]\r
-\r
     # add forced library instances (specified under LibraryClasses sections)\r
     #\r
     # If a module has a MODULE_TYPE of USER_DEFINED,\r
@@ -104,9 +101,9 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
                 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]\r
 \r
     # add forced library instances (specified in module overrides)\r
-    for LibraryClass in PlatformModule.LibraryClasses:\r
+    for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:\r
         if LibraryClass.startswith("NULL"):\r
-            Module.LibraryClasses[LibraryClass] = PlatformModule.LibraryClasses[LibraryClass]\r
+            Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]\r
 \r
     # EdkII module\r
     LibraryConsumerList = [Module]\r
@@ -114,19 +111,29 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
     ConsumedByList = OrderedListDict()\r
     LibraryInstance = OrderedDict()\r
 \r
+    if FileName:\r
+        EdkLogger.verbose("")\r
+        EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))\r
+\r
     while len(LibraryConsumerList) > 0:\r
         M = LibraryConsumerList.pop()\r
         for LibraryClassName in M.LibraryClasses:\r
             if LibraryClassName not in LibraryInstance:\r
                 # override library instance for this module\r
-                if LibraryClassName in PlatformModule.LibraryClasses:\r
-                    LibraryPath = PlatformModule.LibraryClasses[LibraryClassName]\r
+                if LibraryClassName in Platform.Modules[str(Module)].LibraryClasses:\r
+                    LibraryPath = Platform.Modules[str(Module)].LibraryClasses[LibraryClassName]\r
                 else:\r
                     LibraryPath = Platform.LibraryClasses[LibraryClassName, ModuleType]\r
                 if LibraryPath is None or LibraryPath == "":\r
                     LibraryPath = M.LibraryClasses[LibraryClassName]\r
                     if LibraryPath is None or LibraryPath == "":\r
-                        return []\r
+                        if FileName:\r
+                            EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,\r
+                                            "Instance of library class [%s] is not found" % LibraryClassName,\r
+                                            File=FileName,\r
+                                            ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), Arch, str(Module)))\r
+                        else:\r
+                            return []\r
 \r
                 LibraryModule = BuildDatabase[LibraryPath, Arch, Target, Toolchain]\r
                 # for those forced library instance (NULL library), add a fake library class\r
@@ -137,10 +144,18 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
                      or (ModuleType != SUP_MODULE_USER_DEFINED\r
                          and ModuleType not in LibraryModule.LibraryClass[0].SupModList):\r
                     # only USER_DEFINED can link against any library instance despite of its SupModList\r
-                    return []\r
+                    if FileName:\r
+                        EdkLogger.error("build", OPTION_MISSING,\r
+                                        "Module type [%s] is not supported by library instance [%s]" \\r
+                                        % (ModuleType, LibraryPath), File=FileName,\r
+                                        ExtraData="consumed by [%s]" % str(Module))\r
+                    else:\r
+                        return []\r
 \r
                 LibraryInstance[LibraryClassName] = LibraryModule\r
                 LibraryConsumerList.append(LibraryModule)\r
+                if FileName:\r
+                    EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))\r
             else:\r
                 LibraryModule = LibraryInstance[LibraryClassName]\r
 \r
@@ -167,7 +182,7 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
     for LibraryClassName in LibraryInstance:\r
         M = LibraryInstance[LibraryClassName]\r
         LibraryList.append(M)\r
-        if len(ConsumedByList[M]) == 0:\r
+        if not ConsumedByList[M]:\r
             Q.append(M)\r
 \r
     #\r
@@ -188,7 +203,7 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
                     # remove edge e from the graph if Node has no constructor\r
                     ConsumedByList[Item].remove(Node)\r
                     EdgeRemoved = True\r
-                    if len(ConsumedByList[Item]) == 0:\r
+                    if not ConsumedByList[Item]:\r
                         # insert Item into Q\r
                         Q.insert(0, Item)\r
                         break\r
@@ -210,7 +225,7 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
             # remove edge e from the graph\r
             ConsumedByList[Item].remove(Node)\r
 \r
-            if len(ConsumedByList[Item]) != 0:\r
+            if ConsumedByList[Item]:\r
                 continue\r
             # insert Item into Q, if Item has no other incoming edges\r
             Q.insert(0, Item)\r
@@ -219,8 +234,13 @@ def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, To
     # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle\r
     #\r
     for Item in LibraryList:\r
-        if len(ConsumedByList[Item]) != 0 and Item in Constructor and len(Constructor) > 1:\r
-            return []\r
+        if ConsumedByList[Item] and Item in Constructor and len(Constructor) > 1:\r
+            if FileName:\r
+                ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join(str(L) for L in ConsumedByList[Item])\r
+                EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),\r
+                                ExtraData=ErrorMessage, File=FileName)\r
+            else:\r
+                return []\r
         if Item not in SortedLibraryList:\r
             SortedLibraryList.append(Item)\r
 \r