X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=Tools%2FSource%2FMigrationTools%2Forg%2Ftianocore%2Fmigration%2FSourceFileReplacer.java;h=67e5bc25677ee4fdd9067039a14e41f5aaad6d2c;hp=c1c36981d5d8ac78f86e1cadade3fa99e97d5937;hb=eff5170cce433b0cb6a77691237af414b6ce3818;hpb=5ea254f612159f26b7921a95f0d9cfd5719eae1e diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java b/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java index c1c36981d5..67e5bc2567 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java @@ -17,22 +17,12 @@ import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; -public final class SourceFileReplacer { - SourceFileReplacer(String path, String outpath, ModuleInfo moduleinfo, Database database, UI fp) { - modulepath = path; - outputpath = outpath; - mi = moduleinfo; - db = database; - ui = fp; - } - private String modulepath; - private String outputpath; +public final class SourceFileReplacer implements Common.ForDoAll { + private static final SourceFileReplacer SFReplacer = new SourceFileReplacer(); private ModuleInfo mi; - private Database db; - private UI ui; - private boolean showdetails = false; + private static boolean showdetails = true; // set this as default now, may be changed in the future - private class r8tor9 { + private static class r8tor9 { r8tor9(String r8, String r9) { r8thing = r8; r9thing = r9; @@ -42,73 +32,30 @@ public final class SourceFileReplacer { } // these sets are used only for printing log of the changes in current file - private Set filefunc = new HashSet(); - private Set filemacro = new HashSet(); - private Set fileguid = new HashSet(); - private Set fileppi = new HashSet(); - private Set fileprotocol = new HashSet(); - private Set filer8only = new HashSet(); + private static final Set filefunc = new HashSet(); + private static final Set filemacro = new HashSet(); + private static final Set fileguid = new HashSet(); + private static final Set fileppi = new HashSet(); + private static final Set fileprotocol = new HashSet(); + private static final Set filer8only = new HashSet(); - private static final String r8only = "EfiLibInstallDriverBinding " + - "EfiLibInstallAllDriverProtocols " + - "EfiLibCompareLanguage " + - "BufToHexString " + - "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!! - "EfiValueToHexStr " + - "HexStringToBuf " + - "IsHexDigit " + - "NibbleToHexChar " + - "GetHob " + - "GetHobListSize " + - "GetHobVersion " + - "GetHobBootMode " + - "GetCpuHobInfo " + - "GetDxeCoreHobInfo " + - "GetNextFirmwareVolumeHob " + - "GetNextGuidHob " + - "GetPalEntryHobInfo " + - "GetIoPortSpaceAddressHobInfo "; + private static final String addincludefile(String wholeline, String hfile) { + return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n"); + } - public void flush() throws Exception { - String outname = null; - String inname = null; - if (ui.yesOrNo("Changes will be made to the Source Code. View details?")) { - showdetails = true; - } - - Iterator di = mi.localmodulesources.iterator(); - while (di.hasNext()) { - inname = di.next(); - if (inname.contains(".c") || inname.contains(".C")) { - if (inname.contains(".C")) { - outname = inname.replaceFirst(".C", ".c"); - } else { - outname = inname; - } - ui.println("\nModifying file: " + inname); - Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname); - } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) { - if (inname.contains(".H")) { - outname = inname.replaceFirst(".H", ".h"); - } else { - outname = inname; - } - ui.println("\nCopying file: " + inname); - Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), outputpath + File.separator + "Migration_" + mi.modulename + File.separator + outname); - } - } - - if (!mi.hashr8only.isEmpty()) { - addr8only(); + private final String convertdxs(String wholeline) { + if (mi.getModuleType().equals("PEIM")) { + return addincludefile(wholeline, "\\"); + } else { + return addincludefile(wholeline, "\\"); } } - private void addr8only() throws Exception { + private final void addr8only() throws Exception { String paragraph = null; - String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c"); - Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c"); - PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c"))); - PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(outputpath + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h"))); + String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c"); + PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c"))); + PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h"))); Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL); Matcher mtrr8only = ptnr8only.matcher(line); Matcher mtrr8onlyhead; @@ -136,28 +83,17 @@ public final class SourceFileReplacer { } // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you! - private String sourcefilereplace(String filename) throws Exception { - BufferedReader rd = new BufferedReader(new FileReader(filename)); - StringBuffer wholefile = new StringBuffer(); - String line; - String r8thing; - String r9thing; - r8tor9 temp; + private final String sourcefilereplace(String wholeline) throws Exception { boolean addr8 = false; Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed ! //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE); - while ((line = rd.readLine()) != null) { - wholefile.append(line + "\n"); - } - line = wholefile.toString(); - // replace BS -> gBS , RT -> gRT - Matcher mat = pat.matcher(line); + Matcher mat = pat.matcher(wholeline); if (mat.find()) { // add a library here - ui.println("Converting all BS->gBS, RT->gRT"); - line = mat.replaceAll("g$1$2$3"); //unknown correctiveness + MigrationTool.ui.println("Converting all BS->gBS, RT->gRT"); + wholeline = mat.replaceAll("g$1$2$3"); //unknown correctiveness } mat.reset(); while (mat.find()) { @@ -168,16 +104,12 @@ public final class SourceFileReplacer { mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); } } - /* // remove EFI_DRIVER_ENTRY_POINT - Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}"); - Matcher matentrypoint = patentrypoint.matcher(line); - if (matentrypoint.find()) { - ui.println("Deleting Entry_Point"); - line = matentrypoint.replaceAll(""); - } - */ + wholeline = wholeline.replaceAll("(EFI_\\w+_ENTRY_POINT)", MigrationTool.MIGRATIONCOMMENT + " $1"); + // start replacing names + String r8thing; + String r9thing; Iterator it; // Converting non-locla function it = mi.hashnonlocalfunc.iterator(); @@ -191,18 +123,19 @@ public final class SourceFileReplacer { mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l } else { // - mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here + mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here } - - if ((r9thing = db.getR9Func(r8thing)) != null) { + + r8tor9 temp; + if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) { if (!r8thing.equals(r9thing)) { - if (line.contains(r8thing)) { - line = line.replaceAll(r8thing, r9thing); + if (wholeline.contains(r8thing)) { + wholeline = wholeline.replaceAll(r8thing, r9thing); filefunc.add(new r8tor9(r8thing, r9thing)); Iterator rt = filefunc.iterator(); while (rt.hasNext()) { temp = rt.next(); - if (r8only.contains(temp.r8thing)) { + if (MigrationTool.db.r8only.contains(temp.r8thing)) { filer8only.add(r8thing); mi.hashr8only.add(r8thing); addr8 = true; @@ -213,73 +146,73 @@ public final class SourceFileReplacer { } } //is any of the guids changed? if (addr8 == true) { - line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n"); + wholeline = addincludefile(wholeline, "\"R8Lib.h\""); } // Converting macro it = mi.hashnonlocalmacro.iterator(); while (it.hasNext()) { //macros are all assumed MdePkg currently r8thing = it.next(); - //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); - if ((r9thing = db.getR9Macro(r8thing)) != null) { - if (line.contains(r8thing)) { - line = line.replaceAll(r8thing, r9thing); + //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); + if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) { + if (wholeline.contains(r8thing)) { + wholeline = wholeline.replaceAll(r8thing, r9thing); filemacro.add(new r8tor9(r8thing, r9thing)); } } } // Converting guid - replaceGuid(line, mi.guid, "guid", fileguid); - replaceGuid(line, mi.ppi, "ppi", fileppi); - replaceGuid(line, mi.protocol, "protocol", fileprotocol); + replaceGuid(wholeline, mi.guid, "guid", fileguid); + replaceGuid(wholeline, mi.ppi, "ppi", fileppi); + replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol); // Converting Pei // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #% Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE); if (mi.moduletype.contains("PEIM")) { - Matcher mtrpei = ptnpei.matcher(line); + Matcher mtrpei = ptnpei.matcher(wholeline); while (mtrpei.find()) { // ! add a library here ! - line = mtrpei.replaceAll("PeiServices$1#%$2"); + wholeline = mtrpei.replaceAll("PeiServices$1#%$2"); mi.hashrequiredr9libs.add("PeiServicesLib"); } mtrpei.reset(); - if (line.contains("PeiServicesCopyMem")) { - line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem"); + if (wholeline.contains("PeiServicesCopyMem")) { + wholeline = wholeline.replaceAll("PeiServicesCopyMem#%", "CopyMem"); mi.hashrequiredr9libs.add("BaseMemoryLib"); } - if (line.contains("PeiServicesSetMem")) { - line = line.replaceAll("PeiServicesSetMem#%", "SetMem"); + if (wholeline.contains("PeiServicesSetMem")) { + wholeline = wholeline.replaceAll("PeiServicesSetMem#%", "SetMem"); mi.hashrequiredr9libs.add("BaseMemoryLib"); } // Second , find all #% to drop the arg "PeiServices" Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE); - Matcher mtrpeiarg = ptnpeiarg.matcher(line); + Matcher mtrpeiarg = ptnpeiarg.matcher(wholeline); while (mtrpeiarg.find()) { - line = mtrpeiarg.replaceAll("$1"); + wholeline = mtrpeiarg.replaceAll("$1"); } } Matcher mtrmac; - mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line); + mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline); if (mtrmac.find()) { - line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)"); + wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)"); } - mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line); + mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline); if (mtrmac.find()) { - line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)"); + wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)"); } - mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line); + mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline); if (mtrmac.find()) { - line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)"); + wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)"); } - mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line); + mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline); if (mtrmac.find()) { - line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)"); + wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)"); } - if (line.contains("EFI_UINTN_ALIGN_MASK")) { - line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)"); + if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) { + wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)"); } show(filefunc, "function"); @@ -288,7 +221,7 @@ public final class SourceFileReplacer { show(fileppi, "ppi"); show(fileprotocol, "protocol"); if (!filer8only.isEmpty()) { - ui.println("Converting r8only : " + filer8only); + MigrationTool.ui.println("Converting r8only : " + filer8only); } filefunc.clear(); @@ -298,32 +231,32 @@ public final class SourceFileReplacer { fileprotocol.clear(); filer8only.clear(); - return line; + return wholeline; } - private void show(Set hash, String sh) { + private static final void show(Set hash, String sh) { Iterator it = hash.iterator(); r8tor9 temp; if (showdetails) { if (!hash.isEmpty()) { - ui.print("Converting " + sh + " : "); + MigrationTool.ui.print("Converting " + sh + " : "); while (it.hasNext()) { temp = it.next(); - ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] "); + MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] "); } - ui.println(""); + MigrationTool.ui.println(""); } } } - private void replaceGuid(String line, Set hash, String kind, Set filehash) { + private static final void replaceGuid(String line, Set hash, String kind, Set filehash) { Iterator it; String r8thing; String r9thing; it = hash.iterator(); while (it.hasNext()) { r8thing = it.next(); - if ((r9thing = db.getR9Guidname(r8thing)) != null) { + if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) { if (!r8thing.equals(r9thing)) { if (line.contains(r8thing)) { line = line.replaceAll(r8thing, r9thing); @@ -333,4 +266,60 @@ public final class SourceFileReplacer { } } } + + //-----------------------------------ForDoAll-----------------------------------// + public void run(String filepath) throws Exception { + String outname = null; + String inname = filepath.replace(mi.modulepath + File.separator, ""); + String tempinpath = mi.modulepath + File.separator + "temp" + File.separator; + String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator; + + if (inname.contains(".c") || inname.contains(".C")) { + if (inname.contains(".C")) { + outname = inname.replaceFirst(".C", ".c"); + } else { + outname = inname; + } + MigrationTool.ui.println("\nModifying file: " + inname); + Common.string2file(sourcefilereplace(Common.file2string(tempinpath + inname)), tempoutpath + outname); + } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".uni")) { + if (inname.contains(".H")) { + outname = inname.replaceFirst(".H", ".h"); + } else { + outname = inname; + } + MigrationTool.ui.println("\nCopying file: " + inname); + Common.string2file(Common.file2string(tempinpath + inname), tempoutpath + outname); + } else if (inname.contains(".dxs")) { + outname = inname; + MigrationTool.ui.println("\nModifying file: " + inname); + Common.string2file(convertdxs(Common.file2string(tempinpath + inname)), tempoutpath + outname); + } + } + + public boolean dirFilter(String filepath) { + return true; + } + + public boolean fileFilter(String filepath) { + return true; + } + //-----------------------------------ForDoAll-----------------------------------// + + private final void setModuleInfo(ModuleInfo moduleinfo) { + mi = moduleinfo; + } + + private final void start() throws Exception { + Common.toDoAll(mi.localmodulesources, this); + + if (!mi.hashr8only.isEmpty()) { + addr8only(); + } + } + + public static final void fireAt(ModuleInfo moduleinfo) throws Exception { + SFReplacer.setModuleInfo(moduleinfo); + SFReplacer.start(); + } }