]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Autogen - modify to use standard parent/child class relationships
authorCarsey, Jaben </o=Intel/ou=Americas01/cn=Workers/cn=Carsey, Jaben>
Thu, 15 Mar 2018 00:20:27 +0000 (08:20 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Tue, 27 Mar 2018 01:02:30 +0000 (09:02 +0800)
use __new__ and __init__ to create/manage/initialize objects in standard flow.

Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@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

index 95e3e912b168a48362955e93c7c10ad73e540969..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
 #   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
 \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
     #   @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
         # 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
 \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
 \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
 # 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
     #\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
     #   @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
               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
 #  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
     #\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
                 "0x10001"  : 2,      #  TARGET_*********_****_***********_ATTRIBUTE\r
                 "0x00001"  : 1}      #  ******_*********_****_***********_ATTRIBUTE (Lowest)\r
 \r
-    ## The real constructor of PlatformAutoGen\r
+    ## Initialize PlatformAutoGen\r
     #\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
     #\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
     #   @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
         EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen platform [%s] [%s]" % (PlatformFile, Arch))\r
         GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (PlatformFile, Arch, Toolchain, Target)\r
 \r
@@ -2776,15 +2782,29 @@ class PlatformAutoGen(AutoGen):
 # to the [depex] section in module's inf file.\r
 #\r
 class ModuleAutoGen(AutoGen):\r
 # 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
     ## 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
     #\r
     #   @param      Workspace           EdkIIWorkspaceBuild object\r
     #   @param      ModuleFile          The path of module file\r
@@ -2793,7 +2813,7 @@ class ModuleAutoGen(AutoGen):
     #   @param      Arch                The arch the module supports\r
     #   @param      PlatformFile        Platform meta-file\r
     #\r
     #   @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
         EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen module [%s] [%s]" % (ModuleFile, Arch))\r
         GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (ModuleFile, Arch, Toolchain, Target)\r
 \r
@@ -2802,11 +2822,6 @@ class ModuleAutoGen(AutoGen):
 \r
         self.MetaFile = ModuleFile\r
         self.PlatformInfo = PlatformAutoGen(Workspace, PlatformFile, Target, Toolchain, Arch)\r
 \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
 \r
         self.SourceDir = self.MetaFile.SubDir\r
         self.SourceDir = mws.relpath(self.SourceDir, self.WorkspaceDir)\r