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