]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
Coding Style
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / ModuleReader.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
71f30e51 15import java.io.BufferedReader;\r
16import java.io.File;\r
17import java.io.FileReader;\r
18import java.io.StringReader;\r
19import java.util.Iterator;\r
20import java.util.regex.Matcher;\r
21import java.util.regex.Pattern;\r
446e26ee 22\r
71f30e51 23import org.tianocore.FilenameDocument;\r
24import org.tianocore.ModuleSurfaceAreaDocument;\r
25import org.tianocore.MsaHeaderDocument;\r
26import org.tianocore.SourceFilesDocument;\r
0dc8c589 27\r
39e5e412 28public final class ModuleReader implements Common.ForDoAll {\r
71f30e51 29 private static final ModuleReader modulereader = new ModuleReader();\r
30\r
31 private ModuleInfo mi;\r
32\r
33 private final CommentLaplace commentlaplace = new CommentLaplace();\r
34\r
35 private static final Pattern ptninfequation = Pattern\r
36 .compile("([^\\s]*)\\s*=\\s*([^\\s]*)");\r
37\r
38 private static final Pattern ptnsection = Pattern.compile(\r
39 "\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE);\r
40\r
41 private static final Pattern ptnfilename = Pattern.compile("[^\\s]+");\r
42\r
43 public final void ModuleScan() throws Exception {\r
44 Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll",\r
45 String.class), mi, null, Common.FILE);\r
46\r
47 // inf&msa\r
48 String filename = null;\r
49 if (mi.msaorinf.isEmpty()) {\r
50 MigrationTool.ui.println("No INF nor MSA file found!");\r
51 System.exit(0);\r
52 } else {\r
53 if (mi.msaorinf.size() == 1) {\r
54 filename = (String) mi.msaorinf.toArray()[0];\r
55 } else {\r
56 filename = MigrationTool.ui.choose(\r
57 "Found .inf or .msa file for module\n" + mi.modulepath\r
58 + "\nChoose one Please", mi.msaorinf.toArray());\r
59 }\r
60 }\r
61\r
62 if (filename.contains(".inf")) {\r
63 readInf(filename);\r
64 } else if (filename.contains(".msa")) {\r
65 readMsa(filename);\r
66 }\r
67 // inf&msa\r
68\r
69 preProcessModule();\r
70 }\r
71\r
72 private final void readMsa(String name) throws Exception {\r
73 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory\r
74 .parse(new File(mi.modulepath + File.separator + name));\r
75 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc\r
76 .getModuleSurfaceArea();\r
77 MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();\r
78\r
79 mi.modulename = msaheader.getModuleName();\r
80 mi.guidvalue = msaheader.getGuidValue();\r
81 mi.moduletype = msaheader.getModuleType().toString(); // ???\r
82\r
83 SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();\r
84\r
85 String temp;\r
86 Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList()\r
87 .iterator();\r
88 while (li.hasNext()) {\r
89 if (!mi.localmodulesources.contains(temp = li.next().toString())) {\r
90 System.out.println("Source File Missing! : " + temp);\r
91 }\r
92 }\r
93 }\r
94\r
95 private final String extractLicense(String wholeline) throws Exception {\r
96 String tempLine;\r
97 String license = null;\r
98\r
99 BufferedReader rd = new BufferedReader(new StringReader(wholeline));\r
100 while ((tempLine = rd.readLine()) != null) {\r
101 if (tempLine.contains("#")) {\r
102 if (tempLine.contains("Copyright")) {\r
103 //\r
104 // Find license info.\r
105 // \r
106 license = "";\r
107 while ((tempLine = rd.readLine()) != null) {\r
108 if (!tempLine.contains("#")\r
109 || tempLine.contains("Module Name:")\r
110 || tempLine.contains("Abstract:")) {\r
111 //\r
112 // We assume license ends here.\r
113 // \r
114 break;\r
115 }\r
116 license += " "\r
117 + tempLine\r
118 .replaceAll("\\s*[#]\\s*(.*)", "$1\n");\r
119 }\r
120 break;\r
121 }\r
122 }\r
123 }\r
124 return license;\r
125 }\r
126\r
127 private final void readInf(String name) throws Exception {\r
128 System.out.println("\nParsing INF file: " + name);\r
129 String wholeline;\r
130 Matcher mtrinfequation;\r
131 Matcher mtrsection;\r
132 Matcher mtrfilename;\r
133\r
134 wholeline = Common.file2string(mi.modulepath + File.separator + name);\r
135 mi.license = extractLicense(wholeline);\r
136 mtrsection = ptnsection.matcher(wholeline);\r
137 while (mtrsection.find()) {\r
138 if (mtrsection.group(1).matches("defines")) {\r
139 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
140 while (mtrinfequation.find()) {\r
141 if (mtrinfequation.group(1).matches("BASE_NAME")) {\r
142 mi.modulename = mtrinfequation.group(2);\r
143 }\r
144 if (mtrinfequation.group(1).matches("FILE_GUID")) {\r
145 mi.guidvalue = mtrinfequation.group(2);\r
146 }\r
147 if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {\r
148 mi.moduletype = mtrinfequation.group(2);\r
149 }\r
150 }\r
151 }\r
152 if (mtrsection.group(1).contains("nmake.")) {\r
153 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
154 while (mtrinfequation.find()) {\r
155 if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {\r
156 mi.entrypoint = mtrinfequation.group(2);\r
157 }\r
158 if (mtrinfequation.group(1).matches("DPX_SOURCE")) {\r
159 if (!mi.localmodulesources.contains(mtrinfequation\r
160 .group(2))) {\r
161 MigrationTool.ui.println("DPX File Missing! : "\r
162 + mtrinfequation.group(2));\r
163 }\r
164 }\r
165 }\r
166 }\r
167 if (mtrsection.group(1).contains("sources.")) {\r
168 mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
169 while (mtrfilename.find()) {\r
170 mi.infsources.add(mtrfilename.group());\r
171 if (!mi.localmodulesources.contains(mtrfilename.group())) {\r
172 MigrationTool.ui\r
173 .println("Warn: Source File Missing! : "\r
174 + mtrfilename.group());\r
175 }\r
176 }\r
177 }\r
178 if (mtrsection.group(1).matches("includes.")) {\r
179 mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
180 while (mtrfilename.find()) {\r
181 mi.infincludes.add(mtrfilename.group());\r
182 }\r
183 }\r
184 }\r
185 }\r
186\r
187 private final void preProcessModule() throws Exception {\r
188 // according to .inf file, add extraordinary includes and sourcefiles\r
189 Common.dirCopy(mi.modulepath, mi.temppath); // collect all\r
190 // Laplace.namechange to\r
191 // here???\r
192\r
193 if (!mi.infincludes.isEmpty()) {\r
194 Iterator<String> it = mi.infincludes.iterator();\r
195 String tempincludename = null;\r
196 while (it.hasNext()) {\r
197 tempincludename = it.next();\r
198 if (tempincludename.contains("..")) {\r
199 Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename);\r
200 if (mtr.find() && !mtr.group(2).matches(".")) {\r
201 Common.oneLevelDirCopy(mi.modulepath.replaceAll(\r
202 Common.STRSEPARATER, "$1")\r
203 + File.separator + mtr.group(2), mi.temppath,\r
204 ".h");\r
205 } else {\r
206 Common.oneLevelDirCopy(mi.modulepath.replaceAll(\r
207 Common.STRSEPARATER, "$1"), mi.temppath, ".h");\r
208 }\r
209 }\r
210 }\r
211 }\r
212 if (!mi.infsources.isEmpty()) {\r
213 Iterator<String> it = mi.infsources.iterator();\r
214 String tempsourcename = null;\r
215 while (it.hasNext()) {\r
216 tempsourcename = it.next();\r
217 if (tempsourcename.contains("..")) {\r
218 Common.ensureDir(mi.temppath + File.separator\r
219 + "MT_Parent_Sources");\r
220 Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename);\r
221 if (mtr.find()) {\r
222 Common.fileCopy(mi.modulepath.replaceAll(\r
223 Common.STRSEPARATER, "$1")\r
224 + File.separator + mtr.group(2), mi.temppath\r
225 + File.separator + "MT_Parent_Sources"\r
226 + File.separator + mtr.group(2));\r
227 }\r
228 }\r
229 }\r
230 }\r
231\r
232 Common.toDoAll(mi.temppath, this, Common.FILE);\r
233\r
234 parsePreProcessedSourceCode();\r
235\r
236 }\r
237\r
238 private final void parsePreProcessedSourceCode() throws Exception {\r
239 BufferedReader rd = null;\r
240 String ifile = null;\r
241 String line = null;\r
242 String temp = null;\r
243\r
244 Iterator<String> ii = mi.localmodulesources.iterator();\r
245 while (ii.hasNext()) {\r
246 temp = ii.next();\r
247 if (temp.contains(".c") || temp.contains(".dxs")) {\r
248 mi.preprocessedccodes.add(temp);\r
249 }\r
250 }\r
251\r
252 ii = mi.preprocessedccodes.iterator();\r
253\r
254 Pattern patefifuncc = Pattern.compile(\r
255 "g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)", Pattern.MULTILINE);\r
256 Matcher matguid;\r
257 Matcher matfuncc;\r
258 Matcher matfuncd;\r
259 Matcher matenclosereplace;\r
260 Matcher matefifuncc;\r
261 Matcher matmacro;\r
262\r
263 while (ii.hasNext()) {\r
264 StringBuffer wholefile = new StringBuffer();\r
265 ifile = ii.next();\r
266 rd = new BufferedReader(new FileReader(mi.temppath + File.separator\r
267 + ifile));\r
268 while ((line = rd.readLine()) != null) {\r
269 wholefile.append(line + '\n');\r
270 }\r
271 line = wholefile.toString();\r
272\r
273 // find guid\r
274 matguid = Guid.ptnguid.matcher(line); // several ways to implement\r
275 // this , which one is\r
276 // faster ? :\r
277 while (matguid.find()) { // 1.currently , find once , then call\r
278 // to identify which is it\r
279 if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use\r
280 // 3\r
281 // different\r
282 // matchers\r
283 // ,\r
284 // search\r
285 // 3\r
286 // times\r
287 // to\r
288 // find\r
289 // each\r
290 // matguid.appendReplacement(result,\r
291 // MigrationTool.db.getR9Guidname(temp)); // search the\r
292 // database for all 3 kinds of guids , high cost\r
293 }\r
294 }\r
295 // matguid.appendTail(result);\r
296 // line = result.toString();\r
297\r
298 // find EFI call in form of '->' , many\r
299 // 'gUnicodeCollationInterface->' like things are not changed\r
300 // This item is not simply replaced , special operation is required.\r
301 matefifuncc = patefifuncc.matcher(line);\r
302 while (matefifuncc.find()) {\r
303 mi.hashEFIcall.add(matefifuncc.group(2));\r
304 }\r
305\r
306 // find function call\r
307 matfuncc = Func.ptnfuncc.matcher(line);\r
308 while (matfuncc.find()) {\r
309 if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r
310 // MigrationTool.ui.println(ifile + " dofunc " + temp);\r
311 // matfuncc.appendReplacement(result,\r
312 // MigrationTool.db.getR9Func(temp));\r
313 }\r
314 }\r
315 // matfuncc.appendTail(result);\r
316 // line = result.toString();\r
317\r
318 // find macro\r
319 matmacro = Macro.ptntmacro.matcher(line);\r
320 while (matmacro.find()) {\r
321 if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r
322 }\r
323 }\r
324\r
325 // find function definition\r
326 // replace all {} to @\r
327 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
328 line = matenclosereplace.replaceAll("@");\r
329 }\r
330\r
331 matfuncd = Func.ptnfuncd.matcher(line);\r
332 while (matfuncd.find()) {\r
333 if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r
334 }\r
335 }\r
336 }\r
337\r
338 // op on hash\r
339 Iterator<String> funcci = mi.hashfuncc.iterator();\r
340 while (funcci.hasNext()) {\r
341 if (!mi.hashfuncd.contains(temp = funcci.next())\r
342 && !mi.hashEFIcall.contains(temp)) {\r
343 mi.hashnonlocalfunc.add(temp); // this set contains both\r
344 // changed and not changed items\r
345 }\r
346 }\r
347 }\r
348\r
349 public class CommentLaplace extends Common.Laplace {\r
350 public String operation(String wholeline) {\r
351 StringBuffer wholebuffer = new StringBuffer();\r
352 String templine = null;\r
353 Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");\r
354 Pattern ptninclude = Pattern.compile("#include\\s*(.*)");\r
355 Matcher mtrinclude = ptninclude.matcher(wholeline);\r
356 Matcher mtrincludefile = null;\r
357 while (mtrinclude.find()) {\r
358 mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));\r
359 if (mtrincludefile.find()\r
360 && mi.localmodulesources.contains(mtrincludefile\r
361 .group(1))) {\r
362 templine = mtrinclude.group();\r
363 } else {\r
364 templine = MigrationTool.MIGRATIONCOMMENT\r
365 + mtrinclude.group();\r
366 }\r
367 mtrinclude.appendReplacement(wholebuffer, templine);\r
368 }\r
369 mtrinclude.appendTail(wholebuffer);\r
370 return wholebuffer.toString();\r
371 }\r
372\r
373 public boolean recognize(String filename) {\r
374 return filename.contains(".c") || filename.contains(".h")\r
375 || filename.contains(".dxs");\r
376 }\r
377\r
378 public String namechange(String oldname) {\r
379 return oldname;\r
380 }\r
381 }\r
382\r
383 // -----------------------------------ForDoAll-----------------------------------//\r
384 public void run(String filepath) throws Exception {\r
385 String name = mi.temppath + File.separator\r
386 + filepath.replace(mi.temppath + File.separator, "");\r
387 if (commentlaplace.recognize(name)) {\r
388 commentlaplace.transform(name, name);\r
389 }\r
390 }\r
391\r
392 public boolean filter(File dir) {\r
393 return true;\r
394 }\r
395\r
396 // -----------------------------------ForDoAll-----------------------------------//\r
397\r
398 public final void setModuleInfo(ModuleInfo m) {\r
399 mi = m;\r
400 }\r
401\r
402 public static final void aimAt(ModuleInfo mi) throws Exception {\r
403 modulereader.setModuleInfo(mi);\r
404 modulereader.ModuleScan();\r
405 }\r
719cebfe 406}\r