]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/GenFds.py
BaseTools: AutoGen and GenFds share the parser data.
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GenFds.py
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