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
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.
13 package org
.tianocore
.migration
;
17 import java
.util
.regex
.*;
20 Class ModuleInfo is built for scanning the source files, it contains all the needed
21 information and all the temporary data.
23 public class ModuleInfo
{
24 ModuleInfo(String modulepath
) throws Exception
{
25 this.modulepath
= modulepath
;
27 ui
.println("Choose where to place the result");
28 if ((outputpath
= ui
.getFilepath()) == null) {
29 outputpath
= modulepath
;
31 ui
.println(outputpath
);
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
39 public String modulepath
= null;
41 public String outputpath
= null;
43 public String modulename
= null;
44 public String guidvalue
= null;
45 public String moduletype
= null;
46 public String entrypoint
= null;
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
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
>();
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
>();
64 private static String migrationcomment
= "//%$//";
66 private void moduleScan() throws Exception
{
67 Common
.toDoAll(modulepath
, ModuleInfo
.class.getMethod("enroll", String
.class), this, null, Common
.FILE
);
69 String filename
= null;
70 if (msaorinf
.isEmpty()) {
71 ui
.println("No INF nor MSA file found!");
74 filename
= ui
.choose("Found .inf or .msa file for module\n" + modulepath
+ "\nChoose one Please", msaorinf
.toArray());
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);
83 CommentOutNonLocalHFile();
84 parsePreProcessedSourceCode();
86 new SourceFileReplacer(modulepath
, outputpath
, this, db
, ui
).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
89 if (ui
.yesOrNo("Parse of the Module Information has completed. View details?")) {
90 ui
.println("\nModule Information : ");
91 ui
.println("Entrypoint : " + entrypoint
);
92 show(protocol
, "Protocol : ");
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 : ");
103 new MsaWriter(modulepath
, outputpath
, this, db
, ui
).flush();
105 Common
.deleteDir(modulepath
+ File
.separator
+ "temp");
106 //Common.toDoAll(modulepath + File.separator + "temp", Common.class.getMethod("deleteDir", String.class), null, null, Common.DIR);
108 ui
.println("Errors Left : " + db
.error
);
109 ui
.println("Complete!");
110 ui
.println("Your R9 module was placed here: " + modulepath
+ File
.separator
+ "result");
111 ui
.println("Your logfile was placed here: " + modulepath
);
114 private void show(Set
<String
> hash
, String show
) {
115 ui
.println(show
+ hash
.size());
119 // add '//' to all non-local include lines
120 private void CommentOutNonLocalHFile() throws IOException
{
126 Pattern ptninclude
= Pattern
.compile("[\"<](.*[.]h)[\">]");
129 Iterator
<String
> ii
= localmodulesources
.iterator();
130 while ( ii
.hasNext() ) {
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))) {
140 line
= migrationcomment
+ line
;
143 outfile
.append(line
+ '\n');
150 private void parsePreProcessedSourceCode() throws Exception
{
151 //Cl cl = new Cl(modulepath);
152 //cl.execute("Fat.c");
153 //cl.generateAll(preprocessedccodes);
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;
163 Iterator
<String
> ii
= localmodulesources
.iterator();
164 while (ii
.hasNext()) {
166 if (temp
.contains(".c")) {
167 preprocessedccodes
.add(temp
);
171 ii
= preprocessedccodes
.iterator();
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
);
178 Matcher matenclosereplace
;
180 Matcher matentrypoint
;
183 while (ii
.hasNext()) {
184 StringBuffer wholefile
= new StringBuffer();
186 rd
= new BufferedReader(new FileReader(modulepath
+ File
.separator
+ "temp" + File
.separator
+ ifile
));
187 while ((line
= rd
.readLine()) != null) {
188 wholefile
.append(line
+ '\n');
190 line
= wholefile
.toString();
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");
199 hashrequiredr9libs
.add("UefiDriverEntryPoint");
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, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
210 //matguid.appendTail(result);
211 //line = result.toString();
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));
220 // find function call
221 matfuncc
= Func
.ptnfuncc
.matcher(line
);
222 while (matfuncc
.find()) {
223 if ((temp
= Func
.register(matfuncc
, this, db
)) != null) {
224 //ui.println(ifile + " dofunc " + temp);
225 //matfuncc.appendReplacement(result, db.getR9Func(temp));
228 //matfuncc.appendTail(result);
229 //line = result.toString();
232 matmacro
= Macro
.ptntmacro
.matcher(line
);
233 while (matmacro
.find()) {
234 if ((temp
= Macro
.register(matmacro
, this, db
)) != null) {
238 // find function definition
239 // replace all {} to @
240 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
241 line
= matenclosereplace
.replaceAll("@");
244 matfuncd
= Func
.ptnfuncd
.matcher(line
);
245 while (matfuncd
.find()) {
246 if ((temp
= Func
.register(matfuncd
, this, db
)) != null) {
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
260 public final void enroll(String filepath
) throws Exception
{
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]);
272 public static final void seekModule(String filepath
) throws Exception
{
273 if (isModule(filepath
)) {
274 //System.out.println("I'm in");
275 new ModuleInfo(filepath
);
279 private static final boolean isModule(String path
) {
280 String
[] list
= new File(path
).list();
281 for (int i
= 0 ; i
< list
.length
; i
++) {
282 if (!new File(list
[i
]).isDirectory()) {
283 if (list
[i
].contains(".inf") || list
[i
].contains(".msa")) {
291 public static final void main(String[] args) throws Exception {
292 ui = FirstPanel.init();