2 File is TargetFile class which is used to generate the new target.txt.
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
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.
13 package org
.tianocore
.context
;
15 import java
.io
.BufferedReader
;
16 import java
.io
.BufferedWriter
;
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
;
26 public class TargetFile
{
29 * check the validity of path and file
30 * @param String filename : the name of target file
31 * @return true or false
33 public static boolean setFile(String filename
) {
35 String workspacePath
= System
.getenv("WORKSPACE");
37 Fd
= new File(workspacePath
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ filename
);
39 if (Fd
.exists() == true) {
40 if (createTempFile(filename
+ "tmp") == false) {
43 if (readwriteFile() == false) {
50 } catch (IOException e
) {
51 System
.out
.printf("%n%s", "Create the file:target.txt failed!");
55 TargetFile
.writeFile(Fd
);
61 * validate the filename
62 * @param String filename : the name of target file
64 * @return true or false
66 public static boolean validateFilename(String filename
) {
68 String workspacePath
= System
.getenv("WORKSPACE");
70 Fd
= new File(workspacePath
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ filename
);
72 if (Fd
.exists() == true && Fd
.canRead() == true)
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
84 public static boolean createTempFile(String filename
) {
86 String workspacePath
= System
.getenv("WORKSPACE");
88 TempFd
= new File(workspacePath
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ filename
+ "tmp");
90 if (TempFd
.exists() == true) {
91 if (TempFd
.delete() == false) {
92 System
.out
.println("\n# delete file failed !");
97 TempFd
.createNewFile();
98 } catch (IOException e
) {
99 System
.out
.printf("%n%s",
100 "Create the temp file:target.txttmp failed!");
108 * read from target.txt and write to target.txttmp, del target.txt, rename
109 * @param no paremeter
110 * @return true or false
112 public static boolean readwriteFile() {
114 if (Fd
.canRead() != true)
117 BufferedReader br
= null;
118 BufferedWriter bw
= null;
119 String textLine
= null;
122 br
= new BufferedReader(new FileReader(Fd
));
123 } catch (FileNotFoundException e
) {
125 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
129 bw
= new BufferedWriter(new FileWriter(TempFd
));
130 } catch (IOException e
) {
131 System
.out
.println("\n# create the BufferedWriter failed!");
136 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt
139 while ((textLine
= br
.readLine()) != null) {
141 // the line is composed of Space
143 if (textLine
.trim().compareToIgnoreCase("") == 0) {
148 // the line starts with "#", and no "="
150 else if ((textLine
.trim().charAt(0) == '#') && (textLine
.indexOf("=") == -1)){
155 //modify at the first time, and there should be "*ACTIVE_PLATFORM*=*" in the line
157 if (textLine
.indexOf("ACTIVE_PLATFORM") != -1) {
159 if(textLine
.trim().charAt(0) == '#'){
160 if(ParseParameter
.npflag
== true) {
161 bw
.write(ParseParameter
.curpstr
);
169 if(ParseParameter
.npflag
== true) {
170 bw
.write(ParseParameter
.curpstr
);
177 } else if (textLine
.indexOf("TARGET_ARCH") != -1) {
179 if(textLine
.trim().charAt(0) == '#'){
180 if(ParseParameter
.naflag
== true) {
181 bw
.write(ParseParameter
.curastr
);
189 if(ParseParameter
.naflag
== true) {
190 bw
.write(ParseParameter
.curastr
);
197 } else if (textLine
.indexOf("TARGET") != -1) {
199 if(textLine
.trim().charAt(0) == '#'){
200 if(ParseParameter
.ntflag
== true) {
201 bw
.write(ParseParameter
.curtstr
);
209 if(ParseParameter
.ntflag
== true) {
210 bw
.write(ParseParameter
.curtstr
);
217 } else if (textLine
.indexOf("TOOL_CHAIN_CONF") != -1) {
219 if(textLine
.trim().charAt(0) == '#'){
220 if(ParseParameter
.ncflag
== true) {
221 bw
.write(ParseParameter
.curcstr
);
229 if(ParseParameter
.ncflag
== true) {
230 bw
.write(ParseParameter
.curcstr
);
237 } else if (textLine
.indexOf("TOOL_CHAIN_TAG") != -1) {
239 if(textLine
.trim().charAt(0) == '#'){
240 if(ParseParameter
.nnflag
== true) {
241 bw
.write(ParseParameter
.curnstr
);
249 if(ParseParameter
.nnflag
== true) {
250 bw
.write(ParseParameter
.curnstr
);
257 } else if (textLine
.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
259 if(textLine
.trim().charAt(0) == '#'){
260 if(ParseParameter
.nmflag
== true) {
261 bw
.write(ParseParameter
.curmstr
);
269 if(ParseParameter
.nmflag
== true) {
270 bw
.write(ParseParameter
.curmstr
);
277 }else if (textLine
.indexOf("MULTIPLE_THREAD") != -1) {
279 if(textLine
.trim().charAt(0) == '#'){
280 if(ParseParameter
.nmeflag
== true) {
281 bw
.write(ParseParameter
.curmestr
);
289 if(ParseParameter
.nmeflag
== true) {
290 bw
.write(ParseParameter
.curmestr
);
301 //user maybe delete the line *ACTIVE_PLATFORM*=*
303 if( (pflag
== true) && (ParseParameter
.npflag
== true) ){
304 bw
.write(ParseParameter
.curpstr
);
306 } else if ( (tflag
== true) && (ParseParameter
.ntflag
== true) ){
307 bw
.write(ParseParameter
.curtstr
);
309 } else if ( (aflag
== true) && (ParseParameter
.naflag
== true) ){
310 bw
.write(ParseParameter
.curastr
);
312 } else if ( (cflag
== true) && (ParseParameter
.ncflag
== true) ){
313 bw
.write(ParseParameter
.curcstr
);
315 } else if ( (nflag
== true) && (ParseParameter
.nnflag
== true) ){
316 bw
.write(ParseParameter
.curnstr
);
318 } else if ( (meflag
== true) && (ParseParameter
.nmeflag
== true) ){
319 bw
.write(ParseParameter
.curmestr
);
321 } else if ( (mflag
== true) && (ParseParameter
.nmflag
== true) ){
322 bw
.write(ParseParameter
.curmstr
);
325 } catch (IOException e
) {
326 System
.out
.println("\n# read or write file error!");
333 } catch (IOException e
) {
335 .println("\n# close BufferedReader&BufferedWriter error");
339 if (Fd
.delete() == false) {
340 System
.out
.println("\n# delete file failed !");
343 if (TempFd
.renameTo(Fd
) == false) {
344 System
.out
.println("\n# rename file failed !");
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
356 public static boolean readFile() {
358 BufferedReader br
= null;
359 String textLine
= null;
362 br
= new BufferedReader(new FileReader(Fd
));
363 } catch (FileNotFoundException e
) {
365 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
369 while ((textLine
= br
.readLine()) != null) {
371 // the line is composed of Space
373 if (textLine
.trim().compareToIgnoreCase("") == 0) {
377 // the line starts with "#"
379 else if ((textLine
.trim().charAt(0) == '#')){
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('=');
406 } catch (IOException e
) {
407 System
.out
.println("\n# read file error!");
413 } catch (IOException e
) {
415 .println("\n# close BufferedReader error");
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
427 private static boolean writeFile(File fd
) {
429 if (fd
.canWrite() != true)
432 FileOutputStream outputFile
= null;
434 outputFile
= new FileOutputStream(fd
);
435 } catch (FileNotFoundException e
) {
437 .println("\n# can't find the file when open the output stream !");
440 FileChannel outputChannel
= outputFile
.getChannel();
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());
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();
460 ByteBuffer bufofCP
= ByteBuffer
.allocate(Copyright
.length());
461 bufofCP
.put(Copyright
.getBytes()).flip();
462 outputChannel
.write(bufofCP
);
464 ByteBuffer bufofFI
= ByteBuffer
.allocate(Fileinfo
.length());
465 bufofFI
.put(Fileinfo
.getBytes()).flip();
466 outputChannel
.write(bufofFI
);
468 ByteBuffer buffer0
= ByteBuffer
.allocate(pusage
.length());
469 buffer0
.put(pusage
.getBytes()).flip();
470 outputChannel
.write(buffer0
);
471 outputChannel
.write(buffers
[0]);
473 ByteBuffer buffer1
= ByteBuffer
.allocate(tusage
.length());
474 buffer1
.put(tusage
.getBytes()).flip();
475 outputChannel
.write(buffer1
);
476 outputChannel
.write(buffers
[1]);
478 ByteBuffer buffer2
= ByteBuffer
.allocate(ausage
.length());
479 buffer2
.put(ausage
.getBytes()).flip();
480 outputChannel
.write(buffer2
);
481 outputChannel
.write(buffers
[2]);
483 ByteBuffer buffer3
= ByteBuffer
.allocate(cusage
.length());
484 buffer3
.put(cusage
.getBytes()).flip();
485 outputChannel
.write(buffer3
);
486 outputChannel
.write(buffers
[3]);
488 ByteBuffer buffer4
= ByteBuffer
.allocate(nusage
.length());
489 buffer4
.put(nusage
.getBytes()).flip();
490 outputChannel
.write(buffer4
);
491 outputChannel
.write(buffers
[4]);
493 ByteBuffer buffer5
= ByteBuffer
.allocate(meusage
.length());
494 buffer4
.put(meusage
.getBytes()).flip();
495 outputChannel
.write(buffer5
);
496 outputChannel
.write(buffers
[5]);
498 ByteBuffer buffer6
= ByteBuffer
.allocate(musage
.length());
499 buffer4
.put(musage
.getBytes()).flip();
500 outputChannel
.write(buffer6
);
501 outputChannel
.write(buffers
[6]);
504 } catch (IOException e
) {
505 System
.out
.println("\n# The operations of file failed !");
512 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
514 private static File TempFd
;
517 /// point to target.txt.
519 private static File Fd
;
522 /// when the flag is true, the corresponding str should be add at the end of file.
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;
532 private static final String Copyright
= "#\n"
533 + "# Copyright (c) 2006, Intel Corporation\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"
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";
543 private static final String Fileinfo
= "#\n"
544 + "# Filename: target.template\n"
546 + "# ALL Paths are Relative to WORKSPACE\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";
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";
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"
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"
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";
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";
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";