]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/ContextTool/org/tianocore/context/TargetFile.java
add some features:
[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.curpstr.length() >= ParseParameter.plength) {
161 bw.write(ParseParameter.curpstr);
162 bw.newLine();
163 pflag = false;
164 }
165 continue;
166 }
167 if(ParseParameter.curpstr.length() >= ParseParameter.plength) {
168 bw.write(ParseParameter.curpstr);
169 } else {
170 bw.write(textLine);
171 }
172 bw.newLine();
173 pflag = false;
174 }
175 } else if (textLine.indexOf("TARGET_ARCH") != -1) {
176 if(aflag == true){
177 if(textLine.trim().charAt(0) == '#'){
178 if(ParseParameter.curastr.length() >= ParseParameter.alength) {
179 bw.write(ParseParameter.curastr);
180 bw.newLine();
181 aflag = false;
182 }
183 continue;
184 }
185 if(ParseParameter.curastr.length() >= ParseParameter.alength) {
186 bw.write(ParseParameter.curastr);
187 } else {
188 bw.write(textLine);
189 }
190 bw.newLine();
191 aflag = false;
192 }
193 } else if (textLine.indexOf("TARGET") != -1) {
194 if(tflag == true){
195 if(textLine.trim().charAt(0) == '#'){
196 if(ParseParameter.curtstr.length() >= ParseParameter.tlength) {
197 bw.write(ParseParameter.curtstr);
198 bw.newLine();
199 tflag = false;
200 }
201 continue;
202 }
203 if(ParseParameter.curtstr.length() >= ParseParameter.tlength) {
204 bw.write(ParseParameter.curtstr);
205 } else {
206 bw.write(textLine);
207 }
208 bw.newLine();
209 tflag = false;
210 }
211 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {
212 if(cflag == true){
213 if(textLine.trim().charAt(0) == '#'){
214 if(ParseParameter.curcstr.length() >= ParseParameter.clength) {
215 bw.write(ParseParameter.curcstr);
216 bw.newLine();
217 cflag = false;
218 }
219 continue;
220 }
221 if(ParseParameter.curcstr.length() >= ParseParameter.clength) {
222 bw.write(ParseParameter.curcstr);
223 } else {
224 bw.write(textLine);
225 }
226 bw.newLine();
227 cflag = false;
228 }
229 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {
230 if(nflag == true){
231 if(textLine.trim().charAt(0) == '#'){
232 if(ParseParameter.curnstr.length() >= ParseParameter.nlength) {
233 bw.write(ParseParameter.curnstr);
234 bw.newLine();
235 nflag = false;
236 }
237 continue;
238 }
239 if(ParseParameter.curnstr.length() >= ParseParameter.nlength) {
240 bw.write(ParseParameter.curnstr);
241 } else {
242 bw.write(textLine);
243 }
244 bw.newLine();
245 nflag = false;
246 }
247 } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
248 if(mflag == true){
249 if(textLine.trim().charAt(0) == '#'){
250 if(ParseParameter.curmstr.length() >= ParseParameter.mlength) {
251 bw.write(ParseParameter.curmstr);
252 bw.newLine();
253 mflag = false;
254 }
255 continue;
256 }
257 if(ParseParameter.curmstr.length() >= ParseParameter.mlength) {
258 bw.write(ParseParameter.curmstr);
259 } else {
260 bw.write(textLine);
261 }
262 bw.newLine();
263 mflag = false;
264 }
265 }else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {
266 if(meflag == true){
267 if(textLine.trim().charAt(0) == '#'){
268 if(ParseParameter.curmestr.length() >= ParseParameter.melength) {
269 bw.write(ParseParameter.curmestr);
270 bw.newLine();
271 meflag = false;
272 }
273 continue;
274 }
275 if(ParseParameter.curmestr.length() >= ParseParameter.melength) {
276 bw.write(ParseParameter.curmestr);
277 } else {
278 bw.write(textLine);
279 }
280 bw.newLine();
281 meflag = false;
282 }
283 }
284 }
285 }
286 //
287 //user maybe delete the line *ACTIVE_PLATFORM*=*
288 //
289 if( (pflag == true) && (ParseParameter.curpstr.length() >= ParseParameter.plength) ){
290 bw.write(ParseParameter.curpstr);
291 bw.newLine();
292 } else if ( (tflag == true) && (ParseParameter.curtstr.length() >= ParseParameter.tlength) ){
293 bw.write(ParseParameter.curtstr);
294 bw.newLine();
295 } else if ( (aflag == true) && (ParseParameter.curastr.length() >= ParseParameter.alength) ){
296 bw.write(ParseParameter.curastr);
297 bw.newLine();
298 } else if ( (cflag == true) && (ParseParameter.curcstr.length() >= ParseParameter.clength) ){
299 bw.write(ParseParameter.curcstr);
300 bw.newLine();
301 } else if ( (nflag == true) && (ParseParameter.curnstr.length() >= ParseParameter.nlength) ){
302 bw.write(ParseParameter.curnstr);
303 bw.newLine();
304 } else if ( (meflag == true) && (ParseParameter.curmestr.length() >= ParseParameter.melength) ){
305 bw.write(ParseParameter.curmestr);
306 bw.newLine();
307 } else if ( (mflag == true) && (ParseParameter.curmstr.length() >= ParseParameter.mlength) ){
308 bw.write(ParseParameter.curmstr);
309 bw.newLine();
310 }
311 } catch (IOException e) {
312 System.out.println("\n# read or write file error!");
313 return false;
314 }
315
316 try {
317 br.close();
318 bw.close();
319 } catch (IOException e) {
320 System.out
321 .println("\n# close BufferedReader&BufferedWriter error");
322 return false;
323 }
324
325 if (Fd.delete() == false) {
326 System.out.println("\n# delete file failed !");
327 return false;
328 }
329 if (TempFd.renameTo(Fd) == false) {
330 System.out.println("\n# rename file failed !");
331 return false;
332 }
333
334 return true;
335 }
336
337 /**
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
341 **/
342 public static boolean readFile() {
343
344 BufferedReader br = null;
345 String textLine = null;
346
347 try {
348 br = new BufferedReader(new FileReader(Fd));
349 } catch (FileNotFoundException e) {
350 System.out
351 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
352 return false;
353 }
354 try {
355 while ((textLine = br.readLine()) != null) {
356 //
357 // the line is composed of Space
358 //
359 if (textLine.trim().compareToIgnoreCase("") == 0) {
360 continue;
361 }
362 //
363 // the line starts with "#"
364 //
365 else if ((textLine.trim().charAt(0) == '#')){
366 continue;
367 } else {
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('=');
389 }
390 }
391 }
392 } catch (IOException e) {
393 System.out.println("\n# read file error!");
394 return false;
395 }
396
397 try {
398 br.close();
399 } catch (IOException e) {
400 System.out
401 .println("\n# close BufferedReader error");
402 return false;
403 }
404 return true;
405 }
406
407 private static String convertStr(String str){
408 String convertStr = null;
409
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";
422 }
423
424 return convertStr;
425 }
426
427
428 /**
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
432 **/
433 private static boolean writeFile(File fd) {
434
435 if (fd.canWrite() != true)
436 return false;
437
438 FileOutputStream outputFile = null;
439 try {
440 outputFile = new FileOutputStream(fd);
441 } catch (FileNotFoundException e) {
442 System.out
443 .println("\n# can't find the file when open the output stream !");
444 return false;
445 }
446 FileChannel outputChannel = outputFile.getChannel();
447
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());
456
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();
464
465 try {
466 ByteBuffer bufofCP = ByteBuffer.allocate(Copyright.length());
467 bufofCP.put(Copyright.getBytes()).flip();
468 outputChannel.write(bufofCP);
469
470 ByteBuffer bufofFI = ByteBuffer.allocate(Fileinfo.length());
471 bufofFI.put(Fileinfo.getBytes()).flip();
472 outputChannel.write(bufofFI);
473
474 ByteBuffer buffer0 = ByteBuffer.allocate(pusage.length());
475 buffer0.put(pusage.getBytes()).flip();
476 outputChannel.write(buffer0);
477 outputChannel.write(buffers[0]);
478
479 ByteBuffer buffer1 = ByteBuffer.allocate(tusage.length());
480 buffer1.put(tusage.getBytes()).flip();
481 outputChannel.write(buffer1);
482 outputChannel.write(buffers[1]);
483
484 ByteBuffer buffer2 = ByteBuffer.allocate(ausage.length());
485 buffer2.put(ausage.getBytes()).flip();
486 outputChannel.write(buffer2);
487 outputChannel.write(buffers[2]);
488
489 ByteBuffer buffer3 = ByteBuffer.allocate(cusage.length());
490 buffer3.put(cusage.getBytes()).flip();
491 outputChannel.write(buffer3);
492 outputChannel.write(buffers[3]);
493
494 ByteBuffer buffer4 = ByteBuffer.allocate(nusage.length());
495 buffer4.put(nusage.getBytes()).flip();
496 outputChannel.write(buffer4);
497 outputChannel.write(buffers[4]);
498
499 ByteBuffer buffer5 = ByteBuffer.allocate(meusage.length());
500 buffer4.put(meusage.getBytes()).flip();
501 outputChannel.write(buffer5);
502 outputChannel.write(buffers[5]);
503
504 ByteBuffer buffer6 = ByteBuffer.allocate(musage.length());
505 buffer4.put(musage.getBytes()).flip();
506 outputChannel.write(buffer6);
507 outputChannel.write(buffers[6]);
508
509 outputFile.close();
510 } catch (IOException e) {
511 System.out.println("\n# The operations of file failed !");
512 return false;
513 }
514 return true;
515 }
516
517 ///
518 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
519 ///
520 private static File TempFd;
521
522 ///
523 /// point to target.txt.
524 ///
525 private static File Fd;
526
527 ///
528 /// when the flag is true, the corresponding str should be add at the end of file.
529 ///
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;
537
538 private static final String Copyright = "#\n"
539 + "# Copyright (c) 2006, Intel Corporation\n"
540 + "#\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"
545 + "\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";
548
549 private static final String Fileinfo = "#\n"
550 + "# Filename: target.template\n"
551 + "#\n"
552 + "# ALL Paths are Relative to WORKSPACE\n"
553 + "\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";
556
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";
564
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"
571 + "# targets.\n";
572
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"
584 + "# built.\n";
585
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";
591
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";
596
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";
608 }