]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: AutoGen and GenFds share the parser data.
authorZhao, ZhiqiangX <zhiqiangx.zhao@intel.com>
Fri, 23 Nov 2018 07:04:45 +0000 (15:04 +0800)
committerBobCF <bob.c.feng@intel.com>
Fri, 7 Dec 2018 02:12:05 +0000 (10:12 +0800)
V2:
Extract the common part of new API and the original main() function
into one function.

V1:
https://bugzilla.tianocore.org/show_bug.cgi?id=1288

Currently, AutoGen and GenFds run in different python interpreters. The
parser are duplicated. This patch is going to create new API for GenFds
and have the build to call that API instead of executing GenFds.py. As
such, the GenFds and build can share the parser data.

This patch is expected to save the time of GenFds about 2~3 seconds.
More details will be logged in BZ.

This is the summary measure data generated from python cProfile for
building Ovmf.

Currently:
8379147 function calls (8135450 primitive calls) in 12.580 seconds

After applying this patch:
3428712 function calls (3418881 primitive calls) in 8.944 seconds

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: ZhiqiangX Zhao <zhiqiangx.zhao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Carsey Jaben <jaben.carsey@intel.com>
Cc: Bob Feng <bob.c.feng@intel.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools/Source/Python/GenFds/GenFds.py
BaseTools/Source/Python/build/build.py

index 25417c4470619b37da0277d4a681b87aa341ec1e..12e53010a559920fec0e6bd4c2a5a2bf1a3a4f3f 100644 (file)
@@ -935,6 +935,10 @@ class WorkspaceAutoGen(AutoGen):
     def GenFdsCommand(self):\r
         return (GenMake.TopLevelMakefile(self)._TEMPLATE_.Replace(GenMake.TopLevelMakefile(self)._TemplateDict)).strip()\r
 \r
+    @property\r
+    def GenFdsCommandDict(self):\r
+        return GenMake.TopLevelMakefile(self)._TemplateDict\r
+\r
     ## Create makefile for the platform and modules in it\r
     #\r
     #   @param      CreateDepsMakeFile      Flag indicating if the makefile for\r
index 0513f488fca3d398a154a576437f19fc92ad093e..da484d0bb1f5dfca9baa47e3b80c737035e676a5 100644 (file)
@@ -35,7 +35,7 @@ from Common.Misc import DirCache, PathClass, GuidStructureStringToGuidString
 from Common.Misc import SaveFileOnChange, ClearDuplicatedInf\r
 from Common.BuildVersion import gBUILD_VERSION\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
-from Common.BuildToolError import FatalError, GENFDS_ERROR, CODE_ERROR, FORMAT_INVALID, RESOURCE_NOT_AVAILABLE, FILE_NOT_FOUND, OPTION_MISSING, FORMAT_NOT_SUPPORTED,OPTION_VALUE_INVALID\r
+from Common.BuildToolError import FatalError, GENFDS_ERROR, CODE_ERROR, FORMAT_INVALID, RESOURCE_NOT_AVAILABLE, FILE_NOT_FOUND, OPTION_MISSING, FORMAT_NOT_SUPPORTED, OPTION_VALUE_INVALID, PARAMETER_INVALID\r
 from Workspace.WorkspaceDatabase import WorkspaceDatabase\r
 \r
 from .FdfParser import FdfParser, Warning\r
@@ -59,43 +59,45 @@ __copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation  All rights reserv
 def main():\r
     global Options\r
     Options = myOptionParser()\r
+    EdkLogger.Initialize()\r
+    return GenFdsApi(OptionsToCommandDict(Options))\r
 \r
+def GenFdsApi(FdsCommandDict, WorkSpaceDataBase=None):\r
     global Workspace\r
     Workspace = ""\r
     ArchList = None\r
     ReturnCode = 0\r
 \r
-    EdkLogger.Initialize()\r
     try:\r
-        if Options.verbose:\r
+        if FdsCommandDict.get("verbose"):\r
             EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
             GenFdsGlobalVariable.VerboseMode = True\r
 \r
-        if Options.FixedAddress:\r
+        if FdsCommandDict.get("FixedAddress"):\r
             GenFdsGlobalVariable.FixedLoadAddress = True\r
 \r
-        if Options.quiet:\r
+        if FdsCommandDict.get("quiet"):\r
             EdkLogger.SetLevel(EdkLogger.QUIET)\r
-        if Options.debug:\r
-            EdkLogger.SetLevel(Options.debug + 1)\r
-            GenFdsGlobalVariable.DebugLevel = Options.debug\r
+        if FdsCommandDict.get("debug"):\r
+            EdkLogger.SetLevel(FdsCommandDict.get("debug") + 1)\r
+            GenFdsGlobalVariable.DebugLevel = FdsCommandDict.get("debug")\r
         else:\r
             EdkLogger.SetLevel(EdkLogger.INFO)\r
 \r
-        if not Options.Workspace:\r
+        if not FdsCommandDict.get("Workspace",os.environ.get('WORKSPACE')):\r
             EdkLogger.error("GenFds", OPTION_MISSING, "WORKSPACE not defined",\r
                             ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
-        elif not os.path.exists(Options.Workspace):\r
+        elif not os.path.exists(FdsCommandDict.get("Workspace",os.environ.get('WORKSPACE'))):\r
             EdkLogger.error("GenFds", PARAMETER_INVALID, "WORKSPACE is invalid",\r
                             ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
         else:\r
-            Workspace = os.path.normcase(Options.Workspace)\r
+            Workspace = os.path.normcase(FdsCommandDict.get("Workspace",os.environ.get('WORKSPACE')))\r
             GenFdsGlobalVariable.WorkSpaceDir = Workspace\r
             if 'EDK_SOURCE' in os.environ:\r
                 GenFdsGlobalVariable.EdkSourceDir = os.path.normcase(os.environ['EDK_SOURCE'])\r
-            if Options.debug:\r
+            if FdsCommandDict.get("debug"):\r
                 GenFdsGlobalVariable.VerboseLogger("Using Workspace:" + Workspace)\r
-            if Options.GenfdsMultiThread:\r
+            if FdsCommandDict.get("GenfdsMultiThread"):\r
                 GenFdsGlobalVariable.EnableGenfdsMultiThread = True\r
         os.chdir(GenFdsGlobalVariable.WorkSpaceDir)\r
 \r
@@ -103,8 +105,8 @@ def main():
         PackagesPath = os.getenv("PACKAGES_PATH")\r
         mws.setWs(GenFdsGlobalVariable.WorkSpaceDir, PackagesPath)\r
 \r
-        if Options.filename:\r
-            FdfFilename = Options.filename\r
+        if FdsCommandDict.get("fdf_file"):\r
+            FdfFilename = FdsCommandDict.get("fdf_file")[0].Path\r
             FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)\r
 \r
             if FdfFilename[0:2] == '..':\r
@@ -119,14 +121,14 @@ def main():
         else:\r
             EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")\r
 \r
-        if Options.BuildTarget:\r
-            GenFdsGlobalVariable.TargetName = Options.BuildTarget\r
+        if FdsCommandDict.get("build_target"):\r
+            GenFdsGlobalVariable.TargetName = FdsCommandDict.get("build_target")\r
 \r
-        if Options.ToolChain:\r
-            GenFdsGlobalVariable.ToolChainTag = Options.ToolChain\r
+        if FdsCommandDict.get("toolchain_tag"):\r
+            GenFdsGlobalVariable.ToolChainTag = FdsCommandDict.get("toolchain_tag")\r
 \r
-        if Options.activePlatform:\r
-            ActivePlatform = Options.activePlatform\r
+        if FdsCommandDict.get("active_platform"):\r
+            ActivePlatform = FdsCommandDict.get("active_platform")\r
             ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)\r
 \r
             if ActivePlatform[0:2] == '..':\r
@@ -140,12 +142,12 @@ def main():
         else:\r
             EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")\r
 \r
-        GlobalData.BuildOptionPcd = Options.OptionPcd if Options.OptionPcd else {}\r
+        GlobalData.BuildOptionPcd = FdsCommandDict.get("OptionPcd") if FdsCommandDict.get("OptionPcd") else {}\r
         GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform))\r
 \r
-        if Options.ConfDirectory:\r
+        if FdsCommandDict.get("conf_directory"):\r
             # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
-            ConfDirectoryPath = os.path.normpath(Options.ConfDirectory)\r
+            ConfDirectoryPath = os.path.normpath(FdsCommandDict.get("conf_directory"))\r
             if ConfDirectoryPath.startswith('"'):\r
                 ConfDirectoryPath = ConfDirectoryPath[1:]\r
             if ConfDirectoryPath.endswith('"'):\r
@@ -169,14 +171,14 @@ def main():
             TargetTxt.LoadTargetTxtFile(BuildConfigurationFile)\r
             # if no build target given in command line, get it from target.txt\r
             if not GenFdsGlobalVariable.TargetName:\r
-                BuildTargetList = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]\r
+                BuildTargetList = TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TARGET]\r
                 if len(BuildTargetList) != 1:\r
                     EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for Target.")\r
                 GenFdsGlobalVariable.TargetName = BuildTargetList[0]\r
 \r
             # if no tool chain given in command line, get it from target.txt\r
             if not GenFdsGlobalVariable.ToolChainTag:\r
-                ToolChainList = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]\r
+                ToolChainList = TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TOOL_CHAIN_TAG]\r
                 if ToolChainList is None or len(ToolChainList) == 0:\r
                     EdkLogger.error("GenFds", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.")\r
                 if len(ToolChainList) != 1:\r
@@ -186,10 +188,10 @@ def main():
             EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)\r
 \r
         #Set global flag for build mode\r
-        GlobalData.gIgnoreSource = Options.IgnoreSources\r
+        GlobalData.gIgnoreSource = FdsCommandDict.get("IgnoreSources")\r
 \r
-        if Options.Macros:\r
-            for Pair in Options.Macros:\r
+        if FdsCommandDict.get("macro"):\r
+            for Pair in FdsCommandDict.get("macro"):\r
                 if Pair.startswith('"'):\r
                     Pair = Pair[1:]\r
                 if Pair.endswith('"'):\r
@@ -224,8 +226,11 @@ def main():
 \r
         """call Workspace build create database"""\r
         GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath))\r
-        BuildWorkSpace = WorkspaceDatabase(GlobalData.gDatabasePath)\r
-        BuildWorkSpace.InitDatabase()\r
+        if WorkSpaceDataBase:\r
+            BuildWorkSpace = WorkSpaceDataBase\r
+        else:\r
+            BuildWorkSpace = WorkspaceDatabase(GlobalData.gDatabasePath)\r
+            BuildWorkSpace.InitDatabase()\r
 \r
         #\r
         # Get files real name in workspace dir\r
@@ -233,23 +238,23 @@ def main():
         GlobalData.gAllFiles = DirCache(Workspace)\r
         GlobalData.gWorkspace = Workspace\r
 \r
-        if Options.archList:\r
-            ArchList = Options.archList.split(',')\r
+        if FdsCommandDict.get("build_architecture_list"):\r
+            ArchList = FdsCommandDict.get("build_architecture_list").split(',')\r
         else:\r
-            ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, Options.BuildTarget, Options.ToolChain].SupArchList\r
+            ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].SupArchList\r
 \r
-        TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, Options.BuildTarget, Options.ToolChain].SupArchList) & set(ArchList)\r
+        TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].SupArchList) & set(ArchList)\r
         if len(TargetArchList) == 0:\r
             EdkLogger.error("GenFds", GENFDS_ERROR, "Target ARCH %s not in platform supported ARCH %s" % (str(ArchList), str(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON].SupArchList)))\r
 \r
         for Arch in ArchList:\r
-            GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].OutputDirectory)\r
+            GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].OutputDirectory)\r
 \r
         # assign platform name based on last entry in ArchList\r
-        GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, ArchList[-1], Options.BuildTarget, Options.ToolChain].PlatformName\r
+        GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, ArchList[-1], FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].PlatformName\r
 \r
-        if Options.outputDir:\r
-            OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(Options.outputDir)\r
+        if FdsCommandDict.get("platform_build_directory"):\r
+            OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdsCommandDict.get("platform_build_directory"))\r
             if not os.path.isabs (OutputDirFromCommandLine):\r
                 OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)\r
             for Arch in ArchList:\r
@@ -271,32 +276,35 @@ def main():
             GenFdsGlobalVariable.OutputDirDict[Key] = OutputDir\r
 \r
         """ Parse Fdf file, has to place after build Workspace as FDF may contain macros from DSC file """\r
-        FdfParserObj = FdfParser(FdfFilename)\r
-        FdfParserObj.ParseFile()\r
+        if WorkSpaceDataBase:\r
+            FdfParserObj = GlobalData.gFdfParser\r
+        else:\r
+            FdfParserObj = FdfParser(FdfFilename)\r
+            FdfParserObj.ParseFile()\r
 \r
         if FdfParserObj.CycleReferenceCheck():\r
             EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Cycle Reference Detected in FDF file")\r
 \r
-        if Options.uiFdName:\r
-            if Options.uiFdName.upper() in FdfParserObj.Profile.FdDict:\r
-                GenFds.OnlyGenerateThisFd = Options.uiFdName\r
+        if FdsCommandDict.get("fd"):\r
+            if FdsCommandDict.get("fd")[0].upper() in FdfParserObj.Profile.FdDict:\r
+                GenFds.OnlyGenerateThisFd = FdsCommandDict.get("fd")[0]\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
-                                "No such an FD in FDF file: %s" % Options.uiFdName)\r
+                                "No such an FD in FDF file: %s" % FdsCommandDict.get("fd")[0])\r
 \r
-        if Options.uiFvName:\r
-            if Options.uiFvName.upper() in FdfParserObj.Profile.FvDict:\r
-                GenFds.OnlyGenerateThisFv = Options.uiFvName\r
+        if FdsCommandDict.get("fv"):\r
+            if FdsCommandDict.get("fv")[0].upper() in FdfParserObj.Profile.FvDict:\r
+                GenFds.OnlyGenerateThisFv = FdsCommandDict.get("fv")[0]\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
-                                "No such an FV in FDF file: %s" % Options.uiFvName)\r
+                                "No such an FV in FDF file: %s" % FdsCommandDict.get("fv")[0])\r
 \r
-        if Options.uiCapName:\r
-            if Options.uiCapName.upper() in FdfParserObj.Profile.CapsuleDict:\r
-                GenFds.OnlyGenerateThisCap = Options.uiCapName\r
+        if FdsCommandDict.get("cap"):\r
+            if FdsCommandDict.get("cap")[0].upper() in FdfParserObj.Profile.CapsuleDict:\r
+                GenFds.OnlyGenerateThisCap = FdsCommandDict.get("cap")[0]\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
-                                "No such a Capsule in FDF file: %s" % Options.uiCapName)\r
+                                "No such a Capsule in FDF file: %s" % FdsCommandDict.get("cap")[0])\r
 \r
         GenFdsGlobalVariable.WorkSpace = BuildWorkSpace\r
         if ArchList:\r
@@ -337,7 +345,7 @@ def main():
         EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False)\r
         ReturnCode = FORMAT_INVALID\r
     except FatalError as X:\r
-        if Options.debug is not None:\r
+        if FdsCommandDict.get("debug") is not None:\r
             import traceback\r
             EdkLogger.quiet(traceback.format_exc())\r
         ReturnCode = X.args[0]\r
@@ -356,6 +364,30 @@ def main():
         ClearDuplicatedInf()\r
     return ReturnCode\r
 \r
+def OptionsToCommandDict(Options):\r
+    FdsCommandDict = {}\r
+    FdsCommandDict["verbose"] = Options.verbose\r
+    FdsCommandDict["FixedAddress"] = Options.FixedAddress\r
+    FdsCommandDict["quiet"] = Options.quiet\r
+    FdsCommandDict["debug"] = Options.debug\r
+    FdsCommandDict["Workspace"] = Options.Workspace\r
+    FdsCommandDict["GenfdsMultiThread"] = Options.GenfdsMultiThread\r
+    FdsCommandDict["fdf_file"] = [PathClass(Options.filename)] if Options.filename else []\r
+    FdsCommandDict["build_target"] = Options.BuildTarget\r
+    FdsCommandDict["toolchain_tag"] = Options.ToolChain\r
+    FdsCommandDict["active_platform"] = Options.activePlatform\r
+    FdsCommandDict["OptionPcd"] = Options.OptionPcd\r
+    FdsCommandDict["conf_directory"] = Options.ConfDirectory\r
+    FdsCommandDict["IgnoreSources"] = Options.IgnoreSources\r
+    FdsCommandDict["macro"] = Options.Macros\r
+    FdsCommandDict["build_architecture_list"] = Options.archList\r
+    FdsCommandDict["platform_build_directory"] = Options.outputDir\r
+    FdsCommandDict["fd"] = [Options.uiFdName] if Options.uiFdName else []\r
+    FdsCommandDict["fv"] = [Options.uiFvName] if Options.uiFvName else []\r
+    FdsCommandDict["cap"] = [Options.uiCapName] if Options.uiCapName else []\r
+    return FdsCommandDict\r
+\r
+\r
 gParamCheck = []\r
 def SingleCheckCallback(option, opt_str, value, parser):\r
     if option not in gParamCheck:\r
@@ -716,6 +748,7 @@ class GenFds(object):
             os.remove(GuidXRefFileName)\r
         GuidXRefFile.close()\r
 \r
+\r
 if __name__ == '__main__':\r
     r = main()\r
     ## 0-127 is a safe return range, and 1 is a standard default error\r
index 5eeb626cfbbb1825a40c4ee3f2a6e2ad1528396e..cf864d0ef51908c3e6fd58470a162be5151f6335 100644 (file)
@@ -51,7 +51,7 @@ from PatchPcdValue.PatchPcdValue import *
 \r
 import Common.EdkLogger\r
 import Common.GlobalData as GlobalData\r
-from GenFds.GenFds import GenFds\r
+from GenFds.GenFds import GenFds, GenFdsApi\r
 \r
 from collections import OrderedDict, defaultdict\r
 \r
@@ -1391,7 +1391,7 @@ class Build():
 \r
         # genfds\r
         if Target == 'fds':\r
-            LaunchCommand(AutoGenObject.GenFdsCommand, AutoGenObject.MakeFileDir)\r
+            GenFdsApi(AutoGenObject.GenFdsCommandDict, self.Db)\r
             return True\r
 \r
         # run\r
@@ -2135,7 +2135,7 @@ class Build():
                         # Generate FD image if there's a FDF file found\r
                         #\r
                         GenFdsStart = time.time()\r
-                        LaunchCommand(Wa.GenFdsCommand, os.getcwd())\r
+                        GenFdsApi(Wa.GenFdsCommandDict, self.Db)\r
 \r
                         #\r
                         # Create MAP file for all platform FVs after GenFds.\r