]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/GenFvImageTask.java
1) Added dependency check for flashmap, genfvimage, peirebase tasks
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / GenFvImageTask.java
index 6f3cd7bd7b8c9afe49868547ee633a321af70ef5..d37156dd7095826677ac94740e89ce435f9ee54e 100644 (file)
@@ -23,14 +23,18 @@ import org.apache.tools.ant.taskdefs.LogStreamHandler;
 import org.apache.tools.ant.types.Commandline;\r
 \r
 import java.io.File;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
 import java.io.InputStreamReader;\r
 import java.lang.ProcessBuilder;\r
-import java.util.ArrayList;\r
 import java.util.LinkedList;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
 import java.util.List;\r
-import java.util.Map;\r
+import java.util.StringTokenizer;\r
+import java.util.Iterator;\r
+import java.io.BufferedReader;\r
+import java.io.FileReader;\r
+\r
+import org.tianocore.common.logger.EdkLog;\r
 \r
 /**\r
   GenFvImageTask\r
@@ -39,26 +43,22 @@ import java.util.Map;
   \r
 **/\r
 public class GenFvImageTask extends Task implements EfiDefine{\r
-    ///\r
-    /// tool name\r
-    ///\r
+    //\r
+    // tool name\r
+    //\r
     static final private String toolName = "GenFvImage";\r
-    ///\r
-    /// The name of input inf file\r
-    ///\r
-    private String infFile="";\r
-    ///\r
-    /// The target architecture.\r
-    ///\r
-    private String arch="";\r
-    ///\r
-    /// Output directory\r
-    ///\r
+    //\r
+    // Pattern to match the section header (e.g. [options], [files])\r
+    // \r
+    static final private Pattern sectionHeader = Pattern.compile("\\[([^\\[\\]]+)\\]");\r
+    //\r
+    // The name of input inf file\r
+    //\r
+    private FileArg infFile = new FileArg();\r
+    //\r
+    // Output directory\r
+    //\r
     private String outputDir = ".";\r
-    ///\r
-    /// argument list\r
-    ///\r
-    LinkedList<String> argList = new LinkedList<String>();\r
 \r
     /**\r
       execute\r
@@ -68,36 +68,48 @@ public class GenFvImageTask extends Task implements EfiDefine{
     **/\r
     public void execute() throws BuildException  {\r
         Project project = this.getOwningTarget().getProject();\r
-        String path = project.getProperty("env.Framework_Tools_Path");\r
-        if (path == null) {\r
-            path = "";\r
-        } else {\r
-            path += File.separatorChar;\r
+        String path = project.getProperty("env.FRAMEWORK_TOOLS_PATH");\r
+\r
+        if (isUptodate()) {\r
+            EdkLog.log(this, EdkLog.EDK_VERBOSE, infFile.toFileList() + " is uptodate!");\r
+            return;\r
         }\r
 \r
-        if (arch != null && arch.length() > 0) {\r
-            argList.addFirst(path + toolName + "_" + arch);\r
+        String command;\r
+        if (path == null) {\r
+            command = toolName;\r
         } else {\r
-            argList.addFirst(path + toolName);\r
+            command = path + File.separator + toolName;\r
         }\r
 \r
-        ///\r
-        /// lauch the program\r
-        ///\r
-        ProcessBuilder pb = new ProcessBuilder(argList);\r
-        pb.directory(new File(outputDir));\r
+        String argument = "" + infFile;\r
+        //\r
+        // lauch the program\r
+        //\r
         int exitCode = 0;\r
         try {\r
-            Process cmdProc = pb.start();\r
-            InputStreamReader cmdOut = new InputStreamReader(cmdProc.getInputStream());\r
-            char[] buf = new char[1024];\r
+            Commandline cmdline = new Commandline();\r
+            cmdline.setExecutable(command);\r
+            cmdline.createArgument().setLine(argument);\r
 \r
-            exitCode = cmdProc.waitFor();\r
+            LogStreamHandler streamHandler = new LogStreamHandler(this,\r
+                    Project.MSG_INFO, Project.MSG_WARN);\r
+            Execute runner = new Execute(streamHandler, null);\r
+\r
+            runner.setAntRun(project);\r
+            runner.setCommandline(cmdline.getCommandline());\r
+            runner.setWorkingDirectory(new File(outputDir)); \r
+            //\r
+            // log command line string.\r
+            //\r
+            EdkLog.log(this, EdkLog.EDK_VERBOSE, Commandline.toString(cmdline.getCommandline()));\r
+            EdkLog.log(this, infFile.toFileList());\r
+\r
+            exitCode = runner.execute();\r
             if (exitCode != 0) {\r
-                int len = cmdOut.read(buf, 0, 1024);\r
-                log(new String(buf, 0, len), Project.MSG_ERR);\r
+                EdkLog.log(this, "ERROR = " + Integer.toHexString(exitCode));\r
             } else {\r
-                log("GenFvImage - DONE!", Project.MSG_VERBOSE);\r
+                EdkLog.log(this, EdkLog.EDK_VERBOSE, "GenFvImage succeeded!");\r
             }\r
         } catch (Exception e) {\r
             throw new BuildException(e.getMessage());\r
@@ -106,7 +118,6 @@ public class GenFvImageTask extends Task implements EfiDefine{
                 throw new BuildException("GenFvImage: failed to generate FV file!");\r
             }\r
         }\r
-\r
     }\r
     /**\r
       getInfFile\r
@@ -115,7 +126,7 @@ public class GenFvImageTask extends Task implements EfiDefine{
       @return String    name of infFile\r
     **/\r
     public String getInfFile() {\r
-        return infFile;\r
+        return infFile.getValue();\r
     }\r
     \r
     /**\r
@@ -126,32 +137,9 @@ public class GenFvImageTask extends Task implements EfiDefine{
       @param infFile  name of infFile\r
     **/\r
     public void setInfFile(String infFile) {\r
-        this.infFile = infFile;\r
-        argList.add("-I");\r
-        argList.add(infFile);\r
+        this.infFile.setArg(" -I ", infFile);\r
     }\r
     \r
-    /**\r
-      getArch\r
-      \r
-      This function is to get class member of arch.\r
-      @return           The target architecture.\r
-    **/\r
-    public String getArch() {\r
-        return arch;\r
-    }\r
-    \r
-    /**\r
-      setArch\r
-      \r
-      This function is to set class member of arch. \r
-      \r
-      @param arch       The target architecture.\r
-    **/\r
-    public void setArch(String arch) {\r
-        this.arch = arch;\r
-    }\r
-\r
     /**\r
       getOutputDir\r
       \r
@@ -173,4 +161,117 @@ public class GenFvImageTask extends Task implements EfiDefine{
     public void setOutputDir(String outputDir) {\r
         this.outputDir = outputDir;\r
     }\r
-}
\ No newline at end of file
+\r
+    //\r
+    // dependency check\r
+    // \r
+    private boolean isUptodate() {\r
+        String infName = this.infFile.getValue();\r
+        String fvName = "";\r
+        List<String> ffsFiles = new LinkedList<String>();\r
+        File inf = new File(infName);\r
+\r
+        try {\r
+            FileReader reader = new FileReader(inf);\r
+            BufferedReader in = new BufferedReader(reader);\r
+            String str;\r
+\r
+            //\r
+            // Read the inf file line by line\r
+            // \r
+            boolean inFiles = false;\r
+            boolean inOptions = false;\r
+            while ((str = in.readLine()) != null) {\r
+                str = str.trim();\r
+                if (str.length() == 0) {\r
+                    continue;\r
+                }\r
+\r
+                Matcher matcher = sectionHeader.matcher(str);\r
+                if (matcher.find()) {\r
+                    //\r
+                    // We take care of only "options" and "files" section\r
+                    // \r
+                    String sectionName = str.substring(matcher.start(1), matcher.end(1));\r
+                    if (sectionName.equalsIgnoreCase("options")) {\r
+                        inOptions = true;\r
+                        inFiles = false;\r
+                    } else if (sectionName.equalsIgnoreCase("files")) {\r
+                        inFiles = true;\r
+                        inOptions = false;\r
+                    } else {\r
+                        inFiles = false;\r
+                        inOptions = false;\r
+                    }\r
+                    continue;\r
+                }\r
+\r
+                //\r
+                // skip invalid line\r
+                // \r
+                int equalMarkPos = str.indexOf("=");\r
+                if (equalMarkPos < 0) {\r
+                    continue;\r
+                }\r
+\r
+                //\r
+                // we have only interest in EFI_FILE_NAME\r
+                // \r
+                String fileNameFlag = str.substring(0, equalMarkPos).trim();\r
+                String fileName = str.substring(equalMarkPos + 1).trim();\r
+                if (!fileNameFlag.equalsIgnoreCase("EFI_FILE_NAME")\r
+                    || fileName.length() == 0) {\r
+                    continue;\r
+                }\r
+\r
+                if (inFiles) {\r
+                    //\r
+                    // files specified beneath the [files] section are source files\r
+                    // \r
+                    ffsFiles.add(fileName);\r
+                } else if (inOptions) {\r
+                    //\r
+                    // file specified beneath the [options] section is the target file\r
+                    // \r
+                    fvName = outputDir + File.separator + fileName;\r
+                }\r
+            }\r
+        } catch (Exception ex) {\r
+            throw new BuildException(ex.getMessage());\r
+        }\r
+\r
+        //\r
+        // if destionation file doesn't exist, we need to generate it.\r
+        // \r
+        File fvFile = new File(fvName);\r
+        if (!fvFile.exists()) {\r
+            EdkLog.log(this, EdkLog.EDK_VERBOSE, fvName + " doesn't exist!");\r
+            return false;\r
+        }\r
+\r
+        //\r
+        // the inf file itself will be taken as source file, check its timestamp\r
+        // against the target file\r
+        // \r
+        long fvFileTimeStamp = fvFile.lastModified();\r
+        if (inf.lastModified() > fvFileTimeStamp) {\r
+            EdkLog.log(this, EdkLog.EDK_VERBOSE, infName + " has been changed since last build!");\r
+            return false;\r
+        }\r
+\r
+        //\r
+        // no change in the inf file, we need to check each source files in it\r
+        // against the target file\r
+        // \r
+        for (Iterator it = ffsFiles.iterator(); it.hasNext(); ) {\r
+            String fileName = (String)it.next();\r
+            File file = new File(fileName);\r
+            if (file.lastModified() > fvFileTimeStamp) {\r
+                EdkLog.log(this, EdkLog.EDK_VERBOSE, fileName + " has been changed since last build!");\r
+                return false;\r
+            }\r
+        }\r
+\r
+        return true;\r
+    }\r
+}\r