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 |
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 |
75 | \r |
39e5e412 |
76 | private final void readInf(String name) throws Exception {\r |
27e0221a |
77 | System.out.println("\nParsing INF file: " + name);\r |
78 | String wholeline;\r |
79 | Matcher mtrinfequation;\r |
80 | Matcher mtrsection;\r |
81 | Matcher mtrfilename;\r |
0dc8c589 |
82 | \r |
27e0221a |
83 | wholeline = Common.file2string(mi.modulepath + File.separator + name);\r |
84 | mtrsection = ptnsection.matcher(wholeline);\r |
85 | while (mtrsection.find()) {\r |
86 | if (mtrsection.group(1).matches("defines")) {\r |
87 | mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r |
88 | while (mtrinfequation.find()) {\r |
89 | if (mtrinfequation.group(1).matches("BASE_NAME")) {\r |
90 | mi.modulename = mtrinfequation.group(2);\r |
91 | }\r |
92 | if (mtrinfequation.group(1).matches("FILE_GUID")) {\r |
93 | mi.guidvalue = mtrinfequation.group(2);\r |
94 | }\r |
95 | if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {\r |
96 | mi.moduletype = mtrinfequation.group(2);\r |
97 | }\r |
98 | }\r |
99 | }\r |
77dd49a7 |
100 | if (mtrsection.group(1).contains("nmake.")) {\r |
27e0221a |
101 | mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r |
102 | while (mtrinfequation.find()) {\r |
103 | if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {\r |
104 | mi.entrypoint = mtrinfequation.group(2);\r |
105 | }\r |
106 | if (mtrinfequation.group(1).matches("DPX_SOURCE")) {\r |
107 | if (!mi.localmodulesources.contains(mtrinfequation.group(2))) {\r |
108 | MigrationTool.ui.println("DPX File Missing! : " + mtrinfequation.group(2));\r |
109 | }\r |
110 | }\r |
111 | }\r |
112 | }\r |
113 | if (mtrsection.group(1).contains("sources.")) {\r |
114 | mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r |
115 | while (mtrfilename.find()) {\r |
372a4642 |
116 | mi.infsources.add(mtrfilename.group());\r |
27e0221a |
117 | if (!mi.localmodulesources.contains(mtrfilename.group())) {\r |
39e5e412 |
118 | MigrationTool.ui.println("Warn: Source File Missing! : " + mtrfilename.group());\r |
27e0221a |
119 | }\r |
120 | }\r |
121 | }\r |
0e9d14c4 |
122 | if (mtrsection.group(1).matches("includes.")) {\r |
39e5e412 |
123 | mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r |
124 | while (mtrfilename.find()) {\r |
372a4642 |
125 | mi.infincludes.add(mtrfilename.group());\r |
39e5e412 |
126 | }\r |
127 | }\r |
27e0221a |
128 | }\r |
129 | }\r |
130 | \r |
39e5e412 |
131 | private final void preProcessModule() throws Exception {\r |
372a4642 |
132 | // according to .inf file, add extraordinary includes and sourcefiles\r |
133 | Common.dirCopy(mi.modulepath, mi.temppath); // collect all Laplace.namechange to here???\r |
39e5e412 |
134 | \r |
372a4642 |
135 | if (!mi.infincludes.isEmpty()) {\r |
39e5e412 |
136 | Iterator<String> it = mi.infincludes.iterator();\r |
137 | String tempincludename = null;\r |
372a4642 |
138 | while (it.hasNext()) {\r |
139 | tempincludename = it.next();\r |
140 | if (tempincludename.contains("..")) {\r |
141 | Matcher mtr = Common.PTNSEPARATER.matcher(tempincludename);\r |
142 | if (mtr.find() && !mtr.group(2).matches(".")) {\r |
143 | Common.oneLevelDirCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1") + File.separator + mtr.group(2), mi.temppath, ".h");\r |
144 | } else {\r |
145 | Common.oneLevelDirCopy(mi.modulepath.replaceAll(Common.STRSEPARATER, "$1"), mi.temppath, ".h");\r |
146 | }\r |
147 | }\r |
148 | }\r |
149 | }\r |
150 | if (!mi.infsources.isEmpty()) {\r |
39e5e412 |
151 | Iterator<String> it = mi.infsources.iterator();\r |
152 | String tempsourcename = null;\r |
153 | while (it.hasNext()) {\r |
372a4642 |
154 | tempsourcename = it.next();\r |
155 | if (tempsourcename.contains("..")) {\r |
156 | Common.ensureDir(mi.temppath + File.separator + "MT_Parent_Sources");\r |
157 | Matcher mtr = Common.PTNSEPARATER.matcher(tempsourcename);\r |
158 | if (mtr.find()) {\r |
159 | 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 |
160 | }\r |
161 | }\r |
39e5e412 |
162 | }\r |
372a4642 |
163 | }\r |
446e26ee |
164 | \r |
39e5e412 |
165 | //CommentOutNonLocalHFile();\r |
df87de9b |
166 | Common.toDoAll(mi.temppath, this, Common.FILE);\r |
39e5e412 |
167 | \r |
168 | parsePreProcessedSourceCode();\r |
446e26ee |
169 | \r |
27e0221a |
170 | }\r |
446e26ee |
171 | \r |
39e5e412 |
172 | private final void parsePreProcessedSourceCode() throws Exception {\r |
27e0221a |
173 | //Cl cl = new Cl(modulepath);\r |
174 | //cl.execute("Fat.c");\r |
175 | //cl.generateAll(preprocessedccodes);\r |
176 | //\r |
177 | //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r |
178 | //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r |
179 | //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add\r |
180 | BufferedReader rd = null;\r |
181 | String ifile = null;\r |
182 | String line = null;\r |
183 | String temp = null;\r |
184 | \r |
185 | Iterator<String> ii = mi.localmodulesources.iterator();\r |
186 | while (ii.hasNext()) {\r |
187 | temp = ii.next();\r |
49324055 |
188 | if (temp.contains(".c") || temp.contains(".dxs")) {\r |
27e0221a |
189 | mi.preprocessedccodes.add(temp);\r |
190 | }\r |
191 | }\r |
192 | \r |
193 | ii = mi.preprocessedccodes.iterator();\r |
194 | \r |
195 | Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r |
27e0221a |
196 | Matcher matguid;\r |
197 | Matcher matfuncc;\r |
198 | Matcher matfuncd;\r |
199 | Matcher matenclosereplace;\r |
200 | Matcher matefifuncc;\r |
27e0221a |
201 | Matcher matmacro;\r |
202 | \r |
203 | while (ii.hasNext()) {\r |
204 | StringBuffer wholefile = new StringBuffer();\r |
205 | ifile = ii.next();\r |
df87de9b |
206 | rd = new BufferedReader(new FileReader(mi.temppath + File.separator + ifile));\r |
27e0221a |
207 | while ((line = rd.readLine()) != null) {\r |
208 | wholefile.append(line + '\n');\r |
209 | }\r |
210 | line = wholefile.toString();\r |
211 | \r |
27e0221a |
212 | // find guid\r |
213 | matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :\r |
214 | while (matguid.find()) { // 1.currently , find once , then call to identify which is it\r |
215 | if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use 3 different matchers , search 3 times to find each\r |
216 | //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost\r |
217 | }\r |
218 | }\r |
219 | //matguid.appendTail(result);\r |
220 | //line = result.toString();\r |
446e26ee |
221 | \r |
27e0221a |
222 | // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r |
223 | // This item is not simply replaced , special operation is required.\r |
224 | matefifuncc = patefifuncc.matcher(line);\r |
225 | while (matefifuncc.find()) {\r |
226 | mi.hashEFIcall.add(matefifuncc.group(2));\r |
227 | }\r |
446e26ee |
228 | \r |
27e0221a |
229 | // find function call\r |
230 | matfuncc = Func.ptnfuncc.matcher(line);\r |
231 | while (matfuncc.find()) {\r |
232 | if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r |
233 | //MigrationTool.ui.println(ifile + " dofunc " + temp);\r |
234 | //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));\r |
235 | }\r |
236 | }\r |
237 | //matfuncc.appendTail(result);\r |
238 | //line = result.toString();\r |
446e26ee |
239 | \r |
27e0221a |
240 | // find macro\r |
241 | matmacro = Macro.ptntmacro.matcher(line);\r |
242 | while (matmacro.find()) {\r |
243 | if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r |
244 | }\r |
245 | }\r |
246 | \r |
247 | // find function definition\r |
248 | // replace all {} to @\r |
249 | while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r |
250 | line = matenclosereplace.replaceAll("@");\r |
251 | }\r |
446e26ee |
252 | \r |
27e0221a |
253 | matfuncd = Func.ptnfuncd.matcher(line);\r |
254 | while (matfuncd.find()) {\r |
255 | if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r |
256 | }\r |
257 | }\r |
258 | }\r |
259 | \r |
260 | // op on hash\r |
261 | Iterator<String> funcci = mi.hashfuncc.iterator();\r |
262 | while (funcci.hasNext()) {\r |
263 | if (!mi.hashfuncd.contains(temp = funcci.next()) && !mi.hashEFIcall.contains(temp)) {\r |
264 | mi.hashnonlocalfunc.add(temp); // this set contains both changed and not changed items\r |
265 | }\r |
266 | }\r |
267 | }\r |
39e5e412 |
268 | \r |
269 | public class CommentLaplace extends Common.Laplace {\r |
270 | public String operation(String wholeline) {\r |
372a4642 |
271 | StringBuffer wholebuffer = new StringBuffer();\r |
272 | String templine = null;\r |
39e5e412 |
273 | Pattern ptnincludefile = Pattern.compile("[\"<](.*[.]h)[\">]");\r |
274 | Pattern ptninclude = Pattern.compile("#include\\s*(.*)");\r |
275 | Matcher mtrinclude = ptninclude.matcher(wholeline);\r |
276 | Matcher mtrincludefile = null;\r |
277 | while (mtrinclude.find()) {\r |
372a4642 |
278 | mtrincludefile = ptnincludefile.matcher(mtrinclude.group(1));\r |
279 | if (mtrincludefile.find() && mi.localmodulesources.contains(mtrincludefile.group(1))) {\r |
280 | templine = mtrinclude.group();\r |
281 | } else {\r |
282 | templine = MigrationTool.MIGRATIONCOMMENT + mtrinclude.group();\r |
283 | }\r |
284 | mtrinclude.appendReplacement(wholebuffer, templine);\r |
39e5e412 |
285 | }\r |
286 | mtrinclude.appendTail(wholebuffer);\r |
287 | return wholebuffer.toString();\r |
288 | }\r |
289 | \r |
290 | public boolean recognize(String filename) {\r |
372a4642 |
291 | return filename.contains(".c") || filename.contains(".h");\r |
39e5e412 |
292 | }\r |
293 | \r |
294 | public String namechange(String oldname) {\r |
372a4642 |
295 | return oldname;\r |
39e5e412 |
296 | }\r |
297 | }\r |
298 | \r |
299 | //-----------------------------------ForDoAll-----------------------------------//\r |
300 | public void run(String filepath) throws Exception {\r |
372a4642 |
301 | String name = mi.temppath + File.separator + filepath.replace(mi.temppath + File.separator, "");\r |
302 | commentlaplace.transform(name, name);\r |
39e5e412 |
303 | }\r |
304 | \r |
305 | public boolean filter(File dir) {\r |
306 | return true;\r |
307 | }\r |
308 | //-----------------------------------ForDoAll-----------------------------------//\r |
309 | \r |
310 | public final void setModuleInfo(ModuleInfo m) {\r |
372a4642 |
311 | mi = m;\r |
39e5e412 |
312 | }\r |
313 | \r |
314 | public static final void aimAt(ModuleInfo mi) throws Exception {\r |
372a4642 |
315 | modulereader.setModuleInfo(mi);\r |
316 | modulereader.ModuleScan();\r |
39e5e412 |
317 | }\r |
719cebfe |
318 | }\r |