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