X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=Tools%2FSource%2FFrameworkTasks%2Forg%2Ftianocore%2Fframework%2Ftasks%2FTool.java;h=6eb9f65e316906eed39b179a5a42552c8e10d465;hp=67e0a865a675abaebd1c8c751e52594124ee61c3;hb=eeaddae2960be8747f99ad155cd4b405f62d94a9;hpb=3f7b510edbe3c10b533f36f490e591782d14e929 diff --git a/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java b/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java index 67e0a865a6..6eb9f65e31 100644 --- a/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java +++ b/Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java @@ -17,67 +17,65 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Random; import org.apache.tools.ant.BuildException; +import org.tianocore.common.logger.EdkLog; /** Class Tool is to define an external tool to be used for genffsfile **/ public class Tool implements EfiDefine, Section { - String toolName = ""; - List toolArgList = new ArrayList(); - String outputPath; - String outPutFileName ; - List inputFiles = new ArrayList(); - + private String toolName = ""; + private ToolArg toolArgList = new ToolArg(); + private Input inputFiles = new Input(); + private Input tempInputFile = new Input(); + private String outputPath; + private String outputFileName ; + private List
gensectList = new ArrayList
(); /** Call extern tool @param buffer The buffer to put the result with alignment **/ - public void toBuffer (DataOutputStream buffer, DataOutputStream orgBuffer){ - File OutputFile; - byte data; - + public void toBuffer (DataOutputStream buffer){ /// /// call extern tool /// try { executeTool (); } catch (Exception e) { - throw new BuildException("Call to executeTool failed!\n"); + throw new BuildException("Call to executeTool failed!\n" + e.getMessage()); } /// /// check if file exist /// - OutputFile = new File (this.outPutFileName); - long fileLen = OutputFile.length(); - if (!OutputFile.exists()) { - throw new BuildException("The file " + outPutFileName + " does not exist!\n"); + File outputFile = new File (this.outputFileName); + if (!outputFile.exists()) { + throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n"); } /// /// Read output file and write it's cotains to buffer /// + FileInputStream fs = null; + DataInputStream in = null; try { - FileInputStream fs = new FileInputStream (this.outPutFileName); - DataInputStream In = new DataInputStream (fs); + fs = new FileInputStream (outputFile); + in = new DataInputStream (fs); - int i = 0; - while (i < fileLen) { - data = In.readByte(); - buffer.writeByte(data); - // - // Add data to org file - // - orgBuffer.writeByte(data); - i ++; - } + + int fileLen = (int)outputFile.length(); + byte[] data = new byte[fileLen]; + in.read(data); + buffer.write(data, 0, fileLen); /// /// 4 byte alignment @@ -85,13 +83,22 @@ public class Tool implements EfiDefine, Section { while ((fileLen & 0x03) != 0) { fileLen++; buffer.writeByte(0); - orgBuffer.writeByte(0); } - In.close(); - } catch (Exception e) { - System.out.print(e.getMessage()); + EdkLog.log(e.getMessage()); throw new BuildException("Tool call, toBuffer failed!\n"); + } finally { + try { + if (in != null) { + in.close(); + } + if (fs != null) { + fs.close(); + } + outputFile.delete(); + } catch (Exception e) { + EdkLog.log("WARNING: Cannot close " + outputFile.getPath()); + } } } @@ -102,41 +109,62 @@ public class Tool implements EfiDefine, Section { String command = ""; String argument = ""; command = toolName; - Iterator argIter = toolArgList.iterator(); - Iterator inputIter = inputFiles.iterator(); - ToolArg toolArg; - Input file = null; - - /// - /// argument of tools - /// - while (argIter.hasNext()) { - toolArg = (ToolArg)argIter.next(); - argument = argument + toolArg.getLine() + " "; - - } - - /// - /// input files for tools - /// - argument = argument + "-i "; - while (inputIter.hasNext()) { - file = (Input)inputIter.next(); - argument = argument + file.getFile() + " "; - } - - outPutFileName = outputPath + File.separatorChar + (new File(file.getFile())).getName() + ".crc"; - argument = argument + " -o " + outPutFileName; + + // + // Get each section which under the compress {}; + // And add it is contains to File; + // + Section sect; + try{ + Iterator SectionIter = this.gensectList.iterator(); + while (SectionIter.hasNext()){ + sect = (Section)SectionIter.next(); + // + // Parse element + // + File outputFile = File.createTempFile("temp", "sec1", new File(outputPath)); + FileOutputStream bo = new FileOutputStream(outputFile); + DataOutputStream Do = new DataOutputStream (bo); + // + // Call each section class's toBuffer function. + // + try { + sect.toBuffer(Do); + } + catch (BuildException e) { + EdkLog.log(e.getMessage()); + throw new BuildException ("GenSection failed at Tool!"); + } finally { + if (Do != null){ + Do.close(); + } + + } + this.tempInputFile.insFile(outputFile.getPath()); + } + } catch (IOException e){ + throw new BuildException ("Gensection failed at tool!"); + } try { - + Random ran = new Random(9999); + this.outputFileName = "Temp" + ran.nextInt(); + argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ") + + tempInputFile.toString(" ")+ " -o " + outputFileName; + EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument); /// /// execute command line /// - Process crcProcess = Runtime.getRuntime().exec(command + " " + argument); - crcProcess.waitFor(); - } catch (Exception e) { - System.out.print (e.getMessage()); + Process process = Runtime.getRuntime().exec(command + " " + argument); + process.waitFor(); + Iterator tempFile = tempInputFile.getNameList().iterator(); + while (tempFile.hasNext()){ + File file = new File((String)tempFile.next()); + if (file.exists()) { + file.delete(); + } + } + } catch (Exception e) { EdkLog.log(e.getMessage()); throw new BuildException("Execution of externalTool task failed!\n"); } } @@ -146,8 +174,8 @@ public class Tool implements EfiDefine, Section { @param toolArg The ToolArg object containing arguments for the tool **/ - public void addToolArg (ToolArg toolArg) { - toolArgList.add (toolArg); + public void addConfiguredToolArg (ToolArg toolArg) { + toolArgList.insert(toolArg); } /** @@ -191,9 +219,24 @@ public class Tool implements EfiDefine, Section { @param file The Input objec which represents a file **/ - public void addInput(Input file) { - inputFiles.add(file); + public void addConfiguredInput(Input file) { + inputFiles.insert(file); } + +// /** +// addTool +// +// This function is to add instance of Tool to list. +// +// @param tool instance of Tool. +// **/ +// public void addTool(Tool tool){ +// this.toolList.add(tool); +// } + + public void addGenSection(GenSectionTask genSect){ + this.gensectList.add(genSect); + } }