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