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