]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
editable comment style
[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 {
22 private static ModuleInfo mi;
23
24 private static final Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*=\\s*([^\\s]*)");
25 private static final Pattern ptnsection = Pattern.compile("\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE);
26 private static final Pattern ptnfilename = Pattern.compile("[^\\s]+");
27
28 public static final void ModuleScan(ModuleInfo m) throws Exception {
29 mi = m;
30
31 Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll", String.class), mi, null, Common.FILE);
32
33 String filename = null;
34 if (mi.msaorinf.isEmpty()) {
35 MigrationTool.ui.println("No INF nor MSA file found!");
36 System.exit(0);
37 } else {
38 if (mi.msaorinf.size() == 1) {
39 filename = (String)mi.msaorinf.toArray()[0];
40 } else {
41 filename = MigrationTool.ui.choose("Found .inf or .msa file for module\n" + mi.modulepath + "\nChoose one Please", mi.msaorinf.toArray());
42 }
43 }
44 if (filename.contains(".inf")) {
45 readInf(filename);
46 } else if (filename.contains(".msa")) {
47 readMsa(filename);
48 }
49
50 CommentOutNonLocalHFile();
51 parsePreProcessedSourceCode();
52
53 }
54
55 private static final void readMsa(String name) throws Exception {
56 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(new File(mi.modulepath + File.separator + name));
57 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.getModuleSurfaceArea();
58 MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();
59
60 mi.modulename = msaheader.getModuleName();
61 mi.guidvalue = msaheader.getGuidValue();
62 mi.moduletype = msaheader.getModuleType().toString(); // ???
63
64 SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();
65
66 String temp;
67 Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList().iterator();
68 while (li.hasNext()) {
69 if (!mi.localmodulesources.contains(temp = li.next().toString())) {
70 System.out.println("Source File Missing! : " + temp);
71 }
72 }
73 }
74
75 private static final void readInf(String name) throws Exception {
76 System.out.println("\nParsing INF file: " + name);
77 String wholeline;
78 Matcher mtrinfequation;
79 Matcher mtrsection;
80 Matcher mtrfilename;
81
82 wholeline = Common.file2string(mi.modulepath + File.separator + name);
83 mtrsection = ptnsection.matcher(wholeline);
84 while (mtrsection.find()) {
85 if (mtrsection.group(1).matches("defines")) {
86 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));
87 while (mtrinfequation.find()) {
88 if (mtrinfequation.group(1).matches("BASE_NAME")) {
89 mi.modulename = mtrinfequation.group(2);
90 }
91 if (mtrinfequation.group(1).matches("FILE_GUID")) {
92 mi.guidvalue = mtrinfequation.group(2);
93 }
94 if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {
95 mi.moduletype = mtrinfequation.group(2);
96 }
97 }
98 }
99 if (mtrsection.group(1).matches("nmake.common")) {
100 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));
101 while (mtrinfequation.find()) {
102 if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {
103 mi.entrypoint = mtrinfequation.group(2);
104 }
105 if (mtrinfequation.group(1).matches("DPX_SOURCE")) {
106 if (!mi.localmodulesources.contains(mtrinfequation.group(2))) {
107 MigrationTool.ui.println("DPX File Missing! : " + mtrinfequation.group(2));
108 }
109 }
110 }
111 }
112 if (mtrsection.group(1).contains("sources.")) {
113 mtrfilename = ptnfilename.matcher(mtrsection.group(2));
114 while (mtrfilename.find()) {
115 if (!mi.localmodulesources.contains(mtrfilename.group())) {
116 MigrationTool.ui.println("Source File Missing! : " + mtrfilename.group());
117 }
118 }
119 }
120 }
121 }
122
123 // add '//' to all non-local include lines
124 private static final void CommentOutNonLocalHFile() throws IOException {
125 BufferedReader rd;
126 String line;
127 String curFile;
128 PrintWriter outfile;
129
130 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");
131 Matcher mtrinclude;
132
133 Iterator<String> ii = mi.localmodulesources.iterator();
134 while ( ii.hasNext() ) {
135 curFile = ii.next();
136 rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + curFile));
137 Common.ensureDir(mi.modulepath + File.separator + "temp" + File.separator + curFile);
138 outfile = new PrintWriter(new BufferedWriter(new FileWriter(mi.modulepath + File.separator + "temp" + File.separator + curFile)));
139
140 /*
141 if (curFile.contains(".dxs")) {
142 if (mi.moduletype.contains("PEI")) {
143
144 } else {
145
146 }
147 }
148 */
149 while ((line = rd.readLine()) != null) {
150 if (line.contains("#include")) {
151 mtrinclude = ptninclude.matcher(line);
152 if (mtrinclude.find() && mi.localmodulesources.contains(mtrinclude.group(1))) {
153 } else {
154 line = MigrationTool.MIGRATIONCOMMENT + line;
155 }
156 }
157 outfile.append(line + '\n');
158 }
159 outfile.flush();
160 outfile.close();
161
162 }
163 }
164
165 private static final void parsePreProcessedSourceCode() throws Exception {
166 //Cl cl = new Cl(modulepath);
167 //cl.execute("Fat.c");
168 //cl.generateAll(preprocessedccodes);
169 //
170 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
171 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
172 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
173 BufferedReader rd = null;
174 String ifile = null;
175 String line = null;
176 String temp = null;
177
178 Iterator<String> ii = mi.localmodulesources.iterator();
179 while (ii.hasNext()) {
180 temp = ii.next();
181 if (temp.contains(".c")) {
182 mi.preprocessedccodes.add(temp);
183 }
184 }
185
186 ii = mi.preprocessedccodes.iterator();
187
188 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);
189 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);
190 Matcher matguid;
191 Matcher matfuncc;
192 Matcher matfuncd;
193 Matcher matenclosereplace;
194 Matcher matefifuncc;
195 Matcher matentrypoint;
196 Matcher matmacro;
197
198 while (ii.hasNext()) {
199 StringBuffer wholefile = new StringBuffer();
200 ifile = ii.next();
201 rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + "temp" + File.separator + ifile));
202 while ((line = rd.readLine()) != null) {
203 wholefile.append(line + '\n');
204 }
205 line = wholefile.toString();
206
207 // if this is a Pei phase module , add these library class to .msa
208 matentrypoint = patentrypoint.matcher(line);
209 if (matentrypoint.find()) {
210 mi.entrypoint = matentrypoint.group(2);
211 if (matentrypoint.group(1).matches("PEIM")) {
212 mi.hashrequiredr9libs.add("PeimEntryPoint");
213 } else {
214 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");
215 }
216 }
217
218 // find guid
219 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :
220 while (matguid.find()) { // 1.currently , find once , then call to identify which is it
221 if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use 3 different matchers , search 3 times to find each
222 //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
223 }
224 }
225 //matguid.appendTail(result);
226 //line = result.toString();
227
228 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
229 // This item is not simply replaced , special operation is required.
230 matefifuncc = patefifuncc.matcher(line);
231 while (matefifuncc.find()) {
232 mi.hashEFIcall.add(matefifuncc.group(2));
233 }
234
235 // find function call
236 matfuncc = Func.ptnfuncc.matcher(line);
237 while (matfuncc.find()) {
238 if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {
239 //MigrationTool.ui.println(ifile + " dofunc " + temp);
240 //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));
241 }
242 }
243 //matfuncc.appendTail(result);
244 //line = result.toString();
245
246 // find macro
247 matmacro = Macro.ptntmacro.matcher(line);
248 while (matmacro.find()) {
249 if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {
250 }
251 }
252
253 // find function definition
254 // replace all {} to @
255 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {
256 line = matenclosereplace.replaceAll("@");
257 }
258
259 matfuncd = Func.ptnfuncd.matcher(line);
260 while (matfuncd.find()) {
261 if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {
262 }
263 }
264 }
265
266 // op on hash
267 Iterator<String> funcci = mi.hashfuncc.iterator();
268 while (funcci.hasNext()) {
269 if (!mi.hashfuncd.contains(temp = funcci.next()) && !mi.hashEFIcall.contains(temp)) {
270 mi.hashnonlocalfunc.add(temp); // this set contains both changed and not changed items
271 }
272 }
273 }
274 }