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