Add MigrationTool.java
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / ModuleInfo.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17import java.util.regex.*;\r
18\r
19/*\r
20 Class ModuleInfo is built for scanning the source files, it contains all the needed\r
21information and all the temporary data.\r
22*/\r
23public class ModuleInfo {\r
5ea254f6 24 ModuleInfo(String modulepath) throws Exception {\r
0dc8c589 25 this.modulepath = modulepath;\r
5ea254f6 26 \r
27 ui.println("Choose where to place the result");\r
28 if ((outputpath = ui.getFilepath()) == null) {\r
29 outputpath = modulepath; \r
30 }\r
31 ui.println(outputpath);\r
32 \r
0dc8c589 33 moduleScan();\r
34 }\r
5ea254f6 35\r
d1d10647 36 public static UI ui = null; //if MIM is still usefull, this can be given to it\r
37 public static Database db = null; //if MIM is still usefull, this can be given to it\r
5ea254f6 38 \r
39 public String modulepath = null;\r
0dc8c589 40 \r
5ea254f6 41 public String outputpath = null;\r
0dc8c589 42 \r
43 public String modulename = null;\r
44 public String guidvalue = null;\r
45 public String moduletype = null;\r
46 public String entrypoint = null;\r
47 \r
48 public Set<String> localmodulesources = new HashSet<String>(); //contains both .c and .h\r
0dc8c589 49 public Set<String> preprocessedccodes = new HashSet<String>();\r
e6a5df3b 50 public Set<String> msaorinf = new HashSet<String>(); //only a little, hash may be too big for this\r
0dc8c589 51 \r
52 public Set<String> hashfuncc = new HashSet<String>();\r
53 public Set<String> hashfuncd = new HashSet<String>();\r
54 public Set<String> hashnonlocalfunc = new HashSet<String>();\r
55 public Set<String> hashnonlocalmacro = new HashSet<String>();\r
56 public Set<String> hashEFIcall = new HashSet<String>();\r
57 public Set<String> hashr8only = new HashSet<String>();\r
58 \r
59 public Set<String> hashrequiredr9libs = new HashSet<String>(); // hashrequiredr9libs is now all added in SourceFileReplacer \r
60 public Set<String> guid = new HashSet<String>();\r
61 public Set<String> protocol = new HashSet<String>();\r
62 public Set<String> ppi = new HashSet<String>();\r
63 \r
64 private static String migrationcomment = "//%$//";\r
65 \r
e6a5df3b 66 private void moduleScan() throws Exception {\r
5ea254f6 67 Common.toDoAll(modulepath, ModuleInfo.class.getMethod("enroll", String.class), this, null, Common.FILE);\r
68 \r
e6a5df3b 69 String filename = null;\r
70 if (msaorinf.isEmpty()) {\r
f7fee0ea 71 ui.println("No INF nor MSA file found!");\r
e6a5df3b 72 System.exit(0);\r
0dc8c589 73 } else {\r
5ea254f6 74 filename = ui.choose("Found .inf or .msa file for module\n" + modulepath + "\nChoose one Please", msaorinf.toArray());\r
e6a5df3b 75 }\r
5ea254f6 76 //ModuleReader mr = new ModuleReader(modulepath, this, db, ui);\r
e6a5df3b 77 if (filename.contains(".inf")) {\r
5ea254f6 78 ModuleReader.readInf(filename, this);\r
e6a5df3b 79 } else if (filename.contains(".msa")) {\r
5ea254f6 80 ModuleReader.readMsa(filename, this);\r
0dc8c589 81 }\r
82 \r
83 CommentOutNonLocalHFile();\r
84 parsePreProcessedSourceCode();\r
5ea254f6 85\r
86 new SourceFileReplacer(modulepath, outputpath, this, db, ui).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"\r
0dc8c589 87 \r
88 // show result\r
f7fee0ea 89 if (ui.yesOrNo("Parse of the Module Information has completed. View details?")) {\r
0dc8c589 90 ui.println("\nModule Information : ");\r
91 ui.println("Entrypoint : " + entrypoint);\r
92 show(protocol, "Protocol : ");\r
93 show(ppi, "Ppi : ");\r
94 show(guid, "Guid : ");\r
95 show(hashfuncc, "call : ");\r
96 show(hashfuncd, "def : ");\r
97 show(hashEFIcall, "EFIcall : ");\r
98 show(hashnonlocalmacro, "macro : ");\r
99 show(hashnonlocalfunc, "nonlocal : ");\r
100 show(hashr8only, "hashr8only : ");\r
101 }\r
102 \r
5ea254f6 103 new MsaWriter(modulepath, outputpath, this, db, ui).flush();\r
104\r
105 Common.deleteDir(modulepath + File.separator + "temp");\r
106 //Common.toDoAll(modulepath + File.separator + "temp", Common.class.getMethod("deleteDir", String.class), null, null, Common.DIR);\r
0dc8c589 107 \r
108 ui.println("Errors Left : " + db.error);\r
109 ui.println("Complete!");\r
f7fee0ea 110 ui.println("Your R9 module was placed here: " + modulepath + File.separator + "result");\r
111 ui.println("Your logfile was placed here: " + modulepath);\r
0dc8c589 112 }\r
113 \r
114 private void show(Set<String> hash, String show) {\r
115 ui.println(show + hash.size());\r
116 ui.println(hash);\r
117 }\r
e6a5df3b 118 \r
0dc8c589 119 // add '//' to all non-local include lines\r
120 private void CommentOutNonLocalHFile() throws IOException {\r
121 BufferedReader rd;\r
122 String line;\r
123 String curFile;\r
124 PrintWriter outfile;\r
125\r
126 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");\r
e6a5df3b 127 Matcher mtrinclude;\r
5ea254f6 128\r
0dc8c589 129 Iterator<String> ii = localmodulesources.iterator();\r
130 while ( ii.hasNext() ) {\r
131 curFile = ii.next();\r
132 rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));\r
90503bad 133 Common.ensureDir(modulepath + File.separator + "temp" + File.separator + curFile);\r
0dc8c589 134 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));\r
135 while ((line = rd.readLine()) != null) {\r
136 if (line.contains("#include")) {\r
e6a5df3b 137 mtrinclude = ptninclude.matcher(line);\r
138 if (mtrinclude.find() && localmodulesources.contains(mtrinclude.group(1))) {\r
0dc8c589 139 } else {\r
140 line = migrationcomment + line;\r
141 }\r
142 }\r
143 outfile.append(line + '\n');\r
144 }\r
145 outfile.flush();\r
146 outfile.close();\r
147 }\r
148 }\r
e6a5df3b 149\r
0dc8c589 150 private void parsePreProcessedSourceCode() throws Exception {\r
151 //Cl cl = new Cl(modulepath);\r
152 //cl.execute("Fat.c");\r
153 //cl.generateAll(preprocessedccodes);\r
154 //\r
155 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r
156 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r
157 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add\r
0dc8c589 158 BufferedReader rd = null;\r
159 String ifile = null;\r
160 String line = null;\r
161 String temp = null;\r
e6a5df3b 162 \r
163 Iterator<String> ii = localmodulesources.iterator();\r
164 while (ii.hasNext()) {\r
165 temp = ii.next();\r
166 if (temp.contains(".c")) {\r
167 preprocessedccodes.add(temp);\r
168 }\r
169 }\r
170 \r
171 ii = preprocessedccodes.iterator();\r
0dc8c589 172 \r
173 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r
174 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);\r
175 Matcher matguid;\r
176 Matcher matfuncc;\r
177 Matcher matfuncd;\r
178 Matcher matenclosereplace;\r
179 Matcher matefifuncc;\r
180 Matcher matentrypoint;\r
181 Matcher matmacro;\r
182 \r
183 while (ii.hasNext()) {\r
184 StringBuffer wholefile = new StringBuffer();\r
185 ifile = ii.next();\r
186 rd = new BufferedReader(new FileReader(modulepath + File.separator + "temp" + File.separator + ifile));\r
187 while ((line = rd.readLine()) != null) {\r
188 wholefile.append(line + '\n');\r
189 }\r
190 line = wholefile.toString();\r
191 \r
192 // if this is a Pei phase module , add these library class to .msa\r
193 matentrypoint = patentrypoint.matcher(line);\r
194 if (matentrypoint.find()) {\r
195 entrypoint = matentrypoint.group(2);\r
196 if (matentrypoint.group(1).matches("PEIM")) {\r
197 hashrequiredr9libs.add("PeimEntryPoint");\r
198 } else {\r
199 hashrequiredr9libs.add("UefiDriverEntryPoint");\r
200 }\r
201 }\r
202 \r
203 // find guid\r
204 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :\r
205 while (matguid.find()) { // 1.currently , find once , then call to identify which is it\r
5ea254f6 206 if ((temp = Guid.register(matguid, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each\r
0dc8c589 207 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost\r
208 }\r
209 }\r
210 //matguid.appendTail(result);\r
211 //line = result.toString();\r
212\r
213 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r
214 // This item is not simply replaced , special operation is required.\r
215 matefifuncc = patefifuncc.matcher(line);\r
216 while (matefifuncc.find()) {\r
217 hashEFIcall.add(matefifuncc.group(2));\r
218 }\r
219\r
220 // find function call\r
221 matfuncc = Func.ptnfuncc.matcher(line);\r
222 while (matfuncc.find()) {\r
223 if ((temp = Func.register(matfuncc, this, db)) != null) {\r
224 //ui.println(ifile + " dofunc " + temp);\r
225 //matfuncc.appendReplacement(result, db.getR9Func(temp));\r
226 }\r
227 }\r
228 //matfuncc.appendTail(result);\r
229 //line = result.toString();\r
230\r
231 // find macro\r
232 matmacro = Macro.ptntmacro.matcher(line);\r
233 while (matmacro.find()) {\r
234 if ((temp = Macro.register(matmacro, this, db)) != null) {\r
235 }\r
236 }\r
237 \r
238 // find function definition\r
239 // replace all {} to @\r
240 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
241 line = matenclosereplace.replaceAll("@");\r
242 }\r
243\r
244 matfuncd = Func.ptnfuncd.matcher(line);\r
245 while (matfuncd.find()) {\r
246 if ((temp = Func.register(matfuncd, this, db)) != null) {\r
247 }\r
248 }\r
249 }\r
250 \r
251 // op on hash\r
252 Iterator<String> funcci = hashfuncc.iterator();\r
253 while (funcci.hasNext()) {\r
254 if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {\r
255 hashnonlocalfunc.add(temp); // this set contains both changed and not changed items\r
256 }\r
257 }\r
258 }\r
259 \r
5ea254f6 260 public final void enroll(String filepath) throws Exception {\r
261 String[] temp;\r
262 if (filepath.contains(".c") || filepath.contains(".C") || filepath.contains(".h") || \r
263 filepath.contains(".H") || filepath.contains(".dxs") || filepath.contains(".uni")) {\r
264 temp = filepath.split("\\\\");\r
265 localmodulesources.add(temp[temp.length - 1]);\r
266 } else if (filepath.contains(".inf") || filepath.contains(".msa")) {\r
267 temp = filepath.split("\\\\");\r
268 msaorinf.add(temp[temp.length - 1]);\r
269 }\r
270 }\r
271 \r
272 public static final void seekModule(String filepath) throws Exception {\r
273 if (isModule(filepath)) {\r
274 //System.out.println("I'm in");\r
275 new ModuleInfo(filepath);\r
276 }\r
277 }\r
278\r
279 private static final boolean isModule(String path) {\r
280 String[] list = new File(path).list();\r
281 for (int i = 0 ; i < list.length ; i++) {\r
282 if (!new File(list[i]).isDirectory()) {\r
283 if (list[i].contains(".inf") || list[i].contains(".msa")) {\r
284 return true;\r
285 }\r
286 }\r
287 }\r
288 return false;\r
289 }\r
d1d10647 290/*\r
5ea254f6 291 public static final void main(String[] args) throws Exception {\r
292 ui = FirstPanel.init();\r
293 db = new Database();\r
0dc8c589 294 }\r
d1d10647 295 */\r
e6a5df3b 296}