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");
421 private static String
convertStr(String str
){
422 String convertStr
= null;
424 if( str
.compareTo("-p") == 0 ){
425 convertStr
= "ACTIVE_PLATFORM";
426 }else if( str
.compareTo("-a") == 0){
427 convertStr
= "TARGET_ARCH";
428 }else if( str
.compareTo("-t") == 0){
429 convertStr
= "TARGET";
430 }else if( str
.compareTo("-c") == 0){
431 convertStr
= "TOOL_CHAIN_CONF";
432 }else if( str
.compareTo("-n") == 0){
433 convertStr
= "TOOL_CHAIN_TAG";
434 }else if( str
.compareTo("-m") == 0){
435 convertStr
= "MAX_CONCURRENT_THREAD_NUMBER";
443 * according to user's input args, write the file directly
444 * @param File fd : the File of the target file
445 * @return true or false
447 private static boolean writeFile(File fd
) {
449 if (fd
.canWrite() != true)
452 FileOutputStream outputFile
= null;
454 outputFile
= new FileOutputStream(fd
);
455 } catch (FileNotFoundException e
) {
457 .println("\n# can't find the file when open the output stream !");
460 FileChannel outputChannel
= outputFile
.getChannel();
462 ByteBuffer
[] buffers
= new ByteBuffer
[7];
463 buffers
[0] = ByteBuffer
.allocate(ParseParameter
.pstr
.toString().length());
464 buffers
[1] = ByteBuffer
.allocate(ParseParameter
.tstr
.toString().length());
465 buffers
[2] = ByteBuffer
.allocate(ParseParameter
.astr
.toString().length());
466 buffers
[3] = ByteBuffer
.allocate(ParseParameter
.cstr
.toString().length());
467 buffers
[4] = ByteBuffer
.allocate(ParseParameter
.nstr
.toString().length());
468 buffers
[5] = ByteBuffer
.allocate(ParseParameter
.mestr
.toString().length());
469 buffers
[6] = ByteBuffer
.allocate(ParseParameter
.mstr
.toString().length());
471 buffers
[0].put(ParseParameter
.pstr
.toString().getBytes()).flip();
472 buffers
[1].put(ParseParameter
.tstr
.toString().getBytes()).flip();
473 buffers
[2].put(ParseParameter
.astr
.toString().getBytes()).flip();
474 buffers
[3].put(ParseParameter
.cstr
.toString().getBytes()).flip();
475 buffers
[4].put(ParseParameter
.nstr
.toString().getBytes()).flip();
476 buffers
[5].put(ParseParameter
.mestr
.toString().getBytes()).flip();
477 buffers
[6].put(ParseParameter
.mstr
.toString().getBytes()).flip();
480 ByteBuffer bufofCP
= ByteBuffer
.allocate(Copyright
.length());
481 bufofCP
.put(Copyright
.getBytes()).flip();
482 outputChannel
.write(bufofCP
);
484 ByteBuffer bufofFI
= ByteBuffer
.allocate(Fileinfo
.length());
485 bufofFI
.put(Fileinfo
.getBytes()).flip();
486 outputChannel
.write(bufofFI
);
488 ByteBuffer buffer0
= ByteBuffer
.allocate(pusage
.length());
489 buffer0
.put(pusage
.getBytes()).flip();
490 outputChannel
.write(buffer0
);
491 outputChannel
.write(buffers
[0]);
493 ByteBuffer buffer1
= ByteBuffer
.allocate(tusage
.length());
494 buffer1
.put(tusage
.getBytes()).flip();
495 outputChannel
.write(buffer1
);
496 outputChannel
.write(buffers
[1]);
498 ByteBuffer buffer2
= ByteBuffer
.allocate(ausage
.length());
499 buffer2
.put(ausage
.getBytes()).flip();
500 outputChannel
.write(buffer2
);
501 outputChannel
.write(buffers
[2]);
503 ByteBuffer buffer3
= ByteBuffer
.allocate(cusage
.length());
504 buffer3
.put(cusage
.getBytes()).flip();
505 outputChannel
.write(buffer3
);
506 outputChannel
.write(buffers
[3]);
508 ByteBuffer buffer4
= ByteBuffer
.allocate(nusage
.length());
509 buffer4
.put(nusage
.getBytes()).flip();
510 outputChannel
.write(buffer4
);
511 outputChannel
.write(buffers
[4]);
513 ByteBuffer buffer5
= ByteBuffer
.allocate(meusage
.length());
514 buffer4
.put(meusage
.getBytes()).flip();
515 outputChannel
.write(buffer5
);
516 outputChannel
.write(buffers
[5]);
518 ByteBuffer buffer6
= ByteBuffer
.allocate(musage
.length());
519 buffer4
.put(musage
.getBytes()).flip();
520 outputChannel
.write(buffer6
);
521 outputChannel
.write(buffers
[6]);
524 } catch (IOException e
) {
525 System
.out
.println("\n# The operations of file failed !");
532 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
534 private static File TempFd
;
537 /// point to target.txt.
539 private static File Fd
;
542 /// when the flag is true, the corresponding str should be add at the end of file.
544 private static boolean pflag
= true;
545 private static boolean tflag
= true;
546 private static boolean aflag
= true;
547 private static boolean cflag
= true;
548 private static boolean nflag
= true;
549 private static boolean mflag
= true;
550 private static boolean meflag
= true;
552 private static final String Copyright
= "#\n"
553 + "# Copyright (c) 2006, Intel Corporation\n"
555 + "# All rights reserved. This program and the accompanying materials\n"
556 + "# are licensed and made available under the terms and conditions of the BSD License\n"
557 + "# which accompanies this distribution. The full text of the license may be found at\n"
558 + "# http://opensource.org/licenses/bsd-license.php\n"
560 + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"
561 + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";
563 private static final String Fileinfo
= "#\n"
564 + "# Filename: target.template\n"
566 + "# ALL Paths are Relative to WORKSPACE\n"
568 + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"
569 + "# Un-set an option by either commenting out the line, or not setting a value.\n";
571 private static final String pusage
= "#\n"
572 + "# PROPERTY Type Use Description\n"
573 + "# ---------------- -------- -------- -----------------------------------------------------------\n"
574 + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"
575 + "# of the platform FPD file that will be used for the build\n"
576 + "# This line is required if and only if the current working\n"
577 + "# directory does not contain one or more FPD files.\n";
579 private static final String tusage
= "\n\n"
580 + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"
581 + "# UserDefined; separated by a space character. \n"
582 + "# If the line is missing or no value is specified, all\n"
583 + "# valid targets specified in the FPD file will attempt \n"
584 + "# to be built. The following line will build all platform\n"
587 private static final String ausage
= "\n\n"
588 + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"
589 + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"
590 + "# Multiple values can be specified on a single line, using \n"
591 + "# space charaters to separate the values. These are used \n"
592 + "# during the parsing of an FPD file, restricting the build\n"
593 + "# output target(s.)\n"
594 + "# The Build Target ARCH is determined by a logical AND of:\n"
595 + "# FPD BuildOptions: <SupportedArchitectures> tag\n"
596 + "# If not specified, then all valid architectures specified \n"
597 + "# in the FPD file, for which tools are available, will be \n"
600 private static final String cusage
= "\n\n"
601 + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"
602 + "# the tools to use for the build. If not specified, \n"
603 + "# tools_def.txt will be used for the build. This file \n"
604 + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";
606 private static final String nusage
= "\n\n"
607 + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"
608 + "# If not specified, all applicable TagName tools will be \n"
609 + "# used for the build. The list uses space character separation.\n";
611 private static final String musage
= "\n\n"
612 + "# MULTIPLE_THREAD FLAG Optional Flag to enable multi-thread build. If not specified, default\n"
613 + "# is \"Disable\". If your computer is multi-core or multiple CPUs,\n"
614 + "# enabling this feature will bring much benefit. For multi-thread\n"
615 + "# built, the log will write to ${BUILD_DIR}/build.log.\n"
616 + "# This feature is only for PLATFORM build, and clean, cleanall or\n"
617 + "# stand-alone module build is still using the normal way.\n";
618 private static final String meusage
= "\n\n"
619 + "# MAX_CONCURRENT_THREAD_NUMBER NUMBER Optional The number of concurrent threads. Default is 2. Recommend to\n"
620 + "# set this value to one more than the number of your compurter\n"
621 + "# cores or CPUs.\n";