]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/ContextTool/org/tianocore/context/TargetFile.java
76c4b183d6174215c76aeba41a41ef60f6a1a975
[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.npflag == true) {
161 bw.write(ParseParameter.curpstr);
162 }else{
163 bw.write(textLine);
164 }
165 bw.newLine();
166 pflag = false;
167 continue;
168 }
169 if(ParseParameter.npflag == true) {
170 bw.write(ParseParameter.curpstr);
171 } else {
172 bw.write(textLine);
173 }
174 bw.newLine();
175 pflag = false;
176 }
177 } else if (textLine.indexOf("TARGET_ARCH") != -1) {
178 if(aflag == true){
179 if(textLine.trim().charAt(0) == '#'){
180 if(ParseParameter.naflag == true) {
181 bw.write(ParseParameter.curastr);
182 }else{
183 bw.write(textLine);
184 }
185 bw.newLine();
186 aflag = false;
187 continue;
188 }
189 if(ParseParameter.naflag == true) {
190 bw.write(ParseParameter.curastr);
191 } else {
192 bw.write(textLine);
193 }
194 bw.newLine();
195 aflag = false;
196 }
197 } else if (textLine.indexOf("TARGET") != -1) {
198 if(tflag == true){
199 if(textLine.trim().charAt(0) == '#'){
200 if(ParseParameter.ntflag == true) {
201 bw.write(ParseParameter.curtstr);
202 }else{
203 bw.write(textLine);
204 }
205 bw.newLine();
206 tflag = false;
207 continue;
208 }
209 if(ParseParameter.ntflag == true) {
210 bw.write(ParseParameter.curtstr);
211 } else {
212 bw.write(textLine);
213 }
214 bw.newLine();
215 tflag = false;
216 }
217 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {
218 if(cflag == true){
219 if(textLine.trim().charAt(0) == '#'){
220 if(ParseParameter.ncflag == true) {
221 bw.write(ParseParameter.curcstr);
222 }else{
223 bw.write(textLine);
224 }
225 bw.newLine();
226 cflag = false;
227 continue;
228 }
229 if(ParseParameter.ncflag == true) {
230 bw.write(ParseParameter.curcstr);
231 } else {
232 bw.write(textLine);
233 }
234 bw.newLine();
235 cflag = false;
236 }
237 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {
238 if(nflag == true){
239 if(textLine.trim().charAt(0) == '#'){
240 if(ParseParameter.nnflag == true) {
241 bw.write(ParseParameter.curnstr);
242 }else{
243 bw.write(textLine);
244 }
245 bw.newLine();
246 nflag = false;
247 continue;
248 }
249 if(ParseParameter.nnflag == true) {
250 bw.write(ParseParameter.curnstr);
251 } else {
252 bw.write(textLine);
253 }
254 bw.newLine();
255 nflag = false;
256 }
257 } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
258 if(mflag == true){
259 if(textLine.trim().charAt(0) == '#'){
260 if(ParseParameter.nmflag == true) {
261 bw.write(ParseParameter.curmstr);
262 }else{
263 bw.write(textLine);
264 }
265 bw.newLine();
266 mflag = false;
267 continue;
268 }
269 if(ParseParameter.nmflag == true) {
270 bw.write(ParseParameter.curmstr);
271 } else {
272 bw.write(textLine);
273 }
274 bw.newLine();
275 mflag = false;
276 }
277 }else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {
278 if(meflag == true){
279 if(textLine.trim().charAt(0) == '#'){
280 if(ParseParameter.nmeflag == true) {
281 bw.write(ParseParameter.curmestr);
282 }else{
283 bw.write(textLine);
284 }
285 bw.newLine();
286 meflag = false;
287 continue;
288 }
289 if(ParseParameter.nmeflag == true) {
290 bw.write(ParseParameter.curmestr);
291 } else {
292 bw.write(textLine);
293 }
294 bw.newLine();
295 meflag = false;
296 }
297 }
298 }
299 }
300 //
301 //user maybe delete the line *ACTIVE_PLATFORM*=*
302 //
303 if( (pflag == true) && (ParseParameter.npflag == true) ){
304 bw.write(ParseParameter.curpstr);
305 bw.newLine();
306 } else if ( (tflag == true) && (ParseParameter.ntflag == true) ){
307 bw.write(ParseParameter.curtstr);
308 bw.newLine();
309 } else if ( (aflag == true) && (ParseParameter.naflag == true) ){
310 bw.write(ParseParameter.curastr);
311 bw.newLine();
312 } else if ( (cflag == true) && (ParseParameter.ncflag == true) ){
313 bw.write(ParseParameter.curcstr);
314 bw.newLine();
315 } else if ( (nflag == true) && (ParseParameter.nnflag == true) ){
316 bw.write(ParseParameter.curnstr);
317 bw.newLine();
318 } else if ( (meflag == true) && (ParseParameter.nmeflag == true) ){
319 bw.write(ParseParameter.curmestr);
320 bw.newLine();
321 } else if ( (mflag == true) && (ParseParameter.nmflag == true) ){
322 bw.write(ParseParameter.curmstr);
323 bw.newLine();
324 }
325 } catch (IOException e) {
326 System.out.println("\n# read or write file error!");
327 return false;
328 }
329
330 try {
331 br.close();
332 bw.close();
333 } catch (IOException e) {
334 System.out
335 .println("\n# close BufferedReader&BufferedWriter error");
336 return false;
337 }
338
339 if (Fd.delete() == false) {
340 System.out.println("\n# delete file failed !");
341 return false;
342 }
343 if (TempFd.renameTo(Fd) == false) {
344 System.out.println("\n# rename file failed !");
345 return false;
346 }
347
348 return true;
349 }
350
351 /**
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
355 **/
356 public static boolean readFile() {
357
358 BufferedReader br = null;
359 String textLine = null;
360
361 try {
362 br = new BufferedReader(new FileReader(Fd));
363 } catch (FileNotFoundException e) {
364 System.out
365 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
366 return false;
367 }
368 try {
369 while ((textLine = br.readLine()) != null) {
370 //
371 // the line is composed of Space
372 //
373 if (textLine.trim().compareToIgnoreCase("") == 0) {
374 continue;
375 }
376 //
377 // the line starts with "#"
378 //
379 else if ((textLine.trim().charAt(0) == '#')){
380 continue;
381 } else {
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('=');
403 }
404 }
405 }
406 } catch (IOException e) {
407 System.out.println("\n# read file error!");
408 return false;
409 }
410
411 try {
412 br.close();
413 } catch (IOException e) {
414 System.out
415 .println("\n# close BufferedReader error");
416 return false;
417 }
418 return true;
419 }
420
421 private static String convertStr(String str){
422 String convertStr = null;
423
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";
436 }
437
438 return convertStr;
439 }
440
441
442 /**
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
446 **/
447 private static boolean writeFile(File fd) {
448
449 if (fd.canWrite() != true)
450 return false;
451
452 FileOutputStream outputFile = null;
453 try {
454 outputFile = new FileOutputStream(fd);
455 } catch (FileNotFoundException e) {
456 System.out
457 .println("\n# can't find the file when open the output stream !");
458 return false;
459 }
460 FileChannel outputChannel = outputFile.getChannel();
461
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());
470
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();
478
479 try {
480 ByteBuffer bufofCP = ByteBuffer.allocate(Copyright.length());
481 bufofCP.put(Copyright.getBytes()).flip();
482 outputChannel.write(bufofCP);
483
484 ByteBuffer bufofFI = ByteBuffer.allocate(Fileinfo.length());
485 bufofFI.put(Fileinfo.getBytes()).flip();
486 outputChannel.write(bufofFI);
487
488 ByteBuffer buffer0 = ByteBuffer.allocate(pusage.length());
489 buffer0.put(pusage.getBytes()).flip();
490 outputChannel.write(buffer0);
491 outputChannel.write(buffers[0]);
492
493 ByteBuffer buffer1 = ByteBuffer.allocate(tusage.length());
494 buffer1.put(tusage.getBytes()).flip();
495 outputChannel.write(buffer1);
496 outputChannel.write(buffers[1]);
497
498 ByteBuffer buffer2 = ByteBuffer.allocate(ausage.length());
499 buffer2.put(ausage.getBytes()).flip();
500 outputChannel.write(buffer2);
501 outputChannel.write(buffers[2]);
502
503 ByteBuffer buffer3 = ByteBuffer.allocate(cusage.length());
504 buffer3.put(cusage.getBytes()).flip();
505 outputChannel.write(buffer3);
506 outputChannel.write(buffers[3]);
507
508 ByteBuffer buffer4 = ByteBuffer.allocate(nusage.length());
509 buffer4.put(nusage.getBytes()).flip();
510 outputChannel.write(buffer4);
511 outputChannel.write(buffers[4]);
512
513 ByteBuffer buffer5 = ByteBuffer.allocate(meusage.length());
514 buffer4.put(meusage.getBytes()).flip();
515 outputChannel.write(buffer5);
516 outputChannel.write(buffers[5]);
517
518 ByteBuffer buffer6 = ByteBuffer.allocate(musage.length());
519 buffer4.put(musage.getBytes()).flip();
520 outputChannel.write(buffer6);
521 outputChannel.write(buffers[6]);
522
523 outputFile.close();
524 } catch (IOException e) {
525 System.out.println("\n# The operations of file failed !");
526 return false;
527 }
528 return true;
529 }
530
531 ///
532 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
533 ///
534 private static File TempFd;
535
536 ///
537 /// point to target.txt.
538 ///
539 private static File Fd;
540
541 ///
542 /// when the flag is true, the corresponding str should be add at the end of file.
543 ///
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;
551
552 private static final String Copyright = "#\n"
553 + "# Copyright (c) 2006, Intel Corporation\n"
554 + "#\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"
559 + "\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";
562
563 private static final String Fileinfo = "#\n"
564 + "# Filename: target.template\n"
565 + "#\n"
566 + "# ALL Paths are Relative to WORKSPACE\n"
567 + "\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";
570
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";
578
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"
585 + "# targets.\n";
586
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"
598 + "# built.\n";
599
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";
605
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";
610
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";
622 }