]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java
FV attribute editor alpha.
[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
45 if (filename.contains(".inf")) {
46 readInf(filename);
47 } else if (filename.contains(".msa")) {
48 readMsa(filename);
49 }
50
51 CommentOutNonLocalHFile();
52 parsePreProcessedSourceCode();
53
54 }
55
56 private static 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 static 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 if (!mi.localmodulesources.contains(mtrfilename.group())) {
117 MigrationTool.ui.println("Source File Missing! : " + mtrfilename.group());
118 }
119 }
120 }
121 }
122 }
123
124 // add '//' to all non-local include lines
125 private static final void CommentOutNonLocalHFile() throws IOException {
126 BufferedReader rd;
127 String line;
128 String curFile;
129 PrintWriter outfile;
130
131 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");
132 Matcher mtrinclude;
133
134 Iterator<String> ii = mi.localmodulesources.iterator();
135 while ( ii.hasNext() ) {
136 curFile = ii.next();
137 rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + curFile));
138 Common.ensureDir(mi.modulepath + File.separator + "temp" + File.separator + curFile);
139 outfile = new PrintWriter(new BufferedWriter(new FileWriter(mi.modulepath + File.separator + "temp" + File.separator + curFile)));
140
141 /*
142 if (curFile.contains(".dxs")) {
143 if (mi.moduletype.contains("PEI")) {
144
145 } else {
146
147 }
148 }
149 */
150 while ((line = rd.readLine()) != null) {
151 if (line.contains("#include")) {
152 mtrinclude = ptninclude.matcher(line);
153 if (mtrinclude.find() && mi.localmodulesources.contains(mtrinclude.group(1))) {
154 } else {
155 line = MigrationTool.MIGRATIONCOMMENT + line;
156 }
157 }
158 outfile.append(line + '\n');
159 }
160 outfile.flush();
161 outfile.close();
162
163 }
164 }
165
166 private static final void parsePreProcessedSourceCode() throws Exception {
167 //Cl cl = new Cl(modulepath);
168 //cl.execute("Fat.c");
169 //cl.generateAll(preprocessedccodes);
170 //
171 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
172 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
173 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
174 BufferedReader rd = null;
175 String ifile = null;
176 String line = null;
177 String temp = null;
178
179 Iterator<String> ii = mi.localmodulesources.iterator();
180 while (ii.hasNext()) {
181 temp = ii.next();
182 if (temp.contains(".c")) {
183 mi.preprocessedccodes.add(temp);
184 }
185 }
186
187 ii = mi.preprocessedccodes.iterator();
188
189 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);
190 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);
191 Matcher matguid;
192 Matcher matfuncc;
193 Matcher matfuncd;
194 Matcher matenclosereplace;
195 Matcher matefifuncc;
196 Matcher matentrypoint;
197 Matcher matmacro;
198
199 while (ii.hasNext()) {
200 StringBuffer wholefile = new StringBuffer();
201 ifile = ii.next();
202 rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + "temp" + File.separator + ifile));
203 while ((line = rd.readLine()) != null) {
204 wholefile.append(line + '\n');
205 }
206 line = wholefile.toString();
207
208 // if this is a Pei phase module , add these library class to .msa
209 matentrypoint = patentrypoint.matcher(line);
210 if (matentrypoint.find()) {
211 mi.entrypoint = matentrypoint.group(2);
212 if (matentrypoint.group(1).matches("PEIM")) {
213 mi.hashrequiredr9libs.add("PeimEntryPoint");
214 } else {
215 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");
216 }
217 }
218
219 // find guid
220 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :
221 while (matguid.find()) { // 1.currently , find once , then call to identify which is it
222 if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use 3 different matchers , search 3 times to find each
223 //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
224 }
225 }
226 //matguid.appendTail(result);
227 //line = result.toString();
228
229 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
230 // This item is not simply replaced , special operation is required.
231 matefifuncc = patefifuncc.matcher(line);
232 while (matefifuncc.find()) {
233 mi.hashEFIcall.add(matefifuncc.group(2));
234 }
235
236 // find function call
237 matfuncc = Func.ptnfuncc.matcher(line);
238 while (matfuncc.find()) {
239 if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {
240 //MigrationTool.ui.println(ifile + " dofunc " + temp);
241 //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));
242 }
243 }
244 //matfuncc.appendTail(result);
245 //line = result.toString();
246
247 // find macro
248 matmacro = Macro.ptntmacro.matcher(line);
249 while (matmacro.find()) {
250 if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {
251 }
252 }
253
254 // find function definition
255 // replace all {} to @
256 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {
257 line = matenclosereplace.replaceAll("@");
258 }
259
260 matfuncd = Func.ptnfuncd.matcher(line);
261 while (matfuncd.find()) {
262 if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {
263 }
264 }
265 }
266
267 // op on hash
268 Iterator<String> funcci = mi.hashfuncc.iterator();
269 while (funcci.hasNext()) {
270 if (!mi.hashfuncd.contains(temp = funcci.next()) && !mi.hashEFIcall.contains(temp)) {
271 mi.hashnonlocalfunc.add(temp); // this set contains both changed and not changed items
272 }
273 }
274 }
275 }