update GetPerformanceCounterProperties() declare
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / Tool.java
CommitLineData
878ddf1f 1/** @file\r
2This file is to define nested element which is meant for specifying a tool\r
3\r
4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14package org.tianocore.framework.tasks;\r
15\r
a1ffb10f 16import java.io.ByteArrayOutputStream;\r
878ddf1f 17import java.io.DataInputStream;\r
18import java.io.DataOutputStream;\r
19import java.io.File;\r
20import java.io.FileInputStream;\r
a1ffb10f 21import java.io.FileOutputStream;\r
22import java.io.IOException;\r
878ddf1f 23import java.util.ArrayList;\r
24import java.util.Iterator;\r
25import java.util.List;\r
26\r
27import org.apache.tools.ant.BuildException;\r
a1ffb10f 28import org.tianocore.common.logger.EdkLog;\r
878ddf1f 29\r
30/**\r
31 Class Tool is to define an external tool to be used for genffsfile\r
32 **/\r
33public class Tool implements EfiDefine, Section {\r
34\r
35 String toolName = "";\r
36 List<Object> toolArgList = new ArrayList<Object>();\r
37 String outputPath;\r
82810f3b 38 File outputFile ;\r
878ddf1f 39 List<Input> inputFiles = new ArrayList<Input>();\r
a1ffb10f 40 List<Section> gensectList = new ArrayList<Section>();\r
41 String inputArg = "-i ";\r
878ddf1f 42 /**\r
43 Call extern tool\r
44\r
45 @param buffer The buffer to put the result with alignment\r
46 **/\r
87379bbe 47 public void toBuffer (DataOutputStream buffer){\r
878ddf1f 48 ///\r
49 /// call extern tool\r
50 ///\r
51 try {\r
52 executeTool ();\r
53 } catch (Exception e) {\r
3f7b510e 54 throw new BuildException("Call to executeTool failed!\n");\r
878ddf1f 55 }\r
56\r
57 ///\r
58 /// check if file exist\r
59 ///\r
82810f3b 60 //File OutputFile = new File (this.outPutFileName);\r
61 if (!outputFile.exists()) {\r
62 throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");\r
878ddf1f 63 }\r
64\r
65 ///\r
66 /// Read output file and write it's cotains to buffer\r
67 ///\r
82810f3b 68 FileInputStream fs = null;\r
69 DataInputStream in = null;\r
878ddf1f 70 try {\r
82810f3b 71 fs = new FileInputStream (outputFile);\r
72 in = new DataInputStream (fs);\r
73\r
74\r
75 int fileLen = (int)outputFile.length();\r
76 byte[] data = new byte[fileLen];\r
77 in.read(data);\r
78 buffer.write(data, 0, fileLen);\r
878ddf1f 79\r
80 ///\r
81 /// 4 byte alignment\r
82 ///\r
83 while ((fileLen & 0x03) != 0) {\r
84 fileLen++;\r
85 buffer.writeByte(0);\r
86 }\r
878ddf1f 87 } catch (Exception e) {\r
88 System.out.print(e.getMessage());\r
3f7b510e 89 throw new BuildException("Tool call, toBuffer failed!\n");\r
82810f3b 90 } finally {\r
91 try {\r
92 if (in != null) {\r
93 in.close();\r
94 }\r
95 if (fs != null) {\r
96 fs.close();\r
97 }\r
98 } catch (Exception e) {\r
99 System.out.println("WARNING: Cannot close " + outputFile.getPath());\r
100 }\r
878ddf1f 101 }\r
102 }\r
103\r
104 ///\r
105 /// execute external tool for genffsfile\r
106 ///\r
107 private void executeTool () {\r
108 String command = "";\r
109 String argument = "";\r
110 command = toolName;\r
111 Iterator argIter = toolArgList.iterator();\r
112 Iterator inputIter = inputFiles.iterator();\r
113 ToolArg toolArg;\r
114 Input file = null;\r
a1ffb10f 115 \r
116 \r
117 //\r
118 // Get each section which under the compress {};\r
119 // And add it is contains to File;\r
120 //\r
121 Section sect;\r
122 try{\r
123 Iterator SectionIter = this.gensectList.iterator();\r
124 while (SectionIter.hasNext()){\r
125 sect = (Section)SectionIter.next();\r
126 //\r
127 // Parse <genSection> element\r
128 //\r
129 File outputFile = File.createTempFile("temp", "sec1",new File(outputPath));\r
130 FileOutputStream bo = new FileOutputStream(outputFile);\r
131 DataOutputStream Do = new DataOutputStream (bo);\r
132 //\r
133 // Call each section class's toBuffer function.\r
134 //\r
135 try {\r
136 sect.toBuffer(Do);\r
137 }\r
138 catch (BuildException e) {\r
139 System.out.print(e.getMessage());\r
140 throw new BuildException ("GenSection failed at Tool!");\r
141 } \r
142 Do.close();\r
143 this.inputArg += outputFile.getPath() + " "; \r
144 } \r
145 } catch (IOException e){\r
146 throw new BuildException ("Gensection failed at tool!");\r
147 }\r
878ddf1f 148\r
a1ffb10f 149 \r
878ddf1f 150 ///\r
151 /// argument of tools\r
152 ///\r
153 while (argIter.hasNext()) {\r
154 toolArg = (ToolArg)argIter.next();\r
155 argument = argument + toolArg.getLine() + " ";\r
156\r
157 }\r
158\r
159 ///\r
160 /// input files for tools\r
161 ///\r
878ddf1f 162 while (inputIter.hasNext()) {\r
163 file = (Input)inputIter.next();\r
a1ffb10f 164 inputArg += file.toString(" ");\r
878ddf1f 165 }\r
878ddf1f 166 try {\r
a1ffb10f 167 outputFile = File.createTempFile("temp", null, new File(outputPath));\r
168 argument = argument + inputArg + " -o " + outputFile.getPath();\r
169 EdkLog.log(EdkLog.EDK_INFO, argument);\r
878ddf1f 170 ///\r
171 /// execute command line\r
172 ///\r
a1ffb10f 173 Process process = Runtime.getRuntime().exec(command + " " + argument);\r
174 process.waitFor();\r
878ddf1f 175 } catch (Exception e) {\r
176 System.out.print (e.getMessage());\r
3f7b510e 177 throw new BuildException("Execution of externalTool task failed!\n");\r
878ddf1f 178 }\r
179 }\r
180\r
181 /**\r
182 Add method of ANT task/datatype for nested ToolArg type of element\r
183\r
184 @param toolArg The ToolArg object containing arguments for the tool\r
185 **/\r
186 public void addToolArg (ToolArg toolArg) {\r
187 toolArgList.add (toolArg);\r
188 }\r
189\r
190 /**\r
191 Get method of ANT task/datatype for attribute "OutputPath"\r
192\r
193 @returns The name of output path\r
194 **/\r
195 public String getOutputPath() {\r
196 return outputPath;\r
197 }\r
198\r
199 /**\r
200 Set method of ANT task/datatype for attribute "OutputPath"\r
201\r
202 @param outputPath The name of output path\r
203 **/\r
204 public void setOutputPath(String outPutPath) {\r
205 this.outputPath = outPutPath;\r
206 }\r
207\r
208 /**\r
209 Get method of ANT task/datatype for attribute "ToolName"\r
210\r
211 @returns The name of the tool.\r
212 **/\r
213 public String getToolName() {\r
214 return toolName;\r
215 }\r
216\r
217 /**\r
218 Set method of ANT task/datatype for attribute "ToolName"\r
219\r
220 @param toolName The name of the tool\r
221 **/\r
222 public void setToolName(String toolName) {\r
223 this.toolName = toolName;\r
224 }\r
225\r
226 /**\r
227 Add method of ANT task/datatype for nested Input type of element\r
228\r
229 @param file The Input objec which represents a file\r
230 **/\r
231 public void addInput(Input file) {\r
232 inputFiles.add(file);\r
233 }\r
a1ffb10f 234 \r
235// /**\r
236// addTool\r
237// \r
238// This function is to add instance of Tool to list.\r
239// \r
240// @param tool instance of Tool.\r
241// **/\r
242// public void addTool(Tool tool){\r
243// this.toolList.add(tool);\r
244// }\r
245 \r
246 public void addGenSection(GenSectionTask genSect){\r
247 this.gensectList.add(genSect);\r
248 }\r
249 \r
878ddf1f 250}\r
251\r
252\r