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