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