]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java
remodel 1
[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) throws Exception {
25 this.modulepath = modulepath;
26
27 MigrationTool.ui.println("Choose where to place the result");
28 if ((outputpath = MigrationTool.ui.getFilepath()) == null) {
29 outputpath = modulepath;
30 }
31 MigrationTool.ui.println(outputpath);
32
33 moduleScan();
34 }
35
36 //public static UI ui = null; //if MIM is still usefull, this can be given to it
37 public static Database db = null; //if MIM is still usefull, this can be given to it
38
39 public String modulepath = null;
40
41 public String outputpath = null;
42
43 public String modulename = null;
44 public String guidvalue = null;
45 public String moduletype = null;
46 public String entrypoint = null;
47
48 public Set<String> localmodulesources = new HashSet<String>(); //contains both .c and .h
49 public Set<String> preprocessedccodes = new HashSet<String>();
50 public Set<String> msaorinf = new HashSet<String>(); //only a little, hash may be too big for this
51
52 public Set<String> hashfuncc = new HashSet<String>();
53 public Set<String> hashfuncd = new HashSet<String>();
54 public Set<String> hashnonlocalfunc = new HashSet<String>();
55 public Set<String> hashnonlocalmacro = new HashSet<String>();
56 public Set<String> hashEFIcall = new HashSet<String>();
57 public Set<String> hashr8only = new HashSet<String>();
58
59 public Set<String> hashrequiredr9libs = new HashSet<String>(); // hashrequiredr9libs is now all added in SourceFileReplacer
60 public Set<String> guid = new HashSet<String>();
61 public Set<String> protocol = new HashSet<String>();
62 public Set<String> ppi = new HashSet<String>();
63
64 private static String migrationcomment = "//%$//";
65
66 private void moduleScan() throws Exception {
67 Common.toDoAll(modulepath, ModuleInfo.class.getMethod("enroll", String.class), this, null, Common.FILE);
68
69 String filename = null;
70 if (msaorinf.isEmpty()) {
71 MigrationTool.ui.println("No INF nor MSA file found!");
72 System.exit(0);
73 } else {
74 filename = MigrationTool.ui.choose("Found .inf or .msa file for module\n" + modulepath + "\nChoose one Please", msaorinf.toArray());
75 }
76 //ModuleReader mr = new ModuleReader(modulepath, this, db, ui);
77 if (filename.contains(".inf")) {
78 ModuleReader.readInf(filename, this);
79 } else if (filename.contains(".msa")) {
80 ModuleReader.readMsa(filename, this);
81 }
82
83 CommentOutNonLocalHFile();
84 parsePreProcessedSourceCode();
85
86 new SourceFileReplacer(modulepath, outputpath, this).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
87
88 // show result
89 if (MigrationTool.ui.yesOrNo("Parse of the Module Information has completed. View details?")) {
90 MigrationTool.ui.println("\nModule Information : ");
91 MigrationTool.ui.println("Entrypoint : " + entrypoint);
92 show(protocol, "Protocol : ");
93 show(ppi, "Ppi : ");
94 show(guid, "Guid : ");
95 show(hashfuncc, "call : ");
96 show(hashfuncd, "def : ");
97 show(hashEFIcall, "EFIcall : ");
98 show(hashnonlocalmacro, "macro : ");
99 show(hashnonlocalfunc, "nonlocal : ");
100 show(hashr8only, "hashr8only : ");
101 }
102
103 new MsaWriter(modulepath, outputpath, this).flush();
104
105 Common.deleteDir(modulepath + File.separator + "temp");
106 //Common.toDoAll(modulepath + File.separator + "temp", Common.class.getMethod("deleteDir", String.class), null, null, Common.DIR);
107
108 MigrationTool.ui.println("Errors Left : " + MigrationTool.db.error);
109 MigrationTool.ui.println("Complete!");
110 MigrationTool.ui.println("Your R9 module was placed here: " + modulepath + File.separator + "result");
111 MigrationTool.ui.println("Your logfile was placed here: " + modulepath);
112 }
113
114 private void show(Set<String> hash, String show) {
115 MigrationTool.ui.println(show + hash.size());
116 MigrationTool.ui.println(hash);
117 }
118
119 // add '//' to all non-local include lines
120 private 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 = localmodulesources.iterator();
130 while ( ii.hasNext() ) {
131 curFile = ii.next();
132 rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));
133 Common.ensureDir(modulepath + File.separator + "temp" + File.separator + curFile);
134 outfile = new PrintWriter(new BufferedWriter(new FileWriter(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() && localmodulesources.contains(mtrinclude.group(1))) {
139 } else {
140 line = migrationcomment + line;
141 }
142 }
143 outfile.append(line + '\n');
144 }
145 outfile.flush();
146 outfile.close();
147 }
148 }
149
150 private 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 = localmodulesources.iterator();
164 while (ii.hasNext()) {
165 temp = ii.next();
166 if (temp.contains(".c")) {
167 preprocessedccodes.add(temp);
168 }
169 }
170
171 ii = 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(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 entrypoint = matentrypoint.group(2);
196 if (matentrypoint.group(1).matches("PEIM")) {
197 hashrequiredr9libs.add("PeimEntryPoint");
198 } else {
199 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, this, db)) != null) { // 2.use 3 different matchers , search 3 times to find each
207 //matguid.appendReplacement(result, MigrationTool.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 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, this, db)) != null) {
224 //MigrationTool.ui.println(ifile + " dofunc " + temp);
225 //matfuncc.appendReplacement(result, MigrationTool.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, this, 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, this, db)) != null) {
247 }
248 }
249 }
250
251 // op on hash
252 Iterator<String> funcci = hashfuncc.iterator();
253 while (funcci.hasNext()) {
254 if (!hashfuncd.contains(temp = funcci.next()) && !hashEFIcall.contains(temp)) {
255 hashnonlocalfunc.add(temp); // this set contains both changed and not changed items
256 }
257 }
258 }
259
260 public final void enroll(String filepath) throws Exception {
261 String[] temp;
262 if (filepath.contains(".c") || filepath.contains(".C") || filepath.contains(".h") ||
263 filepath.contains(".H") || filepath.contains(".dxs") || filepath.contains(".uni")) {
264 temp = filepath.split("\\\\");
265 localmodulesources.add(temp[temp.length - 1]);
266 } else if (filepath.contains(".inf") || filepath.contains(".msa")) {
267 temp = filepath.split("\\\\");
268 msaorinf.add(temp[temp.length - 1]);
269 }
270 }
271
272 public static final void seekModule(String filepath) throws Exception {
273 if (isModule(filepath)) {
274 new ModuleInfo(filepath);
275 }
276 }
277
278 private static final boolean isModule(String path) {
279 String[] list = new File(path).list();
280 for (int i = 0 ; i < list.length ; i++) {
281 if (!new File(list[i]).isDirectory()) {
282 if (list[i].contains(".inf") || list[i].contains(".msa")) {
283 return true;
284 }
285 }
286 }
287 return false;
288 }
289
290 public static final void triger(String path) throws Exception {
291 MigrationTool.ui.println("Project Migration");
292 MigrationTool.ui.println("Copyright (c) 2006, Intel Corporation");
293 Common.toDoAll(path, ModuleInfo.class.getMethod("seekModule", String.class), null, null, Common.DIR);
294 }
295 }