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