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