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 parsePath(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
);
60 * create a empty temp file, which is located at the same directory with target file
61 * @param String filename : the name of target temp file
62 * @return true or false
64 private static boolean createTempFile(String filename
) {
66 String workspacePath
= System
.getenv("WORKSPACE");
68 TempFd
= new File(workspacePath
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ filename
);
70 if (TempFd
.exists() == true) {
71 if (TempFd
.delete() == false) {
72 System
.out
.println("\n# delete file failed !");
77 TempFd
.createNewFile();
78 } catch (IOException e
) {
79 System
.out
.printf("%n%s",
80 "Create the temp file:target.txttmp failed!");
88 * read from target.txt and write to target.txttmp, del target.txt, rename
90 * @return true or false
92 private static boolean readwriteFile() {
94 if (Fd
.canRead() != true)
97 BufferedReader br
= null;
98 BufferedWriter bw
= null;
99 String textLine
= null;
102 br
= new BufferedReader(new FileReader(Fd
));
103 } catch (FileNotFoundException e
) {
105 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
109 bw
= new BufferedWriter(new FileWriter(TempFd
));
110 } catch (IOException e
) {
111 System
.out
.println("\n# create the BufferedWriter failed!");
116 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt
119 while ((textLine
= br
.readLine()) != null) {
121 // the line is composed of Space
123 if (textLine
.trim().compareToIgnoreCase("") == 0) {
128 // the line starts with "#", and no "="
130 else if ((textLine
.trim().charAt(0) == '#') && (textLine
.indexOf("=") == -1)){
135 //modify at the first time, and there should be *ACTIVE_PLATFORM*=* in the line
137 if (textLine
.indexOf("ACTIVE_PLATFORM") != -1) {
139 if(textLine
.trim().charAt(0) == '#'){
140 if(ParseParameter
.pstr
.length() > ParseParameter
.length
) {
141 bw
.write(ParseParameter
.pstr
);
147 if(ParseParameter
.pstr
.length() > ParseParameter
.length
) {
148 bw
.write(ParseParameter
.pstr
);
155 } else if (textLine
.indexOf("TARGET_ARCH") != -1) {
157 if(textLine
.trim().charAt(0) == '#'){
158 if(ParseParameter
.astr
.length() > ParseParameter
.length
) {
159 bw
.write(ParseParameter
.astr
);
165 if(ParseParameter
.astr
.length() > ParseParameter
.length
) {
166 bw
.write(ParseParameter
.astr
);
173 } else if (textLine
.indexOf("TARGET") != -1) {
175 if(textLine
.trim().charAt(0) == '#'){
176 if(ParseParameter
.tstr
.length() > ParseParameter
.length
) {
177 bw
.write(ParseParameter
.tstr
);
183 if(ParseParameter
.tstr
.length() > ParseParameter
.length
) {
184 bw
.write(ParseParameter
.tstr
);
191 } else if (textLine
.indexOf("TOOL_CHAIN_CONF") != -1) {
193 if(textLine
.trim().charAt(0) == '#'){
194 if(ParseParameter
.cstr
.length() > ParseParameter
.length
) {
195 bw
.write(ParseParameter
.cstr
);
201 if(ParseParameter
.cstr
.length() > ParseParameter
.length
) {
202 bw
.write(ParseParameter
.cstr
);
209 } else if (textLine
.indexOf("TOOL_CHAIN_TAG") != -1) {
211 if(textLine
.trim().charAt(0) == '#'){
212 if(ParseParameter
.nstr
.length() > ParseParameter
.length
) {
213 bw
.write(ParseParameter
.nstr
);
219 if(ParseParameter
.nstr
.length() > ParseParameter
.length
) {
220 bw
.write(ParseParameter
.nstr
);
227 } else if (textLine
.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
229 if(textLine
.trim().charAt(0) == '#'){
230 if(ParseParameter
.mstr
.length() > ParseParameter
.length
) {
231 bw
.write(ParseParameter
.mstr
);
237 if(ParseParameter
.mstr
.length() > ParseParameter
.length
) {
238 bw
.write(ParseParameter
.mstr
);
245 }else if (textLine
.indexOf("MULTIPLE_THREAD") != -1) {
247 if(textLine
.trim().charAt(0) == '#'){
248 if(ParseParameter
.mestr
.length() > ParseParameter
.length
) {
249 bw
.write(ParseParameter
.mestr
);
255 if(ParseParameter
.mestr
.length() > ParseParameter
.length
) {
256 bw
.write(ParseParameter
.mestr
);
267 //user maybe delete the line *ACTIVE_PLATFORM*=*
269 if( (pflag
== true) && (ParseParameter
.pstr
.length() > ParseParameter
.length
) ){
270 bw
.write(ParseParameter
.pstr
);
272 } else if ( (tflag
== true) && (ParseParameter
.tstr
.length() > ParseParameter
.length
) ){
273 bw
.write(ParseParameter
.tstr
);
275 } else if ( (aflag
== true) && (ParseParameter
.astr
.length() > ParseParameter
.length
) ){
276 bw
.write(ParseParameter
.astr
);
278 } else if ( (cflag
== true) && (ParseParameter
.cstr
.length() > ParseParameter
.length
) ){
279 bw
.write(ParseParameter
.cstr
);
281 } else if ( (nflag
== true) && (ParseParameter
.nstr
.length() > ParseParameter
.length
) ){
282 bw
.write(ParseParameter
.nstr
);
284 } else if ( (meflag
== true) && (ParseParameter
.mestr
.length() > ParseParameter
.length
) ){
285 bw
.write(ParseParameter
.mestr
);
287 } else if ( (mflag
== true) && (ParseParameter
.mstr
.length() > ParseParameter
.length
) ){
288 bw
.write(ParseParameter
.mstr
);
291 } catch (IOException e
) {
292 System
.out
.println("\n# read or write file error!");
299 } catch (IOException e
) {
301 .println("\n# close BufferedReader&BufferedWriter error");
305 if (Fd
.delete() == false) {
306 System
.out
.println("\n# delete file failed !");
309 if (TempFd
.renameTo(Fd
) == false) {
310 System
.out
.println("\n# rename file failed !");
318 * according to user's input args, write the file directly
319 * @param File fd : the File of the target file
320 * @return true or false
322 private static boolean writeFile(File fd
) {
324 if (fd
.canWrite() != true)
327 FileOutputStream outputFile
= null;
329 outputFile
= new FileOutputStream(fd
);
330 } catch (FileNotFoundException e
) {
332 .println("\n# can't find the file when open the output stream !");
335 FileChannel outputChannel
= outputFile
.getChannel();
337 ByteBuffer
[] buffers
= new ByteBuffer
[7];
338 buffers
[0] = ByteBuffer
.allocate(ParseParameter
.pstr
.toString().length());
339 buffers
[1] = ByteBuffer
.allocate(ParseParameter
.tstr
.toString().length());
340 buffers
[2] = ByteBuffer
.allocate(ParseParameter
.astr
.toString().length());
341 buffers
[3] = ByteBuffer
.allocate(ParseParameter
.cstr
.toString().length());
342 buffers
[4] = ByteBuffer
.allocate(ParseParameter
.nstr
.toString().length());
343 buffers
[5] = ByteBuffer
.allocate(ParseParameter
.mestr
.toString().length());
344 buffers
[6] = ByteBuffer
.allocate(ParseParameter
.mstr
.toString().length());
346 buffers
[0].put(ParseParameter
.pstr
.toString().getBytes()).flip();
347 buffers
[1].put(ParseParameter
.tstr
.toString().getBytes()).flip();
348 buffers
[2].put(ParseParameter
.astr
.toString().getBytes()).flip();
349 buffers
[3].put(ParseParameter
.cstr
.toString().getBytes()).flip();
350 buffers
[4].put(ParseParameter
.nstr
.toString().getBytes()).flip();
351 buffers
[5].put(ParseParameter
.mestr
.toString().getBytes()).flip();
352 buffers
[6].put(ParseParameter
.mstr
.toString().getBytes()).flip();
355 ByteBuffer bufofCP
= ByteBuffer
.allocate(Copyright
.length());
356 bufofCP
.put(Copyright
.getBytes()).flip();
357 outputChannel
.write(bufofCP
);
359 ByteBuffer bufofFI
= ByteBuffer
.allocate(Fileinfo
.length());
360 bufofFI
.put(Fileinfo
.getBytes()).flip();
361 outputChannel
.write(bufofFI
);
363 ByteBuffer buffer0
= ByteBuffer
.allocate(pusage
.length());
364 buffer0
.put(pusage
.getBytes()).flip();
365 outputChannel
.write(buffer0
);
366 outputChannel
.write(buffers
[0]);
368 ByteBuffer buffer1
= ByteBuffer
.allocate(tusage
.length());
369 buffer1
.put(tusage
.getBytes()).flip();
370 outputChannel
.write(buffer1
);
371 outputChannel
.write(buffers
[1]);
373 ByteBuffer buffer2
= ByteBuffer
.allocate(ausage
.length());
374 buffer2
.put(ausage
.getBytes()).flip();
375 outputChannel
.write(buffer2
);
376 outputChannel
.write(buffers
[2]);
378 ByteBuffer buffer3
= ByteBuffer
.allocate(cusage
.length());
379 buffer3
.put(cusage
.getBytes()).flip();
380 outputChannel
.write(buffer3
);
381 outputChannel
.write(buffers
[3]);
383 ByteBuffer buffer4
= ByteBuffer
.allocate(nusage
.length());
384 buffer4
.put(nusage
.getBytes()).flip();
385 outputChannel
.write(buffer4
);
386 outputChannel
.write(buffers
[4]);
388 ByteBuffer buffer5
= ByteBuffer
.allocate(meusage
.length());
389 buffer4
.put(meusage
.getBytes()).flip();
390 outputChannel
.write(buffer5
);
391 outputChannel
.write(buffers
[5]);
393 ByteBuffer buffer6
= ByteBuffer
.allocate(musage
.length());
394 buffer4
.put(musage
.getBytes()).flip();
395 outputChannel
.write(buffer6
);
396 outputChannel
.write(buffers
[6]);
399 } catch (IOException e
) {
400 System
.out
.println("\n# The operations of file failed !");
407 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
409 private static File TempFd
;
412 /// point to target.txt.
414 private static File Fd
;
417 /// when the flag is true, the corresponding str should be add at the end of file.
419 private static boolean pflag
= true;
420 private static boolean tflag
= true;
421 private static boolean aflag
= true;
422 private static boolean cflag
= true;
423 private static boolean nflag
= true;
424 private static boolean mflag
= true;
425 private static boolean meflag
= true;
427 private static final String Copyright
= "#\n"
428 + "# Copyright (c) 2006, Intel Corporation\n"
430 + "# All rights reserved. This program and the accompanying materials\n"
431 + "# are licensed and made available under the terms and conditions of the BSD License\n"
432 + "# which accompanies this distribution. The full text of the license may be found at\n"
433 + "# http://opensource.org/licenses/bsd-license.php\n"
435 + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"
436 + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";
438 private static final String Fileinfo
= "#\n"
439 + "# Filename: target.template\n"
441 + "# ALL Paths are Relative to WORKSPACE\n"
443 + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"
444 + "# Un-set an option by either commenting out the line, or not setting a value.\n";
446 private static final String pusage
= "#\n"
447 + "# PROPERTY Type Use Description\n"
448 + "# ---------------- -------- -------- -----------------------------------------------------------\n"
449 + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"
450 + "# of the platform FPD file that will be used for the build\n"
451 + "# This line is required if and only if the current working\n"
452 + "# directory does not contain one or more FPD files.\n";
454 private static final String tusage
= "\n\n"
455 + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"
456 + "# UserDefined; separated by a space character. \n"
457 + "# If the line is missing or no value is specified, all\n"
458 + "# valid targets specified in the FPD file will attempt \n"
459 + "# to be built. The following line will build all platform\n"
462 private static final String ausage
= "\n\n"
463 + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"
464 + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"
465 + "# Multiple values can be specified on a single line, using \n"
466 + "# space charaters to separate the values. These are used \n"
467 + "# during the parsing of an FPD file, restricting the build\n"
468 + "# output target(s.)\n"
469 + "# The Build Target ARCH is determined by a logical AND of:\n"
470 + "# FPD BuildOptions: <SupportedArchitectures> tag\n"
471 + "# If not specified, then all valid architectures specified \n"
472 + "# in the FPD file, for which tools are available, will be \n"
475 private static final String cusage
= "\n\n"
476 + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"
477 + "# the tools to use for the build. If not specified, \n"
478 + "# tools_def.txt will be used for the build. This file \n"
479 + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";
481 private static final String nusage
= "\n\n"
482 + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"
483 + "# If not specified, all applicable TagName tools will be \n"
484 + "# used for the build. The list uses space character separation.\n";
486 private static final String musage
= "\n\n"
487 + "# MULTIPLE_THREAD FLAG Optional Flag to enable multi-thread build. If not specified, default\n"
488 + "# is \"Disable\". If your computer is multi-core or multiple CPUs,\n"
489 + "# enabling this feature will bring much benefit. For multi-thread\n"
490 + "# built, the log will write to ${BUILD_DIR}/build.log.\n"
491 + "# This feature is only for PLATFORM build, and clean, cleanall or\n"
492 + "# stand-alone module build is still using the normal way.\n";
493 private static final String meusage
= "\n\n"
494 + "# MAX_CONCURRENT_THREAD_NUMBER NUMBER Optional The number of concurrent threads. Default is 2. Recommend to\n"
495 + "# set this value to one more than the number of your compurter\n"
496 + "# cores or CPUs.\n";