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
, UI ui
, Database db
) throws Exception
{
25 this.modulepath
= modulepath
;
31 private String modulepath
= null;
32 private Database db
= null;
35 public String modulename
= null;
36 public String guidvalue
= null;
37 public String moduletype
= null;
38 public String entrypoint
= null;
40 public Set
<String
> localmodulesources
= new HashSet
<String
>(); //contains both .c and .h
41 public Set
<String
> localmoduleheaders
= new HashSet
<String
>();
42 public Set
<String
> preprocessedccodes
= new HashSet
<String
>();
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
>();
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
>();
56 private static String migrationcomment
= "//%$//";
58 private void moduleScan() throws Exception
{
59 String
[] list
= new File(modulepath
).list();
60 boolean hasInf
= false;
61 String infname
= null;
62 boolean hasMsa
= false;
63 String msaname
= null;
65 for (int i
= 0 ; i
< list
.length
; i
++) {
66 if (new File(list
[i
]).isDirectory()) {
69 if (list
[i
].contains(".c") || list
[i
].contains(".C")) {
70 localmodulesources
.add(list
[i
]);
71 } else if (list
[i
].contains(".h") || list
[i
].contains(".H")) {
72 localmodulesources
.add(list
[i
]);
73 localmoduleheaders
.add(list
[i
]); //the case that several .inf or .msa found is not concerned
74 } else if (list
[i
].contains(".inf")) {
75 if (ui
.yesOrNo("Found .inf file : " + list
[i
] + "\nUse this file as this module's .inf ?")) {
81 } else if (list
[i
].contains(".msa")) {
82 if (ui
.yesOrNo("Found .msa file : " + list
[i
] + "\nUse this file as this module's .msa ?")) {
92 ModuleReader mr
= new ModuleReader(modulepath
, this, db
);
93 if (hasInf
) { // this sequence shows using .inf as default
98 ui
.println("No Inf Nor Msa Found");
101 CommentOutNonLocalHFile();
102 parsePreProcessedSourceCode();
104 new SourceFileReplacer(modulepath
, this, db
, ui
).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
107 if (ui
.yesOrNo("Parse Module Information Complete . See details ?")) {
108 ui
.println("\nModule Information : ");
109 ui
.println("Entrypoint : " + entrypoint
);
110 show(protocol
, "Protocol : ");
112 show(guid
, "Guid : ");
113 show(hashfuncc
, "call : ");
114 show(hashfuncd
, "def : ");
115 show(hashEFIcall
, "EFIcall : ");
116 show(hashnonlocalmacro
, "macro : ");
117 show(hashnonlocalfunc
, "nonlocal : ");
118 show(hashr8only
, "hashr8only : ");
121 new MsaWriter(modulepath
, this, db
).flush();
123 // remove temp directory
124 //File tempdir = new File(modulepath + File.separator + "temp");
125 //System.out.println("Deleting Dir");
126 //if (tempdir.exists()) tempdir.d;
128 ui
.println("Errors Left : " + db
.error
);
129 ui
.println("Complete!");
130 ui
.println("Your R9 module is placed at " + modulepath
+ File
.separator
+ "result");
131 ui
.println("Your logfile is placed at " + modulepath
);
134 private void show(Set
<String
> hash
, String show
) {
135 ui
.println(show
+ hash
.size());
139 // add '//' to all non-local include lines
140 private void CommentOutNonLocalHFile() throws IOException
{
146 Pattern ptninclude
= Pattern
.compile("[\"<](.*[.]h)[\">]");
149 File tempdir
= new File(modulepath
+ File
.separator
+ "temp" + File
.separator
);
150 if (!tempdir
.exists()) tempdir
.mkdir();
152 Iterator
<String
> ii
= localmodulesources
.iterator();
153 while ( ii
.hasNext() ) {
155 rd
= new BufferedReader(new FileReader(modulepath
+ File
.separator
+ curFile
));
156 outfile
= new PrintWriter(new BufferedWriter(new FileWriter(modulepath
+ File
.separator
+ "temp" + File
.separator
+ curFile
)));
157 while ((line
= rd
.readLine()) != null) {
158 if (line
.contains("#include")) {
159 mtcinclude
= ptninclude
.matcher(line
);
160 if (mtcinclude
.find() && localmoduleheaders
.contains(mtcinclude
.group(1))) {
162 line
= migrationcomment
+ line
;
165 outfile
.append(line
+ '\n');
172 private void search(String line, Pattern ptn, Method md) {
173 matmacro = Func.ptntmacro.matcher(line);
174 while (matmacro.find()) {
175 if ((temp = Func.registerMacro(matmacro, this, db)) != null) {
180 private void parsePreProcessedSourceCode() throws Exception
{
181 //Cl cl = new Cl(modulepath);
182 //cl.execute("Fat.c");
183 //cl.generateAll(preprocessedccodes);
185 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
186 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
187 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
188 String
[] list
= new File(modulepath
).list();
189 for (int i
= 0 ; i
< list
.length
; i
++) {
190 if (list
[i
].contains(".c")) { // without CL , change to .i
191 preprocessedccodes
.add(list
[i
]);
195 Iterator
<String
> ii
= preprocessedccodes
.iterator();
196 BufferedReader rd
= null;
200 //StringBuffer result = new StringBuffer();
202 Pattern patefifuncc
= Pattern
.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern
.MULTILINE
);
203 Pattern patentrypoint
= Pattern
.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern
.MULTILINE
);
207 Matcher matenclosereplace
;
209 Matcher matentrypoint
;
212 while (ii
.hasNext()) {
213 StringBuffer wholefile
= new StringBuffer();
215 rd
= new BufferedReader(new FileReader(modulepath
+ File
.separator
+ "temp" + File
.separator
+ ifile
));
216 while ((line
= rd
.readLine()) != null) {
217 wholefile
.append(line
+ '\n');
219 line
= wholefile
.toString();
221 // if this is a Pei phase module , add these library class to .msa
222 matentrypoint
= patentrypoint
.matcher(line
);
223 if (matentrypoint
.find()) {
224 entrypoint
= matentrypoint
.group(2);
225 if (matentrypoint
.group(1).matches("PEIM")) {
226 hashrequiredr9libs
.add("PeimEntryPoint");
228 hashrequiredr9libs
.add("UefiDriverEntryPoint");
233 matguid
= Guid
.ptnguid
.matcher(line
); // several ways to implement this , which one is faster ? :
234 while (matguid
.find()) { // 1.currently , find once , then call to identify which is it
235 if ((temp
= Guid
.register(matguid
, this, db
)) != null) { // 2.use 3 different matchers , search 3 times to find each
236 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
239 //matguid.appendTail(result);
240 //line = result.toString();
242 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
243 // This item is not simply replaced , special operation is required.
244 matefifuncc
= patefifuncc
.matcher(line
);
245 while (matefifuncc
.find()) {
246 hashEFIcall
.add(matefifuncc
.group(2));
249 // find function call
250 matfuncc
= Func
.ptnfuncc
.matcher(line
);
251 while (matfuncc
.find()) {
252 if ((temp
= Func
.register(matfuncc
, this, db
)) != null) {
253 //ui.println(ifile + " dofunc " + temp);
254 //matfuncc.appendReplacement(result, db.getR9Func(temp));
257 //matfuncc.appendTail(result);
258 //line = result.toString();
261 matmacro
= Macro
.ptntmacro
.matcher(line
);
262 while (matmacro
.find()) {
263 if ((temp
= Macro
.register(matmacro
, this, db
)) != null) {
267 // find function definition
268 // replace all {} to @
269 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
270 line
= matenclosereplace
.replaceAll("@");
273 matfuncd
= Func
.ptnfuncd
.matcher(line
);
274 while (matfuncd
.find()) {
275 if ((temp
= Func
.register(matfuncd
, this, db
)) != null) {
281 Iterator
<String
> funcci
= hashfuncc
.iterator();
282 while (funcci
.hasNext()) {
283 if (!hashfuncd
.contains(temp
= funcci
.next()) && !hashEFIcall
.contains(temp
)) {
284 hashnonlocalfunc
.add(temp
); // this set contains both changed and not changed items
289 public static void main(String
[] args
) throws Exception
{