To fix EDKT284.
[mirror_edk2.git] / Tools / 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 parsePath(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 * 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
63 **/
64 private static boolean createTempFile(String filename) {
65
66 String workspacePath = System.getenv("WORKSPACE");
67
68 TempFd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);
69
70 if (TempFd.exists() == true) {
71 if (TempFd.delete() == false) {
72 System.out.println("\n# delete file failed !");
73 return false;
74 }
75 }
76 try {
77 TempFd.createNewFile();
78 } catch (IOException e) {
79 System.out.printf("%n%s",
80 "Create the temp file:target.txttmp failed!");
81 return false;
82 }
83
84 return true;
85 }
86
87 /**
88 * read from target.txt and write to target.txttmp, del target.txt, rename
89 * @param no paremeter
90 * @return true or false
91 **/
92 private static boolean readwriteFile() {
93
94 if (Fd.canRead() != true)
95 return false;
96
97 BufferedReader br = null;
98 BufferedWriter bw = null;
99 String textLine = null;
100
101 try {
102 br = new BufferedReader(new FileReader(Fd));
103 } catch (FileNotFoundException e) {
104 System.out
105 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
106 return false;
107 }
108 try {
109 bw = new BufferedWriter(new FileWriter(TempFd));
110 } catch (IOException e) {
111 System.out.println("\n# create the BufferedWriter failed!");
112 return false;
113 }
114
115 //
116 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt
117 //
118 try {
119 while ((textLine = br.readLine()) != null) {
120 //
121 // the line is composed of Space
122 //
123 if (textLine.trim().compareToIgnoreCase("") == 0) {
124 bw.write(textLine);
125 bw.newLine();
126 }
127 //
128 // the line starts with "#", and no "="
129 //
130 else if ((textLine.trim().charAt(0) == '#') && (textLine.indexOf("=") == -1)){
131 bw.write(textLine);
132 bw.newLine();
133 } else {
134 //
135 //modify at the first time, and there should be *ACTIVE_PLATFORM*=* in the line
136 //
137 if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {
138 if(pflag == true){
139 if(textLine.trim().charAt(0) == '#'){
140 if(ParseParameter.pstr.length() > ParseParameter.length) {
141 bw.write(ParseParameter.pstr);
142 bw.newLine();
143 pflag = false;
144 }
145 continue;
146 }
147 if(ParseParameter.pstr.length() > ParseParameter.length) {
148 bw.write(ParseParameter.pstr);
149 } else {
150 bw.write(textLine);
151 }
152 bw.newLine();
153 pflag = false;
154 }
155 } else if (textLine.indexOf("TARGET_ARCH") != -1) {
156 if(aflag == true){
157 if(textLine.trim().charAt(0) == '#'){
158 if(ParseParameter.astr.length() > ParseParameter.length) {
159 bw.write(ParseParameter.astr);
160 bw.newLine();
161 aflag = false;
162 }
163 continue;
164 }
165 if(ParseParameter.astr.length() > ParseParameter.length) {
166 bw.write(ParseParameter.astr);
167 } else {
168 bw.write(textLine);
169 }
170 bw.newLine();
171 aflag = false;
172 }
173 } else if (textLine.indexOf("TARGET") != -1) {
174 if(tflag == true){
175 if(textLine.trim().charAt(0) == '#'){
176 if(ParseParameter.tstr.length() > ParseParameter.length) {
177 bw.write(ParseParameter.tstr);
178 bw.newLine();
179 tflag = false;
180 }
181 continue;
182 }
183 if(ParseParameter.tstr.length() > ParseParameter.length) {
184 bw.write(ParseParameter.tstr);
185 } else {
186 bw.write(textLine);
187 }
188 bw.newLine();
189 tflag = false;
190 }
191 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {
192 if(cflag == true){
193 if(textLine.trim().charAt(0) == '#'){
194 if(ParseParameter.cstr.length() > ParseParameter.length) {
195 bw.write(ParseParameter.cstr);
196 bw.newLine();
197 cflag = false;
198 }
199 continue;
200 }
201 if(ParseParameter.cstr.length() > ParseParameter.length) {
202 bw.write(ParseParameter.cstr);
203 } else {
204 bw.write(textLine);
205 }
206 bw.newLine();
207 cflag = false;
208 }
209 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {
210 if(nflag == true){
211 if(textLine.trim().charAt(0) == '#'){
212 if(ParseParameter.nstr.length() > ParseParameter.length) {
213 bw.write(ParseParameter.nstr);
214 bw.newLine();
215 nflag = false;
216 }
217 continue;
218 }
219 if(ParseParameter.nstr.length() > ParseParameter.length) {
220 bw.write(ParseParameter.nstr);
221 } else {
222 bw.write(textLine);
223 }
224 bw.newLine();
225 nflag = false;
226 }
227 } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {
228 if(mflag == true){
229 if(textLine.trim().charAt(0) == '#'){
230 if(ParseParameter.mstr.length() > ParseParameter.length) {
231 bw.write(ParseParameter.mstr);
232 bw.newLine();
233 mflag = false;
234 }
235 continue;
236 }
237 if(ParseParameter.mstr.length() > ParseParameter.length) {
238 bw.write(ParseParameter.mstr);
239 } else {
240 bw.write(textLine);
241 }
242 bw.newLine();
243 mflag = false;
244 }
245 }else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {
246 if(meflag == true){
247 if(textLine.trim().charAt(0) == '#'){
248 if(ParseParameter.mestr.length() > ParseParameter.length) {
249 bw.write(ParseParameter.mestr);
250 bw.newLine();
251 meflag = false;
252 }
253 continue;
254 }
255 if(ParseParameter.mestr.length() > ParseParameter.length) {
256 bw.write(ParseParameter.mestr);
257 } else {
258 bw.write(textLine);
259 }
260 bw.newLine();
261 meflag = false;
262 }
263 }
264 }
265 }
266 //
267 //user maybe delete the line *ACTIVE_PLATFORM*=*
268 //
269 if( (pflag == true) && (ParseParameter.pstr.length() > ParseParameter.length) ){
270 bw.write(ParseParameter.pstr);
271 bw.newLine();
272 } else if ( (tflag == true) && (ParseParameter.tstr.length() > ParseParameter.length) ){
273 bw.write(ParseParameter.tstr);
274 bw.newLine();
275 } else if ( (aflag == true) && (ParseParameter.astr.length() > ParseParameter.length) ){
276 bw.write(ParseParameter.astr);
277 bw.newLine();
278 } else if ( (cflag == true) && (ParseParameter.cstr.length() > ParseParameter.length) ){
279 bw.write(ParseParameter.cstr);
280 bw.newLine();
281 } else if ( (nflag == true) && (ParseParameter.nstr.length() > ParseParameter.length) ){
282 bw.write(ParseParameter.nstr);
283 bw.newLine();
284 } else if ( (meflag == true) && (ParseParameter.mestr.length() > ParseParameter.length) ){
285 bw.write(ParseParameter.mestr);
286 bw.newLine();
287 } else if ( (mflag == true) && (ParseParameter.mstr.length() > ParseParameter.length) ){
288 bw.write(ParseParameter.mstr);
289 bw.newLine();
290 }
291 } catch (IOException e) {
292 System.out.println("\n# read or write file error!");
293 return false;
294 }
295
296 try {
297 br.close();
298 bw.close();
299 } catch (IOException e) {
300 System.out
301 .println("\n# close BufferedReader&BufferedWriter error");
302 return false;
303 }
304
305 if (Fd.delete() == false) {
306 System.out.println("\n# delete file failed !");
307 return false;
308 }
309 if (TempFd.renameTo(Fd) == false) {
310 System.out.println("\n# rename file failed !");
311 return false;
312 }
313
314 return true;
315 }
316
317 /**
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
321 **/
322 private static boolean writeFile(File fd) {
323
324 if (fd.canWrite() != true)
325 return false;
326
327 FileOutputStream outputFile = null;
328 try {
329 outputFile = new FileOutputStream(fd);
330 } catch (FileNotFoundException e) {
331 System.out
332 .println("\n# can't find the file when open the output stream !");
333 return false;
334 }
335 FileChannel outputChannel = outputFile.getChannel();
336
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());
345
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();
353
354 try {
355 ByteBuffer bufofCP = ByteBuffer.allocate(Copyright.length());
356 bufofCP.put(Copyright.getBytes()).flip();
357 outputChannel.write(bufofCP);
358
359 ByteBuffer bufofFI = ByteBuffer.allocate(Fileinfo.length());
360 bufofFI.put(Fileinfo.getBytes()).flip();
361 outputChannel.write(bufofFI);
362
363 ByteBuffer buffer0 = ByteBuffer.allocate(pusage.length());
364 buffer0.put(pusage.getBytes()).flip();
365 outputChannel.write(buffer0);
366 outputChannel.write(buffers[0]);
367
368 ByteBuffer buffer1 = ByteBuffer.allocate(tusage.length());
369 buffer1.put(tusage.getBytes()).flip();
370 outputChannel.write(buffer1);
371 outputChannel.write(buffers[1]);
372
373 ByteBuffer buffer2 = ByteBuffer.allocate(ausage.length());
374 buffer2.put(ausage.getBytes()).flip();
375 outputChannel.write(buffer2);
376 outputChannel.write(buffers[2]);
377
378 ByteBuffer buffer3 = ByteBuffer.allocate(cusage.length());
379 buffer3.put(cusage.getBytes()).flip();
380 outputChannel.write(buffer3);
381 outputChannel.write(buffers[3]);
382
383 ByteBuffer buffer4 = ByteBuffer.allocate(nusage.length());
384 buffer4.put(nusage.getBytes()).flip();
385 outputChannel.write(buffer4);
386 outputChannel.write(buffers[4]);
387
388 ByteBuffer buffer5 = ByteBuffer.allocate(meusage.length());
389 buffer4.put(meusage.getBytes()).flip();
390 outputChannel.write(buffer5);
391 outputChannel.write(buffers[5]);
392
393 ByteBuffer buffer6 = ByteBuffer.allocate(musage.length());
394 buffer4.put(musage.getBytes()).flip();
395 outputChannel.write(buffer6);
396 outputChannel.write(buffers[6]);
397
398 outputFile.close();
399 } catch (IOException e) {
400 System.out.println("\n# The operations of file failed !");
401 return false;
402 }
403 return true;
404 }
405
406 ///
407 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
408 ///
409 private static File TempFd;
410
411 ///
412 /// point to target.txt.
413 ///
414 private static File Fd;
415
416 ///
417 /// when the flag is true, the corresponding str should be add at the end of file.
418 ///
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;
426
427 private static final String Copyright = "#\n"
428 + "# Copyright (c) 2006, Intel Corporation\n"
429 + "#\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"
434 + "\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";
437
438 private static final String Fileinfo = "#\n"
439 + "# Filename: target.template\n"
440 + "#\n"
441 + "# ALL Paths are Relative to WORKSPACE\n"
442 + "\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";
445
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";
453
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"
460 + "# targets.\n";
461
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"
473 + "# built.\n";
474
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";
480
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";
485
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";
497 }