Add Binary or Source type check before providing PCD item choices when editing platfo...
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / ModuleReader.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17import java.util.regex.*;\r
446e26ee 18\r
0dc8c589 19import org.tianocore.*;\r
20\r
5ea254f6 21public final class ModuleReader {\r
27e0221a 22 private static ModuleInfo mi;\r
23 \r
24 private static final Pattern ptninfequation = Pattern.compile("([^\\s]*)\\s*=\\s*([^\\s]*)");\r
25 private static final Pattern ptnsection = Pattern.compile("\\[([^\\[\\]]*)\\]([^\\[\\]]*)\\n", Pattern.MULTILINE);\r
26 private static final Pattern ptnfilename = Pattern.compile("[^\\s]+");\r
27 \r
28 public static final void ModuleScan(ModuleInfo m) throws Exception {\r
29 mi = m;\r
cfdfbaca 30\r
27e0221a 31 Common.toDoAll(mi.modulepath, ModuleInfo.class.getMethod("enroll", String.class), mi, null, Common.FILE);\r
cfdfbaca 32\r
27e0221a 33 String filename = null;\r
34 if (mi.msaorinf.isEmpty()) {\r
35 MigrationTool.ui.println("No INF nor MSA file found!");\r
36 System.exit(0);\r
37 } else {\r
38 if (mi.msaorinf.size() == 1) {\r
39 filename = (String)mi.msaorinf.toArray()[0];\r
40 } else {\r
41 filename = MigrationTool.ui.choose("Found .inf or .msa file for module\n" + mi.modulepath + "\nChoose one Please", mi.msaorinf.toArray());\r
42 }\r
43 }\r
5f4eb6b6 44\r
27e0221a 45 if (filename.contains(".inf")) {\r
46 readInf(filename);\r
47 } else if (filename.contains(".msa")) {\r
48 readMsa(filename);\r
49 }\r
cfdfbaca 50\r
27e0221a 51 CommentOutNonLocalHFile();\r
52 parsePreProcessedSourceCode();\r
446e26ee 53\r
27e0221a 54 }\r
55 \r
56 private static final void readMsa(String name) throws Exception {\r
57 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(new File(mi.modulepath + File.separator + name));\r
58 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.getModuleSurfaceArea();\r
59 MsaHeaderDocument.MsaHeader msaheader = msa.getMsaHeader();\r
7bcb8d17 60\r
27e0221a 61 mi.modulename = msaheader.getModuleName();\r
62 mi.guidvalue = msaheader.getGuidValue();\r
63 mi.moduletype = msaheader.getModuleType().toString(); // ???\r
7bcb8d17 64\r
27e0221a 65 SourceFilesDocument.SourceFiles sourcefiles = msa.getSourceFiles();\r
66 \r
67 String temp;\r
68 Iterator<FilenameDocument.Filename> li = sourcefiles.getFilenameList().iterator();\r
69 while (li.hasNext()) {\r
70 if (!mi.localmodulesources.contains(temp = li.next().toString())) {\r
71 System.out.println("Source File Missing! : " + temp);\r
72 }\r
73 }\r
74 }\r
75 \r
76 private static final void readInf(String name) throws Exception {\r
77 System.out.println("\nParsing INF file: " + name);\r
78 String wholeline;\r
79 Matcher mtrinfequation;\r
80 Matcher mtrsection;\r
81 Matcher mtrfilename;\r
0dc8c589 82\r
27e0221a 83 wholeline = Common.file2string(mi.modulepath + File.separator + name);\r
84 mtrsection = ptnsection.matcher(wholeline);\r
85 while (mtrsection.find()) {\r
86 if (mtrsection.group(1).matches("defines")) {\r
87 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
88 while (mtrinfequation.find()) {\r
89 if (mtrinfequation.group(1).matches("BASE_NAME")) {\r
90 mi.modulename = mtrinfequation.group(2);\r
91 }\r
92 if (mtrinfequation.group(1).matches("FILE_GUID")) {\r
93 mi.guidvalue = mtrinfequation.group(2);\r
94 }\r
95 if (mtrinfequation.group(1).matches("COMPONENT_TYPE")) {\r
96 mi.moduletype = mtrinfequation.group(2);\r
97 }\r
98 }\r
99 }\r
100 if (mtrsection.group(1).matches("nmake.common")) {\r
101 mtrinfequation = ptninfequation.matcher(mtrsection.group(2));\r
102 while (mtrinfequation.find()) {\r
103 if (mtrinfequation.group(1).matches("IMAGE_ENTRY_POINT")) {\r
104 mi.entrypoint = mtrinfequation.group(2);\r
105 }\r
106 if (mtrinfequation.group(1).matches("DPX_SOURCE")) {\r
107 if (!mi.localmodulesources.contains(mtrinfequation.group(2))) {\r
108 MigrationTool.ui.println("DPX File Missing! : " + mtrinfequation.group(2));\r
109 }\r
110 }\r
111 }\r
112 }\r
113 if (mtrsection.group(1).contains("sources.")) {\r
114 mtrfilename = ptnfilename.matcher(mtrsection.group(2));\r
115 while (mtrfilename.find()) {\r
116 if (!mi.localmodulesources.contains(mtrfilename.group())) {\r
117 MigrationTool.ui.println("Source File Missing! : " + mtrfilename.group());\r
118 }\r
119 }\r
120 }\r
121 }\r
122 }\r
123 \r
124 // add '//' to all non-local include lines\r
125 private static final void CommentOutNonLocalHFile() throws IOException {\r
126 BufferedReader rd;\r
127 String line;\r
128 String curFile;\r
129 PrintWriter outfile;\r
446e26ee 130\r
27e0221a 131 Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");\r
132 Matcher mtrinclude;\r
446e26ee 133\r
27e0221a 134 Iterator<String> ii = mi.localmodulesources.iterator();\r
135 while ( ii.hasNext() ) {\r
136 curFile = ii.next();\r
137 rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + curFile));\r
138 Common.ensureDir(mi.modulepath + File.separator + "temp" + File.separator + curFile);\r
139 outfile = new PrintWriter(new BufferedWriter(new FileWriter(mi.modulepath + File.separator + "temp" + File.separator + curFile)));\r
140 \r
27e0221a 141 while ((line = rd.readLine()) != null) {\r
142 if (line.contains("#include")) {\r
143 mtrinclude = ptninclude.matcher(line);\r
144 if (mtrinclude.find() && mi.localmodulesources.contains(mtrinclude.group(1))) {\r
145 } else {\r
146 line = MigrationTool.MIGRATIONCOMMENT + line;\r
147 }\r
148 }\r
149 outfile.append(line + '\n');\r
150 }\r
151 outfile.flush();\r
152 outfile.close();\r
153 \r
154 }\r
155 }\r
446e26ee 156\r
27e0221a 157 private static final void parsePreProcessedSourceCode() throws Exception {\r
158 //Cl cl = new Cl(modulepath);\r
159 //cl.execute("Fat.c");\r
160 //cl.generateAll(preprocessedccodes);\r
161 //\r
162 //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r
163 //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r
164 //String[] list = new File(modulepath + File.separator + "temp").list(); // without CL , add\r
165 BufferedReader rd = null;\r
166 String ifile = null;\r
167 String line = null;\r
168 String temp = null;\r
169 \r
170 Iterator<String> ii = mi.localmodulesources.iterator();\r
171 while (ii.hasNext()) {\r
172 temp = ii.next();\r
173 if (temp.contains(".c")) {\r
174 mi.preprocessedccodes.add(temp);\r
175 }\r
176 }\r
177 \r
178 ii = mi.preprocessedccodes.iterator();\r
179 \r
180 Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r
181 Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);\r
182 Matcher matguid;\r
183 Matcher matfuncc;\r
184 Matcher matfuncd;\r
185 Matcher matenclosereplace;\r
186 Matcher matefifuncc;\r
187 Matcher matentrypoint;\r
188 Matcher matmacro;\r
189 \r
190 while (ii.hasNext()) {\r
191 StringBuffer wholefile = new StringBuffer();\r
192 ifile = ii.next();\r
193 rd = new BufferedReader(new FileReader(mi.modulepath + File.separator + "temp" + File.separator + ifile));\r
194 while ((line = rd.readLine()) != null) {\r
195 wholefile.append(line + '\n');\r
196 }\r
197 line = wholefile.toString();\r
198 \r
199 // if this is a Pei phase module , add these library class to .msa\r
200 matentrypoint = patentrypoint.matcher(line);\r
201 if (matentrypoint.find()) {\r
202 mi.entrypoint = matentrypoint.group(2);\r
203 if (matentrypoint.group(1).matches("PEIM")) {\r
204 mi.hashrequiredr9libs.add("PeimEntryPoint");\r
205 } else {\r
206 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");\r
207 }\r
208 }\r
209 \r
210 // find guid\r
211 matguid = Guid.ptnguid.matcher(line); // several ways to implement this , which one is faster ? :\r
212 while (matguid.find()) { // 1.currently , find once , then call to identify which is it\r
213 if ((temp = Guid.register(matguid, mi, MigrationTool.db)) != null) { // 2.use 3 different matchers , search 3 times to find each\r
214 //matguid.appendReplacement(result, MigrationTool.db.getR9Guidname(temp)); // search the database for all 3 kinds of guids , high cost\r
215 }\r
216 }\r
217 //matguid.appendTail(result);\r
218 //line = result.toString();\r
446e26ee 219\r
27e0221a 220 // find EFI call in form of '->' , many 'gUnicodeCollationInterface->' like things are not changed\r
221 // This item is not simply replaced , special operation is required.\r
222 matefifuncc = patefifuncc.matcher(line);\r
223 while (matefifuncc.find()) {\r
224 mi.hashEFIcall.add(matefifuncc.group(2));\r
225 }\r
446e26ee 226\r
27e0221a 227 // find function call\r
228 matfuncc = Func.ptnfuncc.matcher(line);\r
229 while (matfuncc.find()) {\r
230 if ((temp = Func.register(matfuncc, mi, MigrationTool.db)) != null) {\r
231 //MigrationTool.ui.println(ifile + " dofunc " + temp);\r
232 //matfuncc.appendReplacement(result, MigrationTool.db.getR9Func(temp));\r
233 }\r
234 }\r
235 //matfuncc.appendTail(result);\r
236 //line = result.toString();\r
446e26ee 237\r
27e0221a 238 // find macro\r
239 matmacro = Macro.ptntmacro.matcher(line);\r
240 while (matmacro.find()) {\r
241 if ((temp = Macro.register(matmacro, mi, MigrationTool.db)) != null) {\r
242 }\r
243 }\r
244 \r
245 // find function definition\r
246 // replace all {} to @\r
247 while ((matenclosereplace = Func.ptnbrace.matcher(line)).find()) {\r
248 line = matenclosereplace.replaceAll("@");\r
249 }\r
446e26ee 250\r
27e0221a 251 matfuncd = Func.ptnfuncd.matcher(line);\r
252 while (matfuncd.find()) {\r
253 if ((temp = Func.register(matfuncd, mi, MigrationTool.db)) != null) {\r
254 }\r
255 }\r
256 }\r
257 \r
258 // op on hash\r
259 Iterator<String> funcci = mi.hashfuncc.iterator();\r
260 while (funcci.hasNext()) {\r
261 if (!mi.hashfuncd.contains(temp = funcci.next()) && !mi.hashEFIcall.contains(temp)) {\r
262 mi.hashnonlocalfunc.add(temp); // this set contains both changed and not changed items\r
263 }\r
264 }\r
265 }\r
719cebfe 266}\r