Follow changes on grammar in messages by Larry
[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
24 ModuleInfo(String modulepath, UI ui, Database db) throws Exception {\r
25 this.modulepath = modulepath;\r
26 this.ui = ui;\r
27 this.db = db;\r
28 moduleScan();\r
29 }\r
30 \r
31 private String modulepath = null;\r
32 private Database db = null;\r
33 private UI ui = null;\r
34 \r
35 public String modulename = null;\r
36 public String guidvalue = null;\r
37 public String moduletype = null;\r
38 public String entrypoint = null;\r
39 \r
40 public Set<String> localmodulesources = new HashSet<String>(); //contains both .c and .h\r
0dc8c589 41 public Set<String> preprocessedccodes = new HashSet<String>();\r
e6a5df3b 42 public Set<String> msaorinf = new HashSet<String>(); //only a little, hash may be too big for this\r
0dc8c589 43 \r
44 public Set<String> hashfuncc = new HashSet<String>();\r
45 public Set<String> hashfuncd = new HashSet<String>();\r
46 public Set<String> hashnonlocalfunc = new HashSet<String>();\r
47 public Set<String> hashnonlocalmacro = new HashSet<String>();\r
48 public Set<String> hashEFIcall = new HashSet<String>();\r
49 public Set<String> hashr8only = new HashSet<String>();\r
50 \r
51 public Set<String> hashrequiredr9libs = new HashSet<String>(); // hashrequiredr9libs is now all added in SourceFileReplacer \r
52 public Set<String> guid = new HashSet<String>();\r
53 public Set<String> protocol = new HashSet<String>();\r
54 public Set<String> ppi = new HashSet<String>();\r
55 \r
56 private static String migrationcomment = "//%$//";\r
57 \r
e6a5df3b 58 private void dirScan(String subpath) throws Exception {\r
59 String[] list = new File(modulepath + File.separator + subpath).list(); // if no sub , separator need?\r
60 File test;\r
0dc8c589 61 \r
62 for (int i = 0 ; i < list.length ; i++) {\r
e6a5df3b 63 test = new File(modulepath + File.separator + subpath + list[i]);\r
64 if (test.isDirectory()) {\r
65 if (list[i].contains("result") || list[i].contains("temp")) {\r
66 } else {\r
67 dirScan(subpath + list[i] + File.separator);\r
68 }\r
0dc8c589 69 } else {\r
e6a5df3b 70 if (list[i].contains(".c") || list[i].contains(".C") || list[i].contains(".h") || \r
71 list[i].contains(".H") || list[i].contains(".dxs") || list[i].contains(".uni")) {\r
72 localmodulesources.add(subpath + list[i]);\r
73 } else if (list[i].contains(".inf") || list[i].contains(".msa")) {\r
74 msaorinf.add(subpath + list[i]);\r
0dc8c589 75 }\r
76 }\r
77 }\r
e6a5df3b 78 }\r
79 \r
80 private void moduleScan() throws Exception {\r
81 dirScan("");\r
82 String filename = null;\r
83 if (msaorinf.isEmpty()) {\r
c5d67e99 84 ui.println("No INF nor MSA file found!");\r
e6a5df3b 85 System.exit(0);\r
0dc8c589 86 } else {\r
e6a5df3b 87 filename = ui.choose("Found .inf or .msa file in the module\nChoose one Please", msaorinf.toArray());\r
88 }\r
89 ModuleReader mr = new ModuleReader(modulepath, this, db);\r
90 if (filename.contains(".inf")) {\r
91 mr.readInf(filename);\r
92 } else if (filename.contains(".msa")) {\r
93 mr.readMsa(filename);\r
0dc8c589 94 }\r
95 \r
96 CommentOutNonLocalHFile();\r
97 parsePreProcessedSourceCode();\r
98 \r
99 new SourceFileReplacer(modulepath, this, db, ui).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"\r
100 \r
101 // show result\r
c5d67e99 102 if (ui.yesOrNo("Parse of the Module Information has completed. View details?")) {\r
0dc8c589 103 ui.println("\nModule Information : ");\r
104 ui.println("Entrypoint : " + entrypoint);\r
105 show(protocol, "Protocol : ");\r
106 show(ppi, "Ppi : ");\r
107 show(guid, "Guid : ");\r
108 show(hashfuncc, "call : ");\r
109 show(hashfuncd, "def : ");\r
110 show(hashEFIcall, "EFIcall : ");\r
111 show(hashnonlocalmacro, "macro : ");\r
112 show(hashnonlocalfunc, "nonlocal : ");\r
113 show(hashr8only, "hashr8only : ");\r
114 }\r
115 \r
116 new MsaWriter(modulepath, this, db).flush();\r
117 \r
118 // remove temp directory\r
119 //File tempdir = new File(modulepath + File.separator + "temp");\r
120 //System.out.println("Deleting Dir");\r
121 //if (tempdir.exists()) tempdir.d;\r
122 \r
123 ui.println("Errors Left : " + db.error);\r
124 ui.println("Complete!");\r
c5d67e99 125 ui.println("Your R9 module was placed here: " + modulepath + File.separator + "result");\r
126 ui.println("Your logfile was placed here: " + modulepath);\r
0dc8c589 127 }\r
128 \r
129 private void show(Set<String> hash, String show) {\r
130 ui.println(show + hash.size());\r
131 ui.println(hash);\r
132 }\r
133\r
e6a5df3b 134 public void ensureDir(String objFileWhole) {\r
135 Pattern ptnseparate = Pattern.compile("(.*)\\\\[^\\\\]*");\r
136 Matcher mtrseparate;\r
137 File tempdir;\r
138\r
139 mtrseparate = ptnseparate.matcher(objFileWhole);\r
140 if (mtrseparate.find()) {\r
141 tempdir = new File(mtrseparate.group(1));\r
142 if (!tempdir.exists()) tempdir.mkdirs();\r
143 }\r
144 \r
145 }\r
146 \r
0dc8c589 147 // add '//' to all non-local include lines\r
148 private void CommentOutNonLocalHFile() throws IOException {\r
149 BufferedReader rd;\r
150 String line;\r
151 String curFile;\r
152 PrintWriter outfile;\r
153\r
154 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");\r
e6a5df3b 155 Matcher mtrinclude;\r
0dc8c589 156 \r
157 Iterator<String> ii = localmodulesources.iterator();\r
158 while ( ii.hasNext() ) {\r
159 curFile = ii.next();\r
160 rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));\r
e6a5df3b 161 ensureDir(modulepath + File.separator + "temp" + File.separator + curFile);\r
0dc8c589 162 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));\r
163 while ((line = rd.readLine()) != null) {\r
164 if (line.contains("#include")) {\r
e6a5df3b 165 mtrinclude = ptninclude.matcher(line);\r
166 if (mtrinclude.find() && localmodulesources.contains(mtrinclude.group(1))) {\r
0dc8c589 167 } else {\r
168 line = migrationcomment + line;\r
169 }\r
170 }\r
171 outfile.append(line + '\n');\r
172 }\r
173 outfile.flush();\r
174 outfile.close();\r
175 }\r
176 }\r
e6a5df3b 177\r
0dc8c589 178 private void parsePreProcessedSourceCode() throws Exception {\r
179 //Cl cl = new Cl(modulepath);\r
180 //cl.execute("Fat.c");\r
181 //cl.generateAll(preprocessedccodes);\r
182 //\r
183 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r
184 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r
185 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add\r
0dc8c589 186 BufferedReader rd = null;\r
187 String ifile = null;\r
188 String line = null;\r
189 String temp = null;\r
e6a5df3b 190 \r
191 Iterator<String> ii = localmodulesources.iterator();\r
192 while (ii.hasNext()) {\r
193 temp = ii.next();\r
194 if (temp.contains(".c")) {\r
195 preprocessedccodes.add(temp);\r
196 }\r
197 }\r
198 \r
199 ii = preprocessedccodes.iterator();\r
0dc8c589 200 \r
201 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r
202 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);\r
203 Matcher matguid;\r
204 Matcher matfuncc;\r
205 Matcher matfuncd;\r
206 Matcher matenclosereplace;\r
207 Matcher matefifuncc;\r
208 Matcher matentrypoint;\r
209 Matcher matmacro;\r
210 \r
211 while (ii.hasNext()) {\r
212 StringBuffer wholefile = new StringBuffer();\r
213 ifile = ii.next();\r
214 rd = new BufferedReader(new FileReader(modulepath + File.separator + "temp" + File.separator + ifile));\r
215 while ((line = rd.readLine()) != null) {\r
216 wholefile.append(line + '\n');\r
217 }\r
218 line = wholefile.toString();\r
219 \r
220 // if this is a Pei phase module , add these library class to .msa\r
221 matentrypoint = patentrypoint.matcher(line);\r
222 if (matentrypoint.find()) {\r
223 entrypoint = matentrypoint.group(2);\r
224 if (matentrypoint.group(1).matches("PEIM")) {\r
225 hashrequiredr9libs.add("PeimEntryPoint");\r
226 } else {\r
227 hashrequiredr9libs.add("UefiDriverEntryPoint");\r
228 }\r
229 }\r
230 \r
231 // find guid\r
232 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :\r
233 while (matguid.find()) { // 1.currently , find once , then call to identify which is it\r
234 if ((temp = Guid.register(matguid, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each\r
235 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost\r
236 }\r
237 }\r
238 //matguid.appendTail(result);\r
239 //line = result.toString();\r
240\r
241 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r
242 // This item is not simply replaced , special operation is required.\r
243 matefifuncc = patefifuncc.matcher(line);\r
244 while (matefifuncc.find()) {\r
245 hashEFIcall.add(matefifuncc.group(2));\r
246 }\r
247\r
248 // find function call\r
249 matfuncc = Func.ptnfuncc.matcher(line);\r
250 while (matfuncc.find()) {\r
251 if ((temp = Func.register(matfuncc, this, db)) != null) {\r
252 //ui.println(ifile + " dofunc " + temp);\r
253 //matfuncc.appendReplacement(result, db.getR9Func(temp));\r
254 }\r
255 }\r
256 //matfuncc.appendTail(result);\r
257 //line = result.toString();\r
258\r
259 // find macro\r
260 matmacro = Macro.ptntmacro.matcher(line);\r
261 while (matmacro.find()) {\r
262 if ((temp = Macro.register(matmacro, this, db)) != null) {\r
263 }\r
264 }\r
265 \r
266 // find function definition\r
267 // replace all {} to @\r
268 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
269 line = matenclosereplace.replaceAll("@");\r
270 }\r
271\r
272 matfuncd = Func.ptnfuncd.matcher(line);\r
273 while (matfuncd.find()) {\r
274 if ((temp = Func.register(matfuncd, this, db)) != null) {\r
275 }\r
276 }\r
277 }\r
278 \r
279 // op on hash\r
280 Iterator<String> funcci = hashfuncc.iterator();\r
281 while (funcci.hasNext()) {\r
282 if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {\r
283 hashnonlocalfunc.add(temp); // this set contains both changed and not changed items\r
284 }\r
285 }\r
286 }\r
287 \r
288 public static void main(String[] args) throws Exception {\r
289 FirstPanel.init();\r
290 }\r
e6a5df3b 291}