CorebootPayloadPkg: Conditionally add DebugAgentLib for DXE drivers
[mirror_edk2.git] / BaseTools / Source / Python / TargetTool / TargetTool.py
CommitLineData
97fa0ee9
YL
1## @file\r
2# Target Tool Parser\r
30fdf114 3#\r
1be2ed90 4# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
30fdf114 5#\r
40d841f6 6# This program and the accompanying materials\r
30fdf114
LG
7# are licensed and made available under the terms and conditions of the BSD License\r
8# which accompanies this distribution. The full text of the license may be found at\r
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14\r
1be2ed90 15import Common.LongFilePathOs as os\r
30fdf114
LG
16import sys\r
17import traceback\r
18from optparse import OptionParser\r
19\r
20import Common.EdkLogger as EdkLogger\r
21import Common.BuildToolError as BuildToolError\r
22from Common.DataType import *\r
b36d134f 23from Common.BuildVersion import gBUILD_VERSION\r
1be2ed90 24from Common.LongFilePathSupport import OpenLongFilePath as open\r
30fdf114
LG
25\r
26# To Do 1.set clean, 2. add item, if the line is disabled.\r
27\r
28class TargetTool():\r
29 def __init__(self, opt, args):\r
30 self.WorkSpace = os.path.normpath(os.getenv('WORKSPACE'))\r
31 self.Opt = opt\r
32 self.Arg = args[0]\r
33 self.FileName = os.path.normpath(os.path.join(self.WorkSpace, 'Conf', 'target.txt'))\r
34 if os.path.isfile(self.FileName) == False:\r
35 print "%s does not exist." % self.FileName\r
36 sys.exit(1)\r
37 self.TargetTxtDictionary = {\r
38 TAB_TAT_DEFINES_ACTIVE_PLATFORM : None,\r
39 TAB_TAT_DEFINES_TOOL_CHAIN_CONF : None,\r
30fdf114
LG
40 TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : None,\r
41 TAB_TAT_DEFINES_TARGET : None,\r
42 TAB_TAT_DEFINES_TOOL_CHAIN_TAG : None,\r
43 TAB_TAT_DEFINES_TARGET_ARCH : None,\r
44 TAB_TAT_DEFINES_BUILD_RULE_CONF : None,\r
45 }\r
46 self.LoadTargetTxtFile(self.FileName)\r
47\r
48 def LoadTargetTxtFile(self, filename):\r
49 if os.path.exists(filename) and os.path.isfile(filename):\r
08dd311f 50 return self.ConvertTextFileToDict(filename, '#', '=')\r
30fdf114
LG
51 else:\r
52 raise ParseError('LoadTargetTxtFile() : No Target.txt file exists.')\r
53 return 1\r
54\r
55#\r
56# Convert a text file to a dictionary\r
57#\r
58 def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):\r
59 """Convert a text file to a dictionary of (name:value) pairs."""\r
60 try:\r
61 f = open(FileName,'r')\r
62 for Line in f:\r
63 if Line.startswith(CommentCharacter) or Line.strip() == '':\r
64 continue\r
65 LineList = Line.split(KeySplitCharacter,1)\r
66 if len(LineList) >= 2:\r
67 Key = LineList[0].strip()\r
68 if Key.startswith(CommentCharacter) == False and Key in self.TargetTxtDictionary.keys():\r
69 if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM or Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF \\r
08dd311f 70 or Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER \\r
30fdf114
LG
71 or Key == TAB_TAT_DEFINES_ACTIVE_MODULE:\r
72 self.TargetTxtDictionary[Key] = LineList[1].replace('\\', '/').strip()\r
73 elif Key == TAB_TAT_DEFINES_TARGET or Key == TAB_TAT_DEFINES_TARGET_ARCH \\r
74 or Key == TAB_TAT_DEFINES_TOOL_CHAIN_TAG or Key == TAB_TAT_DEFINES_BUILD_RULE_CONF:\r
75 self.TargetTxtDictionary[Key] = LineList[1].split()\r
76 f.close()\r
77 return 0\r
78 except:\r
79 last_type, last_value, last_tb = sys.exc_info()\r
80 traceback.print_exception(last_type, last_value, last_tb)\r
81\r
82 def Print(self):\r
83 KeyList = self.TargetTxtDictionary.keys()\r
84 errMsg = ''\r
85 for Key in KeyList:\r
86 if type(self.TargetTxtDictionary[Key]) == type([]):\r
87 print "%-30s = %s" % (Key, ''.join(elem + ' ' for elem in self.TargetTxtDictionary[Key]))\r
88 elif self.TargetTxtDictionary[Key] == None:\r
89 errMsg += " Missing %s configuration information, please use TargetTool to set value!" % Key + os.linesep \r
90 else:\r
91 print "%-30s = %s" % (Key, self.TargetTxtDictionary[Key])\r
92 \r
93 if errMsg != '':\r
94 print os.linesep + 'Warning:' + os.linesep + errMsg\r
95 \r
96 def RWFile(self, CommentCharacter, KeySplitCharacter, Num):\r
97 try:\r
98 fr = open(self.FileName, 'r')\r
99 fw = open(os.path.normpath(os.path.join(self.WorkSpace, 'Conf\\targetnew.txt')), 'w')\r
100\r
101 existKeys = []\r
102 for Line in fr:\r
103 if Line.startswith(CommentCharacter) or Line.strip() == '':\r
104 fw.write(Line)\r
105 else:\r
106 LineList = Line.split(KeySplitCharacter,1)\r
107 if len(LineList) >= 2:\r
108 Key = LineList[0].strip()\r
109 if Key.startswith(CommentCharacter) == False and Key in self.TargetTxtDictionary.keys():\r
110 if Key not in existKeys:\r
111 existKeys.append(Key)\r
112 else:\r
113 print "Warning: Found duplicate key item in original configuration files!"\r
114 \r
115 if Num == 0:\r
116 Line = "%-30s = \n" % Key\r
117 else:\r
118 ret = GetConfigureKeyValue(self, Key)\r
119 if ret != None:\r
120 Line = ret\r
121 fw.write(Line)\r
122 for key in self.TargetTxtDictionary.keys():\r
123 if key not in existKeys:\r
124 print "Warning: %s does not exist in original configuration file" % key\r
125 Line = GetConfigureKeyValue(self, key)\r
126 if Line == None:\r
127 Line = "%-30s = " % key\r
128 fw.write(Line)\r
129 \r
130 fr.close()\r
131 fw.close()\r
132 os.remove(self.FileName)\r
133 os.rename(os.path.normpath(os.path.join(self.WorkSpace, 'Conf\\targetnew.txt')), self.FileName)\r
134 \r
135 except:\r
136 last_type, last_value, last_tb = sys.exc_info()\r
137 traceback.print_exception(last_type, last_value, last_tb)\r
138\r
139def GetConfigureKeyValue(self, Key):\r
140 Line = None\r
141 if Key == TAB_TAT_DEFINES_ACTIVE_PLATFORM and self.Opt.DSCFILE != None:\r
142 dscFullPath = os.path.join(self.WorkSpace, self.Opt.DSCFILE)\r
143 if os.path.exists(dscFullPath):\r
144 Line = "%-30s = %s\n" % (Key, self.Opt.DSCFILE)\r
145 else:\r
146 EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND, \r
147 "DSC file %s does not exist!" % self.Opt.DSCFILE, RaiseError=False)\r
148 elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.Opt.TOOL_DEFINITION_FILE != None:\r
149 tooldefFullPath = os.path.join(self.WorkSpace, self.Opt.TOOL_DEFINITION_FILE)\r
150 if os.path.exists(tooldefFullPath):\r
151 Line = "%-30s = %s\n" % (Key, self.Opt.TOOL_DEFINITION_FILE)\r
152 else:\r
153 EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND, \r
154 "Tooldef file %s does not exist!" % self.Opt.TOOL_DEFINITION_FILE, RaiseError=False)\r
08dd311f
LG
155\r
156 elif self.Opt.NUM >= 2:\r
157 Line = "%-30s = %s\n" % (Key, 'Enable')\r
158 elif self.Opt.NUM <= 1:\r
159 Line = "%-30s = %s\n" % (Key, 'Disable') \r
30fdf114
LG
160 elif Key == TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER and self.Opt.NUM != None:\r
161 Line = "%-30s = %s\n" % (Key, str(self.Opt.NUM))\r
30fdf114
LG
162 elif Key == TAB_TAT_DEFINES_TARGET and self.Opt.TARGET != None:\r
163 Line = "%-30s = %s\n" % (Key, ''.join(elem + ' ' for elem in self.Opt.TARGET))\r
164 elif Key == TAB_TAT_DEFINES_TARGET_ARCH and self.Opt.TARGET_ARCH != None:\r
165 Line = "%-30s = %s\n" % (Key, ''.join(elem + ' ' for elem in self.Opt.TARGET_ARCH))\r
166 elif Key == TAB_TAT_DEFINES_TOOL_CHAIN_TAG and self.Opt.TOOL_CHAIN_TAG != None:\r
167 Line = "%-30s = %s\n" % (Key, self.Opt.TOOL_CHAIN_TAG)\r
168 elif Key == TAB_TAT_DEFINES_BUILD_RULE_CONF and self.Opt.BUILD_RULE_FILE != None:\r
169 buildruleFullPath = os.path.join(self.WorkSpace, self.Opt.BUILD_RULE_FILE)\r
170 if os.path.exists(buildruleFullPath):\r
171 Line = "%-30s = %s\n" % (Key, self.Opt.BUILD_RULE_FILE)\r
172 else:\r
173 EdkLogger.error("TagetTool", BuildToolError.FILE_NOT_FOUND, \r
174 "Build rule file %s does not exist!" % self.Opt.BUILD_RULE_FILE, RaiseError=False)\r
175 return Line\r
176\r
b36d134f 177VersionNumber = ("0.01" + " " + gBUILD_VERSION)\r
30fdf114 178__version__ = "%prog Version " + VersionNumber\r
52302d4d 179__copyright__ = "Copyright (c) 2007 - 2010, Intel Corporation All rights reserved."\r
30fdf114
LG
180__usage__ = "%prog [options] {args} \\r
181\nArgs: \\r
182\n Clean clean the all default configuration of target.txt. \\r
183\n Print print the all default configuration of target.txt. \\r
184\n Set replace the default configuration with expected value specified by option."\r
185\r
186gParamCheck = []\r
187def SingleCheckCallback(option, opt_str, value, parser):\r
188 if option not in gParamCheck:\r
189 setattr(parser.values, option.dest, value)\r
190 gParamCheck.append(option)\r
191 else:\r
192 parser.error("Option %s only allows one instance in command line!" % option)\r
193\r
194def RangeCheckCallback(option, opt_str, value, parser):\r
195 if option not in gParamCheck:\r
196 gParamCheck.append(option)\r
197 if value < 1 or value > 8:\r
198 parser.error("The count of multi-thread is not in valid range of 1 ~ 8.")\r
199 else:\r
200 setattr(parser.values, option.dest, value)\r
201 else:\r
202 parser.error("Option %s only allows one instance in command line!" % option)\r
203 \r
204def MyOptionParser():\r
205 parser = OptionParser(version=__version__,prog="TargetTool.exe",usage=__usage__,description=__copyright__)\r
4afd3d04
LG
206 parser.add_option("-a", "--arch", action="append", type="choice", choices=['IA32','X64','IPF','EBC', 'ARM', 'AARCH64','0'], dest="TARGET_ARCH",\r
207 help="ARCHS is one of list: IA32, X64, IPF, 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
30fdf114
LG
208 parser.add_option("-p", "--platform", action="callback", type="string", dest="DSCFILE", callback=SingleCheckCallback,\r
209 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
210 parser.add_option("-c", "--tooldef", action="callback", type="string", dest="TOOL_DEFINITION_FILE", callback=SingleCheckCallback,\r
211 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
212 parser.add_option("-t", "--target", action="append", type="choice", choices=['DEBUG','RELEASE','0'], dest="TARGET",\r
213 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
214 parser.add_option("-n", "--tagname", action="callback", type="string", dest="TOOL_CHAIN_TAG", callback=SingleCheckCallback,\r
215 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
216 parser.add_option("-r", "--buildrule", action="callback", type="string", dest="BUILD_RULE_FILE", callback=SingleCheckCallback,\r
217 help="Specify the build rule configure file, which replaces target.txt's BUILD_RULE_CONF definition. If not specified, the default value Conf/build_rule.txt will be set.")\r
218 parser.add_option("-m", "--multithreadnum", action="callback", type="int", dest="NUM", callback=RangeCheckCallback,\r
219 help="Specify the multi-thread number which replace target.txt's MAX_CONCURRENT_THREAD_NUMBER. If the value is less than 2, MULTIPLE_THREAD will be disabled. If the value is larger than 1, MULTIPLE_THREAD will be enabled.")\r
30fdf114
LG
220 (opt, args)=parser.parse_args()\r
221 return (opt, args)\r
222\r
223if __name__ == '__main__':\r
224 EdkLogger.Initialize()\r
225 EdkLogger.SetLevel(EdkLogger.QUIET)\r
226 if os.getenv('WORKSPACE') == None:\r
227 print "ERROR: WORKSPACE should be specified or edksetup script should be executed before run TargetTool"\r
228 sys.exit(1)\r
229 \r
230 (opt, args) = MyOptionParser()\r
231 if len(args) != 1 or (args[0].lower() != 'print' and args[0].lower() != 'clean' and args[0].lower() != 'set'):\r
232 print "The number of args isn't 1 or the value of args is invalid."\r
233 sys.exit(1)\r
234 if opt.NUM != None and opt.NUM < 1:\r
235 print "The MAX_CONCURRENT_THREAD_NUMBER must be larger than 0."\r
236 sys.exit(1)\r
237 if opt.TARGET != None and len(opt.TARGET) > 1:\r
238 for elem in opt.TARGET:\r
239 if elem == '0':\r
240 print "0 will clear the TARGET setting in target.txt and can't combine with other value."\r
241 sys.exit(1)\r
242 if opt.TARGET_ARCH != None and len(opt.TARGET_ARCH) > 1:\r
243 for elem in opt.TARGET_ARCH:\r
244 if elem == '0':\r
245 print "0 will clear the TARGET_ARCH setting in target.txt and can't combine with other value."\r
246 sys.exit(1)\r
247\r
248 try:\r
249 FileHandle = TargetTool(opt, args)\r
250 if FileHandle.Arg.lower() == 'print':\r
251 FileHandle.Print()\r
252 sys.exit(0)\r
253 elif FileHandle.Arg.lower() == 'clean':\r
254 FileHandle.RWFile('#', '=', 0)\r
255 else:\r
256 FileHandle.RWFile('#', '=', 1)\r
257 except Exception, e:\r
258 last_type, last_value, last_tb = sys.exc_info()\r
259 traceback.print_exception(last_type, last_value, last_tb)\r
260\r