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