]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
Fixed the issue caused by introducing INCLUDE_PATH property;
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / Tool.java
1 /** @file
2 This file is to define nested element which is meant for specifying a tool
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 **/
14 package org.tianocore.framework.tasks;
15
16 import java.io.DataInputStream;
17 import java.io.DataOutputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.tools.ant.BuildException;
25
26 /**
27 Class Tool is to define an external tool to be used for genffsfile
28 **/
29 public class Tool implements EfiDefine, Section {
30
31 String toolName = "";
32 List<Object> toolArgList = new ArrayList<Object>();
33 String outputPath;
34 File outputFile ;
35 List<Input> inputFiles = new ArrayList<Input>();
36
37 /**
38 Call extern tool
39
40 @param buffer The buffer to put the result with alignment
41 **/
42 public void toBuffer (DataOutputStream buffer){
43 ///
44 /// call extern tool
45 ///
46 try {
47 executeTool ();
48 } catch (Exception e) {
49 throw new BuildException("Call to executeTool failed!\n");
50 }
51
52 ///
53 /// check if file exist
54 ///
55 //File OutputFile = new File (this.outPutFileName);
56 if (!outputFile.exists()) {
57 throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");
58 }
59
60 ///
61 /// Read output file and write it's cotains to buffer
62 ///
63 FileInputStream fs = null;
64 DataInputStream in = null;
65 try {
66 fs = new FileInputStream (outputFile);
67 in = new DataInputStream (fs);
68
69
70 int fileLen = (int)outputFile.length();
71 byte[] data = new byte[fileLen];
72 in.read(data);
73 buffer.write(data, 0, fileLen);
74
75 ///
76 /// 4 byte alignment
77 ///
78 while ((fileLen & 0x03) != 0) {
79 fileLen++;
80 buffer.writeByte(0);
81 }
82 } catch (Exception e) {
83 System.out.print(e.getMessage());
84 throw new BuildException("Tool call, toBuffer failed!\n");
85 } finally {
86 try {
87 if (in != null) {
88 in.close();
89 }
90 if (fs != null) {
91 fs.close();
92 }
93 } catch (Exception e) {
94 System.out.println("WARNING: Cannot close " + outputFile.getPath());
95 }
96 }
97 }
98
99 ///
100 /// execute external tool for genffsfile
101 ///
102 private void executeTool () {
103 String command = "";
104 String argument = "";
105 command = toolName;
106 Iterator argIter = toolArgList.iterator();
107 Iterator inputIter = inputFiles.iterator();
108 ToolArg toolArg;
109 Input file = null;
110
111 ///
112 /// argument of tools
113 ///
114 while (argIter.hasNext()) {
115 toolArg = (ToolArg)argIter.next();
116 argument = argument + toolArg.getLine() + " ";
117
118 }
119
120 ///
121 /// input files for tools
122 ///
123 argument += " -i ";
124 while (inputIter.hasNext()) {
125 file = (Input)inputIter.next();
126 argument += file.toString(" ");
127 }
128
129 try {
130 outputFile = File.createTempFile("temp", ".crc", new File(outputPath));
131 argument = argument + " -o " + outputFile.getPath();
132
133 ///
134 /// execute command line
135 ///
136 Process crcProcess = Runtime.getRuntime().exec(command + " " + argument);
137 crcProcess.waitFor();
138 } catch (Exception e) {
139 System.out.print (e.getMessage());
140 throw new BuildException("Execution of externalTool task failed!\n");
141 }
142 }
143
144 /**
145 Add method of ANT task/datatype for nested ToolArg type of element
146
147 @param toolArg The ToolArg object containing arguments for the tool
148 **/
149 public void addToolArg (ToolArg toolArg) {
150 toolArgList.add (toolArg);
151 }
152
153 /**
154 Get method of ANT task/datatype for attribute "OutputPath"
155
156 @returns The name of output path
157 **/
158 public String getOutputPath() {
159 return outputPath;
160 }
161
162 /**
163 Set method of ANT task/datatype for attribute "OutputPath"
164
165 @param outputPath The name of output path
166 **/
167 public void setOutputPath(String outPutPath) {
168 this.outputPath = outPutPath;
169 }
170
171 /**
172 Get method of ANT task/datatype for attribute "ToolName"
173
174 @returns The name of the tool.
175 **/
176 public String getToolName() {
177 return toolName;
178 }
179
180 /**
181 Set method of ANT task/datatype for attribute "ToolName"
182
183 @param toolName The name of the tool
184 **/
185 public void setToolName(String toolName) {
186 this.toolName = toolName;
187 }
188
189 /**
190 Add method of ANT task/datatype for nested Input type of element
191
192 @param file The Input objec which represents a file
193 **/
194 public void addInput(Input file) {
195 inputFiles.add(file);
196 }
197 }
198
199