]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1253 6f19259b...
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / ModuleInfo.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 /*
20 Class ModuleInfo is built for scanning the source files, it contains all the needed
21 information and all the temporary data.
22 */
23 public class ModuleInfo {
24 ModuleInfo(String modulepath, UI ui, Database db) throws Exception {
25 this.modulepath = modulepath;
26 this.ui = ui;
27 this.db = db;
28 moduleScan();
29 }
30
31 private String modulepath = null;
32 private Database db = null;
33 private UI ui = null;
34
35 public String modulename = null;
36 public String guidvalue = null;
37 public String moduletype = null;
38 public String entrypoint = null;
39
40 public Set<String> localmodulesources = new HashSet<String>(); //contains both .c and .h
41 public Set<String> preprocessedccodes = new HashSet<String>();
42
43 public Set<String> hashfuncc = new HashSet<String>();
44 public Set<String> hashfuncd = new HashSet<String>();
45 public Set<String> hashnonlocalfunc = new HashSet<String>();
46 public Set<String> hashnonlocalmacro = new HashSet<String>();
47 public Set<String> hashEFIcall = new HashSet<String>();
48 public Set<String> hashr8only = new HashSet<String>();
49
50 public Set<String> hashrequiredr9libs = new HashSet<String>(); // hashrequiredr9libs is now all added in SourceFileReplacer
51 public Set<String> guid = new HashSet<String>();
52 public Set<String> protocol = new HashSet<String>();
53 public Set<String> ppi = new HashSet<String>();
54
55 private static String migrationcomment = "//%$//";
56
57 private void moduleScan() throws Exception {
58 String[] list = new File(modulepath).list();
59 boolean hasInf = false;
60 String infname = null;
61 boolean hasMsa = false;
62 String msaname = null;
63
64 for (int i = 0 ; i < list.length ; i++) {
65 if (new File(list[i]).isDirectory()) {
66 ;
67 } else {
68 if (list[i].contains(".c") || list[i].contains(".C")) {
69 localmodulesources.add(list[i]);
70 } else if (list[i].contains(".h") || list[i].contains(".H")) {
71 localmodulesources.add(list[i]); //the case that several .inf or .msa found is not concerned
72 } else if (list[i].contains(".dxs")) {
73 localmodulesources.add(list[i]);
74 } else if (list[i].contains(".uni")) {
75 localmodulesources.add(list[i]);
76 } else if (list[i].contains(".inf")) {
77 if (ui.yesOrNo("Found .inf file : " + list[i] + "\nUse this file as this module's .inf ?")) {
78 hasInf = true;
79 infname = list[i];
80 } else {
81 continue;
82 }
83 } else if (list[i].contains(".msa")) {
84 if (ui.yesOrNo("Found .msa file : " + list[i] + "\nUse this file as this module's .msa ?")) {
85 hasMsa = true;
86 msaname = list[i];
87 } else {
88 continue;
89 }
90 }
91 }
92 }
93
94 ModuleReader mr = new ModuleReader(modulepath, this, db);
95 if (hasInf) { // this sequence shows using .inf as default
96 mr.readInf(infname);
97 } else if (hasMsa) {
98 mr.readMsa(msaname);
99 } else {
100 ui.println("No Inf Nor Msa Found");
101 }
102
103 CommentOutNonLocalHFile();
104 parsePreProcessedSourceCode();
105
106 new SourceFileReplacer(modulepath, this, db, ui).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
107
108 // show result
109 if (ui.yesOrNo("Parse Module Information Complete . See details ?")) {
110 ui.println("\nModule Information : ");
111 ui.println("Entrypoint : " + entrypoint);
112 show(protocol, "Protocol : ");
113 show(ppi, "Ppi : ");
114 show(guid, "Guid : ");
115 show(hashfuncc, "call : ");
116 show(hashfuncd, "def : ");
117 show(hashEFIcall, "EFIcall : ");
118 show(hashnonlocalmacro, "macro : ");
119 show(hashnonlocalfunc, "nonlocal : ");
120 show(hashr8only, "hashr8only : ");
121 }
122
123 new MsaWriter(modulepath, this, db).flush();
124
125 // remove temp directory
126 //File tempdir = new File(modulepath + File.separator + "temp");
127 //System.out.println("Deleting Dir");
128 //if (tempdir.exists()) tempdir.d;
129
130 ui.println("Errors Left : " + db.error);
131 ui.println("Complete!");
132 ui.println("Your R9 module is placed at " + modulepath + File.separator + "result");
133 ui.println("Your logfile is placed at " + modulepath);
134 }
135
136 private void show(Set<String> hash, String show) {
137 ui.println(show + hash.size());
138 ui.println(hash);
139 }
140
141 // add '//' to all non-local include lines
142 private void CommentOutNonLocalHFile() throws IOException {
143 BufferedReader rd;
144 String line;
145 String curFile;
146 PrintWriter outfile;
147
148 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");
149 Matcher mtcinclude;
150
151 File tempdir = new File(modulepath + File.separator + "temp" + File.separator);
152 if (!tempdir.exists()) tempdir.mkdir();
153
154 Iterator<String> ii = localmodulesources.iterator();
155 while ( ii.hasNext() ) {
156 curFile = ii.next();
157 rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));
158 outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));
159 while ((line = rd.readLine()) != null) {
160 if (line.contains("#include")) {
161 mtcinclude = ptninclude.matcher(line);
162 if (mtcinclude.find() && localmodulesources.contains(mtcinclude.group(1))) {
163 } else {
164 line = migrationcomment + line;
165 }
166 }
167 outfile.append(line + '\n');
168 }
169 outfile.flush();
170 outfile.close();
171 }
172 }
173 /*
174 private void search(String line, Pattern ptn, Method md) {
175 matmacro = Func.ptntmacro.matcher(line);
176 while (matmacro.find()) {
177 if ((temp = Func.registerMacro(matmacro, this, db)) != null) {
178 }
179 }
180 }
181 */
182 private void parsePreProcessedSourceCode() throws Exception {
183 //Cl cl = new Cl(modulepath);
184 //cl.execute("Fat.c");
185 //cl.generateAll(preprocessedccodes);
186 //
187 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
188 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
189 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
190 String[] list = new File(modulepath).list();
191 for (int i = 0 ; i < list.length ; i++) {
192 if (list[i].contains(".c")) { // without CL , change to .i
193 preprocessedccodes.add(list[i]);
194 }
195 }
196 //
197 Iterator<String> ii = preprocessedccodes.iterator();
198 BufferedReader rd = null;
199 String ifile = null;
200 String line = null;
201 String temp = null;
202 //StringBuffer result = new StringBuffer();
203
204 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);
205 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);
206 Matcher matguid;
207 Matcher matfuncc;
208 Matcher matfuncd;
209 Matcher matenclosereplace;
210 Matcher matefifuncc;
211 Matcher matentrypoint;
212 Matcher matmacro;
213
214 while (ii.hasNext()) {
215 StringBuffer wholefile = new StringBuffer();
216 ifile = ii.next();
217 rd = new BufferedReader(new FileReader(modulepath + File.separator + "temp" + File.separator + ifile));
218 while ((line = rd.readLine()) != null) {
219 wholefile.append(line + '\n');
220 }
221 line = wholefile.toString();
222
223 // if this is a Pei phase module , add these library class to .msa
224 matentrypoint = patentrypoint.matcher(line);
225 if (matentrypoint.find()) {
226 entrypoint = matentrypoint.group(2);
227 if (matentrypoint.group(1).matches("PEIM")) {
228 hashrequiredr9libs.add("PeimEntryPoint");
229 } else {
230 hashrequiredr9libs.add("UefiDriverEntryPoint");
231 }
232 }
233
234 // find guid
235 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :
236 while (matguid.find()) { // 1.currently , find once , then call to identify which is it
237 if ((temp = Guid.register(matguid, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each
238 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
239 }
240 }
241 //matguid.appendTail(result);
242 //line = result.toString();
243
244 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
245 // This item is not simply replaced , special operation is required.
246 matefifuncc = patefifuncc.matcher(line);
247 while (matefifuncc.find()) {
248 hashEFIcall.add(matefifuncc.group(2));
249 }
250
251 // find function call
252 matfuncc = Func.ptnfuncc.matcher(line);
253 while (matfuncc.find()) {
254 if ((temp = Func.register(matfuncc, this, db)) != null) {
255 //ui.println(ifile + " dofunc " + temp);
256 //matfuncc.appendReplacement(result, db.getR9Func(temp));
257 }
258 }
259 //matfuncc.appendTail(result);
260 //line = result.toString();
261
262 // find macro
263 matmacro = Macro.ptntmacro.matcher(line);
264 while (matmacro.find()) {
265 if ((temp = Macro.register(matmacro, this, db)) != null) {
266 }
267 }
268
269 // find function definition
270 // replace all {} to @
271 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {
272 line = matenclosereplace.replaceAll("@");
273 }
274
275 matfuncd = Func.ptnfuncd.matcher(line);
276 while (matfuncd.find()) {
277 if ((temp = Func.register(matfuncd, this, db)) != null) {
278 }
279 }
280 }
281
282 // op on hash
283 Iterator<String> funcci = hashfuncc.iterator();
284 while (funcci.hasNext()) {
285 if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {
286 hashnonlocalfunc.add(temp); // this set contains both changed and not changed items
287 }
288 }
289 }
290
291 public static void main(String[] args) throws Exception {
292 FirstPanel.init();
293 }
294 }