]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
add dxsfile convert
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
index a12be4a9a876308354d594fc255c25f3fdfd57d1..50606a716004c7c7dc99614b6d729becb8c76a77 100644 (file)
@@ -39,7 +39,6 @@ public final class SourceFileReplacer {
        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
@@ -48,6 +47,8 @@ public final class SourceFileReplacer {
                showdetails = true;                     // set this as default now, may be changed in the future\r
                \r
                Iterator<String> di = mi.localmodulesources.iterator();\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
                while (di.hasNext()) {\r
                        inname = di.next();\r
                        if (inname.contains(".c") || inname.contains(".C")) {\r
@@ -57,15 +58,19 @@ public final class SourceFileReplacer {
                                        outname = inname;\r
                                }\r
                                MigrationTool.ui.println("\nModifying file: " + inname);\r
-                               Common.string2file(sourcefilereplace(mi.modulepath + File.separator + "temp" + File.separator + inname), MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + outname);\r
-                       } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\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(mi.modulepath + File.separator + "temp" + File.separator + inname), MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + outname);\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
@@ -74,6 +79,18 @@ public final class SourceFileReplacer {
                }\r
        }\r
        \r
+       private static final String addincludefile(String wholeline, String hfile) {\r
+               return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
+       }\r
+       \r
+       private static 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
                String paragraph = null;\r
                String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
@@ -106,25 +123,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
+       private static 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
@@ -166,8 +175,8 @@ public final class SourceFileReplacer {
                        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
@@ -183,7 +192,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
@@ -192,64 +201,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
@@ -268,7 +277,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