]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Java/Source/GenBuild/org/tianocore/build/FileProcess.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / FileProcess.java
diff --git a/Tools/Java/Source/GenBuild/org/tianocore/build/FileProcess.java b/Tools/Java/Source/GenBuild/org/tianocore/build/FileProcess.java
new file mode 100644 (file)
index 0000000..09ccd1f
--- /dev/null
@@ -0,0 +1,263 @@
+/** @file\r
+  File is FileProcess class which is used to generate ANT script to build \r
+  source files. \r
+  \r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+package org.tianocore.build;\r
+\r
+import java.io.File;\r
+\r
+import org.apache.tools.ant.BuildException;\r
+import org.apache.tools.ant.Project;\r
+import org.w3c.dom.Document;\r
+import org.w3c.dom.Element;\r
+import org.w3c.dom.Node;\r
+\r
+/**\r
+  <p><code>FileProcess</code> is class to generate ANT script to build source\r
+  files.</p>\r
+  \r
+  <p>If file does not specify file type, <code>FileProcess</code> will judge \r
+  by its extension. Following is the current supported extensions. </p>\r
+  \r
+  <pre>   \r
+ Source File Suffix     File Type       Description\r
+    .h                   CHeader      C header file\r
+    .c                   CCode        C source file\r
+    .inc                 ASMHeader    Assembly header file\r
+    .asm                 ASM          Assembly source file, usually for IA32 and X64 Arch and MSFT tool chain\r
+    .S                   ASM          Assembly source file, usually for IPF Arch\r
+    .s                   ASM          Assembly source file, usually for IA32 and X64 Arch and GCC tool chain\r
+    .uni                 UNI          Unicode file\r
+    .vfr                 VFR          Visual Forms Representation File\r
+    .fv                  FV           Firmware Volume\r
+    .SEC                 FFS          Firmware File System file\r
+    .PEI                 FFS          Firmware File System file\r
+    .DXE                 FFS          Firmware File System file\r
+    .APP                 FFS          Firmware File System file\r
+    .FVI                 FFS          Firmware File System file\r
+    .FFS                 FFS          Firmware File System file\r
+    .bmp                 BMP          Graphic File\r
+    .i                   PPCode       IPF PreProcessor Code\r
+  </pre>\r
+  \r
+  @since GenBuild 1.0\r
+**/\r
+public class FileProcess {\r
+    ///\r
+    ///  The mapping information about source suffix, result suffix, file type.\r
+    ///\r
+    public final String[][] fileTypes = { {".h", "", "CHeader" }, \r
+                                          {".c", "", "CCode" },\r
+                                          {".inc", "", "ASMHeader" },\r
+                                          {".asm", "", "ASM" }, \r
+                                          {".S", "", "ASM" },\r
+                                          {".s", "", "ASM" },\r
+                                          {".uni", "", "UNI" },\r
+                                          {".vfr", "", "VFR" },\r
+                                          {".Vfr", "", "VFR" },\r
+                                          {".dxs", "", "DPX"},\r
+                                          {".fv", "", "FV" },\r
+                                          {".efi", "", "EFI" },\r
+                                          {".SEC", "", "FFS" },\r
+                                          {".PEI", "", "FFS" },\r
+                                          {".DXE", "", "FFS" },\r
+                                          {".APP", "", "FFS" },\r
+                                          {".FYI", "", "FFS" },\r
+                                          {".FFS", "", "FFS" },\r
+                                          {".bmp", "", "BMP" },\r
+                                          {".i", "", "PPCode"}};\r
+    ///\r
+    /// Current ANT context. \r
+    ///\r
+    private Project project;\r
+\r
+    ///\r
+    /// Current module's include pathes\r
+    ///\r
+    private String[] includes;\r
+    \r
+    ///\r
+    /// Xml Document.\r
+    ///\r
+    private Document document;\r
+    \r
+    ///\r
+    /// The flag to ensure all unicode files build before others. \r
+    ///\r
+    private boolean unicodeFirst = true;\r
+    \r
+    ///\r
+    /// The flag present whether current module contains Unicode files or not.\r
+    ///\r
+    private boolean unicodeExist = false;\r
+\r
+    /**\r
+      Initialize the project, includes, sourceFiles, document members.\r
+      \r
+      @param project ANT project\r
+      @param includes Module include pathes\r
+      @param sourceFiles Modules source files\r
+      @param document XML document\r
+    **/\r
+    public void init(Project project, String[] includes, Document document) {\r
+        this.document = document;\r
+        this.includes = includes;\r
+        this.project = project;\r
+    }\r
+\r
+    /**\r
+      Parse file without file type. \r
+      \r
+      @param filename Source file name\r
+      @param root Root node\r
+      @param unicodeFirst whether build Unicode file firstly or not\r
+    **/\r
+    public synchronized void parseFile(String filename, Node root, boolean unicodeFirst) {\r
+        this.unicodeFirst = unicodeFirst;\r
+        parseFile(filename, root);\r
+    }\r
+    \r
+    /**\r
+      Get whether current module contains Unicode files or not.\r
+      \r
+      @return Whether current module contains Unicode files or not\r
+    **/\r
+    public boolean isUnicodeExist() {\r
+        return unicodeExist;\r
+    }\r
+\r
+    /**\r
+      Parse file.\r
+      \r
+      @param filename Source file name\r
+      @param filetype Source file type\r
+      @param root Root node\r
+      @param unicodeFirst whether build Unicode file firstly or not\r
+    **/\r
+    public synchronized void parseFile(String filename, String filetype, Node root, boolean unicodeFirst) {\r
+        this.unicodeFirst = unicodeFirst;\r
+        parseFile(filename, filetype, root);\r
+    }\r
+    \r
+    /**\r
+      Find out source file's type. \r
+      \r
+      @param filename Source file name\r
+      @param root Root node\r
+    **/\r
+    public synchronized void parseFile(String filename, Node root) throws BuildException {\r
+        for (int i = 0; i < fileTypes.length; i++) {\r
+            if (filename.endsWith(fileTypes[i][0])) {\r
+                parseFile(filename, fileTypes[i][2], root);\r
+                return ;\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+      Parse file. If flag <code>unicodeFirst</code> is true, then build all\r
+      unicode files firstly. \r
+      \r
+      <p>Note that AutoGen.c is processed specially. It's output path is always\r
+      <code>${DEST_DIR_OUTPUT}</code>, others are <code>${DEST_DIR_OUTPUT}</code>\r
+      and relative to module path. </p>\r
+      \r
+      @param filename Source file name\r
+      @param filetype Source file type\r
+      @param root Root node\r
+    **/\r
+    public synchronized void parseFile(String filename, String filetype, Node root) {\r
+        if (unicodeFirst) {\r
+            if ( ! filetype.equalsIgnoreCase("UNI")){\r
+                return ;\r
+            }\r
+            unicodeExist= true;\r
+        } else {\r
+            if (filetype.equalsIgnoreCase("UNI")){\r
+                return ;\r
+            }\r
+        }\r
+        \r
+        //\r
+        // If file is C or ASM header file, skip it\r
+        //\r
+        if (filetype.equalsIgnoreCase("CHeader") || filetype.equalsIgnoreCase("ASMHeader")) {\r
+            return;\r
+        }\r
+        \r
+        //\r
+        // If file is pre-processor file, skip it\r
+        // \r
+        if (filetype.equalsIgnoreCase("PPCode")) {\r
+            return;\r
+        }\r
+        \r
+        //\r
+        // If define CC_EXT in tools_def.txt file, the source file with \r
+        // different suffix is skipped\r
+        //\r
+        String toolsDefExtName = project.getProperty(filetype + "_EXT");\r
+        if (toolsDefExtName != null) {\r
+            String[] exts = toolsDefExtName.split(" ");\r
+            for (int i = 0; i < exts.length; i++) {\r
+                if ( ! filename.endsWith(exts[i])) {\r
+                    return ;\r
+                }\r
+            }\r
+        }\r
+        \r
+        String module_path = project.getProperty("MODULE_DIR");\r
+        File moduleFile = new File(module_path);\r
+        File sourceFile = new File(filename);\r
+        \r
+        //\r
+        // If source file is AutoGen.c, then Filepath is .\r
+        //\r
+        String sourceFilepath = "";\r
+        String sourceFilename = "";\r
+        String sourceFileext = "";\r
+        if (sourceFile.getPath().endsWith("AutoGen.c")) {\r
+            sourceFilepath = ".";\r
+            sourceFilename = "AutoGen";\r
+            sourceFileext = ".c";\r
+            filetype = "AUTOGEN";\r
+        } else {\r
+            // sourceFile.\r
+            String str = sourceFile.getPath().substring(moduleFile.getPath().length() + 1);\r
+            int index = str.lastIndexOf(File.separatorChar);\r
+            sourceFilepath = ".";\r
+            if (index > 0) {\r
+                sourceFilepath = str.substring(0, index);\r
+                str = str.substring(index + 1);\r
+            }\r
+            sourceFilename = str;\r
+            index = str.lastIndexOf('.');\r
+            if (index > 0) {\r
+                sourceFilename = str.substring(0, index);\r
+                sourceFileext = str.substring(index);\r
+            }\r
+        }\r
+        // <Build_filetype FILEPATH="" FILENAME="" />\r
+        Element ele = document.createElement("Build_" + filetype);\r
+        ele.setAttribute("FILEPATH", sourceFilepath);\r
+        ele.setAttribute("FILENAME", sourceFilename);\r
+        ele.setAttribute("FILEEXT", sourceFileext.substring(1));\r
+        Element includesEle = document.createElement("EXTRA.INC");\r
+        for (int i = 0; i < includes.length; i++) {\r
+            Element includeEle = document.createElement("includepath");\r
+            includeEle.setAttribute("path", project.replaceProperties(includes[i]));\r
+            includesEle.appendChild(includeEle);\r
+        }\r
+        ele.appendChild(includesEle);\r
+        root.appendChild(ele);\r
+    }\r
+}
\ No newline at end of file