]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Java/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / ModuleReader.java
diff --git a/Tools/Java/Source/MigrationTools/org/tianocore/migration/ModuleReader.java b/Tools/Java/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
new file mode 100644 (file)
index 0000000..7880f2b
--- /dev/null
@@ -0,0 +1,318 @@
+/** @file\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
+ **/\r
+package org.tianocore.migration;\r
+\r
+import java.io.*;\r
+import java.util.*;\r
+import java.util.regex.*;\r
+\r
+import org.tianocore.*;\r
+\r
+public final class ModuleReader implements Common.ForDoAll {\r
+    private static final ModuleReader modulereader = new ModuleReader();\r
+    private ModuleInfo mi;\r
+    private final CommentLaplace commentlaplace = new CommentLaplace();\r
+    \r
+    private static final Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*=\\s*([^\\s]*)");\r
+    private static final Pattern ptnsection = Pattern.compile("\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE);\r
+    private static final Pattern ptnfilename = Pattern.compile("[^\\s]+");\r
+    \r
+    public final void ModuleScan() throws Exception {\r
+        Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll", String.class), mi, null, Common.FILE);\r
+\r
+        // inf&msa\r
+        String filename = null;\r
+        if (mi.msaorinf.isEmpty()) {\r
+            MigrationTool.ui.println("No INF nor MSA file found!");\r
+            System.exit(0);\r
+        } else {\r
+            if (mi.msaorinf.size() == 1) {\r
+                filename = (String)mi.msaorinf.toArray()[0];\r
+            } else {\r
+                filename = MigrationTool.ui.choose("Found .inf or .msa file for module\n" + mi.modulepath + "\nChoose one Please", mi.msaorinf.toArray());\r
+            }\r
+        }\r
+\r
+        if (filename.contains(".inf")) {\r
+            readInf(filename);\r
+        } else if (filename.contains(".msa")) {\r
+            readMsa(filename);\r
+        }\r
+        // inf&msa\r
+\r
+        preProcessModule();\r
+    }\r
+    \r
+    private final void readMsa(String name) throws Exception {\r
+        ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(new File(mi.modulepath + File.separator + name));\r
+        ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.getModuleSurfaceArea();\r
+        MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();\r
+\r
+        mi.modulename = msaheader.getModuleName();\r
+        mi.guidvalue = msaheader.getGuidValue();\r
+        mi.moduletype = msaheader.getModuleType().toString();        // ???\r
+\r
+        SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();\r
+        \r
+        String temp;\r
+        Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList().iterator();\r
+        while (li.hasNext()) {\r
+            if (!mi.localmodulesources.contains(temp = li.next().toString())) {\r
+                System.out.println("Source File Missing! : " + temp);\r
+            }\r
+        }\r
+    }\r
+    \r
+    private final void readInf(String name) throws Exception {\r
+        System.out.println("\nParsing INF file: " + name);\r
+        String wholeline;\r
+        Matcher mtrinfequation;\r
+        Matcher mtrsection;\r
+        Matcher mtrfilename;\r
+\r
+        wholeline = Common.file2string(mi.modulepath + File.separator + name);\r
+        mtrsection = ptnsection.matcher(wholeline);\r
+        while (mtrsection.find()) {\r
+            if (mtrsection.group(1).matches("defines")) {\r
+                mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
+                while (mtrinfequation.find()) {\r
+                    if (mtrinfequation.group(1).matches("BASE_NAME")) {\r
+                        mi.modulename = mtrinfequation.group(2);\r
+                    }\r
+                    if (mtrinfequation.group(1).matches("FILE_GUID")) {\r
+                        mi.guidvalue = mtrinfequation.group(2);\r
+                    }\r
+                    if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {\r
+                        mi.moduletype = mtrinfequation.group(2);\r
+                    }\r
+                }\r
+            }\r
+            if (mtrsection.group(1).contains("nmake.")) {\r
+                mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
+                while (mtrinfequation.find()) {\r
+                    if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {\r
+                        mi.entrypoint = mtrinfequation.group(2);\r
+                    }\r
+                    if (mtrinfequation.group(1).matches("DPX_SOURCE")) {\r
+                        if (!mi.localmodulesources.contains(mtrinfequation.group(2))) {\r
+                            MigrationTool.ui.println("DPX File Missing! : " + mtrinfequation.group(2));\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+            if (mtrsection.group(1).contains("sources.")) {\r
+                mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
+                while (mtrfilename.find()) {\r
+                    mi.infsources.add(mtrfilename.group());\r
+                    if (!mi.localmodulesources.contains(mtrfilename.group())) {\r
+                        MigrationTool.ui.println("Warn: Source File Missing! : " + mtrfilename.group());\r
+                    }\r
+                }\r
+            }\r
+            if (mtrsection.group(1).matches("includes.")) {\r
+                mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
+                while (mtrfilename.find()) {\r
+                    mi.infincludes.add(mtrfilename.group());\r
+                }\r
+            }\r
+        }\r
+    }\r
+    \r
+    private final void preProcessModule() throws Exception {\r
+        // according to .inf file, add extraordinary includes and sourcefiles\r
+        Common.dirCopy(mi.modulepath, mi.temppath);    // collect all Laplace.namechange to here???\r
+        \r
+        if (!mi.infincludes.isEmpty()) {\r
+            Iterator<String> it = mi.infincludes.iterator();\r
+            String tempincludename = null;\r
+            while (it.hasNext()) {\r
+                tempincludename = it.next();\r
+                if (tempincludename.contains("..")) {\r
+                    Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename);\r
+                    if (mtr.find() && !mtr.group(2).matches(".")) {\r
+                        Common.oneLevelDirCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1") + File.separator + mtr.group(2), mi.temppath, ".h");\r
+                    } else {\r
+                        Common.oneLevelDirCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1"), mi.temppath, ".h");\r
+                    }\r
+                }\r
+            }\r
+        }\r
+        if (!mi.infsources.isEmpty()) {\r
+            Iterator<String> it = mi.infsources.iterator();\r
+            String tempsourcename = null;\r
+            while (it.hasNext()) {\r
+                tempsourcename = it.next();\r
+                if (tempsourcename.contains("..")) {\r
+                    Common.ensureDir(mi.temppath + File.separator + "MT_Parent_Sources");\r
+                    Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename);\r
+                    if (mtr.find()) {\r
+                        Common.fileCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1") + File.separator + mtr.group(2), mi.temppath + File.separator + "MT_Parent_Sources" + File.separator + mtr.group(2));\r
+                    }\r
+                }\r
+            }\r
+        }\r
+\r
+        //CommentOutNonLocalHFile();\r
+        Common.toDoAll(mi.temppath, this, Common.FILE);\r
+        \r
+        parsePreProcessedSourceCode();\r
+\r
+    }\r
+\r
+    private final void parsePreProcessedSourceCode() throws Exception {\r
+        //Cl cl = new Cl(modulepath);\r
+        //cl.execute("Fat.c");\r
+        //cl.generateAll(preprocessedccodes);\r
+        //\r
+        //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r
+        //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r
+        //String[] list = new File(modulepath + File.separator + "temp").list();    // without CL , add\r
+        BufferedReader rd = null;\r
+        String ifile = null;\r
+        String line = null;\r
+        String temp = null;\r
+        \r
+        Iterator<String> ii = mi.localmodulesources.iterator();\r
+        while (ii.hasNext()) {\r
+            temp = ii.next();\r
+            if (temp.contains(".c")) {\r
+                mi.preprocessedccodes.add(temp);\r
+            }\r
+        }\r
+        \r
+        ii = mi.preprocessedccodes.iterator();\r
+        \r
+        Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r
+        Matcher matguid;\r
+        Matcher matfuncc;\r
+        Matcher matfuncd;\r
+        Matcher matenclosereplace;\r
+        Matcher matefifuncc;\r
+        Matcher matmacro;\r
+        \r
+        while (ii.hasNext()) {\r
+            StringBuffer wholefile = new StringBuffer();\r
+            ifile = ii.next();\r
+            rd = new BufferedReader(new FileReader(mi.temppath + File.separator + ifile));\r
+            while ((line = rd.readLine()) != null) {\r
+                wholefile.append(line + '\n');\r
+            }\r
+            line = wholefile.toString();\r
+            \r
+            // find guid\r
+            matguid = Guid.ptnguid.matcher(line);                                        // several ways to implement this , which one is faster ? :\r
+            while (matguid.find()) {                                                    // 1.currently , find once , then call to identify which is it\r
+                if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) {                // 2.use 3 different matchers , search 3 times to find each\r
+                    //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp));        // search the database for all 3 kinds of guids , high cost\r
+                }\r
+            }\r
+            //matguid.appendTail(result);\r
+            //line = result.toString();\r
+\r
+            // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r
+            // This item is not simply replaced , special operation is required.\r
+            matefifuncc = patefifuncc.matcher(line);\r
+            while (matefifuncc.find()) {\r
+                mi.hashEFIcall.add(matefifuncc.group(2));\r
+            }\r
+\r
+            // find function call\r
+            matfuncc = Func.ptnfuncc.matcher(line);\r
+            while (matfuncc.find()) {\r
+                if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r
+                    //MigrationTool.ui.println(ifile + "  dofunc  " + temp);\r
+                    //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));\r
+                }\r
+            }\r
+            //matfuncc.appendTail(result);\r
+            //line = result.toString();\r
+\r
+            // find macro\r
+            matmacro = Macro.ptntmacro.matcher(line);\r
+            while (matmacro.find()) {\r
+                if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r
+                }\r
+            }\r
+            \r
+            // find function definition\r
+            // replace all {} to @\r
+            while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
+                line = matenclosereplace.replaceAll("@");\r
+            }\r
+\r
+            matfuncd = Func.ptnfuncd.matcher(line);\r
+            while (matfuncd.find()) {\r
+                if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r
+                }\r
+            }\r
+        }\r
+        \r
+        // op on hash\r
+        Iterator<String> funcci = mi.hashfuncc.iterator();\r
+        while (funcci.hasNext()) {\r
+            if (!mi.hashfuncd.contains(temp = funcci.next()) && !mi.hashEFIcall.contains(temp)) {\r
+                mi.hashnonlocalfunc.add(temp);                    // this set contains both changed and not changed items\r
+            }\r
+        }\r
+    }\r
+    \r
+    public class CommentLaplace extends Common.Laplace {\r
+        public String operation(String wholeline) {\r
+            StringBuffer wholebuffer = new StringBuffer();\r
+            String templine = null;\r
+            Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");\r
+            Pattern ptninclude = Pattern.compile("#include\\s*(.*)");\r
+            Matcher mtrinclude = ptninclude.matcher(wholeline);\r
+            Matcher mtrincludefile = null;\r
+            while (mtrinclude.find()) {\r
+                mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));\r
+                if (mtrincludefile.find() && mi.localmodulesources.contains(mtrincludefile.group(1))) {\r
+                    templine = mtrinclude.group();\r
+                } else {\r
+                    templine = MigrationTool.MIGRATIONCOMMENT + mtrinclude.group();\r
+                }\r
+                mtrinclude.appendReplacement(wholebuffer, templine);\r
+            }\r
+            mtrinclude.appendTail(wholebuffer);\r
+            return wholebuffer.toString();\r
+        }\r
+        \r
+        public boolean recognize(String filename) {\r
+            return filename.contains(".c") || filename.contains(".h");\r
+        }\r
+        \r
+        public String namechange(String oldname) {\r
+            return oldname;\r
+        }\r
+    }\r
+\r
+    //-----------------------------------ForDoAll-----------------------------------//\r
+    public void run(String filepath) throws Exception {\r
+        String name = mi.temppath + File.separator + filepath.replace(mi.temppath + File.separator, "");\r
+        commentlaplace.transform(name, name);\r
+    }\r
+\r
+    public boolean filter(File dir) {\r
+        return true;\r
+    }\r
+    //-----------------------------------ForDoAll-----------------------------------//\r
+    \r
+    public final void setModuleInfo(ModuleInfo m) {\r
+        mi = m;\r
+    }\r
+    \r
+    public static final void aimAt(ModuleInfo mi) throws Exception {\r
+        modulereader.setModuleInfo(mi);\r
+        modulereader.ModuleScan();\r
+    }\r
+}\r