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