]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | File is TargetFile class which is used to generate the new target.txt. \r | |
3 | \r | |
4 | Copyright (c) 2006, Intel Corporation\r | |
5 | All rights reserved. This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | **/\r | |
13 | package org.tianocore.context;\r | |
14 | \r | |
15 | import java.io.BufferedReader;\r | |
16 | import java.io.BufferedWriter;\r | |
17 | import java.io.File;\r | |
18 | import java.io.FileNotFoundException;\r | |
19 | import java.io.FileOutputStream;\r | |
20 | import java.io.FileReader;\r | |
21 | import java.io.FileWriter;\r | |
22 | import java.io.IOException;\r | |
23 | import java.nio.ByteBuffer;\r | |
24 | import java.nio.channels.FileChannel;\r | |
25 | \r | |
26 | public class TargetFile {\r | |
27 | \r | |
28 | /** \r | |
29 | * check the validity of path and file\r | |
30 | * @param String filename : the name of target file\r | |
31 | * @return true or false\r | |
32 | **/\r | |
33 | public static boolean parsePath(String filename) {\r | |
34 | \r | |
35 | String workspacePath = System.getenv("WORKSPACE");\r | |
36 | \r | |
37 | Fd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);\r | |
38 | \r | |
39 | if (Fd.exists() == true) {\r | |
40 | if (createTempFile(filename + "tmp") == false) {\r | |
41 | return false;\r | |
42 | }\r | |
43 | if (readwriteFile() == false) {\r | |
44 | return false;\r | |
45 | }\r | |
46 | return true;\r | |
47 | } else {\r | |
48 | try {\r | |
49 | Fd.createNewFile();\r | |
50 | } catch (IOException e) {\r | |
51 | System.out.printf("%n%s", "Create the file:target.txt failed!");\r | |
52 | return false;\r | |
53 | }\r | |
54 | }\r | |
55 | TargetFile.writeFile(Fd);\r | |
56 | return true;\r | |
57 | }\r | |
58 | \r | |
59 | /**\r | |
60 | * create a empty temp file, which is located at the same directory with target file\r | |
61 | * @param String filename : the name of target temp file\r | |
62 | * @return true or false\r | |
63 | **/\r | |
64 | private static boolean createTempFile(String filename) {\r | |
65 | \r | |
66 | String workspacePath = System.getenv("WORKSPACE");\r | |
67 | \r | |
68 | TempFd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);\r | |
69 | \r | |
70 | if (TempFd.exists() == true) {\r | |
71 | if (TempFd.delete() == false) {\r | |
72 | System.out.println("\n# delete file failed !");\r | |
73 | return false;\r | |
74 | }\r | |
75 | }\r | |
76 | try {\r | |
77 | TempFd.createNewFile();\r | |
78 | } catch (IOException e) {\r | |
79 | System.out.printf("%n%s",\r | |
80 | "Create the temp file:target.txttmp failed!");\r | |
81 | return false;\r | |
82 | }\r | |
83 | \r | |
84 | return true;\r | |
85 | }\r | |
86 | \r | |
87 | /**\r | |
88 | * read from target.txt and write to target.txttmp, del target.txt, rename\r | |
89 | * @param no paremeter\r | |
90 | * @return true or false\r | |
91 | **/\r | |
92 | private static boolean readwriteFile() {\r | |
93 | \r | |
94 | if (Fd.canRead() != true)\r | |
95 | return false;\r | |
96 | \r | |
97 | BufferedReader br = null;\r | |
98 | BufferedWriter bw = null;\r | |
99 | String textLine = null;\r | |
100 | \r | |
101 | try {\r | |
102 | br = new BufferedReader(new FileReader(Fd));\r | |
103 | } catch (FileNotFoundException e) {\r | |
104 | System.out\r | |
105 | .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");\r | |
106 | return false;\r | |
107 | }\r | |
108 | try {\r | |
109 | bw = new BufferedWriter(new FileWriter(TempFd));\r | |
110 | } catch (IOException e) {\r | |
111 | System.out.println("\n# create the BufferedWriter failed!");\r | |
112 | return false;\r | |
113 | }\r | |
114 | \r | |
115 | //\r | |
116 | //TARGET_ARCH must be in front of TARGET!!! according to the target.txt\r | |
117 | //\r | |
118 | try {\r | |
119 | while ((textLine = br.readLine()) != null) {\r | |
120 | if (textLine.trim().compareToIgnoreCase("") == 0) {\r | |
121 | bw.write(textLine);\r | |
122 | bw.newLine();\r | |
123 | } else if ((textLine.trim().charAt(0) == '#') && (textLine.indexOf("=") == -1)){\r | |
124 | bw.write(textLine);\r | |
125 | bw.newLine();\r | |
126 | } else {\r | |
127 | if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {\r | |
128 | if(ParseParameter.pstr.length() > ParseParameter.length) {\r | |
129 | bw.write(ParseParameter.pstr);\r | |
130 | } else {\r | |
131 | bw.write(textLine);\r | |
132 | }\r | |
133 | bw.newLine();\r | |
134 | } else if (textLine.indexOf("TARGET_ARCH") != -1) {\r | |
135 | if(ParseParameter.astr.length() > ParseParameter.length) {\r | |
136 | bw.write(ParseParameter.astr);\r | |
137 | } else {\r | |
138 | bw.write(textLine);\r | |
139 | }\r | |
140 | bw.newLine();\r | |
141 | } else if (textLine.indexOf("TARGET") != -1) {\r | |
142 | if(ParseParameter.tstr.length() > ParseParameter.length) {\r | |
143 | bw.write(ParseParameter.tstr);\r | |
144 | } else {\r | |
145 | bw.write(textLine);\r | |
146 | }\r | |
147 | bw.newLine();\r | |
148 | } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {\r | |
149 | if(ParseParameter.cstr.length() > ParseParameter.length) {\r | |
150 | bw.write(ParseParameter.cstr);\r | |
151 | } else {\r | |
152 | bw.write(textLine);\r | |
153 | }\r | |
154 | bw.newLine();\r | |
155 | } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {\r | |
156 | if(ParseParameter.nstr.length() > ParseParameter.length) {\r | |
157 | bw.write(ParseParameter.nstr);\r | |
158 | } else {\r | |
159 | bw.write(textLine);\r | |
160 | }\r | |
161 | bw.newLine();\r | |
162 | }\r | |
163 | }\r | |
164 | }\r | |
165 | } catch (IOException e) {\r | |
166 | System.out.println("\n# read or write file error!");\r | |
167 | return false;\r | |
168 | }\r | |
169 | \r | |
170 | try {\r | |
171 | br.close();\r | |
172 | bw.close();\r | |
173 | } catch (IOException e) {\r | |
174 | System.out\r | |
175 | .println("\n# close BufferedReader&BufferedWriter error");\r | |
176 | return false;\r | |
177 | }\r | |
178 | \r | |
179 | if (Fd.delete() == false) {\r | |
180 | System.out.println("\n# delete file failed !");\r | |
181 | return false;\r | |
182 | }\r | |
183 | if (TempFd.renameTo(Fd) == false) {\r | |
184 | System.out.println("\n# rename file failed !");\r | |
185 | return false;\r | |
186 | }\r | |
187 | \r | |
188 | return true;\r | |
189 | }\r | |
190 | \r | |
191 | /**\r | |
192 | * according to user's input args, write the file directly\r | |
193 | * @param File fd : the File of the target file\r | |
194 | * @return true or false\r | |
195 | **/\r | |
196 | private static boolean writeFile(File fd) {\r | |
197 | \r | |
198 | if (fd.canWrite() != true)\r | |
199 | return false;\r | |
200 | \r | |
201 | FileOutputStream outputFile = null;\r | |
202 | try {\r | |
203 | outputFile = new FileOutputStream(fd);\r | |
204 | } catch (FileNotFoundException e) {\r | |
205 | System.out\r | |
206 | .println("\n# can't find the file when open the output stream !");\r | |
207 | return false;\r | |
208 | }\r | |
209 | FileChannel outputChannel = outputFile.getChannel();\r | |
210 | \r | |
211 | ByteBuffer[] buffers = new ByteBuffer[5];\r | |
212 | buffers[0] = ByteBuffer.allocate(ParseParameter.pstr.toString().length());\r | |
213 | buffers[1] = ByteBuffer.allocate(ParseParameter.tstr.toString().length());\r | |
214 | buffers[2] = ByteBuffer.allocate(ParseParameter.astr.toString().length());\r | |
215 | buffers[3] = ByteBuffer.allocate(ParseParameter.cstr.toString().length());\r | |
216 | buffers[4] = ByteBuffer.allocate(ParseParameter.nstr.toString().length());\r | |
217 | \r | |
218 | buffers[0].put(ParseParameter.pstr.toString().getBytes()).flip();\r | |
219 | buffers[1].put(ParseParameter.tstr.toString().getBytes()).flip();\r | |
220 | buffers[2].put(ParseParameter.astr.toString().getBytes()).flip();\r | |
221 | buffers[3].put(ParseParameter.cstr.toString().getBytes()).flip();\r | |
222 | buffers[4].put(ParseParameter.nstr.toString().getBytes()).flip();\r | |
223 | \r | |
224 | try {\r | |
225 | ByteBuffer bufofCP = ByteBuffer.allocate(Copyright.length());\r | |
226 | bufofCP.put(Copyright.getBytes()).flip();\r | |
227 | outputChannel.write(bufofCP);\r | |
228 | \r | |
229 | ByteBuffer bufofFI = ByteBuffer.allocate(Fileinfo.length());\r | |
230 | bufofFI.put(Fileinfo.getBytes()).flip();\r | |
231 | outputChannel.write(bufofFI);\r | |
232 | \r | |
233 | ByteBuffer buffer0 = ByteBuffer.allocate(pusage.length());\r | |
234 | buffer0.put(pusage.getBytes()).flip();\r | |
235 | outputChannel.write(buffer0);\r | |
236 | outputChannel.write(buffers[0]);\r | |
237 | \r | |
238 | ByteBuffer buffer1 = ByteBuffer.allocate(tusage.length());\r | |
239 | buffer1.put(tusage.getBytes()).flip();\r | |
240 | outputChannel.write(buffer1);\r | |
241 | outputChannel.write(buffers[1]);\r | |
242 | \r | |
243 | ByteBuffer buffer2 = ByteBuffer.allocate(ausage.length());\r | |
244 | buffer2.put(ausage.getBytes()).flip();\r | |
245 | outputChannel.write(buffer2);\r | |
246 | outputChannel.write(buffers[2]);\r | |
247 | \r | |
248 | ByteBuffer buffer3 = ByteBuffer.allocate(cusage.length());\r | |
249 | buffer3.put(cusage.getBytes()).flip();\r | |
250 | outputChannel.write(buffer3);\r | |
251 | outputChannel.write(buffers[3]);\r | |
252 | \r | |
253 | ByteBuffer buffer4 = ByteBuffer.allocate(nusage.length());\r | |
254 | buffer4.put(nusage.getBytes()).flip();\r | |
255 | outputChannel.write(buffer4);\r | |
256 | outputChannel.write(buffers[4]);\r | |
257 | \r | |
258 | outputFile.close();\r | |
259 | } catch (IOException e) {\r | |
260 | System.out.println("\n# The operations of file failed !");\r | |
261 | return false;\r | |
262 | }\r | |
263 | return true;\r | |
264 | }\r | |
265 | \r | |
266 | ///\r | |
267 | /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.\r | |
268 | ///\r | |
269 | private static File TempFd;\r | |
270 | \r | |
271 | ///\r | |
272 | /// point to target.txt.\r | |
273 | ///\r | |
274 | private static File Fd;\r | |
275 | \r | |
276 | private static final String Copyright = "#\n"\r | |
277 | + "# Copyright (c) 2006, Intel Corporation\n"\r | |
278 | + "#\n"\r | |
279 | + "# All rights reserved. This program and the accompanying materials\n"\r | |
280 | + "# are licensed and made available under the terms and conditions of the BSD License\n"\r | |
281 | + "# which accompanies this distribution. The full text of the license may be found at\n"\r | |
282 | + "# http://opensource.org/licenses/bsd-license.php\n"\r | |
283 | + "\n"\r | |
284 | + "# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS,\n"\r | |
285 | + "# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\n";\r | |
286 | \r | |
287 | private static final String Fileinfo = "#\n"\r | |
288 | + "# Filename: target.template\n"\r | |
289 | + "#\n"\r | |
290 | + "# ALL Paths are Relative to WORKSPACE\n"\r | |
291 | + "\n"\r | |
292 | + "# Separate multiple LIST entries with a SINGLE SPACE character, do not use comma characters.\n"\r | |
293 | + "# Un-set an option by either commenting out the line, or not setting a value.\n";\r | |
294 | \r | |
295 | private static final String pusage = "#\n"\r | |
296 | + "# PROPERTY Type Use Description\n"\r | |
297 | + "# ---------------- -------- -------- -----------------------------------------------------------\n"\r | |
298 | + "# ACTIVE_PLATFORM Filename Recommended Specify the WORKSPACE relative Path and Filename\n"\r | |
299 | + "# of the platform FPD file that will be used for the build\n"\r | |
300 | + "# This line is required if and only if the current working\n"\r | |
301 | + "# directory does not contain one or more FPD files.\n";\r | |
302 | \r | |
303 | private static final String tusage = "\n\n"\r | |
304 | + "# TARGET List Optional Zero or more of the following: DEBUG, RELEASE, \n"\r | |
305 | + "# UserDefined; separated by a space character. \n"\r | |
306 | + "# If the line is missing or no value is specified, all\n"\r | |
307 | + "# valid targets specified in the FPD file will attempt \n"\r | |
308 | + "# to be built. The following line will build all platform\n"\r | |
309 | + "# targets.\n";\r | |
310 | \r | |
311 | private static final String ausage = "\n\n"\r | |
312 | + "# TARGET_ARCH List Optional What kind of architecture is the binary being target for.\n"\r | |
313 | + "# One, or more, of the following, IA32, IA64, X64, EBC or ARM.\n"\r | |
314 | + "# Multiple values can be specified on a single line, using \n"\r | |
315 | + "# space charaters to separate the values. These are used \n"\r | |
316 | + "# during the parsing of an FPD file, restricting the build\n"\r | |
317 | + "# output target(s.)\n"\r | |
318 | + "# The Build Target ARCH is determined by a logical AND of:\n"\r | |
319 | + "# FPD BuildOptions: <SupportedArchitectures> tag\n"\r | |
320 | + "# If not specified, then all valid architectures specified \n"\r | |
321 | + "# in the FPD file, for which tools are available, will be \n"\r | |
322 | + "# built.\n";\r | |
323 | \r | |
324 | private static final String cusage = "\n\n"\r | |
325 | + "# TOOL_DEFINITION_FILE Filename Optional Specify the name of the filename to use for specifying \n"\r | |
326 | + "# the tools to use for the build. If not specified, \n"\r | |
327 | + "# tools_def.txt will be used for the build. This file \n"\r | |
328 | + "# MUST be located in the WORKSPACE/Tools/Conf directory.\n";\r | |
329 | \r | |
330 | private static final String nusage = "\n\n"\r | |
331 | + "# TAGNAME List Optional Specify the name(s) of the tools_def.txt TagName to use.\n"\r | |
332 | + "# If not specified, all applicable TagName tools will be \n"\r | |
333 | + "# used for the build. The list uses space character separation.\n";\r | |
334 | }\r |