]>
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 | |
446e26ee | 18 | \r |
0dc8c589 | 19 | import org.tianocore.*;\r |
20 | \r | |
39e5e412 | 21 | public 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 |
df87de9b | 195 | Common.toDoAll(mi.temppath, this, Common.FILE);\r |
39e5e412 | 196 | \r |
197 | parsePreProcessedSourceCode();\r | |
446e26ee | 198 | \r |
27e0221a | 199 | }\r |
446e26ee | 200 | \r |
39e5e412 | 201 | private final void parsePreProcessedSourceCode() throws Exception {\r |
27e0221a | 202 | BufferedReader rd = null;\r |
203 | String ifile = null;\r | |
204 | String line = null;\r | |
205 | String temp = null;\r | |
206 | \r | |
207 | Iterator<String> ii = mi.localmodulesources.iterator();\r | |
208 | while (ii.hasNext()) {\r | |
209 | temp = ii.next();\r | |
49324055 | 210 | if (temp.contains(".c") || temp.contains(".dxs")) {\r |
27e0221a | 211 | mi.preprocessedccodes.add(temp);\r |
212 | }\r | |
213 | }\r | |
214 | \r | |
215 | ii = mi.preprocessedccodes.iterator();\r | |
216 | \r | |
217 | Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r | |
27e0221a | 218 | Matcher matguid;\r |
219 | Matcher matfuncc;\r | |
220 | Matcher matfuncd;\r | |
221 | Matcher matenclosereplace;\r | |
222 | Matcher matefifuncc;\r | |
27e0221a | 223 | Matcher matmacro;\r |
224 | \r | |
225 | while (ii.hasNext()) {\r | |
226 | StringBuffer wholefile = new StringBuffer();\r | |
227 | ifile = ii.next();\r | |
df87de9b | 228 | rd = new BufferedReader(new FileReader(mi.temppath + File.separator + ifile));\r |
27e0221a | 229 | while ((line = rd.readLine()) != null) {\r |
230 | wholefile.append(line + '\n');\r | |
231 | }\r | |
232 | line = wholefile.toString();\r | |
233 | \r | |
27e0221a | 234 | // find guid\r |
235 | matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :\r | |
236 | while (matguid.find()) { // 1.currently , find once , then call to identify which is it\r | |
237 | if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use 3 different matchers , search 3 times to find each\r | |
238 | //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost\r | |
239 | }\r | |
240 | }\r | |
241 | //matguid.appendTail(result);\r | |
242 | //line = result.toString();\r | |
446e26ee | 243 | \r |
27e0221a | 244 | // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r |
245 | // This item is not simply replaced , special operation is required.\r | |
246 | matefifuncc = patefifuncc.matcher(line);\r | |
247 | while (matefifuncc.find()) {\r | |
248 | mi.hashEFIcall.add(matefifuncc.group(2));\r | |
249 | }\r | |
446e26ee | 250 | \r |
27e0221a | 251 | // find function call\r |
252 | matfuncc = Func.ptnfuncc.matcher(line);\r | |
253 | while (matfuncc.find()) {\r | |
254 | if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r | |
255 | //MigrationTool.ui.println(ifile + " dofunc " + temp);\r | |
256 | //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));\r | |
257 | }\r | |
258 | }\r | |
259 | //matfuncc.appendTail(result);\r | |
260 | //line = result.toString();\r | |
446e26ee | 261 | \r |
27e0221a | 262 | // find macro\r |
263 | matmacro = Macro.ptntmacro.matcher(line);\r | |
264 | while (matmacro.find()) {\r | |
265 | if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r | |
266 | }\r | |
267 | }\r | |
268 | \r | |
269 | // find function definition\r | |
270 | // replace all {} to @\r | |
271 | while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r | |
272 | line = matenclosereplace.replaceAll("@");\r | |
273 | }\r | |
446e26ee | 274 | \r |
27e0221a | 275 | matfuncd = Func.ptnfuncd.matcher(line);\r |
276 | while (matfuncd.find()) {\r | |
277 | if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r | |
278 | }\r | |
279 | }\r | |
280 | }\r | |
281 | \r | |
282 | // op on hash\r | |
283 | Iterator<String> funcci = mi.hashfuncc.iterator();\r | |
284 | while (funcci.hasNext()) {\r | |
285 | if (!mi.hashfuncd.contains(temp = funcci.next()) && !mi.hashEFIcall.contains(temp)) {\r | |
286 | mi.hashnonlocalfunc.add(temp); // this set contains both changed and not changed items\r | |
287 | }\r | |
288 | }\r | |
289 | }\r | |
39e5e412 | 290 | \r |
291 | public class CommentLaplace extends Common.Laplace {\r | |
292 | public String operation(String wholeline) {\r | |
372a4642 | 293 | StringBuffer wholebuffer = new StringBuffer();\r |
294 | String templine = null;\r | |
39e5e412 | 295 | Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");\r |
296 | Pattern ptninclude = Pattern.compile("#include\\s*(.*)");\r | |
297 | Matcher mtrinclude = ptninclude.matcher(wholeline);\r | |
298 | Matcher mtrincludefile = null;\r | |
299 | while (mtrinclude.find()) {\r | |
372a4642 | 300 | mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));\r |
301 | if (mtrincludefile.find() && mi.localmodulesources.contains(mtrincludefile.group(1))) {\r | |
302 | templine = mtrinclude.group();\r | |
303 | } else {\r | |
304 | templine = MigrationTool.MIGRATIONCOMMENT + mtrinclude.group();\r | |
305 | }\r | |
306 | mtrinclude.appendReplacement(wholebuffer, templine);\r | |
39e5e412 | 307 | }\r |
308 | mtrinclude.appendTail(wholebuffer);\r | |
309 | return wholebuffer.toString();\r | |
310 | }\r | |
311 | \r | |
312 | public boolean recognize(String filename) {\r | |
a7a70333 | 313 | return filename.contains(".c") || filename.contains(".h") || filename.contains(".dxs");\r |
39e5e412 | 314 | }\r |
315 | \r | |
316 | public String namechange(String oldname) {\r | |
372a4642 | 317 | return oldname;\r |
39e5e412 | 318 | }\r |
319 | }\r | |
320 | \r | |
321 | //-----------------------------------ForDoAll-----------------------------------//\r | |
322 | public void run(String filepath) throws Exception {\r | |
372a4642 | 323 | String name = mi.temppath + File.separator + filepath.replace(mi.temppath + File.separator, "");\r |
1f0b4e9c | 324 | if (commentlaplace.recognize(name)) {\r |
325 | commentlaplace.transform(name, name);\r | |
326 | }\r | |
39e5e412 | 327 | }\r |
328 | \r | |
329 | public boolean filter(File dir) {\r | |
330 | return true;\r | |
331 | }\r | |
332 | //-----------------------------------ForDoAll-----------------------------------//\r | |
333 | \r | |
334 | public final void setModuleInfo(ModuleInfo m) {\r | |
372a4642 | 335 | mi = m;\r |
39e5e412 | 336 | }\r |
337 | \r | |
338 | public static final void aimAt(ModuleInfo mi) throws Exception {\r | |
372a4642 | 339 | modulereader.setModuleInfo(mi);\r |
340 | modulereader.ModuleScan();\r | |
39e5e412 | 341 | }\r |
719cebfe | 342 | }\r |