]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
Removed some output information for debug purpose.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / Tool.java
index 36b095a6e74cb1b8f91d4e109db8503e82302261..6eb9f65e316906eed39b179a5a42552c8e10d465 100644 (file)
@@ -17,63 +17,65 @@ import java.io.DataInputStream;
 import java.io.DataOutputStream;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
 import java.util.ArrayList;\r
 import java.util.Iterator;\r
 import java.util.List;\r
+import java.util.Random;\r
 \r
 import org.apache.tools.ant.BuildException;\r
+import org.tianocore.common.logger.EdkLog;\r
 \r
 /**\r
  Class Tool is to define an external tool to be used for genffsfile\r
  **/\r
 public class Tool implements EfiDefine, Section {\r
 \r
-    String toolName     = "";\r
-    List<Object>   toolArgList  = new ArrayList<Object>();\r
-    String outputPath;\r
-    String outPutFileName ;\r
-    List<Input>    inputFiles = new ArrayList<Input>();\r
-\r
+    private String toolName     = "";\r
+    private ToolArg toolArgList = new ToolArg();\r
+    private Input inputFiles = new Input();\r
+    private Input tempInputFile = new Input();\r
+    private String outputPath;\r
+    private String outputFileName ;\r
+    private List<Section>  gensectList = new ArrayList<Section>();\r
     /**\r
      Call extern tool\r
 \r
      @param     buffer  The buffer to put the result with alignment\r
      **/\r
     public void toBuffer (DataOutputStream buffer){\r
-        File           OutputFile;\r
-        byte           data;\r
-\r
         ///\r
         /// call extern tool\r
         ///\r
         try {\r
             executeTool ();\r
         } catch (Exception e) {\r
-            throw new BuildException("Call tools failed!\n");\r
+            throw new BuildException("Call to executeTool failed!\n" + e.getMessage());\r
         }\r
 \r
         ///\r
         /// check if file exist\r
         ///\r
-        OutputFile = new File (this.outPutFileName);\r
-        long fileLen = OutputFile.length();\r
-        if (!OutputFile.exists()) {\r
-            throw new BuildException("The file " + outPutFileName + " is not exist!\n");\r
+        File outputFile = new File (this.outputFileName);\r
+        if (!outputFile.exists()) {\r
+            throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");\r
         }\r
 \r
         ///\r
         /// Read output file and write it's cotains to buffer\r
         ///\r
+        FileInputStream fs = null;\r
+        DataInputStream in = null;\r
         try {\r
-            FileInputStream fs  = new FileInputStream (this.outPutFileName);\r
-            DataInputStream In  = new DataInputStream (fs);\r
-\r
-            int i = 0;\r
-            while (i < fileLen) {\r
-                data = In.readByte();\r
-                buffer.writeByte(data);\r
-                i ++;\r
-            }\r
+            fs  = new FileInputStream (outputFile);\r
+            in  = new DataInputStream (fs);\r
+\r
+\r
+            int fileLen = (int)outputFile.length();\r
+            byte[] data  = new byte[fileLen];\r
+            in.read(data);\r
+            buffer.write(data, 0, fileLen);\r
 \r
             ///\r
             /// 4 byte alignment\r
@@ -82,11 +84,21 @@ public class Tool implements EfiDefine, Section {
                 fileLen++;\r
                 buffer.writeByte(0);\r
             }\r
-            In.close();\r
-\r
         } catch (Exception e) {\r
-            System.out.print(e.getMessage());\r
-            throw new BuildException("Call tool2buffer failed!\n");\r
+            EdkLog.log(e.getMessage());\r
+            throw new BuildException("Tool call, toBuffer failed!\n");\r
+        } finally {\r
+            try {\r
+                if (in != null) {\r
+                    in.close();\r
+                }\r
+                if (fs != null) {\r
+                    fs.close();\r
+                }\r
+                outputFile.delete(); \r
+            } catch (Exception e) {\r
+                EdkLog.log("WARNING: Cannot close " + outputFile.getPath());\r
+            }\r
         }\r
     }\r
 \r
@@ -97,42 +109,63 @@ public class Tool implements EfiDefine, Section {
         String command   = "";\r
         String argument  = "";\r
         command          = toolName;\r
-        Iterator argIter = toolArgList.iterator();\r
-        Iterator inputIter = inputFiles.iterator();\r
-        ToolArg toolArg;\r
-        Input file = null;\r
-\r
-        ///\r
-        /// argument of tools\r
-        ///\r
-        while (argIter.hasNext()) {\r
-            toolArg = (ToolArg)argIter.next();\r
-            argument = argument + toolArg.getLine() + " ";\r
-\r
-        }\r
-\r
-        ///\r
-        /// input files for tools\r
-        ///\r
-        argument = argument + "-i ";\r
-        while (inputIter.hasNext()) {\r
-            file = (Input)inputIter.next();\r
-            argument = argument + file.getFile() + " ";\r
-        }\r
-\r
-        outPutFileName = outputPath + File.separatorChar + (new File(file.getFile())).getName() + ".crc";\r
-        argument       = argument + " -o " + outPutFileName; \r
+        \r
+        //\r
+        //  Get each section which under the compress {};\r
+        //  And add it is contains to File;\r
+        //\r
+        Section sect;\r
+        try{\r
+            Iterator SectionIter = this.gensectList.iterator();\r
+            while (SectionIter.hasNext()){\r
+                sect = (Section)SectionIter.next();\r
+                //\r
+                // Parse <genSection> element\r
+                //\r
+                File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));\r
+                FileOutputStream bo = new FileOutputStream(outputFile);\r
+                DataOutputStream Do = new DataOutputStream (bo);\r
+                //\r
+                //  Call each section class's toBuffer function.\r
+                //\r
+                try {\r
+                    sect.toBuffer(Do);\r
+                }\r
+                catch (BuildException e) {\r
+                    EdkLog.log(e.getMessage());\r
+                    throw new BuildException ("GenSection failed at Tool!");\r
+                } finally {\r
+                    if (Do != null){\r
+                        Do.close();    \r
+                    }\r
+                    \r
+                } \r
+                this.tempInputFile.insFile(outputFile.getPath());\r
+            }        \r
+        } catch (IOException e){\r
+            throw new BuildException ("Gensection failed at tool!");\r
+        } \r
 \r
         try {\r
-\r
+            Random ran = new Random(9999); \r
+            this.outputFileName = "Temp" + ran.nextInt();\r
+            argument   = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ") \r
+                         + tempInputFile.toString(" ")+ " -o " + outputFileName;\r
+            EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);\r
             ///\r
             /// execute command line\r
             ///\r
-            Process crcProcess = Runtime.getRuntime().exec(command + " " + argument);\r
-            crcProcess.waitFor();\r
-        } catch (Exception e) {\r
-            System.out.print (e.getMessage());\r
-            throw new BuildException("Execute tools fails!\n");\r
+            Process process = Runtime.getRuntime().exec(command + " " + argument);\r
+            process.waitFor();\r
+            Iterator tempFile = tempInputFile.getNameList().iterator();\r
+            while (tempFile.hasNext()){\r
+                File file = new File((String)tempFile.next());\r
+                if (file.exists()) {\r
+                    file.delete();\r
+                }\r
+            }\r
+        } catch (Exception e) {\r            EdkLog.log(e.getMessage());\r
+            throw new BuildException("Execution of externalTool task failed!\n");\r
         }\r
     }\r
 \r
@@ -141,8 +174,8 @@ public class Tool implements EfiDefine, Section {
 \r
      @param     toolArg     The ToolArg object containing arguments for the tool\r
      **/\r
-    public void addToolArg (ToolArg toolArg) {\r
-        toolArgList.add (toolArg);\r
+    public void addConfiguredToolArg (ToolArg toolArg) {\r
+        toolArgList.insert(toolArg);\r
     }\r
 \r
     /**\r
@@ -186,9 +219,24 @@ public class Tool implements EfiDefine, Section {
 \r
      @param     file    The Input objec which represents a file\r
      **/\r
-    public void addInput(Input file) {\r
-        inputFiles.add(file);\r
+    public void addConfiguredInput(Input file) {\r
+        inputFiles.insert(file);\r
     }\r
+    \r
+//    /**\r
+//      addTool\r
+//      \r
+//      This function is to add instance of Tool to list.\r
+//      \r
+//      @param tool             instance of Tool.\r
+//    **/\r
+//    public void addTool(Tool tool){\r
+//        this.toolList.add(tool);\r
+//    }\r
+    \r
+    public void addGenSection(GenSectionTask genSect){\r
+        this.gensectList.add(genSect);\r
+    }    \r
 }\r
 \r
 \r