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