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