From nobody Mon Dec 23 06:54:32 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1521073239511150.51345792026575; Wed, 14 Mar 2018 17:20:39 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id B22762253FB67; Wed, 14 Mar 2018 17:14:10 -0700 (PDT) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E8AF720954BB9 for ; Wed, 14 Mar 2018 17:14:08 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Mar 2018 17:20:31 -0700 Received: from jcarsey-desk1.amr.corp.intel.com ([10.7.159.144]) by orsmga002.jf.intel.com with ESMTP; 14 Mar 2018 17:20:31 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.65; helo=mga03.intel.com; envelope-from=jaben.carsey@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,307,1517904000"; d="scan'208";a="42123973" From: Jaben Carsey To: edk2-devel@lists.01.org Date: Wed, 14 Mar 2018 17:20:27 -0700 Message-Id: <47f1c10f996e33b63e4fb092fa98bbb7251183c0.1521073063.git.jaben.carsey@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [PATCH v1 1/2] BaseTools: Autogen - modify to use standard parent/child class relationships X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" use __new__ and __init__ to create/manage/initialize objects in standard fl= ow. Cc: Yonghong Zhu Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jaben Carsey --- BaseTools/Source/Python/AutoGen/AutoGen.py | 93 ++++++++++++-------- 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/= Python/AutoGen/AutoGen.py index 439e360955a3..5b09e0008ddd 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -159,8 +159,8 @@ ${tail_comments} # This class just implements the cache mechanism of AutoGen objects. # class AutoGen(object): - # database to maintain the objects of xxxAutoGen - _CACHE_ =3D {} # (BuildTarget, ToolChain) : {ARCH : {platform file:= AutoGen object}}} + # database to maintain the objects in each child class + __ObjectCache =3D {} # (BuildTarget, ToolChain, ARCH, platform file= ): AutoGen object =20 ## Factory method # @@ -174,24 +174,19 @@ class AutoGen(object): # @param *args The specific class related parameters # @param **kwargs The specific class related dict parameters # - def __new__(Class, Workspace, MetaFile, Target, Toolchain, Arch, *args= , **kwargs): + def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, = **kwargs): # check if the object has been created - Key =3D (Target, Toolchain) - if Key not in Class._CACHE_ or Arch not in Class._CACHE_[Key] \ - or MetaFile not in Class._CACHE_[Key][Arch]: - AutoGenObject =3D super(AutoGen, Class).__new__(Class) - # call real constructor - if not AutoGenObject._Init(Workspace, MetaFile, Target, Toolch= ain, Arch, *args, **kwargs): - return None - if Key not in Class._CACHE_: - Class._CACHE_[Key] =3D {} - if Arch not in Class._CACHE_[Key]: - Class._CACHE_[Key][Arch] =3D {} - Class._CACHE_[Key][Arch][MetaFile] =3D AutoGenObject - else: - AutoGenObject =3D Class._CACHE_[Key][Arch][MetaFile] + Key =3D (Target, Toolchain, Arch, MetaFile) + try: + # if it exists, just return it directly + return cls.__ObjectCache[Key] + except: + # it didnt exist. create it, cache it, then return it + cls.__ObjectCache[Key] =3D super(AutoGen, cls).__new__(cls) + return cls.__ObjectCache[Key] =20 - return AutoGenObject + def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *arg= s, **kwargs): + super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, T= oolchain, Arch, *args, **kwargs) =20 ## hash() operator # @@ -221,10 +216,16 @@ class AutoGen(object): # architecture. This class will generate top level makefile. # class WorkspaceAutoGen(AutoGen): - ## Real constructor of WorkspaceAutoGen - # - # This method behaves the same as __init__ except that it needs explic= it invoke - # (in super class's __new__ method) + # call super().__init__ then call the worker function with different p= arameter count + def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args= , **kwargs): + try: + self._Init + except: + super(WorkspaceAutoGen, self).__init__(Workspace, MetaFile, Ta= rget, Toolchain, Arch, *args, **kwargs) + self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch,= *args, **kwargs) + self._Init =3D True + =20 + ## Initialize WorkspaceAutoGen # # @param WorkspaceDir Root directory of workspace # @param ActivePlatform Meta-file of active platform @@ -240,7 +241,7 @@ class WorkspaceAutoGen(AutoGen): # @param Caps Capsule list to be generated # @param SkuId SKU id from command line # - def _Init(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchL= ist, MetaFileDb, + def _InitWorker(self, WorkspaceDir, ActivePlatform, Target, Toolchain,= ArchList, MetaFileDb, BuildConfig, ToolDefinition, FlashDefinitionFile=3D'', Fds= =3DNone, Fvs=3DNone, Caps=3DNone, SkuId=3D'', UniFlag=3DNone, Progress=3DNone, BuildModule=3DNone): if Fds is None: @@ -1111,6 +1112,14 @@ class WorkspaceAutoGen(AutoGen): # file in order to generate makefile for platform. # class PlatformAutoGen(AutoGen): + # call super().__init__ then call the worker function with different p= arameter count + def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args= , **kwargs): + try: + self._Init + except: + super(PlatformAutoGen, self).__init__(self, Workspace, MetaFil= e, Target, Toolchain, Arch, *args, **kwargs) + self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch) + self._Init =3D True # # Used to store all PCDs for both PEI and DXE phase, in order to gener= ate=20 # correct PCD database @@ -1139,11 +1148,8 @@ class PlatformAutoGen(AutoGen): "0x10001" : 2, # TARGET_*********_****_***********_= ATTRIBUTE "0x00001" : 1} # ******_*********_****_***********_= ATTRIBUTE (Lowest) =20 - ## The real constructor of PlatformAutoGen + ## Initialize PlatformAutoGen # - # This method is not supposed to be called by users of PlatformAutoGe= n. It's - # only used by factory method __new__() to do real initialization wor= k for an - # object of PlatformAutoGen # # @param Workspace WorkspaceAutoGen object # @param PlatformFile Platform file (DSC file) @@ -1151,7 +1157,7 @@ class PlatformAutoGen(AutoGen): # @param Toolchain Name of tool chain # @param Arch arch of the platform supports # - def _Init(self, Workspace, PlatformFile, Target, Toolchain, Arch): + def _InitWorker(self, Workspace, PlatformFile, Target, Toolchain, Arch= ): EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen platform [%s] [%s]" % = (PlatformFile, Arch)) GlobalData.gProcessingFile =3D "%s [%s, %s, %s]" % (PlatformFile, = Arch, Toolchain, Target) =20 @@ -2776,15 +2782,29 @@ class PlatformAutoGen(AutoGen): # to the [depex] section in module's inf file. # class ModuleAutoGen(AutoGen): + # call super().__init__ then call the worker function with different p= arameter count + def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args= , **kwargs): + try: + self._Init + except: + super(ModuleAutoGen, self).__init__(Workspace, MetaFile, Targe= t, Toolchain, Arch, *args, **kwargs) + self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch,= *args) + self._Init =3D True + ## Cache the timestamps of metafiles of every module in a class variab= le # TimeDict =3D {} =20 - ## The real constructor of ModuleAutoGen - # - # This method is not supposed to be called by users of ModuleAutoGen.= It's - # only used by factory method __new__() to do real initialization wor= k for an - # object of ModuleAutoGen + def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, = **kwargs): + obj =3D super(ModuleAutoGen, cls).__new__(cls, Workspace, MetaFile= , Target, Toolchain, Arch, *args, **kwargs) + # check if this module is employed by active platform + if not PlatformAutoGen(Workspace, args[0], Target, Toolchain, Arch= ).ValidModule(MetaFile): + EdkLogger.verbose("Module [%s] for [%s] is not employed by act= ive platform\n" \ + % (MetaFile, Arch)) + return None + return obj + =20 + ## Initialize ModuleAutoGen # # @param Workspace EdkIIWorkspaceBuild object # @param ModuleFile The path of module file @@ -2793,7 +2813,7 @@ class ModuleAutoGen(AutoGen): # @param Arch The arch the module supports # @param PlatformFile Platform meta-file # - def _Init(self, Workspace, ModuleFile, Target, Toolchain, Arch, Platfo= rmFile): + def _InitWorker(self, Workspace, ModuleFile, Target, Toolchain, Arch, = PlatformFile): EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen module [%s] [%s]" % (M= oduleFile, Arch)) GlobalData.gProcessingFile =3D "%s [%s, %s, %s]" % (ModuleFile, Ar= ch, Toolchain, Target) =20 @@ -2802,11 +2822,6 @@ class ModuleAutoGen(AutoGen): =20 self.MetaFile =3D ModuleFile self.PlatformInfo =3D PlatformAutoGen(Workspace, PlatformFile, Tar= get, Toolchain, Arch) - # check if this module is employed by active platform - if not self.PlatformInfo.ValidModule(self.MetaFile): - EdkLogger.verbose("Module [%s] for [%s] is not employed by act= ive platform\n" \ - % (self.MetaFile, Arch)) - return False =20 self.SourceDir =3D self.MetaFile.SubDir self.SourceDir =3D mws.relpath(self.SourceDir, self.WorkspaceDir) --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel