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
implements Common
.ForDoAll
{
22 private static final ModuleReader modulereader
= new ModuleReader();
23 private ModuleInfo mi
;
24 private final CommentLaplace commentlaplace
= new CommentLaplace();
26 private static final Pattern ptninfequation
= Pattern
.compile("([^\\s]*)\\s*=\\s*([^\\s]*)");
27 private static final Pattern ptnsection
= Pattern
.compile("\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern
.MULTILINE
);
28 private static final Pattern ptnfilename
= Pattern
.compile("[^\\s]+");
30 public final void ModuleScan() throws Exception
{
31 Common
.toDoAll(mi
.modulepath
, ModuleInfo
.class.getMethod("enroll", String
.class), mi
, null, Common
.FILE
);
34 String filename
= null;
35 if (mi
.msaorinf
.isEmpty()) {
36 MigrationTool
.ui
.println("No INF nor MSA file found!");
39 if (mi
.msaorinf
.size() == 1) {
40 filename
= (String
)mi
.msaorinf
.toArray()[0];
42 filename
= MigrationTool
.ui
.choose("Found .inf or .msa file for module\n" + mi
.modulepath
+ "\nChoose one Please", mi
.msaorinf
.toArray());
46 if (filename
.contains(".inf")) {
48 } else if (filename
.contains(".msa")) {
56 private final void readMsa(String name
) throws Exception
{
57 ModuleSurfaceAreaDocument msadoc
= ModuleSurfaceAreaDocument
.Factory
.parse(new File(mi
.modulepath
+ File
.separator
+ name
));
58 ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= msadoc
.getModuleSurfaceArea();
59 MsaHeaderDocument
.MsaHeader msaheader
= msa
.getMsaHeader();
61 mi
.modulename
= msaheader
.getModuleName();
62 mi
.guidvalue
= msaheader
.getGuidValue();
63 mi
.moduletype
= msaheader
.getModuleType().toString(); // ???
65 SourceFilesDocument
.SourceFiles sourcefiles
= msa
.getSourceFiles();
68 Iterator
<FilenameDocument
.Filename
> li
= sourcefiles
.getFilenameList().iterator();
69 while (li
.hasNext()) {
70 if (!mi
.localmodulesources
.contains(temp
= li
.next().toString())) {
71 System
.out
.println("Source File Missing! : " + temp
);
76 private final void readInf(String name
) throws Exception
{
77 System
.out
.println("\nParsing INF file: " + name
);
79 Matcher mtrinfequation
;
83 wholeline
= Common
.file2string(mi
.modulepath
+ File
.separator
+ name
);
84 mtrsection
= ptnsection
.matcher(wholeline
);
85 while (mtrsection
.find()) {
86 if (mtrsection
.group(1).matches("defines")) {
87 mtrinfequation
= ptninfequation
.matcher(mtrsection
.group(2));
88 while (mtrinfequation
.find()) {
89 if (mtrinfequation
.group(1).matches("BASE_NAME")) {
90 mi
.modulename
= mtrinfequation
.group(2);
92 if (mtrinfequation
.group(1).matches("FILE_GUID")) {
93 mi
.guidvalue
= mtrinfequation
.group(2);
95 if (mtrinfequation
.group(1).matches("COMPONENT_TYPE")) {
96 mi
.moduletype
= mtrinfequation
.group(2);
100 if (mtrsection
.group(1).contains("nmake.")) {
101 mtrinfequation
= ptninfequation
.matcher(mtrsection
.group(2));
102 while (mtrinfequation
.find()) {
103 if (mtrinfequation
.group(1).matches("IMAGE_ENTRY_POINT")) {
104 mi
.entrypoint
= mtrinfequation
.group(2);
105 mi
.hashrequiredr9libs
.add("EntryPointLib");
107 if (mtrinfequation
.group(1).matches("DPX_SOURCE")) {
108 if (!mi
.localmodulesources
.contains(mtrinfequation
.group(2))) {
109 MigrationTool
.ui
.println("DPX File Missing! : " + mtrinfequation
.group(2));
114 if (mtrsection
.group(1).contains("sources.")) {
115 mtrfilename
= ptnfilename
.matcher(mtrsection
.group(2));
116 while (mtrfilename
.find()) {
117 mi
.infsources
.add(mtrfilename
.group());
118 if (!mi
.localmodulesources
.contains(mtrfilename
.group())) {
119 MigrationTool
.ui
.println("Warn: Source File Missing! : " + mtrfilename
.group());
123 if (mtrsection
.group(1).matches("includes.")) {
124 mtrfilename
= ptnfilename
.matcher(mtrsection
.group(2));
125 while (mtrfilename
.find()) {
126 mi
.infincludes
.add(mtrfilename
.group());
132 private final void preProcessModule() throws Exception
{
133 // according to .inf file, add extraordinary includes and sourcefiles
134 Common
.dirCopy(mi
.modulepath
, mi
.modulepath
+ File
.separator
+ "temp"); // collect all Laplace.namechange to here???
136 if (!mi
.infincludes
.isEmpty()) {
137 Iterator
<String
> it
= mi
.infincludes
.iterator();
138 String tempincludename
= null;
139 while (it
.hasNext()) {
140 tempincludename
= it
.next();
141 if (tempincludename
.contains("..")) {
142 Matcher mtr
= Common
.PTNSEPARATER
.matcher(tempincludename
);
143 if (mtr
.find() && !mtr
.group(2).matches(".")) {
144 Common
.oneLevelDirCopy(mi
.modulepath
.replaceAll(Common
.STRSEPARATER
, "$1") + File
.separator
+ mtr
.group(2), mi
.modulepath
+ File
.separator
+ "temp", ".h");
146 Common
.oneLevelDirCopy(mi
.modulepath
.replaceAll(Common
.STRSEPARATER
, "$1"), mi
.modulepath
+ File
.separator
+ "temp", ".h");
151 if (!mi
.infsources
.isEmpty()) {
152 Iterator
<String
> it
= mi
.infsources
.iterator();
153 String tempsourcename
= null;
154 while (it
.hasNext()) {
155 tempsourcename
= it
.next();
156 if (tempsourcename
.contains("..")) {
157 Common
.ensureDir(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ "MT_Parent_Sources");
158 Matcher mtr
= Common
.PTNSEPARATER
.matcher(tempsourcename
);
160 Common
.fileCopy(mi
.modulepath
.replaceAll(Common
.STRSEPARATER
, "$1") + File
.separator
+ mtr
.group(2), mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ "MT_Parent_Sources" + File
.separator
+ mtr
.group(2));
166 //CommentOutNonLocalHFile();
167 Common
.toDoAll(mi
.modulepath
+ File
.separator
+ "temp", this, Common
.FILE
);
169 parsePreProcessedSourceCode();
173 private final void parsePreProcessedSourceCode() throws Exception
{
174 //Cl cl = new Cl(modulepath);
175 //cl.execute("Fat.c");
176 //cl.generateAll(preprocessedccodes);
178 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");
179 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");
180 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add
181 BufferedReader rd
= null;
186 Iterator
<String
> ii
= mi
.localmodulesources
.iterator();
187 while (ii
.hasNext()) {
189 if (temp
.contains(".c")) {
190 mi
.preprocessedccodes
.add(temp
);
194 ii
= mi
.preprocessedccodes
.iterator();
196 Pattern patefifuncc
= Pattern
.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern
.MULTILINE
);
197 Pattern patentrypoint
= Pattern
.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern
.MULTILINE
);
201 Matcher matenclosereplace
;
203 Matcher matentrypoint
;
206 while (ii
.hasNext()) {
207 StringBuffer wholefile
= new StringBuffer();
209 rd
= new BufferedReader(new FileReader(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ ifile
));
210 while ((line
= rd
.readLine()) != null) {
211 wholefile
.append(line
+ '\n');
213 line
= wholefile
.toString();
215 // if this is a Pei phase module , add these library class to .msa
216 matentrypoint
= patentrypoint
.matcher(line
);
217 if (matentrypoint
.find()) {
218 mi
.entrypoint
= matentrypoint
.group(2);
219 if (matentrypoint
.group(1).matches("PEIM")) {
220 mi
.hashrequiredr9libs
.add("PeimEntryPoint");
222 mi
.hashrequiredr9libs
.add("UefiDriverEntryPoint");
227 matguid
= Guid
.ptnguid
.matcher(line
); // several ways to implement this , which one is faster ? :
228 while (matguid
.find()) { // 1.currently , find once , then call to identify which is it
229 if ((temp
= Guid
.register(matguid
, mi
, MigrationTool
.db
)) != null) { // 2.use 3 different matchers , search 3 times to find each
230 //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost
233 //matguid.appendTail(result);
234 //line = result.toString();
236 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed
237 // This item is not simply replaced , special operation is required.
238 matefifuncc
= patefifuncc
.matcher(line
);
239 while (matefifuncc
.find()) {
240 mi
.hashEFIcall
.add(matefifuncc
.group(2));
243 // find function call
244 matfuncc
= Func
.ptnfuncc
.matcher(line
);
245 while (matfuncc
.find()) {
246 if ((temp
= Func
.register(matfuncc
, mi
, MigrationTool
.db
)) != null) {
247 //MigrationTool.ui.println(ifile + " dofunc " + temp);
248 //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));
251 //matfuncc.appendTail(result);
252 //line = result.toString();
255 matmacro
= Macro
.ptntmacro
.matcher(line
);
256 while (matmacro
.find()) {
257 if ((temp
= Macro
.register(matmacro
, mi
, MigrationTool
.db
)) != null) {
261 // find function definition
262 // replace all {} to @
263 while ((matenclosereplace
= Func
.ptnbrace
.matcher(line
)).find()) {
264 line
= matenclosereplace
.replaceAll("@");
267 matfuncd
= Func
.ptnfuncd
.matcher(line
);
268 while (matfuncd
.find()) {
269 if ((temp
= Func
.register(matfuncd
, mi
, MigrationTool
.db
)) != null) {
275 Iterator
<String
> funcci
= mi
.hashfuncc
.iterator();
276 while (funcci
.hasNext()) {
277 if (!mi
.hashfuncd
.contains(temp
= funcci
.next()) && !mi
.hashEFIcall
.contains(temp
)) {
278 mi
.hashnonlocalfunc
.add(temp
); // this set contains both changed and not changed items
283 public class CommentLaplace
extends Common
.Laplace
{
284 public String
operation(String wholeline
) {
285 StringBuffer wholebuffer
= new StringBuffer();
286 String templine
= null;
287 Pattern ptnincludefile
= Pattern
.compile("[\"<](.*[.]h)[\">]");
288 Pattern ptninclude
= Pattern
.compile("#include\\s*(.*)");
289 Matcher mtrinclude
= ptninclude
.matcher(wholeline
);
290 Matcher mtrincludefile
= null;
291 while (mtrinclude
.find()) {
292 mtrincludefile
= ptnincludefile
.matcher(mtrinclude
.group(1));
293 if (mtrincludefile
.find() && mi
.localmodulesources
.contains(mtrincludefile
.group(1))) {
294 templine
= mtrinclude
.group();
296 templine
= MigrationTool
.MIGRATIONCOMMENT
+ mtrinclude
.group();
298 mtrinclude
.appendReplacement(wholebuffer
, templine
);
300 mtrinclude
.appendTail(wholebuffer
);
301 return wholebuffer
.toString();
304 public boolean recognize(String filename
) {
305 return filename
.contains(".c") || filename
.contains(".h");
308 public String
namechange(String oldname
) {
313 //-----------------------------------ForDoAll-----------------------------------//
314 public void run(String filepath
) throws Exception
{
315 String name
= mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
+ filepath
.replace(mi
.modulepath
+ File
.separator
+ "temp" + File
.separator
, "");
316 commentlaplace
.transform(name
, name
);
319 public boolean filter(File dir
) {
322 //-----------------------------------ForDoAll-----------------------------------//
324 public final void setModuleInfo(ModuleInfo m
) {
328 public static final void aimAt(ModuleInfo mi
) throws Exception
{
329 modulereader
.setModuleInfo(mi
);
330 modulereader
.ModuleScan();