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