To fix EDKT284.
[mirror_edk2.git] / Tools / Source / ContextTool / org / tianocore / context / TargetFile.java
CommitLineData
987912ad 1/** @file\r
2 File is TargetFile class which is used to generate the new target.txt. \r
3 \r
4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
c74c7a43 13package org.tianocore.context;\r
14\r
15import java.io.BufferedReader;\r
16import java.io.BufferedWriter;\r
17import java.io.File;\r
18import java.io.FileNotFoundException;\r
19import java.io.FileOutputStream;\r
20import java.io.FileReader;\r
21import java.io.FileWriter;\r
22import java.io.IOException;\r
23import java.nio.ByteBuffer;\r
24import java.nio.channels.FileChannel;\r
25\r
26public class TargetFile {\r
27\r
28 /** \r
29 * check the validity of path and file\r
30 * @param String filename : the name of target file\r
31 * @return true or false\r
32 **/\r
33 public static boolean parsePath(String filename) {\r
34\r
35 String workspacePath = System.getenv("WORKSPACE");\r
36 \r
37 Fd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);\r
38\r
39 if (Fd.exists() == true) {\r
40 if (createTempFile(filename + "tmp") == false) {\r
41 return false;\r
42 }\r
43 if (readwriteFile() == false) {\r
44 return false;\r
45 }\r
46 return true;\r
47 } else {\r
48 try {\r
49 Fd.createNewFile();\r
50 } catch (IOException e) {\r
51 System.out.printf("%n%s", "Create the file:target.txt failed!");\r
52 return false;\r
53 }\r
54 }\r
55 TargetFile.writeFile(Fd);\r
56 return true;\r
57 }\r
58\r
59 /**\r
60 * create a empty temp file, which is located at the same directory with target file\r
61 * @param String filename : the name of target temp file\r
62 * @return true or false\r
63 **/\r
64 private static boolean createTempFile(String filename) {\r
65\r
66 String workspacePath = System.getenv("WORKSPACE");\r
67 \r
68 TempFd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);\r
69\r
70 if (TempFd.exists() == true) {\r
71 if (TempFd.delete() == false) {\r
72 System.out.println("\n# delete file failed !");\r
73 return false;\r
74 }\r
75 }\r
76 try {\r
77 TempFd.createNewFile();\r
78 } catch (IOException e) {\r
79 System.out.printf("%n%s",\r
80 "Create the temp file:target.txttmp failed!");\r
81 return false;\r
82 }\r
83\r
84 return true;\r
85 }\r
86\r
87 /**\r
88 * read from target.txt and write to target.txttmp, del target.txt, rename\r
89 * @param no paremeter\r
90 * @return true or false\r
91 **/\r
92 private static boolean readwriteFile() {\r
93\r
94 if (Fd.canRead() != true)\r
95 return false;\r
96\r
97 BufferedReader br = null;\r
98 BufferedWriter bw = null;\r
99 String textLine = null;\r
100\r
101 try {\r
102 br = new BufferedReader(new FileReader(Fd));\r
103 } catch (FileNotFoundException e) {\r
104 System.out\r
105 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");\r
106 return false;\r
107 }\r
108 try {\r
109 bw = new BufferedWriter(new FileWriter(TempFd));\r
110 } catch (IOException e) {\r
111 System.out.println("\n# create the BufferedWriter failed!");\r
112 return false;\r
113 }\r
114 \r
115 //\r
116 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt\r
117 //\r
118 try {\r
119 while ((textLine = br.readLine()) != null) {\r
2c71f6e5 120 //\r
121 // the line is composed of Space\r
122 //\r
c74c7a43 123 if (textLine.trim().compareToIgnoreCase("") == 0) {\r
124 bw.write(textLine);\r
125 bw.newLine();\r
2c71f6e5 126 } \r
127 //\r
128 // the line starts with "#", and no "="\r
129 //\r
130 else if ((textLine.trim().charAt(0) == '#') && (textLine.indexOf("=") == -1)){\r
c74c7a43 131 bw.write(textLine);\r
132 bw.newLine();\r
133 } else {\r
2c71f6e5 134 //\r
135 //modify at the first time, and there should be *ACTIVE_PLATFORM*=* in the line\r
136 //\r
c74c7a43 137 if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {\r
2c71f6e5 138 if(pflag == true){\r
139 if(textLine.trim().charAt(0) == '#'){\r
140 if(ParseParameter.pstr.length() > ParseParameter.length) {\r
141 bw.write(ParseParameter.pstr);\r
142 bw.newLine();\r
143 pflag = false;\r
144 }\r
145 continue;\r
146 }\r
147 if(ParseParameter.pstr.length() > ParseParameter.length) {\r
148 bw.write(ParseParameter.pstr);\r
149 } else {\r
150 bw.write(textLine);\r
151 }\r
152 bw.newLine();\r
153 pflag = false;\r
c74c7a43 154 }\r
c74c7a43 155 } else if (textLine.indexOf("TARGET_ARCH") != -1) {\r
2c71f6e5 156 if(aflag == true){\r
157 if(textLine.trim().charAt(0) == '#'){\r
158 if(ParseParameter.astr.length() > ParseParameter.length) {\r
159 bw.write(ParseParameter.astr);\r
160 bw.newLine();\r
161 aflag = false;\r
162 }\r
163 continue;\r
164 }\r
165 if(ParseParameter.astr.length() > ParseParameter.length) {\r
166 bw.write(ParseParameter.astr);\r
167 } else {\r
168 bw.write(textLine);\r
169 }\r
170 bw.newLine();\r
171 aflag = false;\r
c74c7a43 172 }\r
c74c7a43 173 } else if (textLine.indexOf("TARGET") != -1) {\r
2c71f6e5 174 if(tflag == true){\r
175 if(textLine.trim().charAt(0) == '#'){\r
176 if(ParseParameter.tstr.length() > ParseParameter.length) {\r
177 bw.write(ParseParameter.tstr);\r
178 bw.newLine();\r
179 tflag = false;\r
180 }\r
181 continue;\r
182 }\r
183 if(ParseParameter.tstr.length() > ParseParameter.length) {\r
184 bw.write(ParseParameter.tstr);\r
185 } else {\r
186 bw.write(textLine);\r
187 }\r
188 bw.newLine();\r
189 tflag = false;\r
c74c7a43 190 }\r
c74c7a43 191 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {\r
2c71f6e5 192 if(cflag == true){\r
193 if(textLine.trim().charAt(0) == '#'){\r
194 if(ParseParameter.cstr.length() > ParseParameter.length) {\r
195 bw.write(ParseParameter.cstr);\r
196 bw.newLine();\r
197 cflag = false;\r
198 }\r
199 continue;\r
200 }\r
201 if(ParseParameter.cstr.length() > ParseParameter.length) {\r
202 bw.write(ParseParameter.cstr);\r
203 } else {\r
204 bw.write(textLine);\r
205 }\r
206 bw.newLine();\r
207 cflag = false;\r
c74c7a43 208 }\r
c74c7a43 209 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {\r
2c71f6e5 210 if(nflag == true){\r
211 if(textLine.trim().charAt(0) == '#'){\r
212 if(ParseParameter.nstr.length() > ParseParameter.length) {\r
213 bw.write(ParseParameter.nstr);\r
214 bw.newLine();\r
215 nflag = false;\r
216 }\r
217 continue;\r
218 }\r
219 if(ParseParameter.nstr.length() > ParseParameter.length) {\r
220 bw.write(ParseParameter.nstr);\r
221 } else {\r
222 bw.write(textLine);\r
223 }\r
224 bw.newLine();\r
225 nflag = false;\r
226 }\r
227 } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {\r
228 if(mflag == true){\r
229 if(textLine.trim().charAt(0) == '#'){\r
230 if(ParseParameter.mstr.length() > ParseParameter.length) {\r
231 bw.write(ParseParameter.mstr);\r
232 bw.newLine();\r
233 mflag = false;\r
234 }\r
235 continue;\r
236 }\r
237 if(ParseParameter.mstr.length() > ParseParameter.length) {\r
238 bw.write(ParseParameter.mstr);\r
239 } else {\r
240 bw.write(textLine);\r
241 }\r
242 bw.newLine();\r
243 mflag = false;\r
244 }\r
245 }else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {\r
246 if(meflag == true){\r
247 if(textLine.trim().charAt(0) == '#'){\r
248 if(ParseParameter.mestr.length() > ParseParameter.length) {\r
249 bw.write(ParseParameter.mestr);\r
250 bw.newLine();\r
251 meflag = false;\r
252 }\r
253 continue;\r
254 }\r
255 if(ParseParameter.mestr.length() > ParseParameter.length) {\r
256 bw.write(ParseParameter.mestr);\r
257 } else {\r
258 bw.write(textLine);\r
259 }\r
260 bw.newLine();\r
261 meflag = false;\r
c74c7a43 262 }\r
c74c7a43 263 }\r
264 }\r
265 }\r
2c71f6e5 266 //\r
267 //user maybe delete the line *ACTIVE_PLATFORM*=*\r
268 //\r
269 if( (pflag == true) && (ParseParameter.pstr.length() > ParseParameter.length) ){\r
270 bw.write(ParseParameter.pstr);\r
271 bw.newLine();\r
272 } else if ( (tflag == true) && (ParseParameter.tstr.length() > ParseParameter.length) ){\r
273 bw.write(ParseParameter.tstr);\r
274 bw.newLine();\r
275 } else if ( (aflag == true) && (ParseParameter.astr.length() > ParseParameter.length) ){\r
276 bw.write(ParseParameter.astr);\r
277 bw.newLine();\r
278 } else if ( (cflag == true) && (ParseParameter.cstr.length() > ParseParameter.length) ){\r
279 bw.write(ParseParameter.cstr);\r
280 bw.newLine();\r
281 } else if ( (nflag == true) && (ParseParameter.nstr.length() > ParseParameter.length) ){\r
282 bw.write(ParseParameter.nstr);\r
283 bw.newLine();\r
284 } else if ( (meflag == true) && (ParseParameter.mestr.length() > ParseParameter.length) ){\r
285 bw.write(ParseParameter.mestr);\r
286 bw.newLine();\r
287 } else if ( (mflag == true) && (ParseParameter.mstr.length() > ParseParameter.length) ){\r
288 bw.write(ParseParameter.mstr);\r
289 bw.newLine();\r
290 }\r
c74c7a43 291 } catch (IOException e) {\r
292 System.out.println("\n# read or write file error!");\r
293 return false;\r
294 }\r
295\r
296 try {\r
297 br.close();\r
298 bw.close();\r
299 } catch (IOException e) {\r
300 System.out\r
301 .println("\n# close BufferedReader&BufferedWriter error");\r
302 return false;\r
303 }\r
304\r
305 if (Fd.delete() == false) {\r
306 System.out.println("\n# delete file failed !");\r
307 return false;\r
308 }\r
309 if (TempFd.renameTo(Fd) == false) {\r
310 System.out.println("\n# rename file failed !");\r
311 return false;\r
312 }\r
313\r
314 return true;\r
315 }\r
316\r
317 /**\r
318 * according to user's input args, write the file directly\r
319 * @param File fd : the File of the target file\r
320 * @return true or false\r
321 **/\r
322 private static boolean writeFile(File fd) {\r
323\r
324 if (fd.canWrite() != true)\r
325 return false;\r
326\r
327 FileOutputStream outputFile = null;\r
328 try {\r
329 outputFile = new FileOutputStream(fd);\r
330 } catch (FileNotFoundException e) {\r
331 System.out\r
332 .println("\n# can't find the file when open the output stream !");\r
333 return false;\r
334 }\r
335 FileChannel outputChannel = outputFile.getChannel();\r
336\r
2c71f6e5 337 ByteBuffer[] buffers = new ByteBuffer[7];\r
c74c7a43 338 buffers[0] = ByteBuffer.allocate(ParseParameter.pstr.toString().length());\r
339 buffers[1] = ByteBuffer.allocate(ParseParameter.tstr.toString().length());\r
340 buffers[2] = ByteBuffer.allocate(ParseParameter.astr.toString().length());\r
341 buffers[3] = ByteBuffer.allocate(ParseParameter.cstr.toString().length());\r
342 buffers[4] = ByteBuffer.allocate(ParseParameter.nstr.toString().length());\r
2c71f6e5 343 buffers[5] = ByteBuffer.allocate(ParseParameter.mestr.toString().length());\r
344 buffers[6] = ByteBuffer.allocate(ParseParameter.mstr.toString().length());\r
c74c7a43 345\r
346 buffers[0].put(ParseParameter.pstr.toString().getBytes()).flip();\r
347 buffers[1].put(ParseParameter.tstr.toString().getBytes()).flip();\r
348 buffers[2].put(ParseParameter.astr.toString().getBytes()).flip();\r
349 buffers[3].put(ParseParameter.cstr.toString().getBytes()).flip();\r
350 buffers[4].put(ParseParameter.nstr.toString().getBytes()).flip();\r
2c71f6e5 351 buffers[5].put(ParseParameter.mestr.toString().getBytes()).flip();\r
352 buffers[6].put(ParseParameter.mstr.toString().getBytes()).flip();\r
c74c7a43 353\r
354 try {\r
355 ByteBuffer bufofCP = ByteBuffer.allocate(Copyright.length());\r
356 bufofCP.put(Copyright.getBytes()).flip();\r
357 outputChannel.write(bufofCP);\r
358 \r
359 ByteBuffer bufofFI = ByteBuffer.allocate(Fileinfo.length());\r
360 bufofFI.put(Fileinfo.getBytes()).flip();\r
361 outputChannel.write(bufofFI);\r
362 \r
363 ByteBuffer buffer0 = ByteBuffer.allocate(pusage.length());\r
364 buffer0.put(pusage.getBytes()).flip();\r
365 outputChannel.write(buffer0);\r
366 outputChannel.write(buffers[0]);\r
367 \r
368 ByteBuffer buffer1 = ByteBuffer.allocate(tusage.length());\r
369 buffer1.put(tusage.getBytes()).flip();\r
370 outputChannel.write(buffer1);\r
371 outputChannel.write(buffers[1]);\r
372 \r
373 ByteBuffer buffer2 = ByteBuffer.allocate(ausage.length());\r
374 buffer2.put(ausage.getBytes()).flip();\r
375 outputChannel.write(buffer2);\r
376 outputChannel.write(buffers[2]);\r
377 \r
378 ByteBuffer buffer3 = ByteBuffer.allocate(cusage.length());\r
379 buffer3.put(cusage.getBytes()).flip();\r
380 outputChannel.write(buffer3);\r
381 outputChannel.write(buffers[3]);\r
382 \r
383 ByteBuffer buffer4 = ByteBuffer.allocate(nusage.length());\r
384 buffer4.put(nusage.getBytes()).flip();\r
385 outputChannel.write(buffer4);\r
386 outputChannel.write(buffers[4]);\r
387 \r
2c71f6e5 388 ByteBuffer buffer5 = ByteBuffer.allocate(meusage.length());\r
389 buffer4.put(meusage.getBytes()).flip();\r
390 outputChannel.write(buffer5);\r
391 outputChannel.write(buffers[5]);\r
392 \r
393 ByteBuffer buffer6 = ByteBuffer.allocate(musage.length());\r
394 buffer4.put(musage.getBytes()).flip();\r
395 outputChannel.write(buffer6);\r
396 outputChannel.write(buffers[6]);\r
397 \r
c74c7a43 398 outputFile.close();\r
399 } catch (IOException e) {\r
400 System.out.println("\n# The operations of file failed !");\r
401 return false;\r
402 }\r
403 return true;\r
404 }\r
405\r
406 ///\r
407 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.\r
408 ///\r
409 private static File TempFd;\r
410 \r
411 ///\r
412 /// point to target.txt.\r
413 ///\r
414 private static File Fd;\r
2c71f6e5 415 \r
416 ///\r
417 /// when the flag is true, the corresponding str should be add at the end of file.\r
418 ///\r
419 private static boolean pflag = true;\r
420 private static boolean tflag = true;\r
421 private static boolean aflag = true;\r
422 private static boolean cflag = true;\r
423 private static boolean nflag = true;\r
424 private static boolean mflag = true;\r
425 private static boolean meflag = true;\r
c74c7a43 426\r
427 private static final String Copyright = "#\n"\r
428 + "# Copyright (c) 2006, Intel Corporation\n"\r
429 + "#\n"\r
430 + "# All rights reserved. This program and the accompanying materials\n"\r
431 + "# are licensed and made available under the terms and conditions of the BSD License\n"\r
432 + "# which accompanies this distribution. The full text of the license may be found at\n"\r
433 + "# http://opensource.org/licenses/bsd-license.php\n"\r
434 + "\n"\r
435 + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"\r
436 + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";\r
437\r
438 private static final String Fileinfo = "#\n"\r
439 + "# Filename: target.template\n"\r
440 + "#\n"\r
441 + "# ALL Paths are Relative to WORKSPACE\n"\r
442 + "\n"\r
443 + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"\r
444 + "# Un-set an option by either commenting out the line, or not setting a value.\n";\r
445\r
446 private static final String pusage = "#\n"\r
447 + "# PROPERTY Type Use Description\n"\r
448 + "# ---------------- -------- -------- -----------------------------------------------------------\n"\r
449 + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"\r
450 + "# of the platform FPD file that will be used for the build\n"\r
451 + "# This line is required if and only if the current working\n"\r
452 + "# directory does not contain one or more FPD files.\n";\r
453\r
454 private static final String tusage = "\n\n"\r
455 + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"\r
456 + "# UserDefined; separated by a space character. \n"\r
457 + "# If the line is missing or no value is specified, all\n"\r
458 + "# valid targets specified in the FPD file will attempt \n"\r
459 + "# to be built. The following line will build all platform\n"\r
460 + "# targets.\n";\r
461\r
462 private static final String ausage = "\n\n"\r
463 + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"\r
464 + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"\r
465 + "# Multiple values can be specified on a single line, using \n"\r
466 + "# space charaters to separate the values. These are used \n"\r
467 + "# during the parsing of an FPD file, restricting the build\n"\r
468 + "# output target(s.)\n"\r
469 + "# The Build Target ARCH is determined by a logical AND of:\n"\r
470 + "# FPD BuildOptions: <SupportedArchitectures> tag\n"\r
471 + "# If not specified, then all valid architectures specified \n"\r
472 + "# in the FPD file, for which tools are available, will be \n"\r
473 + "# built.\n";\r
474\r
475 private static final String cusage = "\n\n"\r
476 + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"\r
477 + "# the tools to use for the build. If not specified, \n"\r
478 + "# tools_def.txt will be used for the build. This file \n"\r
479 + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";\r
480\r
481 private static final String nusage = "\n\n"\r
482 + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"\r
483 + "# If not specified, all applicable TagName tools will be \n"\r
484 + "# used for the build. The list uses space character separation.\n";\r
2c71f6e5 485 \r
486 private static final String musage = "\n\n"\r
487 + "# MULTIPLE_THREAD FLAG Optional Flag to enable multi-thread build. If not specified, default\n"\r
488 + "# is \"Disable\". If your computer is multi-core or multiple CPUs,\n" \r
489 + "# enabling this feature will bring much benefit. For multi-thread\n" \r
490 + "# built, the log will write to ${BUILD_DIR}/build.log.\n" \r
491 + "# This feature is only for PLATFORM build, and clean, cleanall or\n"\r
492 + "# stand-alone module build is still using the normal way.\n";\r
493 private static final String meusage = "\n\n"\r
494 + "# MAX_CONCURRENT_THREAD_NUMBER NUMBER Optional The number of concurrent threads. Default is 2. Recommend to\n" \r
495 + "# set this value to one more than the number of your compurter\n"\r
496 + "# cores or CPUs.\n";\r
c74c7a43 497}\r