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
.*;
19 import org
.tianocore
.*;
21 public final class ModuleReader
{
22 private static ModuleInfo mi
;
24 private static final Pattern ptninfequation
= Pattern
.compile("([^\\s]*)\\s*=\\s*([^\\s]*)");
25 private static final Pattern ptnsection
= Pattern
.compile("\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern
.MULTILINE
);
26 private static final Pattern ptnfilename
= Pattern
.compile("[^\\s]+");
28 public static final void ModuleScan(ModuleInfo m
) throws Exception
{
31 Common
.toDoAll(mi
.modulepath
, ModuleInfo
.class.getMethod("enroll", String
.class), mi
, null, Common
.FILE
);
33 String filename
= null;
34 if (mi
.msaorinf
.isEmpty()) {
35 ModuleInfo
.ui
.println("No INF nor MSA file found!");
38 filename
= ModuleInfo
.ui
.choose("Found .inf or .msa file for module\n" + mi
.modulepath
+ "\nChoose one Please", mi
.msaorinf
.toArray());
40 if (filename
.contains(".inf")) {
42 } else if (filename
.contains(".msa")) {
46 CommentOutNonLocalHFile();
47 parsePreProcessedSourceCode();
51 private static final void readMsa(String name
) throws Exception
{
52 ModuleSurfaceAreaDocument msadoc
= ModuleSurfaceAreaDocument
.Factory
.parse(new File(mi
.modulepath
+ File
.separator
+ name
));
53 ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= msadoc
.getModuleSurfaceArea();
54 MsaHeaderDocument
.MsaHeader msaheader
= msa
.getMsaHeader();
56 mi
.modulename
= msaheader
.getModuleName();
57 mi
.guidvalue
= msaheader
.getGuidValue();
58 mi
.moduletype
= msaheader
.getModuleType().toString(); // ???
60 SourceFilesDocument
.SourceFiles sourcefiles
= msa
.getSourceFiles();
63 Iterator
<FilenameDocument
.Filename
> li
= sourcefiles
.getFilenameList().iterator();
64 while (li
.hasNext()) {
65 if (!mi
.localmodulesources
.contains(temp
= li
.next().toString())) {
66 System
.out
.println("Source File Missing! : " + temp
);
71 private static final void readInf(String name
) throws Exception
{
72 System
.out
.println("\nParsing INF file: " + name
);
74 Matcher mtrinfequation
;
78 wholeline
= Common
.file2string(mi
.modulepath
+ File
.separator
+ name
);
79 mtrsection
= ptnsection
.matcher(wholeline
);
80 while (mtrsection
.find()) {
81 if (mtrsection
.group(1).matches("defines")) {
82 mtrinfequation
= ptninfequation
.matcher(mtrsection
.group(2));
83 while (mtrinfequation
.find()) {
84 if (mtrinfequation
.group(1).matches("BASE_NAME")) {
85 mi
.modulename
= mtrinfequation
.group(2);
87 if (mtrinfequation
.group(1).matches("FILE_GUID")) {
88 mi
.guidvalue
= mtrinfequation
.group(2);
90 if (mtrinfequation
.group(1).matches("COMPONENT_TYPE")) {
91 mi
.moduletype
= mtrinfequation
.group(2);
95 if (mtrsection
.group(1).matches("nmake.common")) {
96 mtrinfequation
= ptninfequation
.matcher(mtrsection
.group(2));
97 while (mtrinfequation
.find()) {
98 if (mtrinfequation
.group(1).matches("IMAGE_ENTRY_POINT")) {
99 mi
.entrypoint
= mtrinfequation
.group(2);
101 if (mtrinfequation
.group(1).matches("DPX_SOURCE")) {
102 if (!mi
.localmodulesources
.contains(mtrinfequation
.group(2))) {
103 ModuleInfo
.ui
.println("DPX File Missing! : " + mtrinfequation
.group(2));
108 if (mtrsection
.group(1).contains("sources.")) {
109 mtrfilename
= ptnfilename
.matcher(mtrsection
.group(2));
110 while (mtrfilename
.find()) {
111 if (!mi
.localmodulesources
.contains(mtrfilename
.group())) {
112 ModuleInfo
.ui
.println("Source File Missing! : " + mtrfilename
.group());
119 // add '//' to all non-local include lines
120 private static final void CommentOutNonLocalHFile() throws IOException
{
126 Pattern ptninclude
= Pattern
.compile("[\"<](.*[.]h)[\">]");
129 Iterator
<String
> ii
= mi
.localmodulesources
.iterator();
130 while ( ii
.hasNext() ) {
132 rd
= new BufferedReader(new FileReader(mi
.modulepath
+ File
.separator
+ curFile
));
133 Common
.ensureDir(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ curFile
);
134 outfile
= new PrintWriter(new BufferedWriter(new FileWriter(mi
.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() && mi
.localmodulesources
.contains(mtrinclude
.group(1))) {
140 line
= ModuleInfo
.migrationcomment
+ line
;
143 outfile
.append(line
+ '\n');
150 private static final 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
= mi
.localmodulesources
.iterator();
164 while (ii
.hasNext()) {
166 if (temp
.contains(".c")) {
167 mi
.preprocessedccodes
.add(temp
);
171 ii
= mi
.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(mi
.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 mi
.entrypoint
= matentrypoint
.group(2);
196 if (matentrypoint
.group(1).matches("PEIM")) {
197 mi
.hashrequiredr9libs
.add("PeimEntryPoint");
199 mi
.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
, mi
, ModuleInfo
.db
)) != null) { // 2.use 3 different matchers , search 3 times to find each
207 //matguid.appendReplacement(result, ModuleInfo.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 mi
.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
, mi
, ModuleInfo
.db
)) != null) {
224 //ModuleInfo.ui.println(ifile + " dofunc " + temp);
225 //matfuncc.appendReplacement(result, ModuleInfo.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
, mi
, ModuleInfo
.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
, mi
, ModuleInfo
.db
)) != null) {
252 Iterator
<String
> funcci
= mi
.hashfuncc
.iterator();
253 while (funcci
.hasNext()) {
254 if (!mi
.hashfuncd
.contains(temp
= funcci
.next()) && !mi
.hashEFIcall
.contains(temp
)) {
255 mi
.hashnonlocalfunc
.add(temp
); // this set contains both changed and not changed items