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
> preprocessedccodes
= new HashSet
<String
>();
42 public Set
<String
> msaorinf
= new HashSet
<String
>(); //only a little, hash may be too big for this
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 dirScan(String subpath
) throws Exception
{
59 String
[] list
= new File(modulepath
+ File
.separator
+ subpath
).list(); // if no sub , separator need?
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")) {
67 dirScan(subpath
+ list
[i
] + File
.separator
);
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
]);
80 private void moduleScan() throws Exception
{
82 String filename
= null;
83 if (msaorinf
.isEmpty()) {
84 ui
.println("No INF nor MSA file found!");
87 filename
= ui
.choose("Found .inf or .msa file in the module\nChoose one Please", msaorinf
.toArray());
89 ModuleReader mr
= new ModuleReader(modulepath
, this, db
, ui
);
90 if (filename
.contains(".inf")) {
92 } else if (filename
.contains(".msa")) {
96 CommentOutNonLocalHFile();
97 parsePreProcessedSourceCode();
99 new SourceFileReplacer(modulepath
, this, db
, ui
).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
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 : ");
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 : ");
116 new MsaWriter(modulepath
, this, db
).flush();
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;
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
);
129 private void show(Set
<String
> hash
, String show
) {
130 ui
.println(show
+ hash
.size());
134 // add '//' to all non-local include lines
135 private void CommentOutNonLocalHFile() throws IOException
{
141 Pattern ptninclude
= Pattern
.compile("[\"<](.*[.]h)[\">]");
144 Iterator
<String
> ii
= localmodulesources
.iterator();
145 while ( ii
.hasNext() ) {
147 rd
= new BufferedReader(new FileReader(modulepath
+ File
.separator
+ curFile
));
148 Common
.ensureDir(modulepath
+ File
.separator
+ "temp" + File
.separator
+ curFile
);
149 outfile
= new PrintWriter(new BufferedWriter(new FileWriter(modulepath
+ File
.separator
+ "temp" + File
.separator
+ curFile
)));
150 while ((line
= rd
.readLine()) != null) {
151 if (line
.contains("#include")) {
152 mtrinclude
= ptninclude
.matcher(line
);
153 if (mtrinclude
.find() && localmodulesources
.contains(mtrinclude
.group(1))) {
155 line
= migrationcomment
+ line
;
158 outfile
.append(line
+ '\n');
165 private void parsePreProcessedSourceCode() throws Exception
{
166 //Cl cl = new Cl(modulepath);
167 //cl.execute("Fat.c");
168 //cl.generateAll(preprocessedccodes);
170 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
171 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
172 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
173 BufferedReader rd
= null;
178 Iterator
<String
> ii
= localmodulesources
.iterator();
179 while (ii
.hasNext()) {
181 if (temp
.contains(".c")) {
182 preprocessedccodes
.add(temp
);
186 ii
= preprocessedccodes
.iterator();
188 Pattern patefifuncc
= Pattern
.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern
.MULTILINE
);
189 Pattern patentrypoint
= Pattern
.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern
.MULTILINE
);
193 Matcher matenclosereplace
;
195 Matcher matentrypoint
;
198 while (ii
.hasNext()) {
199 StringBuffer wholefile
= new StringBuffer();
201 rd
= new BufferedReader(new FileReader(modulepath
+ File
.separator
+ "temp" + File
.separator
+ ifile
));
202 while ((line
= rd
.readLine()) != null) {
203 wholefile
.append(line
+ '\n');
205 line
= wholefile
.toString();
207 // if this is a Pei phase module , add these library class to .msa
208 matentrypoint
= patentrypoint
.matcher(line
);
209 if (matentrypoint
.find()) {
210 entrypoint
= matentrypoint
.group(2);
211 if (matentrypoint
.group(1).matches("PEIM")) {
212 hashrequiredr9libs
.add("PeimEntryPoint");
214 hashrequiredr9libs
.add("UefiDriverEntryPoint");
219 matguid
= Guid
.ptnguid
.matcher(line
); // several ways to implement this , which one is faster ? :
220 while (matguid
.find()) { // 1.currently , find once , then call to identify which is it
221 if ((temp
= Guid
.register(matguid
, this, db
)) != null) { // 2.use 3 different matchers , search 3 times to find each
222 //matguid.appendReplacement(result, db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
225 //matguid.appendTail(result);
226 //line = result.toString();
228 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
229 // This item is not simply replaced , special operation is required.
230 matefifuncc
= patefifuncc
.matcher(line
);
231 while (matefifuncc
.find()) {
232 hashEFIcall
.add(matefifuncc
.group(2));
235 // find function call
236 matfuncc
= Func
.ptnfuncc
.matcher(line
);
237 while (matfuncc
.find()) {
238 if ((temp
= Func
.register(matfuncc
, this, db
)) != null) {
239 //ui.println(ifile + " dofunc " + temp);
240 //matfuncc.appendReplacement(result, db.getR9Func(temp));
243 //matfuncc.appendTail(result);
244 //line = result.toString();
247 matmacro
= Macro
.ptntmacro
.matcher(line
);
248 while (matmacro
.find()) {
249 if ((temp
= Macro
.register(matmacro
, this, db
)) != null) {
253 // find function definition
254 // replace all {} to @
255 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
256 line
= matenclosereplace
.replaceAll("@");
259 matfuncd
= Func
.ptnfuncd
.matcher(line
);
260 while (matfuncd
.find()) {
261 if ((temp
= Func
.register(matfuncd
, this, db
)) != null) {
267 Iterator
<String
> funcci
= hashfuncc
.iterator();
268 while (funcci
.hasNext()) {
269 if (!hashfuncd
.contains(temp
= funcci
.next()) && !hashEFIcall
.contains(temp
)) {
270 hashnonlocalfunc
.add(temp
); // this set contains both changed and not changed items
275 public static void main(String
[] args
) throws Exception
{