]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/ContextTool/org/tianocore/context/TargetFile.java
add ContextTool to workspace/Tools
[mirror_edk2.git] / Tools / Source / ContextTool / org / tianocore / context / TargetFile.java
1 package org.tianocore.context;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
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;
13
14 public class TargetFile {
15
16 /**
17 * check the validity of path and file
18 * @param String filename : the name of target file
19 * @return true or false
20 **/
21 public static boolean parsePath(String filename) {
22
23 String workspacePath = System.getenv("WORKSPACE");
24
25 Fd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);
26
27 if (Fd.exists() == true) {
28 if (createTempFile(filename + "tmp") == false) {
29 return false;
30 }
31 if (readwriteFile() == false) {
32 return false;
33 }
34 return true;
35 } else {
36 try {
37 Fd.createNewFile();
38 } catch (IOException e) {
39 System.out.printf("%n%s", "Create the file:target.txt failed!");
40 return false;
41 }
42 }
43 TargetFile.writeFile(Fd);
44 return true;
45 }
46
47 /**
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
51 **/
52 private static boolean createTempFile(String filename) {
53
54 String workspacePath = System.getenv("WORKSPACE");
55
56 TempFd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);
57
58 if (TempFd.exists() == true) {
59 if (TempFd.delete() == false) {
60 System.out.println("\n# delete file failed !");
61 return false;
62 }
63 }
64 try {
65 TempFd.createNewFile();
66 } catch (IOException e) {
67 System.out.printf("%n%s",
68 "Create the temp file:target.txttmp failed!");
69 return false;
70 }
71
72 return true;
73 }
74
75 /**
76 * read from target.txt and write to target.txttmp, del target.txt, rename
77 * @param no paremeter
78 * @return true or false
79 **/
80 private static boolean readwriteFile() {
81
82 if (Fd.canRead() != true)
83 return false;
84
85 BufferedReader br = null;
86 BufferedWriter bw = null;
87 String textLine = null;
88
89 try {
90 br = new BufferedReader(new FileReader(Fd));
91 } catch (FileNotFoundException e) {
92 System.out
93 .println("\n# create the BufferedReader failed, because can't find the file:target.txt!");
94 return false;
95 }
96 try {
97 bw = new BufferedWriter(new FileWriter(TempFd));
98 } catch (IOException e) {
99 System.out.println("\n# create the BufferedWriter failed!");
100 return false;
101 }
102
103 //
104 //TARGET_ARCH must be in front of TARGET!!! according to the target.txt
105 //
106 try {
107 while ((textLine = br.readLine()) != null) {
108 if (textLine.trim().compareToIgnoreCase("") == 0) {
109 bw.write(textLine);
110 bw.newLine();
111 } else if ((textLine.trim().charAt(0) == '#') && (textLine.indexOf("=") == -1)){
112 bw.write(textLine);
113 bw.newLine();
114 } else {
115 if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {
116 if(ParseParameter.pstr.length() > ParseParameter.length) {
117 bw.write(ParseParameter.pstr);
118 } else {
119 bw.write(textLine);
120 }
121 bw.newLine();
122 } else if (textLine.indexOf("TARGET_ARCH") != -1) {
123 if(ParseParameter.astr.length() > ParseParameter.length) {
124 bw.write(ParseParameter.astr);
125 } else {
126 bw.write(textLine);
127 }
128 bw.newLine();
129 } else if (textLine.indexOf("TARGET") != -1) {
130 if(ParseParameter.tstr.length() > ParseParameter.length) {
131 bw.write(ParseParameter.tstr);
132 } else {
133 bw.write(textLine);
134 }
135 bw.newLine();
136 } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {
137 if(ParseParameter.cstr.length() > ParseParameter.length) {
138 bw.write(ParseParameter.cstr);
139 } else {
140 bw.write(textLine);
141 }
142 bw.newLine();
143 } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {
144 if(ParseParameter.nstr.length() > ParseParameter.length) {
145 bw.write(ParseParameter.nstr);
146 } else {
147 bw.write(textLine);
148 }
149 bw.newLine();
150 }
151 }
152 }
153 } catch (IOException e) {
154 System.out.println("\n# read or write file error!");
155 return false;
156 }
157
158 try {
159 br.close();
160 bw.close();
161 } catch (IOException e) {
162 System.out
163 .println("\n# close BufferedReader&BufferedWriter error");
164 return false;
165 }
166
167 if (Fd.delete() == false) {
168 System.out.println("\n# delete file failed !");
169 return false;
170 }
171 if (TempFd.renameTo(Fd) == false) {
172 System.out.println("\n# rename file failed !");
173 return false;
174 }
175
176 return true;
177 }
178
179 /**
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
183 **/
184 private static boolean writeFile(File fd) {
185
186 if (fd.canWrite() != true)
187 return false;
188
189 FileOutputStream outputFile = null;
190 try {
191 outputFile = new FileOutputStream(fd);
192 } catch (FileNotFoundException e) {
193 System.out
194 .println("\n# can't find the file when open the output stream !");
195 return false;
196 }
197 FileChannel outputChannel = outputFile.getChannel();
198
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());
205
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();
211
212 try {
213 ByteBuffer bufofCP = ByteBuffer.allocate(Copyright.length());
214 bufofCP.put(Copyright.getBytes()).flip();
215 outputChannel.write(bufofCP);
216
217 ByteBuffer bufofFI = ByteBuffer.allocate(Fileinfo.length());
218 bufofFI.put(Fileinfo.getBytes()).flip();
219 outputChannel.write(bufofFI);
220
221 ByteBuffer buffer0 = ByteBuffer.allocate(pusage.length());
222 buffer0.put(pusage.getBytes()).flip();
223 outputChannel.write(buffer0);
224 outputChannel.write(buffers[0]);
225
226 ByteBuffer buffer1 = ByteBuffer.allocate(tusage.length());
227 buffer1.put(tusage.getBytes()).flip();
228 outputChannel.write(buffer1);
229 outputChannel.write(buffers[1]);
230
231 ByteBuffer buffer2 = ByteBuffer.allocate(ausage.length());
232 buffer2.put(ausage.getBytes()).flip();
233 outputChannel.write(buffer2);
234 outputChannel.write(buffers[2]);
235
236 ByteBuffer buffer3 = ByteBuffer.allocate(cusage.length());
237 buffer3.put(cusage.getBytes()).flip();
238 outputChannel.write(buffer3);
239 outputChannel.write(buffers[3]);
240
241 ByteBuffer buffer4 = ByteBuffer.allocate(nusage.length());
242 buffer4.put(nusage.getBytes()).flip();
243 outputChannel.write(buffer4);
244 outputChannel.write(buffers[4]);
245
246 outputFile.close();
247 } catch (IOException e) {
248 System.out.println("\n# The operations of file failed !");
249 return false;
250 }
251 return true;
252 }
253
254 ///
255 /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.
256 ///
257 private static File TempFd;
258
259 ///
260 /// point to target.txt.
261 ///
262 private static File Fd;
263
264 private static final String Copyright = "#\n"
265 + "# Copyright (c) 2006, Intel Corporation\n"
266 + "#\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"
271 + "\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";
274
275 private static final String Fileinfo = "#\n"
276 + "# Filename: target.template\n"
277 + "#\n"
278 + "# ALL Paths are Relative to WORKSPACE\n"
279 + "\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";
282
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";
290
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"
297 + "# targets.\n";
298
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"
310 + "# built.\n";
311
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";
317
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";
322 }