diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/Common.java b/Tools/Source/MigrationTools/org/tianocore/migration/Common.java index 2f10521238..0ca7ca8cb3 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/Common.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/Common.java @@ -22,8 +22,8 @@ public final class Common { public static final int FILE = 1; public static final int DIR = 2; - public static final String strseparate = "(.*)\\\\([^\\\\]*)"; - public static final Pattern ptnseparate = Pattern.compile("(.*)\\\\([^\\\\]*)"); + public static final String STRSEPARATER = "(.*)\\\\([^\\\\]*)"; + public static final Pattern PTNSEPARATER = Pattern.compile("(.*)\\\\([^\\\\]*)"); //-------------------------------------regex------------------------------------------// @@ -53,6 +53,7 @@ public final class Common { while ((line = rd.readLine()) != null) { wholefile.append(line + "\n"); } + rd.close(); return wholefile.toString(); } @@ -64,6 +65,10 @@ public final class Common { outfile.close(); } + public static final void fileCopy(String src, String des) throws Exception { + string2file(file2string(src), des); + } + //-----------------------------------file&string---------------------------------------// //--------------------------------------dir--------------------------------------------// @@ -76,7 +81,7 @@ public final class Common { */ public static final void ensureDir(String objFileWhole) { File tempdir; - Matcher mtrseparate = ptnseparate.matcher(objFileWhole); + Matcher mtrseparate = PTNSEPARATER.matcher(objFileWhole); if (mtrseparate.find()) { tempdir = new File(mtrseparate.group(1)); if (!tempdir.exists()) tempdir.mkdirs(); @@ -98,7 +103,7 @@ public final class Common { } public static final String dirCopy_(String src) throws Exception { - Matcher mtrseparate = Common.ptnseparate.matcher(src); + Matcher mtrseparate = Common.PTNSEPARATER.matcher(src); if (mtrseparate.find()) { dirCopy(src, mtrseparate.group(1) + File.separator + "_" + mtrseparate.group(2)); } @@ -109,16 +114,28 @@ public final class Common { String[] list = new File(src).list(); File test; + ensureDir(des); for (int i = 0 ; i < list.length ; i++) { test = new File(src + File.separator + list[i]); if (test.isDirectory()) { dirCopy(src + File.separator + list[i], des + File.separator + list[i]); } else { - ensureDir(des + File.separator + list[i]); + //ensureDir(des + File.separator + list[i]); string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]); } } } + + public static final void oneLevelDirCopy(String src, String des, String type) throws Exception { + String[] list = new File(src).list(); + + ensureDir(des); + for (int i = 0; i < list.length; i++) { + if (list[i].contains(type)) { + string2file(file2string(src + File.separator + list[i]), des + File.separator + list[i]); + } + } + } //--------------------------------------dir--------------------------------------------// diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java b/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java index 95266fc42f..5acf56f60f 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java @@ -180,7 +180,7 @@ public final class FirstPanel extends JPanel implements ActionListener, ItemList } if ( e.getSource() == goButton ) { try { - logfile = new PrintWriter(new BufferedWriter(new FileWriter(startpath.replaceAll(Common.strseparate, "$1") + File.separator + "migration.log"))); + logfile = new PrintWriter(new BufferedWriter(new FileWriter(startpath.replaceAll(Common.STRSEPARATER, "$1") + File.separator + "migration.log"))); MigrationTool.startMigrateAll(startpath); logfile.flush(); logfile.close(); diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/MigrationTool.java b/Tools/Source/MigrationTools/org/tianocore/migration/MigrationTool.java index edb6a95a34..7a37484914 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/MigrationTool.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/MigrationTool.java @@ -31,7 +31,7 @@ public class MigrationTool { private static final void mainFlow(ModuleInfo mi) throws Exception { - ModuleReader.ModuleScan(mi); + ModuleReader.aimAt(mi); //MigrationTool.ui.yesOrNo("go on replace?"); SourceFileReplacer.fireAt(mi); // some adding library actions are taken here,so it must be put before "MsaWriter" @@ -76,7 +76,7 @@ public class MigrationTool { private static final String assignOutPutPath(String inputpath) { if (MigrationTool.defaultoutput) { - return inputpath.replaceAll(Common.strseparate, "$1"); + return inputpath.replaceAll(Common.STRSEPARATER, "$1"); } else { return MigrationTool.ui.getFilepath("Please choose where to place the output module", JFileChooser.DIRECTORIES_ONLY); } diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java index 791ccd23fa..912e31ced9 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java @@ -34,6 +34,8 @@ public final class ModuleInfo { public final Set localmodulesources = new HashSet(); //contains both .c and .h public final Set preprocessedccodes = new HashSet(); public final Set msaorinf = new HashSet(); //only a little, hash may be too big for this + public final Set infincludes = new HashSet(); + public final Set infsources = new HashSet(); public final Set hashfuncc = new HashSet(); public final Set hashfuncd = new HashSet(); diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java index a7f97a975d..a0a53d668c 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java @@ -18,18 +18,19 @@ import java.util.regex.*; import org.tianocore.*; -public final class ModuleReader { - private static ModuleInfo mi; +public final class ModuleReader implements Common.ForDoAll { + private static final ModuleReader modulereader = new ModuleReader(); + private ModuleInfo mi; + private final CommentLaplace commentlaplace = new CommentLaplace(); private static final Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*=\\s*([^\\s]*)"); private static final Pattern ptnsection = Pattern.compile("\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE); private static final Pattern ptnfilename = Pattern.compile("[^\\s]+"); - public static final void ModuleScan(ModuleInfo m) throws Exception { - mi = m; - + public final void ModuleScan() throws Exception { Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll", String.class), mi, null, Common.FILE); + // inf&msa String filename = null; if (mi.msaorinf.isEmpty()) { MigrationTool.ui.println("No INF nor MSA file found!"); @@ -47,13 +48,12 @@ public final class ModuleReader { } else if (filename.contains(".msa")) { readMsa(filename); } + // inf&msa - CommentOutNonLocalHFile(); - parsePreProcessedSourceCode(); - + preProcessModule(); } - private static final void readMsa(String name) throws Exception { + private final void readMsa(String name) throws Exception { ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(new File(mi.modulepath + File.separator + name)); ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.getModuleSurfaceArea(); MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader(); @@ -73,7 +73,7 @@ public final class ModuleReader { } } - private static final void readInf(String name) throws Exception { + private final void readInf(String name) throws Exception { System.out.println("\nParsing INF file: " + name); String wholeline; Matcher mtrinfequation; @@ -113,48 +113,63 @@ public final class ModuleReader { if (mtrsection.group(1).contains("sources.")) { mtrfilename = ptnfilename.matcher(mtrsection.group(2)); while (mtrfilename.find()) { + mi.infsources.add(mtrfilename.group()); if (!mi.localmodulesources.contains(mtrfilename.group())) { - MigrationTool.ui.println("Source File Missing! : " + mtrfilename.group()); + MigrationTool.ui.println("Warn: Source File Missing! : " + mtrfilename.group()); } } } + if (mtrsection.group(1).matches("includes.common")) { + mtrfilename = ptnfilename.matcher(mtrsection.group(2)); + while (mtrfilename.find()) { + mi.infincludes.add(mtrfilename.group()); + } + } } } - // add '//' to all non-local include lines - private static final void CommentOutNonLocalHFile() throws IOException { - BufferedReader rd; - String line; - String curFile; - PrintWriter outfile; - - Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]"); - Matcher mtrinclude; - - Iterator ii = mi.localmodulesources.iterator(); - while ( ii.hasNext() ) { - curFile = ii.next(); - rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + curFile)); - Common.ensureDir(mi.modulepath + File.separator + "temp" + File.separator + curFile); - outfile = new PrintWriter(new BufferedWriter(new FileWriter(mi.modulepath + File.separator + "temp" + File.separator + curFile))); - - while ((line = rd.readLine()) != null) { - if (line.contains("#include")) { - mtrinclude = ptninclude.matcher(line); - if (mtrinclude.find() && mi.localmodulesources.contains(mtrinclude.group(1))) { - } else { - line = MigrationTool.MIGRATIONCOMMENT + line; - } - } - outfile.append(line + '\n'); + private final void preProcessModule() throws Exception { + // according to .inf file, add extraordinary includes and sourcefiles + Common.dirCopy(mi.modulepath, mi.modulepath + File.separator + "temp"); + + if (!mi.infincludes.isEmpty()) { + Iterator it = mi.infincludes.iterator(); + String tempincludename = null; + while (it.hasNext()) { + tempincludename = it.next(); + if (tempincludename.contains("..")) { + Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename); + if (mtr.find() && !mtr.group(2).matches(".")) { + Common.oneLevelDirCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1") + File.separator + mtr.group(2), mi.modulepath + File.separator + "temp", ".h"); + } else { + Common.oneLevelDirCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1"), mi.modulepath + File.separator + "temp", ".h"); + } + } + } + } + if (!mi.infsources.isEmpty()) { + Iterator it = mi.infsources.iterator(); + String tempsourcename = null; + while (it.hasNext()) { + tempsourcename = it.next(); + if (tempsourcename.contains("..")) { + Common.ensureDir(mi.modulepath + File.separator + "temp" + File.separator + "MT_Parent_Sources"); + Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename); + if (mtr.find()) { + Common.fileCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1") + File.separator + mtr.group(2), mi.modulepath + File.separator + "temp" + File.separator + "MT_Parent_Sources" + File.separator + mtr.group(2)); + } + } } - outfile.flush(); - outfile.close(); - - } + } + + //CommentOutNonLocalHFile(); + Common.toDoAll(mi.modulepath + File.separator + "temp", this, Common.FILE); + + parsePreProcessedSourceCode(); + } - private static final void parsePreProcessedSourceCode() throws Exception { + private final void parsePreProcessedSourceCode() throws Exception { //Cl cl = new Cl(modulepath); //cl.execute("Fat.c"); //cl.generateAll(preprocessedccodes); @@ -263,4 +278,54 @@ public final class ModuleReader { } } } + + public class CommentLaplace extends Common.Laplace { + public String operation(String wholeline) { + StringBuffer wholebuffer = new StringBuffer(); + String templine = null; + Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]"); + Pattern ptninclude = Pattern.compile("#include\\s*(.*)"); + Matcher mtrinclude = ptninclude.matcher(wholeline); + Matcher mtrincludefile = null; + while (mtrinclude.find()) { + mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1)); + if (mtrincludefile.find() && mi.localmodulesources.contains(mtrincludefile.group(1))) { + templine = mtrinclude.group(); + } else { + templine = MigrationTool.MIGRATIONCOMMENT + mtrinclude.group(); + } + mtrinclude.appendReplacement(wholebuffer, templine); + } + mtrinclude.appendTail(wholebuffer); + return wholebuffer.toString(); + } + + public boolean recognize(String filename) { + return filename.contains(".c") || filename.contains(".h"); + } + + public String namechange(String oldname) { + return oldname; + } + } + + //-----------------------------------ForDoAll-----------------------------------// + public void run(String filepath) throws Exception { + String name = mi.modulepath + File.separator + "temp" + File.separator + filepath.replace(mi.modulepath + File.separator + "temp" + File.separator, ""); + commentlaplace.transform(name, name); + } + + public boolean filter(File dir) { + return true; + } + //-----------------------------------ForDoAll-----------------------------------// + + public final void setModuleInfo(ModuleInfo m) { + mi = m; + } + + public static final void aimAt(ModuleInfo mi) throws Exception { + modulereader.setModuleInfo(mi); + modulereader.ModuleScan(); + } } diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java b/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java index 4ff8ebe524..bce108f934 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java @@ -104,7 +104,6 @@ public final class SourceFileReplacer implements Common.ForDoAll { private class CLaplace extends Common.Laplace { public String operation(String wholeline) { - boolean addr8 = false; // remove EFI_DRIVER_ENTRY_POINT wholeline = wholeline.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*" + mi.entrypoint + "\\s*\\)\\s*;)", MigrationTool.MIGRATIONCOMMENT + " $1"); // redefine module entry point for some self-relocated modules @@ -390,7 +389,7 @@ public final class SourceFileReplacer implements Common.ForDoAll { //-----------------------------------ForDoAll-----------------------------------// public void run(String filepath) throws Exception { - String inname = filepath.replace(mi.modulepath + File.separator, ""); + String inname = filepath.replace(mi.modulepath + File.separator + "temp" + File.separator, ""); String tempinpath = mi.modulepath + File.separator + "temp" + File.separator; String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator; @@ -418,7 +417,7 @@ public final class SourceFileReplacer implements Common.ForDoAll { Laplaces.add(new CLaplace()); Laplaces.add(new IdleLaplace()); - Common.toDoAll(mi.localmodulesources, this); + Common.toDoAll(mi.modulepath + File.separator + "temp", this, Common.FILE); if (!mi.hashr8only.isEmpty()) { addr8only();