]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/TargetTool/TargetTool.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / TargetTool / TargetTool.py
index 7993023150c4e57ea31a51e76bf09b3c6cbb28d9..7f2479f0f0acbf92563d096f55beb9fd9bdc7c22 100644 (file)
@@ -1,16 +1,13 @@
+## @file\r
+# Target Tool Parser\r
 #\r
-#  Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>\r
 #\r
-#  This program and the accompanying materials\r
-#  are licensed and made available under the terms and conditions of the BSD License\r
-#  which accompanies this distribution.  The full text of the license may be found at\r
-#  http://opensource.org/licenses/bsd-license.php\r
-#\r
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
-import os\r
+from __future__ import print_function\r
+import Common.LongFilePathOs as os\r
 import sys\r
 import traceback\r
 from optparse import OptionParser\r
@@ -18,6 +15,9 @@ from optparse import OptionParser
 import Common.EdkLogger as EdkLogger\r
 import Common.BuildToolError as BuildToolError\r
 from Common.DataType import *\r
+from Common.BuildVersion import gBUILD_VERSION\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\r
+from Common.TargetTxtClassObject import gDefaultTargetTxtFile\r
 \r
 # To Do 1.set clean, 2. add item, if the line is disabled.\r
 \r
@@ -26,9 +26,9 @@ class TargetTool():
         self.WorkSpace = os.path.normpath(os.getenv('WORKSPACE'))\r
         self.Opt       = opt\r
         self.Arg       = args[0]\r
-        self.FileName  = os.path.normpath(os.path.join(self.WorkSpace, 'Conf', 'target.txt'))\r
+        self.FileName  = os.path.normpath(os.path.join(self.WorkSpace, 'Conf', gDefaultTargetTxtFile))\r
         if os.path.isfile(self.FileName) == False:\r
-            print "%s does not exist." % self.FileName\r
+            print("%s does not exist." % self.FileName)\r
             sys.exit(1)\r
         self.TargetTxtDictionary = {\r
             TAB_TAT_DEFINES_ACTIVE_PLATFORM                            : None,\r
@@ -54,14 +54,14 @@ class TargetTool():
     def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):\r
         """Convert a text file to a dictionary of (name:value) pairs."""\r
         try:\r
-            f = open(FileName,'r')\r
+            f = open(FileName, 'r')\r
             for Line in f:\r
                 if Line.startswith(CommentCharacter) or Line.strip() == '':\r
                     continue\r
-                LineList = Line.split(KeySplitCharacter,1)\r
+                LineList = Line.split(KeySplitCharacter, 1)\r
                 if len(LineList) >= 2:\r
                     Key = LineList[0].strip()\r
-                    if Key.startswith(CommentCharacter) == False and Key in self.TargetTxtDictionary.keys():\r
+                    if Key.startswith(CommentCharacter) == False and Key in self.TargetTxtDictionary:\r
                         if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM or Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF \\r
                           or Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER \\r
                           or Key == TAB_TAT_DEFINES_ACTIVE_MODULE:\r
@@ -76,19 +76,18 @@ class TargetTool():
             traceback.print_exception(last_type, last_value, last_tb)\r
 \r
     def Print(self):\r
-        KeyList = self.TargetTxtDictionary.keys()\r
         errMsg  = ''\r
-        for Key in KeyList:\r
-            if type(self.TargetTxtDictionary[Key]) == type([]):\r
-                print "%-30s = %s" % (Key, ''.join(elem + ' ' for elem in self.TargetTxtDictionary[Key]))\r
-            elif self.TargetTxtDictionary[Key] == None:\r
-                errMsg += "  Missing %s configuration information, please use TargetTool to set value!" % Key + os.linesep \r
+        for Key in self.TargetTxtDictionary:\r
+            if isinstance(self.TargetTxtDictionary[Key], type([])):\r
+                print("%-30s = %s" % (Key, ''.join(elem + ' ' for elem in self.TargetTxtDictionary[Key])))\r
+            elif self.TargetTxtDictionary[Key] is None:\r
+                errMsg += "  Missing %s configuration information, please use TargetTool to set value!" % Key + os.linesep\r
             else:\r
-                print "%-30s = %s" % (Key, self.TargetTxtDictionary[Key])\r
-        \r
+                print("%-30s = %s" % (Key, self.TargetTxtDictionary[Key]))\r
+\r
         if errMsg != '':\r
-            print os.linesep + 'Warning:' + os.linesep + errMsg\r
-            \r
+            print(os.linesep + 'Warning:' + os.linesep + errMsg)\r
+\r
     def RWFile(self, CommentCharacter, KeySplitCharacter, Num):\r
         try:\r
             fr = open(self.FileName, 'r')\r
@@ -99,80 +98,80 @@ class TargetTool():
                 if Line.startswith(CommentCharacter) or Line.strip() == '':\r
                     fw.write(Line)\r
                 else:\r
-                    LineList = Line.split(KeySplitCharacter,1)\r
+                    LineList = Line.split(KeySplitCharacter, 1)\r
                     if len(LineList) >= 2:\r
                         Key = LineList[0].strip()\r
-                        if Key.startswith(CommentCharacter) == False and Key in self.TargetTxtDictionary.keys():\r
+                        if Key.startswith(CommentCharacter) == False and Key in self.TargetTxtDictionary:\r
                             if Key not in existKeys:\r
                                 existKeys.append(Key)\r
                             else:\r
-                                print "Warning: Found duplicate key item in original configuration files!"\r
-                                \r
+                                print("Warning: Found duplicate key item in original configuration files!")\r
+\r
                             if Num == 0:\r
                                 Line = "%-30s = \n" % Key\r
                             else:\r
                                 ret = GetConfigureKeyValue(self, Key)\r
-                                if ret != None:\r
+                                if ret is not None:\r
                                     Line = ret\r
                             fw.write(Line)\r
-            for key in self.TargetTxtDictionary.keys():\r
+            for key in self.TargetTxtDictionary:\r
                 if key not in existKeys:\r
-                    print "Warning: %s does not exist in original configuration file" % key\r
+                    print("Warning: %s does not exist in original configuration file" % key)\r
                     Line = GetConfigureKeyValue(self, key)\r
-                    if Line == None:\r
+                    if Line is None:\r
                         Line = "%-30s = " % key\r
                     fw.write(Line)\r
-                \r
+\r
             fr.close()\r
             fw.close()\r
             os.remove(self.FileName)\r
             os.rename(os.path.normpath(os.path.join(self.WorkSpace, 'Conf\\targetnew.txt')), self.FileName)\r
-            \r
+\r
         except:\r
             last_type, last_value, last_tb = sys.exc_info()\r
             traceback.print_exception(last_type, last_value, last_tb)\r
 \r
 def GetConfigureKeyValue(self, Key):\r
     Line = None\r
-    if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM and self.Opt.DSCFILE != None:\r
+    if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM and self.Opt.DSCFILE is not None:\r
         dscFullPath = os.path.join(self.WorkSpace, self.Opt.DSCFILE)\r
         if os.path.exists(dscFullPath):\r
             Line = "%-30s = %s\n" % (Key, self.Opt.DSCFILE)\r
         else:\r
-            EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND, \r
+            EdkLogger.error("TargetTool", BuildToolError.FILE_NOT_FOUND,\r
                             "DSC file %s does not exist!" % self.Opt.DSCFILE, RaiseError=False)\r
-    elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.Opt.TOOL_DEFINITION_FILE != None:\r
+    elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.Opt.TOOL_DEFINITION_FILE is not None:\r
         tooldefFullPath = os.path.join(self.WorkSpace, self.Opt.TOOL_DEFINITION_FILE)\r
         if os.path.exists(tooldefFullPath):\r
             Line = "%-30s = %s\n" % (Key, self.Opt.TOOL_DEFINITION_FILE)\r
         else:\r
-            EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND, \r
+            EdkLogger.error("TargetTool", BuildToolError.FILE_NOT_FOUND,\r
                             "Tooldef file %s does not exist!" % self.Opt.TOOL_DEFINITION_FILE, RaiseError=False)\r
 \r
     elif self.Opt.NUM >= 2:\r
         Line = "%-30s = %s\n" % (Key, 'Enable')\r
     elif self.Opt.NUM <= 1:\r
-        Line = "%-30s = %s\n" % (Key, 'Disable')        \r
-    elif Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER and self.Opt.NUM != None:\r
+        Line = "%-30s = %s\n" % (Key, 'Disable')\r
+    elif Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER and self.Opt.NUM is not None:\r
         Line = "%-30s = %s\n" % (Key, str(self.Opt.NUM))\r
-    elif Key == TAB_TAT_DEFINES_TARGET and self.Opt.TARGET != None:\r
+    elif Key == TAB_TAT_DEFINES_TARGET and self.Opt.TARGET is not None:\r
         Line = "%-30s = %s\n" % (Key, ''.join(elem + ' ' for elem in self.Opt.TARGET))\r
-    elif Key == TAB_TAT_DEFINES_TARGET_ARCH and self.Opt.TARGET_ARCH != None:\r
+    elif Key == TAB_TAT_DEFINES_TARGET_ARCH and self.Opt.TARGET_ARCH is not None:\r
         Line = "%-30s = %s\n" % (Key, ''.join(elem + ' ' for elem in self.Opt.TARGET_ARCH))\r
-    elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_TAG and self.Opt.TOOL_CHAIN_TAG != None:\r
+    elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_TAG and self.Opt.TOOL_CHAIN_TAG is not None:\r
         Line = "%-30s = %s\n" % (Key, self.Opt.TOOL_CHAIN_TAG)\r
-    elif Key == TAB_TAT_DEFINES_BUILD_RULE_CONF and self.Opt.BUILD_RULE_FILE != None:\r
+    elif Key == TAB_TAT_DEFINES_BUILD_RULE_CONF and self.Opt.BUILD_RULE_FILE is not None:\r
         buildruleFullPath = os.path.join(self.WorkSpace, self.Opt.BUILD_RULE_FILE)\r
         if os.path.exists(buildruleFullPath):\r
             Line = "%-30s = %s\n" % (Key, self.Opt.BUILD_RULE_FILE)\r
         else:\r
-            EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND, \r
+            EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND,\r
                             "Build rule file %s does not exist!" % self.Opt.BUILD_RULE_FILE, RaiseError=False)\r
     return Line\r
 \r
-VersionNumber = "0.01"\r
+VersionNumber = ("0.01" + " " + gBUILD_VERSION)\r
 __version__ = "%prog Version " + VersionNumber\r
-__copyright__ = "Copyright (c) 2007 - 2010, Intel Corporation  All rights reserved."\r
+__copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation  All rights reserved."\r
 __usage__ = "%prog [options] {args} \\r
 \nArgs:                                                  \\r
 \n Clean  clean the all default configuration of target.txt. \\r
@@ -196,16 +195,16 @@ def RangeCheckCallback(option, opt_str, value, parser):
             setattr(parser.values, option.dest, value)\r
     else:\r
         parser.error("Option %s only allows one instance in command line!" % option)\r
-        \r
+\r
 def MyOptionParser():\r
-    parser = OptionParser(version=__version__,prog="TargetTool.exe",usage=__usage__,description=__copyright__)\r
-    parser.add_option("-a", "--arch", action="append", type="choice", choices=['IA32','X64','IPF','EBC', 'ARM','0'], dest="TARGET_ARCH",\r
-        help="ARCHS is one of list: IA32, X64, IPF, ARM or EBC, which replaces target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option. 0 will clear this setting in target.txt and can't combine with other value.")\r
+    parser = OptionParser(version=__version__, prog="TargetTool.exe", usage=__usage__, description=__copyright__)\r
+    parser.add_option("-a", "--arch", action="append", dest="TARGET_ARCH",\r
+        help="ARCHS is one of list: IA32, X64, ARM, AARCH64 or EBC, which replaces target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option. 0 will clear this setting in target.txt and can't combine with other value.")\r
     parser.add_option("-p", "--platform", action="callback", type="string", dest="DSCFILE", callback=SingleCheckCallback,\r
         help="Specify a DSC file, which replace target.txt's ACTIVE_PLATFORM definition. 0 will clear this setting in target.txt and can't combine with other value.")\r
     parser.add_option("-c", "--tooldef", action="callback", type="string", dest="TOOL_DEFINITION_FILE", callback=SingleCheckCallback,\r
         help="Specify the WORKSPACE relative path of tool_def.txt file, which replace target.txt's TOOL_CHAIN_CONF definition. 0 will clear this setting in target.txt and can't combine with other value.")\r
-    parser.add_option("-t", "--target", action="append", type="choice", choices=['DEBUG','RELEASE','0'], dest="TARGET",\r
+    parser.add_option("-t", "--target", action="append", type="choice", choices=['DEBUG', 'RELEASE', '0'], dest="TARGET",\r
         help="TARGET is one of list: DEBUG, RELEASE, which replaces target.txt's TARGET definition. To specify more TARGET, please repeat this option. 0 will clear this setting in target.txt and can't combine with other value.")\r
     parser.add_option("-n", "--tagname", action="callback", type="string", dest="TOOL_CHAIN_TAG", callback=SingleCheckCallback,\r
         help="Specify the Tool Chain Tagname, which replaces target.txt's TOOL_CHAIN_TAG definition. 0 will clear this setting in target.txt and can't combine with other value.")\r
@@ -219,26 +218,26 @@ def MyOptionParser():
 if __name__ == '__main__':\r
     EdkLogger.Initialize()\r
     EdkLogger.SetLevel(EdkLogger.QUIET)\r
-    if os.getenv('WORKSPACE') == None:\r
-        print "ERROR: WORKSPACE should be specified or edksetup script should be executed before run TargetTool"\r
+    if os.getenv('WORKSPACE') is None:\r
+        print("ERROR: WORKSPACE should be specified or edksetup script should be executed before run TargetTool")\r
         sys.exit(1)\r
-        \r
+\r
     (opt, args) = MyOptionParser()\r
     if len(args) != 1 or (args[0].lower() != 'print' and args[0].lower() != 'clean' and args[0].lower() != 'set'):\r
-        print "The number of args isn't 1 or the value of args is invalid."\r
+        print("The number of args isn't 1 or the value of args is invalid.")\r
         sys.exit(1)\r
-    if opt.NUM != None and opt.NUM < 1:\r
-        print "The MAX_CONCURRENT_THREAD_NUMBER must be larger than 0."\r
+    if opt.NUM is not None and opt.NUM < 1:\r
+        print("The MAX_CONCURRENT_THREAD_NUMBER must be larger than 0.")\r
         sys.exit(1)\r
-    if opt.TARGET != None and len(opt.TARGET) > 1:\r
+    if opt.TARGET is not None and len(opt.TARGET) > 1:\r
         for elem in opt.TARGET:\r
             if elem == '0':\r
-                print "0 will clear the TARGET setting in target.txt and can't combine with other value."\r
+                print("0 will clear the TARGET setting in target.txt and can't combine with other value.")\r
                 sys.exit(1)\r
-    if opt.TARGET_ARCH != None and len(opt.TARGET_ARCH) > 1:\r
+    if opt.TARGET_ARCH is not None and len(opt.TARGET_ARCH) > 1:\r
         for elem in opt.TARGET_ARCH:\r
             if elem == '0':\r
-                print "0 will clear the TARGET_ARCH setting in target.txt and can't combine with other value."\r
+                print("0 will clear the TARGET_ARCH setting in target.txt and can't combine with other value.")\r
                 sys.exit(1)\r
 \r
     try:\r
@@ -250,7 +249,7 @@ if __name__ == '__main__':
             FileHandle.RWFile('#', '=', 0)\r
         else:\r
             FileHandle.RWFile('#', '=', 1)\r
-    except Exception, e:\r
+    except Exception as e:\r
         last_type, last_value, last_tb = sys.exc_info()\r
         traceback.print_exception(last_type, last_value, last_tb)\r
 \r