1 package org
.tianocore
.context
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileNotFoundException
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.FileReader
;
9 import java
.io
.FileWriter
;
10 import java
.io
.IOException
;
11 import java
.nio
.ByteBuffer
;
12 import java
.nio
.channels
.FileChannel
;
14 public class TargetFile
{
17 * check the validity of path and file
18 * @param String filename : the name of target file
19 * @return true or false
21 public static boolean parsePath(String filename
) {
23 String workspacePath
= System
.getenv("WORKSPACE");
25 Fd
= new File(workspacePath
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ filename
);
27 if (Fd
.exists() == true) {
28 if (createTempFile(filename
+ "tmp") == false) {
31 if (readwriteFile() == false) {
38 } catch (IOException e
) {
39 System
.out
.printf("%n%s", "Create the file:target.txt failed!");
43 TargetFile
.writeFile(Fd
);
48 * create a empty temp file, which is located at the same directory with target file
49 * @param String filename : the name of target temp file
50 * @return true or false
52 private static boolean createTempFile(String filename
) {
54 String workspacePath
= System
.getenv("WORKSPACE");
56 TempFd
= new File(workspacePath
+ File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ filename
);
58 if (TempFd
.exists() == true) {
59 if (TempFd
.delete() == false) {
60 System
.out
.println("\n# delete file failed !");
65 TempFd
.createNewFile();
66 } catch (IOException e
) {
67 System
.out
.printf("%n%s",
68 "Create the temp file:target.txttmp failed!");
76 * read from target.txt and write to target.txttmp, del target.txt, rename
78 * @return true or false
80 private static boolean readwriteFile() {
82 if (Fd
.canRead() != true)
85 BufferedReader br
= null;
86 BufferedWriter bw
= null;
87 String textLine
= null;
90 br
= new BufferedReader(new FileReader(Fd
));
91 } catch (FileNotFoundException e
) {
93 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
97 bw
= new BufferedWriter(new FileWriter(TempFd
));
98 } catch (IOException e
) {
99 System
.out
.println("\n# create the BufferedWriter failed!");
104 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt
107 while ((textLine
= br
.readLine()) != null) {
108 if (textLine
.trim().compareToIgnoreCase("") == 0) {
111 } else if ((textLine
.trim().charAt(0) == '#') && (textLine
.indexOf("=") == -1)){
115 if (textLine
.indexOf("ACTIVE_PLATFORM") != -1) {
116 if(ParseParameter
.pstr
.length() > ParseParameter
.length
) {
117 bw
.write(ParseParameter
.pstr
);
122 } else if (textLine
.indexOf("TARGET_ARCH") != -1) {
123 if(ParseParameter
.astr
.length() > ParseParameter
.length
) {
124 bw
.write(ParseParameter
.astr
);
129 } else if (textLine
.indexOf("TARGET") != -1) {
130 if(ParseParameter
.tstr
.length() > ParseParameter
.length
) {
131 bw
.write(ParseParameter
.tstr
);
136 } else if (textLine
.indexOf("TOOL_CHAIN_CONF") != -1) {
137 if(ParseParameter
.cstr
.length() > ParseParameter
.length
) {
138 bw
.write(ParseParameter
.cstr
);
143 } else if (textLine
.indexOf("TOOL_CHAIN_TAG") != -1) {
144 if(ParseParameter
.nstr
.length() > ParseParameter
.length
) {
145 bw
.write(ParseParameter
.nstr
);
153 } catch (IOException e
) {
154 System
.out
.println("\n# read or write file error!");
161 } catch (IOException e
) {
163 .println("\n# close BufferedReader&BufferedWriter error");
167 if (Fd
.delete() == false) {
168 System
.out
.println("\n# delete file failed !");
171 if (TempFd
.renameTo(Fd
) == false) {
172 System
.out
.println("\n# rename file failed !");
180 * according to user's input args, write the file directly
181 * @param File fd : the File of the target file
182 * @return true or false
184 private static boolean writeFile(File fd
) {
186 if (fd
.canWrite() != true)
189 FileOutputStream outputFile
= null;
191 outputFile
= new FileOutputStream(fd
);
192 } catch (FileNotFoundException e
) {
194 .println("\n# can't find the file when open the output stream !");
197 FileChannel outputChannel
= outputFile
.getChannel();
199 ByteBuffer
[] buffers
= new ByteBuffer
[5];
200 buffers
[0] = ByteBuffer
.allocate(ParseParameter
.pstr
.toString().length());
201 buffers
[1] = ByteBuffer
.allocate(ParseParameter
.tstr
.toString().length());
202 buffers
[2] = ByteBuffer
.allocate(ParseParameter
.astr
.toString().length());
203 buffers
[3] = ByteBuffer
.allocate(ParseParameter
.cstr
.toString().length());
204 buffers
[4] = ByteBuffer
.allocate(ParseParameter
.nstr
.toString().length());
206 buffers
[0].put(ParseParameter
.pstr
.toString().getBytes()).flip();
207 buffers
[1].put(ParseParameter
.tstr
.toString().getBytes()).flip();
208 buffers
[2].put(ParseParameter
.astr
.toString().getBytes()).flip();
209 buffers
[3].put(ParseParameter
.cstr
.toString().getBytes()).flip();
210 buffers
[4].put(ParseParameter
.nstr
.toString().getBytes()).flip();
213 ByteBuffer bufofCP
= ByteBuffer
.allocate(Copyright
.length());
214 bufofCP
.put(Copyright
.getBytes()).flip();
215 outputChannel
.write(bufofCP
);
217 ByteBuffer bufofFI
= ByteBuffer
.allocate(Fileinfo
.length());
218 bufofFI
.put(Fileinfo
.getBytes()).flip();
219 outputChannel
.write(bufofFI
);
221 ByteBuffer buffer0
= ByteBuffer
.allocate(pusage
.length());
222 buffer0
.put(pusage
.getBytes()).flip();
223 outputChannel
.write(buffer0
);
224 outputChannel
.write(buffers
[0]);
226 ByteBuffer buffer1
= ByteBuffer
.allocate(tusage
.length());
227 buffer1
.put(tusage
.getBytes()).flip();
228 outputChannel
.write(buffer1
);
229 outputChannel
.write(buffers
[1]);
231 ByteBuffer buffer2
= ByteBuffer
.allocate(ausage
.length());
232 buffer2
.put(ausage
.getBytes()).flip();
233 outputChannel
.write(buffer2
);
234 outputChannel
.write(buffers
[2]);
236 ByteBuffer buffer3
= ByteBuffer
.allocate(cusage
.length());
237 buffer3
.put(cusage
.getBytes()).flip();
238 outputChannel
.write(buffer3
);
239 outputChannel
.write(buffers
[3]);
241 ByteBuffer buffer4
= ByteBuffer
.allocate(nusage
.length());
242 buffer4
.put(nusage
.getBytes()).flip();
243 outputChannel
.write(buffer4
);
244 outputChannel
.write(buffers
[4]);
247 } catch (IOException e
) {
248 System
.out
.println("\n# The operations of file failed !");
255 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
257 private static File TempFd
;
260 /// point to target.txt.
262 private static File Fd
;
264 private static final String Copyright
= "#\n"
265 + "# Copyright (c) 2006, Intel Corporation\n"
267 + "# All rights reserved. This program and the accompanying materials\n"
268 + "# are licensed and made available under the terms and conditions of the BSD License\n"
269 + "# which accompanies this distribution. The full text of the license may be found at\n"
270 + "# http://opensource.org/licenses/bsd-license.php\n"
272 + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"
273 + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";
275 private static final String Fileinfo
= "#\n"
276 + "# Filename: target.template\n"
278 + "# ALL Paths are Relative to WORKSPACE\n"
280 + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"
281 + "# Un-set an option by either commenting out the line, or not setting a value.\n";
283 private static final String pusage
= "#\n"
284 + "# PROPERTY Type Use Description\n"
285 + "# ---------------- -------- -------- -----------------------------------------------------------\n"
286 + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"
287 + "# of the platform FPD file that will be used for the build\n"
288 + "# This line is required if and only if the current working\n"
289 + "# directory does not contain one or more FPD files.\n";
291 private static final String tusage
= "\n\n"
292 + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"
293 + "# UserDefined; separated by a space character. \n"
294 + "# If the line is missing or no value is specified, all\n"
295 + "# valid targets specified in the FPD file will attempt \n"
296 + "# to be built. The following line will build all platform\n"
299 private static final String ausage
= "\n\n"
300 + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"
301 + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"
302 + "# Multiple values can be specified on a single line, using \n"
303 + "# space charaters to separate the values. These are used \n"
304 + "# during the parsing of an FPD file, restricting the build\n"
305 + "# output target(s.)\n"
306 + "# The Build Target ARCH is determined by a logical AND of:\n"
307 + "# FPD BuildOptions: <SupportedArchitectures> tag\n"
308 + "# If not specified, then all valid architectures specified \n"
309 + "# in the FPD file, for which tools are available, will be \n"
312 private static final String cusage
= "\n\n"
313 + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"
314 + "# the tools to use for the build. If not specified, \n"
315 + "# tools_def.txt will be used for the build. This file \n"
316 + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";
318 private static final String nusage
= "\n\n"
319 + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"
320 + "# If not specified, all applicable TagName tools will be \n"
321 + "# used for the build. The list uses space character separation.\n";