]> 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 c1c36981d5d8ac78f86e1cadade3fa99e97d5937..67e5bc25677ee4fdd9067039a14e41f5aaad6d2c 100644 (file)
@@ -17,22 +17,12 @@ import java.util.*;
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
-public final class SourceFileReplacer {\r
-       SourceFileReplacer(String path, String outpath, ModuleInfo moduleinfo, Database database, UI fp) {\r
-               modulepath = path;\r
-               outputpath = outpath;\r
-               mi = moduleinfo;\r
-               db = database;\r
-               ui = fp;\r
-       }\r
-       private String modulepath;\r
-       private String outputpath;\r
+public final class SourceFileReplacer implements Common.ForDoAll {\r
+       private static final SourceFileReplacer SFReplacer = new SourceFileReplacer();\r
        private ModuleInfo mi;\r
-       private Database db;\r
-       private UI ui;\r
-       private boolean showdetails = false;\r
+       private static boolean showdetails = true;              // set this as default now, may be changed in the future\r
        \r
-       private class r8tor9 {\r
+       private static class r8tor9 {\r
                r8tor9(String r8, String r9) {\r
                        r8thing = r8;\r
                        r9thing = r9;\r
@@ -42,73 +32,30 @@ public final class SourceFileReplacer {
        }\r
        \r
        // these sets are used only for printing log of the changes in current file\r
-       private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
-       private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
-       private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
-       private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
-       private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
-       private Set<String> filer8only = new HashSet<String>();\r
+       private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
+       private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
+       private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
+       private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
+       private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
+       private static final Set<String> filer8only = new HashSet<String>();\r
        \r
-       private static final String r8only = "EfiLibInstallDriverBinding " +\r
-                       "EfiLibInstallAllDriverProtocols " +\r
-                       "EfiLibCompareLanguage " +\r
-                       "BufToHexString " +\r
-                       "EfiStrTrim " +                                                 //is the r8only lib going to be enlarged????  Caution !!!!\r
-                       "EfiValueToHexStr " +\r
-                       "HexStringToBuf " +\r
-                       "IsHexDigit " +\r
-                       "NibbleToHexChar " +\r
-                       "GetHob " +\r
-                       "GetHobListSize " +\r
-                       "GetHobVersion " +\r
-                       "GetHobBootMode " +\r
-                       "GetCpuHobInfo " +\r
-                       "GetDxeCoreHobInfo " +\r
-                       "GetNextFirmwareVolumeHob " +\r
-                       "GetNextGuidHob " +\r
-                       "GetPalEntryHobInfo " +\r
-                       "GetIoPortSpaceAddressHobInfo ";\r
+       private static final String addincludefile(String wholeline, String hfile) {\r
+               return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
+       }\r
        \r
-       public void flush() throws Exception {\r
-               String outname = null;\r
-               String inname = null;\r
-               if (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
-                               ui.println("\nModifying file: " + inname);\r
-                               Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), 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
-                               ui.println("\nCopying file: " + inname);\r
-                               Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
-                       }\r
-               }\r
-\r
-               if (!mi.hashr8only.isEmpty()) {\r
-                       addr8only();\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 void addr8only() throws Exception {\r
+       private final void addr8only() throws Exception {\r
                String paragraph = null;\r
-               String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c");\r
-               Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
-               PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
-               PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
+               String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\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
@@ -136,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 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
-                       ui.println("Converting all BS->gBS, RT->gRT");\r
-                       line = mat.replaceAll("g$1$2$3");                                                       //unknown correctiveness\r
+                       MigrationTool.ui.println("Converting all BS->gBS, RT->gRT");\r
+                       wholeline = mat.replaceAll("g$1$2$3");                                                  //unknown correctiveness\r
                }\r
                mat.reset();\r
                while (mat.find()) {\r
@@ -168,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
-                       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
@@ -191,18 +123,19 @@ public final class SourceFileReplacer {
                                mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");               //a\r
                                mi.hashrequiredr9libs.add("DxeServicesTableLib");                               //l\r
                        } else {                                                                                                                        //\r
-                               mi.hashrequiredr9libs.add(db.getR9Lib(r8thing));                                // add a library here\r
+                               mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));                          // add a library here\r
                        }\r
-                       \r
-                       if ((r9thing = db.getR9Func(r8thing)) != null) {\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
                                                        temp = rt.next();\r
-                                                       if (r8only.contains(temp.r8thing)) {\r
+                                                       if (MigrationTool.db.r8only.contains(temp.r8thing)) {\r
                                                                filer8only.add(r8thing);\r
                                                                mi.hashr8only.add(r8thing);\r
                                                                addr8 = true;\r
@@ -213,73 +146,73 @@ 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
                it = mi.hashnonlocalmacro.iterator();\r
                while (it.hasNext()) {                                          //macros are all assumed MdePkg currently\r
                        r8thing = it.next();\r
-                       //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing));              \r
-                       if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
-                               if (line.contains(r8thing)) {\r
-                                       line = line.replaceAll(r8thing, r9thing);\r
+                       //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing));                \r
+                       if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\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
@@ -288,7 +221,7 @@ public final class SourceFileReplacer {
                show(fileppi, "ppi");\r
                show(fileprotocol, "protocol");\r
                if (!filer8only.isEmpty()) {\r
-                       ui.println("Converting r8only : " + filer8only);\r
+                       MigrationTool.ui.println("Converting r8only : " + filer8only);\r
                }\r
 \r
                filefunc.clear();\r
@@ -298,32 +231,32 @@ public final class SourceFileReplacer {
                fileprotocol.clear();\r
                filer8only.clear();\r
 \r
-               return line;\r
+               return wholeline;\r
        }\r
        \r
-       private void show(Set<r8tor9> hash, String sh) {\r
+       private static final void show(Set<r8tor9> hash, String sh) {\r
                Iterator<r8tor9> it = hash.iterator();\r
                r8tor9 temp;\r
                if (showdetails) {\r
                        if (!hash.isEmpty()) {\r
-                               ui.print("Converting " + sh + " : ");\r
+                               MigrationTool.ui.print("Converting " + sh + " : ");\r
                                while (it.hasNext()) {\r
                                        temp = it.next();\r
-                                       ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
+                                       MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
                                }\r
-                               ui.println("");\r
+                               MigrationTool.ui.println("");\r
                        }\r
                }\r
        }\r
        \r
-       private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
+       private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
                Iterator<String> it;\r
                String r8thing;\r
                String r9thing;\r
                it = hash.iterator();\r
                while (it.hasNext()) {\r
                        r8thing = it.next();\r
-                       if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
+                       if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
                                if (!r8thing.equals(r9thing)) {\r
                                        if (line.contains(r8thing)) {\r
                                                line = line.replaceAll(r8thing, r9thing);\r
@@ -333,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