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