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