]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
Add "ALWAYS_PRODUCED" Library class item for EDK module whose COMPONENT type is ...
[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
a09aa7e2 149 if (mi.moduletype.matches("LIBRARY")) {\r
150 mi.isLibrary = true;\r
151 }\r
71f30e51 152 }\r
153 }\r
154 }\r
155 if (mtrsection.group(1).contains("nmake.")) {\r
156 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
157 while (mtrinfequation.find()) {\r
158 if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {\r
159 mi.entrypoint = mtrinfequation.group(2);\r
160 }\r
161 if (mtrinfequation.group(1).matches("DPX_SOURCE")) {\r
162 if (!mi.localmodulesources.contains(mtrinfequation\r
163 .group(2))) {\r
164 MigrationTool.ui.println("DPX File Missing! : "\r
165 + mtrinfequation.group(2));\r
166 }\r
167 }\r
168 }\r
169 }\r
170 if (mtrsection.group(1).contains("sources.")) {\r
171 mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
172 while (mtrfilename.find()) {\r
173 mi.infsources.add(mtrfilename.group());\r
174 if (!mi.localmodulesources.contains(mtrfilename.group())) {\r
175 MigrationTool.ui\r
176 .println("Warn: Source File Missing! : "\r
177 + mtrfilename.group());\r
178 }\r
179 }\r
180 }\r
181 if (mtrsection.group(1).matches("includes.")) {\r
182 mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
183 while (mtrfilename.find()) {\r
184 mi.infincludes.add(mtrfilename.group());\r
185 }\r
186 }\r
187 }\r
188 }\r
189\r
190 private final void preProcessModule() throws Exception {\r
191 // according to .inf file, add extraordinary includes and sourcefiles\r
192 Common.dirCopy(mi.modulepath, mi.temppath); // collect all\r
193 // Laplace.namechange to\r
194 // here???\r
195\r
196 if (!mi.infincludes.isEmpty()) {\r
197 Iterator<String> it = mi.infincludes.iterator();\r
198 String tempincludename = null;\r
199 while (it.hasNext()) {\r
200 tempincludename = it.next();\r
201 if (tempincludename.contains("..")) {\r
202 Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename);\r
203 if (mtr.find() && !mtr.group(2).matches(".")) {\r
204 Common.oneLevelDirCopy(mi.modulepath.replaceAll(\r
205 Common.STRSEPARATER, "$1")\r
206 + File.separator + mtr.group(2), mi.temppath,\r
207 ".h");\r
208 } else {\r
209 Common.oneLevelDirCopy(mi.modulepath.replaceAll(\r
210 Common.STRSEPARATER, "$1"), mi.temppath, ".h");\r
211 }\r
212 }\r
213 }\r
214 }\r
215 if (!mi.infsources.isEmpty()) {\r
216 Iterator<String> it = mi.infsources.iterator();\r
217 String tempsourcename = null;\r
218 while (it.hasNext()) {\r
219 tempsourcename = it.next();\r
220 if (tempsourcename.contains("..")) {\r
221 Common.ensureDir(mi.temppath + File.separator\r
222 + "MT_Parent_Sources");\r
223 Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename);\r
224 if (mtr.find()) {\r
225 Common.fileCopy(mi.modulepath.replaceAll(\r
226 Common.STRSEPARATER, "$1")\r
227 + File.separator + mtr.group(2), mi.temppath\r
228 + File.separator + "MT_Parent_Sources"\r
229 + File.separator + mtr.group(2));\r
230 }\r
231 }\r
232 }\r
233 }\r
234\r
235 Common.toDoAll(mi.temppath, this, Common.FILE);\r
236\r
237 parsePreProcessedSourceCode();\r
238\r
239 }\r
240\r
241 private final void parsePreProcessedSourceCode() throws Exception {\r
242 BufferedReader rd = null;\r
243 String ifile = null;\r
244 String line = null;\r
245 String temp = null;\r
246\r
247 Iterator<String> ii = mi.localmodulesources.iterator();\r
248 while (ii.hasNext()) {\r
249 temp = ii.next();\r
250 if (temp.contains(".c") || temp.contains(".dxs")) {\r
251 mi.preprocessedccodes.add(temp);\r
252 }\r
253 }\r
254\r
255 ii = mi.preprocessedccodes.iterator();\r
256\r
257 Pattern patefifuncc = Pattern.compile(\r
258 "g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)", Pattern.MULTILINE);\r
259 Matcher matguid;\r
260 Matcher matfuncc;\r
261 Matcher matfuncd;\r
262 Matcher matenclosereplace;\r
263 Matcher matefifuncc;\r
264 Matcher matmacro;\r
265\r
266 while (ii.hasNext()) {\r
267 StringBuffer wholefile = new StringBuffer();\r
268 ifile = ii.next();\r
269 rd = new BufferedReader(new FileReader(mi.temppath + File.separator\r
270 + ifile));\r
271 while ((line = rd.readLine()) != null) {\r
272 wholefile.append(line + '\n');\r
273 }\r
274 line = wholefile.toString();\r
275\r
276 // find guid\r
277 matguid = Guid.ptnguid.matcher(line); // several ways to implement\r
278 // this , which one is\r
279 // faster ? :\r
280 while (matguid.find()) { // 1.currently , find once , then call\r
281 // to identify which is it\r
282 if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use\r
283 // 3\r
284 // different\r
285 // matchers\r
286 // ,\r
287 // search\r
288 // 3\r
289 // times\r
290 // to\r
291 // find\r
292 // each\r
293 // matguid.appendReplacement(result,\r
294 // MigrationTool.db.getR9Guidname(temp)); // search the\r
295 // database for all 3 kinds of guids , high cost\r
296 }\r
297 }\r
298 // matguid.appendTail(result);\r
299 // line = result.toString();\r
300\r
301 // find EFI call in form of '->' , many\r
302 // 'gUnicodeCollationInterface->' like things are not changed\r
303 // This item is not simply replaced , special operation is required.\r
304 matefifuncc = patefifuncc.matcher(line);\r
305 while (matefifuncc.find()) {\r
306 mi.hashEFIcall.add(matefifuncc.group(2));\r
307 }\r
308\r
309 // find function call\r
310 matfuncc = Func.ptnfuncc.matcher(line);\r
311 while (matfuncc.find()) {\r
312 if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r
313 // MigrationTool.ui.println(ifile + " dofunc " + temp);\r
314 // matfuncc.appendReplacement(result,\r
315 // MigrationTool.db.getR9Func(temp));\r
316 }\r
317 }\r
318 // matfuncc.appendTail(result);\r
319 // line = result.toString();\r
320\r
321 // find macro\r
322 matmacro = Macro.ptntmacro.matcher(line);\r
323 while (matmacro.find()) {\r
324 if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r
325 }\r
326 }\r
327\r
328 // find function definition\r
329 // replace all {} to @\r
330 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
331 line = matenclosereplace.replaceAll("@");\r
332 }\r
333\r
334 matfuncd = Func.ptnfuncd.matcher(line);\r
335 while (matfuncd.find()) {\r
336 if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r
337 }\r
338 }\r
339 }\r
340\r
341 // op on hash\r
342 Iterator<String> funcci = mi.hashfuncc.iterator();\r
343 while (funcci.hasNext()) {\r
344 if (!mi.hashfuncd.contains(temp = funcci.next())\r
345 && !mi.hashEFIcall.contains(temp)) {\r
346 mi.hashnonlocalfunc.add(temp); // this set contains both\r
347 // changed and not changed items\r
348 }\r
349 }\r
350 }\r
351\r
352 public class CommentLaplace extends Common.Laplace {\r
353 public String operation(String wholeline) {\r
354 StringBuffer wholebuffer = new StringBuffer();\r
355 String templine = null;\r
356 Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");\r
357 Pattern ptninclude = Pattern.compile("#include\\s*(.*)");\r
358 Matcher mtrinclude = ptninclude.matcher(wholeline);\r
359 Matcher mtrincludefile = null;\r
360 while (mtrinclude.find()) {\r
361 mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));\r
362 if (mtrincludefile.find()\r
363 && mi.localmodulesources.contains(mtrincludefile\r
364 .group(1))) {\r
365 templine = mtrinclude.group();\r
366 } else {\r
8a7f0c4c 367 String line = mtrinclude.group().toLowerCase();\r
368 if (line.contains("pal.h")) {\r
369 templine = "#include <IndustryStandard/Pal.h>\n";\r
370 } else if (line.contains("sal.h")) {\r
371 templine = "#include <IndustryStandard/Sal.h>\n";\r
372 } else if (line.contains("pci22.h")) {\r
373 templine = "#include <IndustryStandard/Pci22.h>\n";\r
374 } else if (line.contains("pci23.h")) {\r
375 templine = "#include <IndustryStandard/Pci23.h>\n";\r
376 } else if (line.contains("pci30.h")) {\r
377 templine = "#include <IndustryStandard/Pci30.h>\n";\r
378 } else if (line.contains("pci.h")) {\r
379 templine = "#include <IndustryStandard/Pci.h>\n";\r
380 } else if (line.contains("acpi.h")) {\r
381 templine = "#include <IndustryStandard/Acpi.h>\n";\r
382 } else if (line.contains("scsi.h")) {\r
383 templine = "#include <IndustryStandard/Scsi.h>\n";\r
384 } else if (line.contains("usb.h")) {\r
385 templine = "#include <IndustryStandard/Usb.h>\n";\r
386 } else {\r
387 templine = MigrationTool.MIGRATIONCOMMENT\r
388 + mtrinclude.group();\r
389 }\r
390 }\r
391 mtrinclude.appendReplacement(wholebuffer, templine);\r
392 }\r
71f30e51 393 mtrinclude.appendTail(wholebuffer);\r
394 return wholebuffer.toString();\r
395 }\r
396\r
397 public boolean recognize(String filename) {\r
398 return filename.contains(".c") || filename.contains(".h")\r
399 || filename.contains(".dxs");\r
400 }\r
401\r
402 public String namechange(String oldname) {\r
403 return oldname;\r
404 }\r
405 }\r
406\r
407 // -----------------------------------ForDoAll-----------------------------------//\r
408 public void run(String filepath) throws Exception {\r
409 String name = mi.temppath + File.separator\r
410 + filepath.replace(mi.temppath + File.separator, "");\r
411 if (commentlaplace.recognize(name)) {\r
412 commentlaplace.transform(name, name);\r
413 }\r
414 }\r
415\r
416 public boolean filter(File dir) {\r
417 return true;\r
418 }\r
419\r
420 // -----------------------------------ForDoAll-----------------------------------//\r
421\r
422 public final void setModuleInfo(ModuleInfo m) {\r
423 mi = m;\r
424 }\r
425\r
426 public static final void aimAt(ModuleInfo mi) throws Exception {\r
427 modulereader.setModuleInfo(mi);\r
428 modulereader.ModuleScan();\r
429 }\r
719cebfe 430}\r