]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java
Add Critic.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
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
f7fee0ea 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
90503bad 89 ModuleReader mr = new ModuleReader(modulepath, this, db, ui);\r
e6a5df3b 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
f7fee0ea 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
fed802b1 116 new MsaWriter(modulepath, this, db, ui).flush();\r
0dc8c589 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
f7fee0ea 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
e6a5df3b 133 \r
0dc8c589 134 // add '//' to all non-local include lines\r
135 private void CommentOutNonLocalHFile() throws IOException {\r
136 BufferedReader rd;\r
137 String line;\r
138 String curFile;\r
139 PrintWriter outfile;\r
140\r
141 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");\r
e6a5df3b 142 Matcher mtrinclude;\r
0dc8c589 143 \r
144 Iterator<String> ii = localmodulesources.iterator();\r
145 while ( ii.hasNext() ) {\r
146 curFile = ii.next();\r
147 rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));\r
90503bad 148 Common.ensureDir(modulepath + File.separator + "temp" + File.separator + curFile);\r
0dc8c589 149 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));\r
150 while ((line = rd.readLine()) != null) {\r
151 if (line.contains("#include")) {\r
e6a5df3b 152 mtrinclude = ptninclude.matcher(line);\r
153 if (mtrinclude.find() && localmodulesources.contains(mtrinclude.group(1))) {\r
0dc8c589 154 } else {\r
155 line = migrationcomment + line;\r
156 }\r
157 }\r
158 outfile.append(line + '\n');\r
159 }\r
160 outfile.flush();\r
161 outfile.close();\r
162 }\r
163 }\r
e6a5df3b 164\r
0dc8c589 165 private void parsePreProcessedSourceCode() throws Exception {\r
166 //Cl cl = new Cl(modulepath);\r
167 //cl.execute("Fat.c");\r
168 //cl.generateAll(preprocessedccodes);\r
169 //\r
170 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r
171 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r
172 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add\r
0dc8c589 173 BufferedReader rd = null;\r
174 String ifile = null;\r
175 String line = null;\r
176 String temp = null;\r
e6a5df3b 177 \r
178 Iterator<String> ii = localmodulesources.iterator();\r
179 while (ii.hasNext()) {\r
180 temp = ii.next();\r
181 if (temp.contains(".c")) {\r
182 preprocessedccodes.add(temp);\r
183 }\r
184 }\r
185 \r
186 ii = preprocessedccodes.iterator();\r
0dc8c589 187 \r
188 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r
189 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);\r
190 Matcher matguid;\r
191 Matcher matfuncc;\r
192 Matcher matfuncd;\r
193 Matcher matenclosereplace;\r
194 Matcher matefifuncc;\r
195 Matcher matentrypoint;\r
196 Matcher matmacro;\r
197 \r
198 while (ii.hasNext()) {\r
199 StringBuffer wholefile = new StringBuffer();\r
200 ifile = ii.next();\r
201 rd = new BufferedReader(new FileReader(modulepath + File.separator + "temp" + File.separator + ifile));\r
202 while ((line = rd.readLine()) != null) {\r
203 wholefile.append(line + '\n');\r
204 }\r
205 line = wholefile.toString();\r
206 \r
207 // if this is a Pei phase module , add these library class to .msa\r
208 matentrypoint = patentrypoint.matcher(line);\r
209 if (matentrypoint.find()) {\r
210 entrypoint = matentrypoint.group(2);\r
211 if (matentrypoint.group(1).matches("PEIM")) {\r
212 hashrequiredr9libs.add("PeimEntryPoint");\r
213 } else {\r
214 hashrequiredr9libs.add("UefiDriverEntryPoint");\r
215 }\r
216 }\r
217 \r
218 // find guid\r
219 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :\r
220 while (matguid.find()) { // 1.currently , find once , then call to identify which is it\r
221 if ((temp = Guid.register(matguid, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each\r
222 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost\r
223 }\r
224 }\r
225 //matguid.appendTail(result);\r
226 //line = result.toString();\r
227\r
228 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r
229 // This item is not simply replaced , special operation is required.\r
230 matefifuncc = patefifuncc.matcher(line);\r
231 while (matefifuncc.find()) {\r
232 hashEFIcall.add(matefifuncc.group(2));\r
233 }\r
234\r
235 // find function call\r
236 matfuncc = Func.ptnfuncc.matcher(line);\r
237 while (matfuncc.find()) {\r
238 if ((temp = Func.register(matfuncc, this, db)) != null) {\r
239 //ui.println(ifile + " dofunc " + temp);\r
240 //matfuncc.appendReplacement(result, db.getR9Func(temp));\r
241 }\r
242 }\r
243 //matfuncc.appendTail(result);\r
244 //line = result.toString();\r
245\r
246 // find macro\r
247 matmacro = Macro.ptntmacro.matcher(line);\r
248 while (matmacro.find()) {\r
249 if ((temp = Macro.register(matmacro, this, db)) != null) {\r
250 }\r
251 }\r
252 \r
253 // find function definition\r
254 // replace all {} to @\r
255 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
256 line = matenclosereplace.replaceAll("@");\r
257 }\r
258\r
259 matfuncd = Func.ptnfuncd.matcher(line);\r
260 while (matfuncd.find()) {\r
261 if ((temp = Func.register(matfuncd, this, db)) != null) {\r
262 }\r
263 }\r
264 }\r
265 \r
266 // op on hash\r
267 Iterator<String> funcci = hashfuncc.iterator();\r
268 while (funcci.hasNext()) {\r
269 if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {\r
270 hashnonlocalfunc.add(temp); // this set contains both changed and not changed items\r
271 }\r
272 }\r
273 }\r
274 \r
275 public static void main(String[] args) throws Exception {\r
276 FirstPanel.init();\r
277 }\r
e6a5df3b 278}