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