]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Move BuildOption parser out of build.py
authorFeng, Bob C <bob.c.feng@intel.com>
Mon, 22 Jul 2019 06:23:40 +0000 (14:23 +0800)
committerFeng, Bob C <bob.c.feng@intel.com>
Fri, 9 Aug 2019 15:15:54 +0000 (23:15 +0800)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1875

Build tool supports user to specify the conf folder.
To make the build options be evaluated at the beginning
of launching build, extract the buildoption function
from build.py to a new .py file.

Signed-off-by: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/Python/Common/TargetTxtClassObject.py
BaseTools/Source/Python/build/build.py
BaseTools/Source/Python/build/buildoptions.py [new file with mode: 0644]

index 79a5acc010745794893b416185dfeff8ab78674b..16cc75ccb7c810f2fcb6e6fc1033bf2dadad030d 100644 (file)
 #\r
 from __future__ import print_function\r
 from __future__ import absolute_import\r
+from buildoptions import BuildOption,BuildTarget\r
+import Common.GlobalData as GlobalData\r
 import Common.LongFilePathOs as os\r
 from . import EdkLogger\r
 from . import DataType\r
 from .BuildToolError import *\r
-from . import GlobalData\r
+\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
+from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 \r
 gDefaultTargetTxtFile = "target.txt"\r
 \r
@@ -141,12 +144,29 @@ class TargetTxtClassObject(object):
 #\r
 # @retval Target An instance of TargetTxtClassObject() with loaded target.txt\r
 #\r
-def TargetTxtDict(ConfDir):\r
+def TargetTxtDict():\r
     Target = TargetTxtClassObject()\r
-    Target.LoadTargetTxtFile(os.path.normpath(os.path.join(ConfDir, gDefaultTargetTxtFile)))\r
+    if BuildOption.ConfDirectory:\r
+        # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
+        ConfDirectoryPath = os.path.normpath(BuildOption.ConfDirectory)\r
+\r
+        if not os.path.isabs(ConfDirectoryPath):\r
+            # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE\r
+            # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf\r
+            ConfDirectoryPath = mws.join(os.environ["WORKSPACE"], ConfDirectoryPath)\r
+    else:\r
+        if "CONF_PATH" in os.environ:\r
+            ConfDirectoryPath = os.path.normcase(os.path.normpath(os.environ["CONF_PATH"]))\r
+        else:\r
+            # Get standard WORKSPACE/Conf use the absolute path to the WORKSPACE/Conf\r
+            ConfDirectoryPath = mws.join(os.environ["WORKSPACE"], 'Conf')\r
+    GlobalData.gConfDirectory = ConfDirectoryPath\r
+    targettxt = os.path.normpath(os.path.join(ConfDirectoryPath, gDefaultTargetTxtFile))\r
+    if os.path.exists(targettxt):\r
+        Target.LoadTargetTxtFile(targettxt)\r
     return Target\r
 \r
-TargetTxt = TargetTxtDict(os.path.join(os.getenv("WORKSPACE"),"Conf"))\r
+TargetTxt = TargetTxtDict()\r
 \r
 ##\r
 #\r
index 4daef5652f8d6b6a41a91874ec85161dbdaf1dc7..f80060a127e8c01e3329d4c88ded19e7c116e808 100644 (file)
@@ -26,7 +26,7 @@ from threading import Thread,Event,BoundedSemaphore
 import threading\r
 from subprocess import Popen,PIPE\r
 from collections import OrderedDict, defaultdict\r
-from optparse import OptionParser\r
+from buildoptions import BuildOption,BuildTarget\r
 from AutoGen.PlatformAutoGen import PlatformAutoGen\r
 from AutoGen.ModuleAutoGen import ModuleAutoGen\r
 from AutoGen.WorkspaceAutoGen import WorkspaceAutoGen\r
@@ -43,7 +43,7 @@ from Common.MultipleWorkspace import MultipleWorkspace as mws
 from Common.BuildToolError import *\r
 from Common.DataType import *\r
 import Common.EdkLogger as EdkLogger\r
-from Common.BuildVersion import gBUILD_VERSION\r
+\r
 from Workspace.WorkspaceDatabase import BuildDB\r
 \r
 from BuildReport import BuildReport\r
@@ -55,10 +55,6 @@ from GenFds.GenFds import GenFds, GenFdsApi
 import multiprocessing as mp\r
 from multiprocessing import Manager\r
 \r
-# Version and Copyright\r
-VersionNumber = "0.60" + ' ' + gBUILD_VERSION\r
-__version__ = "%prog Version " + VersionNumber\r
-__copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation  All rights reserved."\r
 \r
 ## standard targets of build command\r
 gSupportedTarget = ['all', 'genc', 'genmake', 'modules', 'libraries', 'fds', 'clean', 'cleanall', 'cleanlib', 'run']\r
@@ -763,22 +759,7 @@ class Build():
             if GlobalData.gBinCacheDest is not None:\r
                 EdkLogger.error("build", OPTION_VALUE_INVALID, ExtraData="Invalid value of option --binary-destination.")\r
 \r
-        if self.ConfDirectory:\r
-            # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
-            ConfDirectoryPath = os.path.normpath(self.ConfDirectory)\r
-\r
-            if not os.path.isabs(ConfDirectoryPath):\r
-                # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE\r
-                # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf\r
-                ConfDirectoryPath = mws.join(self.WorkspaceDir, ConfDirectoryPath)\r
-        else:\r
-            if "CONF_PATH" in os.environ:\r
-                ConfDirectoryPath = os.path.normcase(os.path.normpath(os.environ["CONF_PATH"]))\r
-            else:\r
-                # Get standard WORKSPACE/Conf use the absolute path to the WORKSPACE/Conf\r
-                ConfDirectoryPath = mws.join(self.WorkspaceDir, 'Conf')\r
-        GlobalData.gConfDirectory = ConfDirectoryPath\r
-        GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath))\r
+        GlobalData.gDatabasePath = os.path.normpath(os.path.join(GlobalData.gConfDirectory, GlobalData.gDatabasePath))\r
         if not os.path.exists(os.path.join(GlobalData.gConfDirectory, '.cache')):\r
             os.makedirs(os.path.join(GlobalData.gConfDirectory, '.cache'))\r
         self.Db = BuildDB\r
@@ -2303,13 +2284,7 @@ def ParseDefines(DefineList=[]):
                 DefineDict[DefineTokenList[0]] = DefineTokenList[1].strip()\r
     return DefineDict\r
 \r
-gParamCheck = []\r
-def SingleCheckCallback(option, opt_str, value, parser):\r
-    if option not in gParamCheck:\r
-        setattr(parser.values, option.dest, value)\r
-        gParamCheck.append(option)\r
-    else:\r
-        parser.error("Option %s only allows one instance in command line!" % option)\r
+\r
 \r
 def LogBuildTime(Time):\r
     if Time:\r
@@ -2323,79 +2298,6 @@ def LogBuildTime(Time):
     else:\r
         return None\r
 \r
-## Parse command line options\r
-#\r
-# Using standard Python module optparse to parse command line option of this tool.\r
-#\r
-#   @retval Opt   A optparse.Values object containing the parsed options\r
-#   @retval Args  Target of build command\r
-#\r
-def MyOptionParser():\r
-    Parser = OptionParser(description=__copyright__, version=__version__, prog="build.exe", usage="%prog [options] [all|fds|genc|genmake|clean|cleanall|cleanlib|modules|libraries|run]")\r
-    Parser.add_option("-a", "--arch", action="append", type="choice", choices=['IA32', 'X64', 'EBC', 'ARM', 'AARCH64'], dest="TargetArch",\r
-        help="ARCHS is one of list: IA32, X64, ARM, AARCH64 or EBC, which overrides target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option.")\r
-    Parser.add_option("-p", "--platform", action="callback", type="string", dest="PlatformFile", callback=SingleCheckCallback,\r
-        help="Build the platform specified by the DSC file name argument, overriding target.txt's ACTIVE_PLATFORM definition.")\r
-    Parser.add_option("-m", "--module", action="callback", type="string", dest="ModuleFile", callback=SingleCheckCallback,\r
-        help="Build the module specified by the INF file name argument.")\r
-    Parser.add_option("-b", "--buildtarget", type="string", dest="BuildTarget", help="Using the TARGET to build the platform, overriding target.txt's TARGET definition.",\r
-                      action="append")\r
-    Parser.add_option("-t", "--tagname", action="append", type="string", dest="ToolChain",\r
-        help="Using the Tool Chain Tagname to build the platform, overriding target.txt's TOOL_CHAIN_TAG definition.")\r
-    Parser.add_option("-x", "--sku-id", action="callback", type="string", dest="SkuId", callback=SingleCheckCallback,\r
-        help="Using this name of SKU ID to build the platform, overriding SKUID_IDENTIFIER in DSC file.")\r
-\r
-    Parser.add_option("-n", action="callback", type="int", dest="ThreadNumber", callback=SingleCheckCallback,\r
-        help="Build the platform using multi-threaded compiler. The value overrides target.txt's MAX_CONCURRENT_THREAD_NUMBER. When value is set to 0, tool automatically detect number of "\\r
-             "processor threads, set value to 1 means disable multi-thread build, and set value to more than 1 means user specify the threads number to build.")\r
-\r
-    Parser.add_option("-f", "--fdf", action="callback", type="string", dest="FdfFile", callback=SingleCheckCallback,\r
-        help="The name of the FDF file to use, which overrides the setting in the DSC file.")\r
-    Parser.add_option("-r", "--rom-image", action="append", type="string", dest="RomImage", default=[],\r
-        help="The name of FD to be generated. The name must be from [FD] section in FDF file.")\r
-    Parser.add_option("-i", "--fv-image", action="append", type="string", dest="FvImage", default=[],\r
-        help="The name of FV to be generated. The name must be from [FV] section in FDF file.")\r
-    Parser.add_option("-C", "--capsule-image", action="append", type="string", dest="CapName", default=[],\r
-        help="The name of Capsule to be generated. The name must be from [Capsule] section in FDF file.")\r
-    Parser.add_option("-u", "--skip-autogen", action="store_true", dest="SkipAutoGen", help="Skip AutoGen step.")\r
-    Parser.add_option("-e", "--re-parse", action="store_true", dest="Reparse", help="Re-parse all meta-data files.")\r
-\r
-    Parser.add_option("-c", "--case-insensitive", action="store_true", dest="CaseInsensitive", default=False, help="Don't check case of file name.")\r
-\r
-    Parser.add_option("-w", "--warning-as-error", action="store_true", dest="WarningAsError", help="Treat warning in tools as error.")\r
-    Parser.add_option("-j", "--log", action="store", dest="LogFile", help="Put log in specified file as well as on console.")\r
-\r
-    Parser.add_option("-s", "--silent", action="store_true", type=None, dest="SilentMode",\r
-        help="Make use of silent mode of (n)make.")\r
-    Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")\r
-    Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed, "\\r
-                                                                               "including library instances selected, final dependency expression, "\\r
-                                                                               "and warning messages, etc.")\r
-    Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")\r
-    Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")\r
-\r
-    Parser.add_option("-y", "--report-file", action="store", dest="ReportFile", help="Create/overwrite the report to the specified filename.")\r
-    Parser.add_option("-Y", "--report-type", action="append", type="choice", choices=['PCD', 'LIBRARY', 'FLASH', 'DEPEX', 'BUILD_FLAGS', 'FIXED_ADDRESS', 'HASH', 'EXECUTION_ORDER'], dest="ReportType", default=[],\r
-        help="Flags that control the type of build report to generate.  Must be one of: [PCD, LIBRARY, FLASH, DEPEX, BUILD_FLAGS, FIXED_ADDRESS, HASH, EXECUTION_ORDER].  "\\r
-             "To specify more than one flag, repeat this option on the command line and the default flag set is [PCD, LIBRARY, FLASH, DEPEX, HASH, BUILD_FLAGS, FIXED_ADDRESS]")\r
-    Parser.add_option("-F", "--flag", action="store", type="string", dest="Flag",\r
-        help="Specify the specific option to parse EDK UNI file. Must be one of: [-c, -s]. -c is for EDK framework UNI file, and -s is for EDK UEFI UNI file. "\\r
-             "This option can also be specified by setting *_*_*_BUILD_FLAGS in [BuildOptions] section of platform DSC. If they are both specified, this value "\\r
-             "will override the setting in [BuildOptions] section of platform DSC.")\r
-    Parser.add_option("-N", "--no-cache", action="store_true", dest="DisableCache", default=False, help="Disable build cache mechanism")\r
-    Parser.add_option("--conf", action="store", type="string", dest="ConfDirectory", help="Specify the customized Conf directory.")\r
-    Parser.add_option("--check-usage", action="store_true", dest="CheckUsage", default=False, help="Check usage content of entries listed in INF file.")\r
-    Parser.add_option("--ignore-sources", action="store_true", dest="IgnoreSources", default=False, help="Focus to a binary build and ignore all source files")\r
-    Parser.add_option("--pcd", action="append", dest="OptionPcd", help="Set PCD value by command line. Format: \"PcdName=Value\" ")\r
-    Parser.add_option("-l", "--cmd-len", action="store", type="int", dest="CommandLength", help="Specify the maximum line length of build command. Default is 4096.")\r
-    Parser.add_option("--hash", action="store_true", dest="UseHashCache", default=False, help="Enable hash-based caching during build process.")\r
-    Parser.add_option("--binary-destination", action="store", type="string", dest="BinCacheDest", help="Generate a cache of binary files in the specified directory.")\r
-    Parser.add_option("--binary-source", action="store", type="string", dest="BinCacheSource", help="Consume a cache of binary files from the specified directory.")\r
-    Parser.add_option("--genfds-multi-thread", action="store_true", dest="GenfdsMultiThread", default=False, help="Enable GenFds multi thread to generate ffs file.")\r
-    Parser.add_option("--disable-include-path-check", action="store_true", dest="DisableIncludePathCheck", default=False, help="Disable the include path check for outside of package.")\r
-    (Opt, Args) = Parser.parse_args()\r
-    return (Opt, Args)\r
-\r
 ## Tool entrance method\r
 #\r
 # This method mainly dispatch specific methods per the command line options.\r
@@ -2418,7 +2320,7 @@ def Main():
     #\r
     # Parse the options and args\r
     #\r
-    (Option, Target) = MyOptionParser()\r
+    Option, Target = BuildOption, BuildTarget\r
     GlobalData.gOptions = Option\r
     GlobalData.gCaseInsensitive = Option.CaseInsensitive\r
 \r
diff --git a/BaseTools/Source/Python/build/buildoptions.py b/BaseTools/Source/Python/build/buildoptions.py
new file mode 100644 (file)
index 0000000..7161aa6
--- /dev/null
@@ -0,0 +1,92 @@
+## @file\r
+# build a platform or a module\r
+#\r
+#  Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>\r
+#  Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2018, Hewlett Packard Enterprise Development, L.P.<BR>\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+\r
+# Version and Copyright\r
+from Common.BuildVersion import gBUILD_VERSION\r
+from optparse import OptionParser\r
+VersionNumber = "0.60" + ' ' + gBUILD_VERSION\r
+__version__ = "%prog Version " + VersionNumber\r
+__copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation  All rights reserved."\r
+\r
+gParamCheck = []\r
+def SingleCheckCallback(option, opt_str, value, parser):\r
+    if option not in gParamCheck:\r
+        setattr(parser.values, option.dest, value)\r
+        gParamCheck.append(option)\r
+    else:\r
+        parser.error("Option %s only allows one instance in command line!" % option)\r
+\r
+def MyOptionParser():\r
+    Parser = OptionParser(description=__copyright__, version=__version__, prog="build.exe", usage="%prog [options] [all|fds|genc|genmake|clean|cleanall|cleanlib|modules|libraries|run]")\r
+    Parser.add_option("-a", "--arch", action="append", type="choice", choices=['IA32', 'X64', 'EBC', 'ARM', 'AARCH64'], dest="TargetArch",\r
+        help="ARCHS is one of list: IA32, X64, ARM, AARCH64 or EBC, which overrides target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option.")\r
+    Parser.add_option("-p", "--platform", action="callback", type="string", dest="PlatformFile", callback=SingleCheckCallback,\r
+        help="Build the platform specified by the DSC file name argument, overriding target.txt's ACTIVE_PLATFORM definition.")\r
+    Parser.add_option("-m", "--module", action="callback", type="string", dest="ModuleFile", callback=SingleCheckCallback,\r
+        help="Build the module specified by the INF file name argument.")\r
+    Parser.add_option("-b", "--buildtarget", type="string", dest="BuildTarget", help="Using the TARGET to build the platform, overriding target.txt's TARGET definition.",\r
+                      action="append")\r
+    Parser.add_option("-t", "--tagname", action="append", type="string", dest="ToolChain",\r
+        help="Using the Tool Chain Tagname to build the platform, overriding target.txt's TOOL_CHAIN_TAG definition.")\r
+    Parser.add_option("-x", "--sku-id", action="callback", type="string", dest="SkuId", callback=SingleCheckCallback,\r
+        help="Using this name of SKU ID to build the platform, overriding SKUID_IDENTIFIER in DSC file.")\r
+\r
+    Parser.add_option("-n", action="callback", type="int", dest="ThreadNumber", callback=SingleCheckCallback,\r
+        help="Build the platform using multi-threaded compiler. The value overrides target.txt's MAX_CONCURRENT_THREAD_NUMBER. When value is set to 0, tool automatically detect number of "\\r
+             "processor threads, set value to 1 means disable multi-thread build, and set value to more than 1 means user specify the threads number to build.")\r
+\r
+    Parser.add_option("-f", "--fdf", action="callback", type="string", dest="FdfFile", callback=SingleCheckCallback,\r
+        help="The name of the FDF file to use, which overrides the setting in the DSC file.")\r
+    Parser.add_option("-r", "--rom-image", action="append", type="string", dest="RomImage", default=[],\r
+        help="The name of FD to be generated. The name must be from [FD] section in FDF file.")\r
+    Parser.add_option("-i", "--fv-image", action="append", type="string", dest="FvImage", default=[],\r
+        help="The name of FV to be generated. The name must be from [FV] section in FDF file.")\r
+    Parser.add_option("-C", "--capsule-image", action="append", type="string", dest="CapName", default=[],\r
+        help="The name of Capsule to be generated. The name must be from [Capsule] section in FDF file.")\r
+    Parser.add_option("-u", "--skip-autogen", action="store_true", dest="SkipAutoGen", help="Skip AutoGen step.")\r
+    Parser.add_option("-e", "--re-parse", action="store_true", dest="Reparse", help="Re-parse all meta-data files.")\r
+\r
+    Parser.add_option("-c", "--case-insensitive", action="store_true", dest="CaseInsensitive", default=False, help="Don't check case of file name.")\r
+\r
+    Parser.add_option("-w", "--warning-as-error", action="store_true", dest="WarningAsError", help="Treat warning in tools as error.")\r
+    Parser.add_option("-j", "--log", action="store", dest="LogFile", help="Put log in specified file as well as on console.")\r
+\r
+    Parser.add_option("-s", "--silent", action="store_true", type=None, dest="SilentMode",\r
+        help="Make use of silent mode of (n)make.")\r
+    Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")\r
+    Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed, "\\r
+                                                                               "including library instances selected, final dependency expression, "\\r
+                                                                               "and warning messages, etc.")\r
+    Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")\r
+    Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")\r
+\r
+    Parser.add_option("-y", "--report-file", action="store", dest="ReportFile", help="Create/overwrite the report to the specified filename.")\r
+    Parser.add_option("-Y", "--report-type", action="append", type="choice", choices=['PCD', 'LIBRARY', 'FLASH', 'DEPEX', 'BUILD_FLAGS', 'FIXED_ADDRESS', 'HASH', 'EXECUTION_ORDER'], dest="ReportType", default=[],\r
+        help="Flags that control the type of build report to generate.  Must be one of: [PCD, LIBRARY, FLASH, DEPEX, BUILD_FLAGS, FIXED_ADDRESS, HASH, EXECUTION_ORDER].  "\\r
+             "To specify more than one flag, repeat this option on the command line and the default flag set is [PCD, LIBRARY, FLASH, DEPEX, HASH, BUILD_FLAGS, FIXED_ADDRESS]")\r
+    Parser.add_option("-F", "--flag", action="store", type="string", dest="Flag",\r
+        help="Specify the specific option to parse EDK UNI file. Must be one of: [-c, -s]. -c is for EDK framework UNI file, and -s is for EDK UEFI UNI file. "\\r
+             "This option can also be specified by setting *_*_*_BUILD_FLAGS in [BuildOptions] section of platform DSC. If they are both specified, this value "\\r
+             "will override the setting in [BuildOptions] section of platform DSC.")\r
+    Parser.add_option("-N", "--no-cache", action="store_true", dest="DisableCache", default=False, help="Disable build cache mechanism")\r
+    Parser.add_option("--conf", action="store", type="string", dest="ConfDirectory", help="Specify the customized Conf directory.")\r
+    Parser.add_option("--check-usage", action="store_true", dest="CheckUsage", default=False, help="Check usage content of entries listed in INF file.")\r
+    Parser.add_option("--ignore-sources", action="store_true", dest="IgnoreSources", default=False, help="Focus to a binary build and ignore all source files")\r
+    Parser.add_option("--pcd", action="append", dest="OptionPcd", help="Set PCD value by command line. Format: \"PcdName=Value\" ")\r
+    Parser.add_option("-l", "--cmd-len", action="store", type="int", dest="CommandLength", help="Specify the maximum line length of build command. Default is 4096.")\r
+    Parser.add_option("--hash", action="store_true", dest="UseHashCache", default=False, help="Enable hash-based caching during build process.")\r
+    Parser.add_option("--binary-destination", action="store", type="string", dest="BinCacheDest", help="Generate a cache of binary files in the specified directory.")\r
+    Parser.add_option("--binary-source", action="store", type="string", dest="BinCacheSource", help="Consume a cache of binary files from the specified directory.")\r
+    Parser.add_option("--genfds-multi-thread", action="store_true", dest="GenfdsMultiThread", default=False, help="Enable GenFds multi thread to generate ffs file.")\r
+    Parser.add_option("--disable-include-path-check", action="store_true", dest="DisableIncludePathCheck", default=False, help="Disable the include path check for outside of package.")\r
+    (Opt, Args) = Parser.parse_args()\r
+    return (Opt, Args)\r
+\r
+BuildOption, BuildTarget = MyOptionParser()\r