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) {
120 if (textLine
.trim().compareToIgnoreCase("") == 0) {
123 } else if ((textLine
.trim().charAt(0) == '#') && (textLine
.indexOf("=") == -1)){
127 if (textLine
.indexOf("ACTIVE_PLATFORM") != -1) {
128 if(ParseParameter
.pstr
.length() > ParseParameter
.length
) {
129 bw
.write(ParseParameter
.pstr
);
134 } else if (textLine
.indexOf("TARGET_ARCH") != -1) {
135 if(ParseParameter
.astr
.length() > ParseParameter
.length
) {
136 bw
.write(ParseParameter
.astr
);
141 } else if (textLine
.indexOf("TARGET") != -1) {
142 if(ParseParameter
.tstr
.length() > ParseParameter
.length
) {
143 bw
.write(ParseParameter
.tstr
);
148 } else if (textLine
.indexOf("TOOL_CHAIN_CONF") != -1) {
149 if(ParseParameter
.cstr
.length() > ParseParameter
.length
) {
150 bw
.write(ParseParameter
.cstr
);
155 } else if (textLine
.indexOf("TOOL_CHAIN_TAG") != -1) {
156 if(ParseParameter
.nstr
.length() > ParseParameter
.length
) {
157 bw
.write(ParseParameter
.nstr
);
165 } catch (IOException e
) {
166 System
.out
.println("\n# read or write file error!");
173 } catch (IOException e
) {
175 .println("\n# close BufferedReader&BufferedWriter error");
179 if (Fd
.delete() == false) {
180 System
.out
.println("\n# delete file failed !");
183 if (TempFd
.renameTo(Fd
) == false) {
184 System
.out
.println("\n# rename file failed !");
192 * according to user's input args, write the file directly
193 * @param File fd : the File of the target file
194 * @return true or false
196 private static boolean writeFile(File fd
) {
198 if (fd
.canWrite() != true)
201 FileOutputStream outputFile
= null;
203 outputFile
= new FileOutputStream(fd
);
204 } catch (FileNotFoundException e
) {
206 .println("\n# can't find the file when open the output stream !");
209 FileChannel outputChannel
= outputFile
.getChannel();
211 ByteBuffer
[] buffers
= new ByteBuffer
[5];
212 buffers
[0] = ByteBuffer
.allocate(ParseParameter
.pstr
.toString().length());
213 buffers
[1] = ByteBuffer
.allocate(ParseParameter
.tstr
.toString().length());
214 buffers
[2] = ByteBuffer
.allocate(ParseParameter
.astr
.toString().length());
215 buffers
[3] = ByteBuffer
.allocate(ParseParameter
.cstr
.toString().length());
216 buffers
[4] = ByteBuffer
.allocate(ParseParameter
.nstr
.toString().length());
218 buffers
[0].put(ParseParameter
.pstr
.toString().getBytes()).flip();
219 buffers
[1].put(ParseParameter
.tstr
.toString().getBytes()).flip();
220 buffers
[2].put(ParseParameter
.astr
.toString().getBytes()).flip();
221 buffers
[3].put(ParseParameter
.cstr
.toString().getBytes()).flip();
222 buffers
[4].put(ParseParameter
.nstr
.toString().getBytes()).flip();
225 ByteBuffer bufofCP
= ByteBuffer
.allocate(Copyright
.length());
226 bufofCP
.put(Copyright
.getBytes()).flip();
227 outputChannel
.write(bufofCP
);
229 ByteBuffer bufofFI
= ByteBuffer
.allocate(Fileinfo
.length());
230 bufofFI
.put(Fileinfo
.getBytes()).flip();
231 outputChannel
.write(bufofFI
);
233 ByteBuffer buffer0
= ByteBuffer
.allocate(pusage
.length());
234 buffer0
.put(pusage
.getBytes()).flip();
235 outputChannel
.write(buffer0
);
236 outputChannel
.write(buffers
[0]);
238 ByteBuffer buffer1
= ByteBuffer
.allocate(tusage
.length());
239 buffer1
.put(tusage
.getBytes()).flip();
240 outputChannel
.write(buffer1
);
241 outputChannel
.write(buffers
[1]);
243 ByteBuffer buffer2
= ByteBuffer
.allocate(ausage
.length());
244 buffer2
.put(ausage
.getBytes()).flip();
245 outputChannel
.write(buffer2
);
246 outputChannel
.write(buffers
[2]);
248 ByteBuffer buffer3
= ByteBuffer
.allocate(cusage
.length());
249 buffer3
.put(cusage
.getBytes()).flip();
250 outputChannel
.write(buffer3
);
251 outputChannel
.write(buffers
[3]);
253 ByteBuffer buffer4
= ByteBuffer
.allocate(nusage
.length());
254 buffer4
.put(nusage
.getBytes()).flip();
255 outputChannel
.write(buffer4
);
256 outputChannel
.write(buffers
[4]);
259 } catch (IOException e
) {
260 System
.out
.println("\n# The operations of file failed !");
267 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
269 private static File TempFd
;
272 /// point to target.txt.
274 private static File Fd
;
276 private static final String Copyright
= "#\n"
277 + "# Copyright (c) 2006, Intel Corporation\n"
279 + "# All rights reserved. This program and the accompanying materials\n"
280 + "# are licensed and made available under the terms and conditions of the BSD License\n"
281 + "# which accompanies this distribution. The full text of the license may be found at\n"
282 + "# http://opensource.org/licenses/bsd-license.php\n"
284 + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"
285 + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";
287 private static final String Fileinfo
= "#\n"
288 + "# Filename: target.template\n"
290 + "# ALL Paths are Relative to WORKSPACE\n"
292 + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"
293 + "# Un-set an option by either commenting out the line, or not setting a value.\n";
295 private static final String pusage
= "#\n"
296 + "# PROPERTY Type Use Description\n"
297 + "# ---------------- -------- -------- -----------------------------------------------------------\n"
298 + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"
299 + "# of the platform FPD file that will be used for the build\n"
300 + "# This line is required if and only if the current working\n"
301 + "# directory does not contain one or more FPD files.\n";
303 private static final String tusage
= "\n\n"
304 + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"
305 + "# UserDefined; separated by a space character. \n"
306 + "# If the line is missing or no value is specified, all\n"
307 + "# valid targets specified in the FPD file will attempt \n"
308 + "# to be built. The following line will build all platform\n"
311 private static final String ausage
= "\n\n"
312 + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"
313 + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"
314 + "# Multiple values can be specified on a single line, using \n"
315 + "# space charaters to separate the values. These are used \n"
316 + "# during the parsing of an FPD file, restricting the build\n"
317 + "# output target(s.)\n"
318 + "# The Build Target ARCH is determined by a logical AND of:\n"
319 + "# FPD BuildOptions: <SupportedArchitectures> tag\n"
320 + "# If not specified, then all valid architectures specified \n"
321 + "# in the FPD file, for which tools are available, will be \n"
324 private static final String cusage
= "\n\n"
325 + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"
326 + "# the tools to use for the build. If not specified, \n"
327 + "# tools_def.txt will be used for the build. This file \n"
328 + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";
330 private static final String nusage
= "\n\n"
331 + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"
332 + "# If not specified, all applicable TagName tools will be \n"
333 + "# used for the build. The list uses space character separation.\n";