]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
add commentout entrypoint
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
index 868509a88b411da0e972907f46b95d2baeaad0c2..67e5bc25677ee4fdd9067039a14e41f5aaad6d2c 100644 (file)
@@ -17,9 +17,10 @@ import java.util.*;
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
-public final class SourceFileReplacer {\r
-       private static ModuleInfo mi;\r
-       private static boolean showdetails = false;\r
+public final class SourceFileReplacer implements Common.ForDoAll {\r
+       private static final SourceFileReplacer SFReplacer = new SourceFileReplacer();\r
+       private ModuleInfo mi;\r
+       private static boolean showdetails = true;              // set this as default now, may be changed in the future\r
        \r
        private static class r8tor9 {\r
                r8tor9(String r8, String r9) {\r
@@ -38,49 +39,23 @@ public final class SourceFileReplacer {
        private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
        private static final Set<String> filer8only = new HashSet<String>();\r
        \r
-       public static final void flush(ModuleInfo moduleinfo) throws Exception {\r
-               \r
-               mi = moduleinfo;\r
-               \r
-               String outname = null;\r
-               String inname = null;\r
-               if (MigrationTool.ui.yesOrNo("Changes will be made to the Source Code.  View details?")) {\r
-                       showdetails = true;\r
-               }\r
-               \r
-               Iterator<String> di = mi.localmodulesources.iterator();\r
-               while (di.hasNext()) {\r
-                       inname = di.next();\r
-                       if (inname.contains(".c") || inname.contains(".C")) {\r
-                               if (inname.contains(".C")) {\r
-                                       outname = inname.replaceFirst(".C", ".c");\r
-                               } else {\r
-                                       outname = inname;\r
-                               }\r
-                               MigrationTool.ui.println("\nModifying file: " + inname);\r
-                               Common.string2file(sourcefilereplace(mi.modulepath + File.separator + "temp" + File.separator + inname), mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
-                       } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\r
-                               if (inname.contains(".H")) {\r
-                                       outname = inname.replaceFirst(".H", ".h");\r
-                               } else {\r
-                                       outname = inname;\r
-                               }\r
-                               MigrationTool.ui.println("\nCopying file: " + inname);\r
-                               Common.string2file(Common.file2string(mi.modulepath + File.separator + "temp" + File.separator + inname), mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
-                       }\r
-               }\r
-\r
-               if (!mi.hashr8only.isEmpty()) {\r
-                       addr8only();\r
+       private static final String addincludefile(String wholeline, String hfile) {\r
+               return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
+       }\r
+       \r
+       private final String convertdxs(String wholeline) {\r
+               if (mi.getModuleType().equals("PEIM")) {\r
+                       return addincludefile(wholeline, "\\<PeimDepex.h\\>");\r
+               } else {\r
+                       return addincludefile(wholeline, "\\<DxeDepex.h\\>");\r
                }\r
        }\r
        \r
-       private static final void addr8only() throws Exception {\r
+       private final void addr8only() throws Exception {\r
                String paragraph = null;\r
                String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
-               Common.ensureDir(mi.modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
-               PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
-               PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(mi.outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
+               PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
+               PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
                Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
                Matcher mtrr8only = ptnr8only.matcher(line);\r
                Matcher mtrr8onlyhead;\r
@@ -108,28 +83,17 @@ public final class SourceFileReplacer {
        }\r
        \r
        // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
-       private static final String sourcefilereplace(String filename) throws Exception {\r
-               BufferedReader rd = new BufferedReader(new FileReader(filename));\r
-               StringBuffer wholefile = new StringBuffer();\r
-               String line;\r
-               String r8thing;\r
-               String r9thing;\r
-               r8tor9 temp;\r
+       private final String sourcefilereplace(String wholeline) throws Exception {\r
                boolean addr8 = false;\r
 \r
                Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE);                                       // ! only two level () bracket allowed !\r
                //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
 \r
-               while ((line = rd.readLine()) != null) {\r
-                       wholefile.append(line + "\n");\r
-               }\r
-               line = wholefile.toString();\r
-               \r
                // replace BS -> gBS , RT -> gRT\r
-               Matcher mat = pat.matcher(line);\r
+               Matcher mat = pat.matcher(wholeline);\r
                if (mat.find()) {                                                                                               // add a library here\r
                        MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");\r
-                       line = mat.replaceAll("g$1$2$3");                                                       //unknown correctiveness\r
+                       wholeline = mat.replaceAll("g$1$2$3");                                                  //unknown correctiveness\r
                }\r
                mat.reset();\r
                while (mat.find()) {\r
@@ -140,16 +104,12 @@ public final class SourceFileReplacer {
                                mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
                        }\r
                }\r
-               /*\r
                // remove EFI_DRIVER_ENTRY_POINT\r
-               Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
-               Matcher matentrypoint = patentrypoint.matcher(line);\r
-               if (matentrypoint.find()) {\r
-                       MigrationTool.ui.println("Deleting Entry_Point");\r
-                       line = matentrypoint.replaceAll("");\r
-               }\r
-               */\r
+               wholeline = wholeline.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
+               \r
                // start replacing names\r
+               String r8thing;\r
+               String r9thing;\r
                Iterator<String> it;\r
                // Converting non-locla function\r
                it = mi.hashnonlocalfunc.iterator();\r
@@ -165,11 +125,12 @@ public final class SourceFileReplacer {
                        } else {                                                                                                                        //\r
                                mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));                          // add a library here\r
                        }\r
-                       \r
+\r
+                       r8tor9 temp;\r
                        if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
                                if (!r8thing.equals(r9thing)) {\r
-                                       if (line.contains(r8thing)) {\r
-                                               line = line.replaceAll(r8thing, r9thing);\r
+                                       if (wholeline.contains(r8thing)) {\r
+                                               wholeline = wholeline.replaceAll(r8thing, r9thing);\r
                                                filefunc.add(new r8tor9(r8thing, r9thing));\r
                                                Iterator<r8tor9> rt = filefunc.iterator();\r
                                                while (rt.hasNext()) {\r
@@ -185,7 +146,7 @@ public final class SourceFileReplacer {
                        }\r
                }                                                                                                                       //is any of the guids changed?\r
                if (addr8 == true) {\r
-                       line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
+                       wholeline = addincludefile(wholeline, "\"R8Lib.h\"");\r
                }\r
                \r
                // Converting macro\r
@@ -194,64 +155,64 @@ public final class SourceFileReplacer {
                        r8thing = it.next();\r
                        //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));                \r
                        if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
-                               if (line.contains(r8thing)) {\r
-                                       line = line.replaceAll(r8thing, r9thing);\r
+                               if (wholeline.contains(r8thing)) {\r
+                                       wholeline = wholeline.replaceAll(r8thing, r9thing);\r
                                        filemacro.add(new r8tor9(r8thing, r9thing));\r
                                }\r
                        }\r
                }\r
 \r
                // Converting guid\r
-               replaceGuid(line, mi.guid, "guid", fileguid);\r
-               replaceGuid(line, mi.ppi, "ppi", fileppi);\r
-               replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
+               replaceGuid(wholeline, mi.guid, "guid", fileguid);\r
+               replaceGuid(wholeline, mi.ppi, "ppi", fileppi);\r
+               replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);\r
 \r
                // Converting Pei\r
                // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
                Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
                if (mi.moduletype.contains("PEIM")) {\r
-                       Matcher mtrpei = ptnpei.matcher(line);\r
+                       Matcher mtrpei = ptnpei.matcher(wholeline);\r
                        while (mtrpei.find()) {                                                                         // ! add a library here !\r
-                               line = mtrpei.replaceAll("PeiServices$1#%$2");\r
+                               wholeline = mtrpei.replaceAll("PeiServices$1#%$2");\r
                                mi.hashrequiredr9libs.add("PeiServicesLib");\r
                        }\r
                        mtrpei.reset();\r
-                       if (line.contains("PeiServicesCopyMem")) {\r
-                               line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
+                       if (wholeline.contains("PeiServicesCopyMem")) {\r
+                               wholeline = wholeline.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
                                mi.hashrequiredr9libs.add("BaseMemoryLib");\r
                        }\r
-                       if (line.contains("PeiServicesSetMem")) {\r
-                               line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
+                       if (wholeline.contains("PeiServicesSetMem")) {\r
+                               wholeline = wholeline.replaceAll("PeiServicesSetMem#%", "SetMem");\r
                                mi.hashrequiredr9libs.add("BaseMemoryLib");\r
                        }\r
 \r
                        // Second , find all #% to drop the arg "PeiServices"\r
                        Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
-                       Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
+                       Matcher mtrpeiarg = ptnpeiarg.matcher(wholeline);\r
                        while (mtrpeiarg.find()) {\r
-                               line = mtrpeiarg.replaceAll("$1");\r
+                               wholeline = mtrpeiarg.replaceAll("$1");\r
                        }\r
                }\r
                \r
                Matcher mtrmac;\r
-               mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
+               mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);\r
                if (mtrmac.find()) {\r
-                       line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
+                       wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
                }\r
-               mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
+               mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);\r
                if (mtrmac.find()) {\r
-                       line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
+                       wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
                }\r
-               mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
+               mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);\r
                if (mtrmac.find()) {\r
-                       line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
+                       wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
                }\r
-               mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
+               mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);\r
                if (mtrmac.find()) {\r
-                       line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
+                       wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
                }\r
-               if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
-                       line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
+               if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {\r
+                       wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
                }\r
 \r
                show(filefunc, "function");\r
@@ -270,7 +231,7 @@ public final class SourceFileReplacer {
                fileprotocol.clear();\r
                filer8only.clear();\r
 \r
-               return line;\r
+               return wholeline;\r
        }\r
        \r
        private static final void show(Set<r8tor9> hash, String sh) {\r
@@ -305,4 +266,60 @@ public final class SourceFileReplacer {
                        }\r
                }\r
        }\r
+       \r
+       //-----------------------------------ForDoAll-----------------------------------//\r
+       public void run(String filepath) throws Exception {\r
+               String outname = null;\r
+               String inname = filepath.replace(mi.modulepath + File.separator, "");\r
+               String tempinpath = mi.modulepath + File.separator + "temp" + File.separator;\r
+               String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;\r
+\r
+               if (inname.contains(".c") || inname.contains(".C")) {\r
+                       if (inname.contains(".C")) {\r
+                               outname = inname.replaceFirst(".C", ".c");\r
+                       } else {\r
+                               outname = inname;\r
+                       }\r
+                       MigrationTool.ui.println("\nModifying file: " + inname);\r
+                       Common.string2file(sourcefilereplace(Common.file2string(tempinpath + inname)), tempoutpath + outname);\r
+               } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".uni")) {\r
+                       if (inname.contains(".H")) {\r
+                               outname = inname.replaceFirst(".H", ".h");\r
+                       } else {\r
+                               outname = inname;\r
+                       }\r
+                       MigrationTool.ui.println("\nCopying file: " + inname);\r
+                       Common.string2file(Common.file2string(tempinpath + inname), tempoutpath + outname);\r
+               } else if (inname.contains(".dxs")) {\r
+                       outname = inname;\r
+                       MigrationTool.ui.println("\nModifying file: " + inname);\r
+                       Common.string2file(convertdxs(Common.file2string(tempinpath + inname)), tempoutpath + outname);\r
+               }\r
+       }\r
+       \r
+       public boolean dirFilter(String filepath) {\r
+               return true;\r
+       }\r
+       \r
+       public boolean fileFilter(String filepath) {\r
+               return true;\r
+       }\r
+       //-----------------------------------ForDoAll-----------------------------------//\r
+       \r
+       private final void setModuleInfo(ModuleInfo moduleinfo) {\r
+               mi = moduleinfo;\r
+       }\r
+       \r
+       private final void start() throws Exception {\r
+               Common.toDoAll(mi.localmodulesources, this);\r
+               \r
+               if (!mi.hashr8only.isEmpty()) {\r
+                       addr8only();\r
+               }\r
+       }\r
+       \r
+       public static final void fireAt(ModuleInfo moduleinfo) throws Exception {\r
+               SFReplacer.setModuleInfo(moduleinfo);\r
+               SFReplacer.start();\r
+       }\r
 }\r