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