Remove dependence check of FD upon FlashMap.fdf
[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
16import java.io.DataInputStream;\r
17import java.io.DataOutputStream;\r
18import java.io.File;\r
19import java.io.FileInputStream;\r
a1ffb10f 20import java.io.FileOutputStream;\r
21import java.io.IOException;\r
878ddf1f 22import java.util.ArrayList;\r
23import java.util.Iterator;\r
24import java.util.List;\r
25\r
26import org.apache.tools.ant.BuildException;\r
a1ffb10f 27import org.tianocore.common.logger.EdkLog;\r
878ddf1f 28\r
29/**\r
30 Class Tool is to define an external tool to be used for genffsfile\r
31 **/\r
32public class Tool implements EfiDefine, Section {\r
33\r
93f5dd0a 34 private String toolName = "";\r
35 private ToolArg toolArgList = new ToolArg();\r
36 private Input inputFiles = new Input();\r
37 private String outputPath;\r
38 private File outputFile ;\r
39 private List<Section> gensectList = new ArrayList<Section>();\r
878ddf1f 40 /**\r
41 Call extern tool\r
42\r
43 @param buffer The buffer to put the result with alignment\r
44 **/\r
87379bbe 45 public void toBuffer (DataOutputStream buffer){\r
878ddf1f 46 ///\r
47 /// call extern tool\r
48 ///\r
49 try {\r
50 executeTool ();\r
51 } catch (Exception e) {\r
0fdb42ac 52 throw new BuildException("Call to executeTool failed!\n" + e.getMessage());\r
878ddf1f 53 }\r
54\r
55 ///\r
56 /// check if file exist\r
57 ///\r
82810f3b 58 //File OutputFile = new File (this.outPutFileName);\r
59 if (!outputFile.exists()) {\r
60 throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");\r
878ddf1f 61 }\r
62\r
63 ///\r
64 /// Read output file and write it's cotains to buffer\r
65 ///\r
82810f3b 66 FileInputStream fs = null;\r
67 DataInputStream in = null;\r
878ddf1f 68 try {\r
82810f3b 69 fs = new FileInputStream (outputFile);\r
70 in = new DataInputStream (fs);\r
71\r
72\r
73 int fileLen = (int)outputFile.length();\r
74 byte[] data = new byte[fileLen];\r
75 in.read(data);\r
76 buffer.write(data, 0, fileLen);\r
878ddf1f 77\r
78 ///\r
79 /// 4 byte alignment\r
80 ///\r
81 while ((fileLen & 0x03) != 0) {\r
82 fileLen++;\r
83 buffer.writeByte(0);\r
84 }\r
878ddf1f 85 } catch (Exception e) {\r
0fdb42ac 86 EdkLog.log(e.getMessage());\r
3f7b510e 87 throw new BuildException("Tool call, toBuffer failed!\n");\r
82810f3b 88 } finally {\r
89 try {\r
90 if (in != null) {\r
91 in.close();\r
92 }\r
93 if (fs != null) {\r
94 fs.close();\r
95 }\r
96 } catch (Exception e) {\r
0fdb42ac 97 EdkLog.log("WARNING: Cannot close " + outputFile.getPath());\r
82810f3b 98 }\r
878ddf1f 99 }\r
100 }\r
101\r
102 ///\r
103 /// execute external tool for genffsfile\r
104 ///\r
105 private void executeTool () {\r
106 String command = "";\r
107 String argument = "";\r
108 command = toolName;\r
a1ffb10f 109 \r
110 //\r
111 // Get each section which under the compress {};\r
112 // And add it is contains to File;\r
113 //\r
114 Section sect;\r
115 try{\r
116 Iterator SectionIter = this.gensectList.iterator();\r
117 while (SectionIter.hasNext()){\r
118 sect = (Section)SectionIter.next();\r
119 //\r
120 // Parse <genSection> element\r
121 //\r
93f5dd0a 122 File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));\r
a1ffb10f 123 FileOutputStream bo = new FileOutputStream(outputFile);\r
124 DataOutputStream Do = new DataOutputStream (bo);\r
125 //\r
126 // Call each section class's toBuffer function.\r
127 //\r
128 try {\r
129 sect.toBuffer(Do);\r
130 }\r
131 catch (BuildException e) {\r
0fdb42ac 132 EdkLog.log(e.getMessage());\r
a1ffb10f 133 throw new BuildException ("GenSection failed at Tool!");\r
134 } \r
135 Do.close();\r
93f5dd0a 136 this.inputFiles.insFile(outputFile.getPath()); \r
a1ffb10f 137 } \r
138 } catch (IOException e){\r
139 throw new BuildException ("Gensection failed at tool!");\r
140 }\r
878ddf1f 141\r
878ddf1f 142 try {\r
a1ffb10f 143 outputFile = File.createTempFile("temp", null, new File(outputPath));\r
93f5dd0a 144 argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ") \r
145 + " -o " + outputFile.getPath();\r
146 EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);\r
878ddf1f 147 ///\r
148 /// execute command line\r
149 ///\r
a1ffb10f 150 Process process = Runtime.getRuntime().exec(command + " " + argument);\r
151 process.waitFor();\r
878ddf1f 152 } catch (Exception e) {\r
0fdb42ac 153 EdkLog.log(e.getMessage());\r
3f7b510e 154 throw new BuildException("Execution of externalTool task failed!\n");\r
878ddf1f 155 }\r
156 }\r
157\r
158 /**\r
159 Add method of ANT task/datatype for nested ToolArg type of element\r
160\r
161 @param toolArg The ToolArg object containing arguments for the tool\r
162 **/\r
93f5dd0a 163 public void addConfiguredToolArg (ToolArg toolArg) {\r
164 toolArgList.insert(toolArg);\r
878ddf1f 165 }\r
166\r
167 /**\r
168 Get method of ANT task/datatype for attribute "OutputPath"\r
169\r
170 @returns The name of output path\r
171 **/\r
172 public String getOutputPath() {\r
173 return outputPath;\r
174 }\r
175\r
176 /**\r
177 Set method of ANT task/datatype for attribute "OutputPath"\r
178\r
179 @param outputPath The name of output path\r
180 **/\r
181 public void setOutputPath(String outPutPath) {\r
182 this.outputPath = outPutPath;\r
183 }\r
184\r
185 /**\r
186 Get method of ANT task/datatype for attribute "ToolName"\r
187\r
188 @returns The name of the tool.\r
189 **/\r
190 public String getToolName() {\r
191 return toolName;\r
192 }\r
193\r
194 /**\r
195 Set method of ANT task/datatype for attribute "ToolName"\r
196\r
197 @param toolName The name of the tool\r
198 **/\r
199 public void setToolName(String toolName) {\r
200 this.toolName = toolName;\r
201 }\r
202\r
203 /**\r
204 Add method of ANT task/datatype for nested Input type of element\r
205\r
206 @param file The Input objec which represents a file\r
207 **/\r
93f5dd0a 208 public void addConfiguredInput(Input file) {\r
209 inputFiles.insert(file);\r
878ddf1f 210 }\r
a1ffb10f 211 \r
212// /**\r
213// addTool\r
214// \r
215// This function is to add instance of Tool to list.\r
216// \r
217// @param tool instance of Tool.\r
218// **/\r
219// public void addTool(Tool tool){\r
220// this.toolList.add(tool);\r
221// }\r
222 \r
223 public void addGenSection(GenSectionTask genSect){\r
224 this.gensectList.add(genSect);\r
93f5dd0a 225 } \r
878ddf1f 226}\r
227\r
228\r