1 package org
.tianocore
.migration
;
5 import java
.util
.regex
.*;
8 Class ModuleInfo is built for scanning the source files, it contains all the needed
9 information and all the temporary data.
11 public class ModuleInfo
{
12 ModuleInfo(String modulepath
, UI ui
, Database db
) throws Exception
{
13 this.modulepath
= modulepath
;
19 private String modulepath
= null;
20 private Database db
= null;
23 public String modulename
= null;
24 public String guidvalue
= null;
25 public String moduletype
= null;
26 public String entrypoint
= null;
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
>();
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
>();
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
>();
44 private static String migrationcomment
= "//%$//";
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;
53 for (int i
= 0 ; i
< list
.length
; i
++) {
54 if (new File(list
[i
]).isDirectory()) {
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 ?")) {
69 } else if (list
[i
].contains(".msa")) {
70 if (ui
.yesOrNo("Found .msa file : " + list
[i
] + "\nUse this file as this module's .msa ?")) {
80 ModuleReader mr
= new ModuleReader(modulepath
, this, db
);
81 if (hasInf
) { // this sequence shows using .inf as default
86 ui
.println("No Inf Nor Msa Found");
89 CommentOutNonLocalHFile();
90 parsePreProcessedSourceCode();
92 new SourceFileReplacer(modulepath
, this, db
, ui
).flush(); // some adding library actions are taken here,so it must be put before "MsaWriter"
95 if (ui
.yesOrNo("Parse Module Information Complete . See details ?")) {
96 ui
.println("\nModule Information : ");
97 ui
.println("Entrypoint : " + entrypoint
);
98 show(protocol
, "Protocol : ");
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 : ");
109 new MsaWriter(modulepath
, this, db
).flush();
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;
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
);
122 private void show(Set
<String
> hash
, String show
) {
123 ui
.println(show
+ hash
.size());
127 // add '//' to all non-local include lines
128 private void CommentOutNonLocalHFile() throws IOException
{
134 Pattern ptninclude
= Pattern
.compile("[\"<](.*[.]h)[\">]");
137 File tempdir
= new File(modulepath
+ File
.separator
+ "temp" + File
.separator
);
138 if (!tempdir
.exists()) tempdir
.mkdir();
140 Iterator
<String
> ii
= localmodulesources
.iterator();
141 while ( ii
.hasNext() ) {
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))) {
150 line
= migrationcomment
+ line
;
153 outfile
.append(line
+ '\n');
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) {
168 private void parsePreProcessedSourceCode() throws Exception
{
169 //Cl cl = new Cl(modulepath);
170 //cl.execute("Fat.c");
171 //cl.generateAll(preprocessedccodes);
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
]);
183 Iterator
<String
> ii
= preprocessedccodes
.iterator();
184 BufferedReader rd
= null;
188 //StringBuffer result = new StringBuffer();
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
);
195 Matcher matenclosereplace
;
197 Matcher matentrypoint
;
200 while (ii
.hasNext()) {
201 StringBuffer wholefile
= new StringBuffer();
203 rd
= new BufferedReader(new FileReader(modulepath
+ File
.separator
+ "temp" + File
.separator
+ ifile
));
204 while ((line
= rd
.readLine()) != null) {
205 wholefile
.append(line
+ '\n');
207 line
= wholefile
.toString();
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");
216 hashrequiredr9libs
.add("UefiDriverEntryPoint");
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
227 //matguid.appendTail(result);
228 //line = result.toString();
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));
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));
245 //matfuncc.appendTail(result);
246 //line = result.toString();
249 matmacro
= Macro
.ptntmacro
.matcher(line
);
250 while (matmacro
.find()) {
251 if ((temp
= Macro
.register(matmacro
, this, db
)) != null) {
255 // find function definition
256 // replace all {} to @
257 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
258 line
= matenclosereplace
.replaceAll("@");
261 matfuncd
= Func
.ptnfuncd
.matcher(line
);
262 while (matfuncd
.find()) {
263 if ((temp
= Func
.register(matfuncd
, this, db
)) != null) {
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
277 public static void main(String
[] args
) throws Exception
{