]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools: Autogen - modify to use standard parent/child class relationships
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / AutoGen.py
index e2589cfbaeb32b9dbbb52fb7362c51c7e7af8d1c..aeeab9190473c7af9b85819d34c8f164e93644b8 100644 (file)
@@ -159,8 +159,8 @@ ${tail_comments}
 #   This class just implements the cache mechanism of AutoGen objects.\r
 #\r
 class AutoGen(object):\r
-    # database to maintain the objects of xxxAutoGen\r
-    _CACHE_ = {}    # (BuildTarget, ToolChain) : {ARCH : {platform file: AutoGen object}}}\r
+    # database to maintain the objects in each child class\r
+    __ObjectCache = {}    # (BuildTarget, ToolChain, ARCH, platform file): AutoGen object\r
 \r
     ## Factory method\r
     #\r
@@ -174,24 +174,19 @@ class AutoGen(object):
     #   @param  *args           The specific class related parameters\r
     #   @param  **kwargs        The specific class related dict parameters\r
     #\r
-    def __new__(Class, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+    def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
         # check if the object has been created\r
-        Key = (Target, Toolchain)\r
-        if Key not in Class._CACHE_ or Arch not in Class._CACHE_[Key] \\r
-           or MetaFile not in Class._CACHE_[Key][Arch]:\r
-            AutoGenObject = super(AutoGen, Class).__new__(Class)\r
-            # call real constructor\r
-            if not AutoGenObject._Init(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
-                return None\r
-            if Key not in Class._CACHE_:\r
-                Class._CACHE_[Key] = {}\r
-            if Arch not in Class._CACHE_[Key]:\r
-                Class._CACHE_[Key][Arch] = {}\r
-            Class._CACHE_[Key][Arch][MetaFile] = AutoGenObject\r
-        else:\r
-            AutoGenObject = Class._CACHE_[Key][Arch][MetaFile]\r
+        Key = (Target, Toolchain, Arch, MetaFile)\r
+        try:\r
+            # if it exists, just return it directly\r
+            return cls.__ObjectCache[Key]\r
+        except:\r
+            # it didnt exist. create it, cache it, then return it\r
+            cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls)\r
+            return cls.__ObjectCache[Key]\r
 \r
-        return AutoGenObject\r
+    def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
 \r
     ## hash() operator\r
     #\r
@@ -221,10 +216,16 @@ class AutoGen(object):
 # architecture. This class will generate top level makefile.\r
 #\r
 class WorkspaceAutoGen(AutoGen):\r
-    ## Real constructor of WorkspaceAutoGen\r
-    #\r
-    # This method behaves the same as __init__ except that it needs explicit invoke\r
-    # (in super class's __new__ method)\r
+    # call super().__init__ then call the worker function with different parameter count\r
+    def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        try:\r
+            self._Init\r
+        except:\r
+            super(WorkspaceAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._Init = True\r
+    \r
+    ## Initialize WorkspaceAutoGen\r
     #\r
     #   @param  WorkspaceDir            Root directory of workspace\r
     #   @param  ActivePlatform          Meta-file of active platform\r
@@ -240,7 +241,7 @@ class WorkspaceAutoGen(AutoGen):
     #   @param  Caps                    Capsule list to be generated\r
     #   @param  SkuId                   SKU id from command line\r
     #\r
-    def _Init(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchList, MetaFileDb,\r
+    def _InitWorker(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchList, MetaFileDb,\r
               BuildConfig, ToolDefinition, FlashDefinitionFile='', Fds=None, Fvs=None, Caps=None, SkuId='', UniFlag=None,\r
               Progress=None, BuildModule=None):\r
         if Fds is None:\r
@@ -1111,6 +1112,14 @@ class WorkspaceAutoGen(AutoGen):
 #  file in order to generate makefile for platform.\r
 #\r
 class PlatformAutoGen(AutoGen):\r
+    # call super().__init__ then call the worker function with different parameter count\r
+    def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        try:\r
+            self._Init\r
+        except:\r
+            super(PlatformAutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch)\r
+            self._Init = True\r
     #\r
     # Used to store all PCDs for both PEI and DXE phase, in order to generate \r
     # correct PCD database\r
@@ -1139,11 +1148,8 @@ class PlatformAutoGen(AutoGen):
                 "0x10001"  : 2,      #  TARGET_*********_****_***********_ATTRIBUTE\r
                 "0x00001"  : 1}      #  ******_*********_****_***********_ATTRIBUTE (Lowest)\r
 \r
-    ## The real constructor of PlatformAutoGen\r
+    ## Initialize PlatformAutoGen\r
     #\r
-    #  This method is not supposed to be called by users of PlatformAutoGen. It's\r
-    #  only used by factory method __new__() to do real initialization work for an\r
-    #  object of PlatformAutoGen\r
     #\r
     #   @param      Workspace       WorkspaceAutoGen object\r
     #   @param      PlatformFile    Platform file (DSC file)\r
@@ -1151,7 +1157,7 @@ class PlatformAutoGen(AutoGen):
     #   @param      Toolchain       Name of tool chain\r
     #   @param      Arch            arch of the platform supports\r
     #\r
-    def _Init(self, Workspace, PlatformFile, Target, Toolchain, Arch):\r
+    def _InitWorker(self, Workspace, PlatformFile, Target, Toolchain, Arch):\r
         EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen platform [%s] [%s]" % (PlatformFile, Arch))\r
         GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (PlatformFile, Arch, Toolchain, Target)\r
 \r
@@ -1368,27 +1374,6 @@ class PlatformAutoGen(AutoGen):
     #  This interface should be invoked explicitly when platform action is created.\r
     #\r
     def CollectPlatformDynamicPcds(self):\r
-        # Override the platform Pcd's value by build option\r
-        if GlobalData.BuildOptionPcd:\r
-            for PcdItem in GlobalData.BuildOptionPcd:\r
-                PlatformPcd = self.Platform.Pcds.get((PcdItem[1],PcdItem[0]))\r
-                if PlatformPcd:\r
-                    if PlatformPcd.DatumType in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64,'BOOLEAN']:\r
-                        for sku in PlatformPcd.SkuInfoList:\r
-                            PlatformPcd.SkuInfoList[sku].DefaultValue = PcdItem[2]\r
-                    else:\r
-                        PcdDefaultValue = StringToArray(PcdItem[2])\r
-                        for sku in PlatformPcd.SkuInfoList:\r
-                            skuinfo = PlatformPcd.SkuInfoList[sku]\r
-                            if skuinfo.VariableGuid:\r
-                                skuinfo.HiiDefaultValue = PcdDefaultValue\r
-                            else:\r
-                                skuinfo.DefaultValue = PcdDefaultValue\r
-                        PlatformPcd.DefaultValue = PcdDefaultValue\r
-                        if PlatformPcd.MaxDatumSize:\r
-                            PlatformPcd.MaxDatumSize = str(max([int(PlatformPcd.MaxDatumSize),len(PcdDefaultValue.split(","))]))\r
-                        else:\r
-                            PlatformPcd.MaxDatumSize = str(len(PcdDefaultValue.split(",")))\r
 \r
         for key in self.Platform.Pcds:\r
             for SinglePcd in GlobalData.MixedPcd:\r
@@ -2373,11 +2358,6 @@ class PlatformAutoGen(AutoGen):
                 TokenCName = PcdItem[0]\r
                 break\r
         if FromPcd != None:\r
-            if GlobalData.BuildOptionPcd:\r
-                for pcd in GlobalData.BuildOptionPcd:\r
-                    if (FromPcd.TokenSpaceGuidCName, FromPcd.TokenCName) == (pcd[0], pcd[1]):\r
-                        FromPcd.DefaultValue = pcd[2]\r
-                        break\r
             if ToPcd.Pending and FromPcd.Type not in [None, '']:\r
                 ToPcd.Type = FromPcd.Type\r
             elif (ToPcd.Type not in [None, '']) and (FromPcd.Type not in [None, ''])\\r
@@ -2802,15 +2782,29 @@ class PlatformAutoGen(AutoGen):
 # to the [depex] section in module's inf file.\r
 #\r
 class ModuleAutoGen(AutoGen):\r
+    # call super().__init__ then call the worker function with different parameter count\r
+    def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        try:\r
+            self._Init\r
+        except:\r
+            super(ModuleAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args)\r
+            self._Init = True\r
+\r
     ## Cache the timestamps of metafiles of every module in a class variable\r
     #\r
     TimeDict = {}\r
 \r
-    ## The real constructor of ModuleAutoGen\r
-    #\r
-    #  This method is not supposed to be called by users of ModuleAutoGen. It's\r
-    #  only used by factory method __new__() to do real initialization work for an\r
-    #  object of ModuleAutoGen\r
+    def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        obj = super(ModuleAutoGen, cls).__new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+        # check if this module is employed by active platform\r
+        if not PlatformAutoGen(Workspace, args[0], Target, Toolchain, Arch).ValidModule(MetaFile):\r
+            EdkLogger.verbose("Module [%s] for [%s] is not employed by active platform\n" \\r
+                              % (MetaFile, Arch))\r
+            return None\r
+        return obj\r
+            \r
+    ## Initialize ModuleAutoGen\r
     #\r
     #   @param      Workspace           EdkIIWorkspaceBuild object\r
     #   @param      ModuleFile          The path of module file\r
@@ -2819,7 +2813,7 @@ class ModuleAutoGen(AutoGen):
     #   @param      Arch                The arch the module supports\r
     #   @param      PlatformFile        Platform meta-file\r
     #\r
-    def _Init(self, Workspace, ModuleFile, Target, Toolchain, Arch, PlatformFile):\r
+    def _InitWorker(self, Workspace, ModuleFile, Target, Toolchain, Arch, PlatformFile):\r
         EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen module [%s] [%s]" % (ModuleFile, Arch))\r
         GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (ModuleFile, Arch, Toolchain, Target)\r
 \r
@@ -2828,11 +2822,6 @@ class ModuleAutoGen(AutoGen):
 \r
         self.MetaFile = ModuleFile\r
         self.PlatformInfo = PlatformAutoGen(Workspace, PlatformFile, Target, Toolchain, Arch)\r
-        # check if this module is employed by active platform\r
-        if not self.PlatformInfo.ValidModule(self.MetaFile):\r
-            EdkLogger.verbose("Module [%s] for [%s] is not employed by active platform\n" \\r
-                              % (self.MetaFile, Arch))\r
-            return False\r
 \r
         self.SourceDir = self.MetaFile.SubDir\r
         self.SourceDir = mws.relpath(self.SourceDir, self.WorkspaceDir)\r
@@ -4090,7 +4079,7 @@ class ModuleAutoGen(AutoGen):
         OutputDir = self.OutputDir.replace('\\', '/').strip('/')\r
         DebugDir = self.DebugDir.replace('\\', '/').strip('/')\r
         for Item in self.CodaTargetList:\r
-            File = Item.Target.Path.replace('\\', '/').strip('/').replace(DebugDir, '').strip('/')\r
+            File = Item.Target.Path.replace('\\', '/').strip('/').replace(DebugDir, '').replace(OutputDir, '').strip('/')\r
             if File not in self.OutputFile:\r
                 self.OutputFile.append(File)\r
             if os.path.isabs(File):\r