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