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
.curpstr
.length() >= ParseParameter
.plength
) {
161 bw
.write(ParseParameter
.curpstr
);
167 if(ParseParameter
.curpstr
.length() >= ParseParameter
.plength
) {
168 bw
.write(ParseParameter
.curpstr
);
175 } else if (textLine
.indexOf("TARGET_ARCH") != -1) {
177 if(textLine
.trim().charAt(0) == '#'){
178 if(ParseParameter
.curastr
.length() >= ParseParameter
.alength
) {
179 bw
.write(ParseParameter
.curastr
);
185 if(ParseParameter
.curastr
.length() >= ParseParameter
.alength
) {
186 bw
.write(ParseParameter
.curastr
);
193 } else if (textLine
.indexOf("TARGET") != -1) {
195 if(textLine
.trim().charAt(0) == '#'){
196 if(ParseParameter
.curtstr
.length() >= ParseParameter
.tlength
) {
197 bw
.write(ParseParameter
.curtstr
);
203 if(ParseParameter
.curtstr
.length() >= ParseParameter
.tlength
) {
204 bw
.write(ParseParameter
.curtstr
);
211 } else if (textLine
.indexOf("TOOL_CHAIN_CONF") != -1) {
213 if(textLine
.trim().charAt(0) == '#'){
214 if(ParseParameter
.curcstr
.length() >= ParseParameter
.clength
) {
215 bw
.write(ParseParameter
.curcstr
);
221 if(ParseParameter
.curcstr
.length() >= ParseParameter
.clength
) {
222 bw
.write(ParseParameter
.curcstr
);
229 } else if (textLine
.indexOf("TOOL_CHAIN_TAG") != -1) {
231 if(textLine
.trim().charAt(0) == '#'){
232 if(ParseParameter
.curnstr
.length() >= ParseParameter
.nlength
) {
233 bw
.write(ParseParameter
.curnstr
);
239 if(ParseParameter
.curnstr
.length() >= ParseParameter
.nlength
) {
240 bw
.write(ParseParameter
.curnstr
);
247 } else if (textLine
.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
249 if(textLine
.trim().charAt(0) == '#'){
250 if(ParseParameter
.curmstr
.length() >= ParseParameter
.mlength
) {
251 bw
.write(ParseParameter
.curmstr
);
257 if(ParseParameter
.curmstr
.length() >= ParseParameter
.mlength
) {
258 bw
.write(ParseParameter
.curmstr
);
265 }else if (textLine
.indexOf("MULTIPLE_THREAD") != -1) {
267 if(textLine
.trim().charAt(0) == '#'){
268 if(ParseParameter
.curmestr
.length() >= ParseParameter
.melength
) {
269 bw
.write(ParseParameter
.curmestr
);
275 if(ParseParameter
.curmestr
.length() >= ParseParameter
.melength
) {
276 bw
.write(ParseParameter
.curmestr
);
287 //user maybe delete the line *ACTIVE_PLATFORM*=*
289 if( (pflag
== true) && (ParseParameter
.curpstr
.length() >= ParseParameter
.plength
) ){
290 bw
.write(ParseParameter
.curpstr
);
292 } else if ( (tflag
== true) && (ParseParameter
.curtstr
.length() >= ParseParameter
.tlength
) ){
293 bw
.write(ParseParameter
.curtstr
);
295 } else if ( (aflag
== true) && (ParseParameter
.curastr
.length() >= ParseParameter
.alength
) ){
296 bw
.write(ParseParameter
.curastr
);
298 } else if ( (cflag
== true) && (ParseParameter
.curcstr
.length() >= ParseParameter
.clength
) ){
299 bw
.write(ParseParameter
.curcstr
);
301 } else if ( (nflag
== true) && (ParseParameter
.curnstr
.length() >= ParseParameter
.nlength
) ){
302 bw
.write(ParseParameter
.curnstr
);
304 } else if ( (meflag
== true) && (ParseParameter
.curmestr
.length() >= ParseParameter
.melength
) ){
305 bw
.write(ParseParameter
.curmestr
);
307 } else if ( (mflag
== true) && (ParseParameter
.curmstr
.length() >= ParseParameter
.mlength
) ){
308 bw
.write(ParseParameter
.curmstr
);
311 } catch (IOException e
) {
312 System
.out
.println("\n# read or write file error!");
319 } catch (IOException e
) {
321 .println("\n# close BufferedReader&BufferedWriter error");
325 if (Fd
.delete() == false) {
326 System
.out
.println("\n# delete file failed !");
329 if (TempFd
.renameTo(Fd
) == false) {
330 System
.out
.println("\n# rename file failed !");
338 * read the file and output the lines which include setting
339 * @param File fd : the File of the target file
340 * @return String: the current setting
342 public static boolean readFile() {
344 BufferedReader br
= null;
345 String textLine
= null;
348 br
= new BufferedReader(new FileReader(Fd
));
349 } catch (FileNotFoundException e
) {
351 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
355 while ((textLine
= br
.readLine()) != null) {
357 // the line is composed of Space
359 if (textLine
.trim().compareToIgnoreCase("") == 0) {
363 // the line starts with "#"
365 else if ((textLine
.trim().charAt(0) == '#')){
368 if (textLine
.indexOf("ACTIVE_PLATFORM") != -1) {
369 ParseParameter
.curpstr
= textLine
;
370 ParseParameter
.plength
= textLine
.indexOf('=');
371 } else if (textLine
.indexOf("TARGET_ARCH") != -1) {
372 ParseParameter
.curastr
= textLine
;
373 ParseParameter
.alength
= textLine
.indexOf('=');
374 } else if (textLine
.indexOf("TARGET") != -1) {
375 ParseParameter
.curtstr
= textLine
;
376 ParseParameter
.tlength
= textLine
.indexOf('=');
377 } else if (textLine
.indexOf("TOOL_CHAIN_CONF") != -1) {
378 ParseParameter
.curcstr
= textLine
;
379 ParseParameter
.clength
= textLine
.indexOf('=');
380 } else if (textLine
.indexOf("TOOL_CHAIN_TAG") != -1) {
381 ParseParameter
.curnstr
= textLine
;
382 ParseParameter
.nlength
= textLine
.indexOf('=');
383 } else if (textLine
.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
384 ParseParameter
.curmstr
= textLine
;
385 ParseParameter
.mlength
= textLine
.indexOf('=');
386 } else if (textLine
.indexOf("MULTIPLE_THREAD") != -1) {
387 ParseParameter
.curmestr
= textLine
;
388 ParseParameter
.melength
= textLine
.indexOf('=');
392 } catch (IOException e
) {
393 System
.out
.println("\n# read file error!");
399 } catch (IOException e
) {
401 .println("\n# close BufferedReader error");
407 private static String
convertStr(String str
){
408 String convertStr
= null;
410 if( str
.compareTo("-p") == 0 ){
411 convertStr
= "ACTIVE_PLATFORM";
412 }else if( str
.compareTo("-a") == 0){
413 convertStr
= "TARGET_ARCH";
414 }else if( str
.compareTo("-t") == 0){
415 convertStr
= "TARGET";
416 }else if( str
.compareTo("-c") == 0){
417 convertStr
= "TOOL_CHAIN_CONF";
418 }else if( str
.compareTo("-n") == 0){
419 convertStr
= "TOOL_CHAIN_TAG";
420 }else if( str
.compareTo("-m") == 0){
421 convertStr
= "MAX_CONCURRENT_THREAD_NUMBER";
429 * according to user's input args, write the file directly
430 * @param File fd : the File of the target file
431 * @return true or false
433 private static boolean writeFile(File fd
) {
435 if (fd
.canWrite() != true)
438 FileOutputStream outputFile
= null;
440 outputFile
= new FileOutputStream(fd
);
441 } catch (FileNotFoundException e
) {
443 .println("\n# can't find the file when open the output stream !");
446 FileChannel outputChannel
= outputFile
.getChannel();
448 ByteBuffer
[] buffers
= new ByteBuffer
[7];
449 buffers
[0] = ByteBuffer
.allocate(ParseParameter
.pstr
.toString().length());
450 buffers
[1] = ByteBuffer
.allocate(ParseParameter
.tstr
.toString().length());
451 buffers
[2] = ByteBuffer
.allocate(ParseParameter
.astr
.toString().length());
452 buffers
[3] = ByteBuffer
.allocate(ParseParameter
.cstr
.toString().length());
453 buffers
[4] = ByteBuffer
.allocate(ParseParameter
.nstr
.toString().length());
454 buffers
[5] = ByteBuffer
.allocate(ParseParameter
.mestr
.toString().length());
455 buffers
[6] = ByteBuffer
.allocate(ParseParameter
.mstr
.toString().length());
457 buffers
[0].put(ParseParameter
.pstr
.toString().getBytes()).flip();
458 buffers
[1].put(ParseParameter
.tstr
.toString().getBytes()).flip();
459 buffers
[2].put(ParseParameter
.astr
.toString().getBytes()).flip();
460 buffers
[3].put(ParseParameter
.cstr
.toString().getBytes()).flip();
461 buffers
[4].put(ParseParameter
.nstr
.toString().getBytes()).flip();
462 buffers
[5].put(ParseParameter
.mestr
.toString().getBytes()).flip();
463 buffers
[6].put(ParseParameter
.mstr
.toString().getBytes()).flip();
466 ByteBuffer bufofCP
= ByteBuffer
.allocate(Copyright
.length());
467 bufofCP
.put(Copyright
.getBytes()).flip();
468 outputChannel
.write(bufofCP
);
470 ByteBuffer bufofFI
= ByteBuffer
.allocate(Fileinfo
.length());
471 bufofFI
.put(Fileinfo
.getBytes()).flip();
472 outputChannel
.write(bufofFI
);
474 ByteBuffer buffer0
= ByteBuffer
.allocate(pusage
.length());
475 buffer0
.put(pusage
.getBytes()).flip();
476 outputChannel
.write(buffer0
);
477 outputChannel
.write(buffers
[0]);
479 ByteBuffer buffer1
= ByteBuffer
.allocate(tusage
.length());
480 buffer1
.put(tusage
.getBytes()).flip();
481 outputChannel
.write(buffer1
);
482 outputChannel
.write(buffers
[1]);
484 ByteBuffer buffer2
= ByteBuffer
.allocate(ausage
.length());
485 buffer2
.put(ausage
.getBytes()).flip();
486 outputChannel
.write(buffer2
);
487 outputChannel
.write(buffers
[2]);
489 ByteBuffer buffer3
= ByteBuffer
.allocate(cusage
.length());
490 buffer3
.put(cusage
.getBytes()).flip();
491 outputChannel
.write(buffer3
);
492 outputChannel
.write(buffers
[3]);
494 ByteBuffer buffer4
= ByteBuffer
.allocate(nusage
.length());
495 buffer4
.put(nusage
.getBytes()).flip();
496 outputChannel
.write(buffer4
);
497 outputChannel
.write(buffers
[4]);
499 ByteBuffer buffer5
= ByteBuffer
.allocate(meusage
.length());
500 buffer4
.put(meusage
.getBytes()).flip();
501 outputChannel
.write(buffer5
);
502 outputChannel
.write(buffers
[5]);
504 ByteBuffer buffer6
= ByteBuffer
.allocate(musage
.length());
505 buffer4
.put(musage
.getBytes()).flip();
506 outputChannel
.write(buffer6
);
507 outputChannel
.write(buffers
[6]);
510 } catch (IOException e
) {
511 System
.out
.println("\n# The operations of file failed !");
518 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
520 private static File TempFd
;
523 /// point to target.txt.
525 private static File Fd
;
528 /// when the flag is true, the corresponding str should be add at the end of file.
530 private static boolean pflag
= true;
531 private static boolean tflag
= true;
532 private static boolean aflag
= true;
533 private static boolean cflag
= true;
534 private static boolean nflag
= true;
535 private static boolean mflag
= true;
536 private static boolean meflag
= true;
538 private static final String Copyright
= "#\n"
539 + "# Copyright (c) 2006, Intel Corporation\n"
541 + "# All rights reserved. This program and the accompanying materials\n"
542 + "# are licensed and made available under the terms and conditions of the BSD License\n"
543 + "# which accompanies this distribution. The full text of the license may be found at\n"
544 + "# http://opensource.org/licenses/bsd-license.php\n"
546 + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"
547 + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";
549 private static final String Fileinfo
= "#\n"
550 + "# Filename: target.template\n"
552 + "# ALL Paths are Relative to WORKSPACE\n"
554 + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"
555 + "# Un-set an option by either commenting out the line, or not setting a value.\n";
557 private static final String pusage
= "#\n"
558 + "# PROPERTY Type Use Description\n"
559 + "# ---------------- -------- -------- -----------------------------------------------------------\n"
560 + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"
561 + "# of the platform FPD file that will be used for the build\n"
562 + "# This line is required if and only if the current working\n"
563 + "# directory does not contain one or more FPD files.\n";
565 private static final String tusage
= "\n\n"
566 + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"
567 + "# UserDefined; separated by a space character. \n"
568 + "# If the line is missing or no value is specified, all\n"
569 + "# valid targets specified in the FPD file will attempt \n"
570 + "# to be built. The following line will build all platform\n"
573 private static final String ausage
= "\n\n"
574 + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"
575 + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"
576 + "# Multiple values can be specified on a single line, using \n"
577 + "# space charaters to separate the values. These are used \n"
578 + "# during the parsing of an FPD file, restricting the build\n"
579 + "# output target(s.)\n"
580 + "# The Build Target ARCH is determined by a logical AND of:\n"
581 + "# FPD BuildOptions: <SupportedArchitectures> tag\n"
582 + "# If not specified, then all valid architectures specified \n"
583 + "# in the FPD file, for which tools are available, will be \n"
586 private static final String cusage
= "\n\n"
587 + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"
588 + "# the tools to use for the build. If not specified, \n"
589 + "# tools_def.txt will be used for the build. This file \n"
590 + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";
592 private static final String nusage
= "\n\n"
593 + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"
594 + "# If not specified, all applicable TagName tools will be \n"
595 + "# used for the build. The list uses space character separation.\n";
597 private static final String musage
= "\n\n"
598 + "# MULTIPLE_THREAD FLAG Optional Flag to enable multi-thread build. If not specified, default\n"
599 + "# is \"Disable\". If your computer is multi-core or multiple CPUs,\n"
600 + "# enabling this feature will bring much benefit. For multi-thread\n"
601 + "# built, the log will write to ${BUILD_DIR}/build.log.\n"
602 + "# This feature is only for PLATFORM build, and clean, cleanall or\n"
603 + "# stand-alone module build is still using the normal way.\n";
604 private static final String meusage
= "\n\n"
605 + "# MAX_CONCURRENT_THREAD_NUMBER NUMBER Optional The number of concurrent threads. Default is 2. Recommend to\n"
606 + "# set this value to one more than the number of your compurter\n"
607 + "# cores or CPUs.\n";