]>
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 | |
71f30e51 | 15 | import java.io.BufferedReader;\r |
16 | import java.io.File;\r | |
17 | import java.io.FileReader;\r | |
18 | import java.util.HashMap;\r | |
19 | import java.util.HashSet;\r | |
20 | import java.util.Iterator;\r | |
21 | import java.util.Map;\r | |
22 | import java.util.Set;\r | |
23 | import java.util.regex.Matcher;\r | |
24 | import java.util.regex.Pattern;\r | |
0dc8c589 | 25 | \r |
e83a1d9a | 26 | import org.apache.xmlbeans.XmlObject;\r |
e83a1d9a | 27 | import org.tianocore.DbPathAndFilename;\r |
28 | import org.tianocore.FrameworkDatabaseDocument;\r | |
71f30e51 | 29 | import org.tianocore.PackageSurfaceAreaDocument;\r |
e83a1d9a | 30 | import org.tianocore.FrameworkDatabaseDocument.FrameworkDatabase;\r |
31 | import org.tianocore.GuidDeclarationsDocument.GuidDeclarations;\r | |
71f30e51 | 32 | import org.tianocore.LibraryClassDeclarationsDocument.LibraryClassDeclarations;\r |
33 | import org.tianocore.LibraryClassDeclarationsDocument.LibraryClassDeclarations.LibraryClass;\r | |
e83a1d9a | 34 | import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;\r |
99f757db | 35 | import org.tianocore.PpiDeclarationsDocument.PpiDeclarations;\r |
36 | import org.tianocore.ProtocolDeclarationsDocument.ProtocolDeclarations;\r | |
e83a1d9a | 37 | \r |
7bcb8d17 | 38 | public final class Database {\r |
71f30e51 | 39 | private static final Database INSTANCE = Database.init();;\r |
40 | \r | |
41 | Database(String path) {\r | |
42 | DatabasePath = path;\r | |
43 | \r | |
44 | try {\r | |
45 | // collectWorkSpaceDatabase();\r | |
46 | importPkgGuid("PkgGuid.csv");\r | |
47 | importDBLib("Library.csv");\r | |
48 | importDBGuid("Guid.csv", "Guid");\r | |
49 | importDBGuid("Ppi.csv", "Ppi");\r | |
50 | importDBGuid("Protocol.csv", "Protocol");\r | |
51 | importDBMacro("Macro.csv");\r | |
52 | importListR8Only();\r | |
53 | } catch (Exception e) {\r | |
54 | System.out.println(e.getMessage());\r | |
55 | }\r | |
56 | }\r | |
57 | \r | |
58 | public String DatabasePath;\r | |
59 | \r | |
60 | public Set<String> error = new HashSet<String>();\r | |
61 | \r | |
62 | public Set<String> r8only = new HashSet<String>();\r | |
63 | \r | |
64 | private Map<String, Guid> hashguid = new HashMap<String, Guid>();\r | |
65 | \r | |
66 | private Map<String, Func> hashfunc = new HashMap<String, Func>();\r | |
67 | \r | |
68 | private Map<String, Macro> hashmacro = new HashMap<String, Macro>();\r | |
69 | \r | |
70 | private Map<String, String> hashPkgGuid = new HashMap<String, String>();\r | |
71 | \r | |
72 | // -------------------------------------import------------------------------------------//\r | |
73 | private void importPkgGuid(String filename) throws Exception {\r | |
74 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath\r | |
75 | + File.separator + filename));\r | |
76 | String line;\r | |
77 | String[] linecontext;\r | |
78 | \r | |
79 | if (rd.ready()) {\r | |
80 | System.out.println("Found " + filename\r | |
81 | + ", Importing Package Guid Database.");\r | |
82 | //\r | |
83 | // Skip the title row.\r | |
84 | // \r | |
85 | line = rd.readLine();\r | |
86 | while ((line = rd.readLine()) != null) {\r | |
87 | if (line.length() != 0) {\r | |
88 | linecontext = line.split(",");\r | |
89 | hashPkgGuid.put(linecontext[0], linecontext[1]);\r | |
90 | }\r | |
91 | }\r | |
92 | }\r | |
93 | }\r | |
94 | \r | |
95 | public Iterator<String> dumpAllPkgGuid() {\r | |
96 | return hashPkgGuid.values().iterator();\r | |
97 | }\r | |
98 | \r | |
99 | private void importDBLib(String filename) throws Exception {\r | |
100 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath\r | |
101 | + File.separator + filename));\r | |
102 | String line;\r | |
103 | String[] linecontext;\r | |
104 | Func lf;\r | |
105 | \r | |
106 | if (rd.ready()) {\r | |
107 | System.out.println("Found " + filename\r | |
108 | + ", Importing Library Database.");\r | |
109 | while ((line = rd.readLine()) != null) {\r | |
110 | if (line.length() != 0) {\r | |
111 | linecontext = line.split(",");\r | |
112 | lf = new Func(linecontext);\r | |
113 | hashfunc.put(lf.r8funcname, lf);\r | |
114 | }\r | |
115 | }\r | |
116 | }\r | |
117 | }\r | |
118 | \r | |
119 | private void importDBGuid(String filename, String type) throws Exception {\r | |
120 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath\r | |
121 | + File.separator + filename));\r | |
122 | String line;\r | |
123 | String[] linecontext;\r | |
124 | Guid gu;\r | |
125 | \r | |
126 | if (rd.ready()) {\r | |
127 | System.out.println("Found " + filename + ", Importing " + type\r | |
128 | + " Database.");\r | |
129 | while ((line = rd.readLine()) != null) {\r | |
130 | if (line.length() != 0) {\r | |
131 | linecontext = line.split(",");\r | |
132 | gu = new Guid(linecontext, type);\r | |
133 | hashguid.put(gu.r8name, gu);\r | |
134 | }\r | |
135 | }\r | |
136 | }\r | |
137 | }\r | |
138 | \r | |
139 | private void importDBMacro(String filename) throws Exception {\r | |
140 | BufferedReader rd = new BufferedReader(new FileReader(DatabasePath\r | |
141 | + File.separator + filename));\r | |
142 | String line;\r | |
143 | String[] linecontext;\r | |
144 | Macro mc;\r | |
145 | \r | |
146 | if (rd.ready()) {\r | |
147 | System.out.println("Found " + filename\r | |
148 | + ", Importing Macro Database.");\r | |
149 | while ((line = rd.readLine()) != null) {\r | |
150 | if (line.length() != 0) {\r | |
151 | linecontext = line.split(",");\r | |
152 | mc = new Macro(linecontext);\r | |
153 | hashmacro.put(mc.r8name, mc);\r | |
154 | }\r | |
155 | }\r | |
156 | }\r | |
157 | }\r | |
158 | \r | |
159 | private void importListR8Only() throws Exception {\r | |
160 | Pattern ptnr8only = Pattern.compile(\r | |
161 | "////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern.DOTALL);\r | |
162 | String wholeline = Common.file2string(DatabasePath + File.separator\r | |
163 | + "R8Lib.c");\r | |
164 | System.out\r | |
165 | .println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");\r | |
166 | Matcher mtrr8only = ptnr8only.matcher(wholeline);\r | |
167 | while (mtrr8only.find()) {\r | |
168 | r8only.add(mtrr8only.group(2));\r | |
169 | }\r | |
170 | }\r | |
171 | \r | |
172 | // -------------------------------------import------------------------------------------//\r | |
173 | \r | |
174 | // -------------------------------------get------------------------------------------//\r | |
175 | \r | |
176 | public String getR9Lib(String r8funcname) {\r | |
177 | String temp = null;\r | |
178 | if (hashfunc.containsKey(r8funcname)) {\r | |
179 | temp = hashfunc.get(r8funcname).r9libname;\r | |
180 | }\r | |
181 | return temp;\r | |
182 | }\r | |
183 | \r | |
184 | public String getR9Func(String r8funcname) {\r | |
185 | String temp = null;\r | |
186 | if (hashfunc.containsKey(r8funcname)) {\r | |
187 | temp = hashfunc.get(r8funcname).r9funcname;\r | |
188 | }\r | |
189 | return temp;\r | |
190 | }\r | |
191 | \r | |
192 | public String getR9Macro(String r8macro) {\r | |
193 | return hashmacro.get(r8macro).r9name; // the verification job of if\r | |
194 | // the macro exists in the\r | |
195 | // database is done when\r | |
196 | // registering it\r | |
197 | }\r | |
198 | \r | |
199 | public String getR9Guidname(String r8Guid) {\r | |
200 | String temp = null;\r | |
201 | try {\r | |
202 | temp = hashguid.get(r8Guid).r9name;\r | |
203 | } catch (NullPointerException e) {\r | |
204 | error.add("getR9Guidname :" + r8Guid);\r | |
205 | }\r | |
206 | return temp;\r | |
207 | }\r | |
208 | \r | |
209 | public String getGuidType(String r8Guid) {\r | |
210 | String temp = null;\r | |
211 | try {\r | |
212 | temp = hashguid.get(r8Guid).type;\r | |
213 | } catch (NullPointerException e) {\r | |
214 | error.add("getR9Guidname :" + r8Guid);\r | |
215 | }\r | |
216 | return temp;\r | |
217 | }\r | |
218 | \r | |
219 | // -------------------------------------get------------------------------------------//\r | |
220 | \r | |
221 | // -------------------------------------has------------------------------------------//\r | |
222 | \r | |
223 | public boolean hasFunc(String r8lib) {\r | |
224 | return hashfunc.containsKey(r8lib);\r | |
225 | }\r | |
226 | \r | |
227 | public boolean hasGuid(String r8guid) {\r | |
228 | return hashguid.containsKey(r8guid);\r | |
229 | }\r | |
230 | \r | |
231 | public boolean hasMacro(String r8macro) {\r | |
232 | return hashmacro.containsKey(r8macro);\r | |
233 | }\r | |
234 | \r | |
235 | // -------------------------------------has------------------------------------------//\r | |
236 | \r | |
237 | // -------------------------------------init------------------------------------------//\r | |
238 | \r | |
239 | private static final Database init() {\r | |
240 | if (System.getenv("WORKSPACE") == null) {\r | |
241 | return new Database("C:" + File.separator + "tianocore"\r | |
242 | + File.separator + "edk2" + File.separator + "Tools"\r | |
243 | + File.separator + "Conf" + File.separator + "Migration");\r | |
244 | } else {\r | |
245 | return new Database(System.getenv("WORKSPACE") + File.separator\r | |
246 | + "Tools" + File.separator + "Conf" + File.separator\r | |
247 | + "Migration");\r | |
248 | }\r | |
249 | }\r | |
250 | \r | |
251 | public static final Database getInstance() {\r | |
252 | return INSTANCE;\r | |
253 | }\r | |
254 | \r | |
255 | private String workspacePath;\r | |
256 | \r | |
257 | private HashMap<String, String> hashDbGuids = new HashMap<String, String>();\r | |
258 | \r | |
259 | private HashMap<String, String> hashDbPpis = new HashMap<String, String>();\r | |
260 | \r | |
261 | private HashMap<String, String> hashDbProtocols = new HashMap<String, String>();\r | |
262 | \r | |
263 | private HashMap<String, String> hashDbLibSymbols = new HashMap<String, String>();\r | |
264 | \r | |
265 | private HashMap<String, String> hashDbLibFunctions = new HashMap<String, String>();\r | |
266 | \r | |
267 | private HashMap<String, String> hashDbLibExterns = new HashMap<String, String>();\r | |
268 | \r | |
269 | private final String regLibClassName = ".*\\W(\\w[\\w\\d]*)\\.h";\r | |
270 | \r | |
271 | private final Pattern ptnLibClassName = Pattern.compile(regLibClassName);\r | |
272 | \r | |
273 | private final String regLibSymbol = "#define\\s+(\\w[\\w\\d]*)";\r | |
274 | \r | |
275 | private final Pattern ptnLibSymbol = Pattern.compile(regLibSymbol);\r | |
276 | \r | |
277 | private final String regLibDataType = "[A-Z][A-Z0-9_]*\\s*\\**";\r | |
278 | \r | |
279 | private final String regLibFunction = regLibDataType\r | |
280 | + "\\s*(?:EFIAPI)?\\s+" + "(\\w[\\w\\d]*)\\s*\\([^)]*\\)\\s*;";\r | |
281 | \r | |
282 | private Pattern ptnLibFunction = Pattern.compile(regLibFunction);\r | |
283 | \r | |
284 | private final String regLibExtern = "extern\\s+" + regLibDataType\r | |
285 | + "\\s*(\\w[\\w\\d]*)";\r | |
286 | \r | |
287 | private final Pattern ptnLibExtern = Pattern.compile(regLibExtern);\r | |
288 | \r | |
289 | private final String convertToOsFilePath(String filePath) {\r | |
290 | return filePath.replace("/", File.separator).replace("\\",\r | |
291 | File.separator);\r | |
292 | }\r | |
293 | \r | |
294 | private final void collectLibHeaderFileInfo(String libHeaderFile,\r | |
295 | String pkgGuid) throws Exception {\r | |
296 | String fileContents;\r | |
297 | String libClassName;\r | |
298 | String libContainer;\r | |
299 | Matcher mtrLibClass;\r | |
300 | Matcher mtrLibSymbol;\r | |
301 | Matcher mtrLibFunction;\r | |
302 | Matcher mtrLibExtern;\r | |
303 | \r | |
304 | System.out.println("Parsing: " + libHeaderFile);\r | |
305 | mtrLibClass = ptnLibClassName.matcher(libHeaderFile);\r | |
306 | if (!mtrLibClass.matches()) {\r | |
307 | throw new Exception("Illegal libary header file");\r | |
308 | }\r | |
309 | libClassName = mtrLibClass.group(1);\r | |
310 | libContainer = libClassName + "@" + pkgGuid;\r | |
311 | \r | |
312 | fileContents = Common.file2string(libHeaderFile);\r | |
313 | mtrLibSymbol = ptnLibSymbol.matcher(fileContents);\r | |
314 | while (mtrLibSymbol.find()) {\r | |
315 | String libSymbol;\r | |
316 | String oldLibContainer;\r | |
317 | \r | |
318 | libSymbol = mtrLibSymbol.group(1);\r | |
319 | oldLibContainer = hashDbLibSymbols.put(libSymbol, libContainer);\r | |
320 | if (oldLibContainer != null) {\r | |
321 | String warnMessage;\r | |
322 | \r | |
323 | warnMessage = "Duplicated Lib Symbol:" + libSymbol + " Found. "\r | |
324 | + "Later package will overide the previous one";\r | |
325 | System.out.println(warnMessage);\r | |
326 | }\r | |
327 | }\r | |
328 | \r | |
329 | mtrLibFunction = ptnLibFunction.matcher(fileContents);\r | |
330 | while (mtrLibFunction.find()) {\r | |
331 | String libFunction;\r | |
332 | String oldLibContainer;\r | |
333 | \r | |
334 | libFunction = mtrLibFunction.group(1);\r | |
335 | oldLibContainer = hashDbLibFunctions.put(libFunction, libContainer);\r | |
336 | if (oldLibContainer != null) {\r | |
337 | String warnMessage;\r | |
338 | \r | |
339 | warnMessage = "Duplicated Lib Function:" + libFunction\r | |
340 | + " Found. "\r | |
341 | + "Later package will overide the previous one";\r | |
342 | System.out.println(warnMessage);\r | |
343 | }\r | |
344 | }\r | |
345 | \r | |
346 | mtrLibExtern = ptnLibExtern.matcher(fileContents);\r | |
347 | while (mtrLibExtern.find()) {\r | |
348 | String libExtern;\r | |
349 | String oldLibContainer;\r | |
350 | \r | |
351 | libExtern = mtrLibExtern.group(1);\r | |
352 | oldLibContainer = hashDbLibExterns.put(libExtern, libContainer);\r | |
353 | if (oldLibContainer != null) {\r | |
354 | String warnMessage;\r | |
355 | \r | |
356 | warnMessage = "Duplicated Lib Extern:" + libExtern + " Found. "\r | |
357 | + "Later package will overide the previous one";\r | |
358 | System.out.println(warnMessage);\r | |
359 | }\r | |
360 | }\r | |
361 | }\r | |
362 | \r | |
363 | private final void collectLibDataBase(PackageSurfaceArea spdDatabase,\r | |
364 | String pkgDirectory) throws Exception {\r | |
365 | String pkgGuid;\r | |
366 | LibraryClassDeclarations libClassDeclarations;\r | |
367 | \r | |
368 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
369 | libClassDeclarations = spdDatabase.getLibraryClassDeclarations();\r | |
370 | if (libClassDeclarations != null) {\r | |
371 | Iterator<LibraryClass> itLibClass;\r | |
372 | \r | |
373 | itLibClass = libClassDeclarations.getLibraryClassList().iterator();\r | |
374 | while (itLibClass.hasNext()) {\r | |
375 | String libHeaderFile;\r | |
376 | \r | |
377 | libHeaderFile = pkgDirectory + File.separator\r | |
378 | + itLibClass.next().getIncludeHeader();\r | |
379 | libHeaderFile = convertToOsFilePath(libHeaderFile);\r | |
380 | try {\r | |
381 | collectLibHeaderFileInfo(libHeaderFile, pkgGuid);\r | |
382 | } catch (Exception e) {\r | |
383 | String errorMessage;\r | |
384 | \r | |
385 | errorMessage = "Error (" + e.getMessage()\r | |
386 | + ")occurs when parsing " + libHeaderFile;\r | |
387 | System.out.println(errorMessage);\r | |
388 | }\r | |
389 | }\r | |
390 | }\r | |
391 | }\r | |
392 | \r | |
393 | private final void collectGuidDatabase(PackageSurfaceArea spdDatabase)\r | |
394 | throws Exception {\r | |
395 | String pkgGuid;\r | |
396 | GuidDeclarations guidDeclarations;\r | |
397 | \r | |
398 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
399 | guidDeclarations = spdDatabase.getGuidDeclarations();\r | |
400 | if (guidDeclarations != null) {\r | |
401 | Iterator<GuidDeclarations.Entry> itGuids;\r | |
402 | \r | |
403 | itGuids = guidDeclarations.getEntryList().iterator();\r | |
404 | while (itGuids.hasNext()) {\r | |
405 | hashDbGuids.put(itGuids.next().getCName(), pkgGuid);\r | |
406 | }\r | |
407 | }\r | |
408 | \r | |
409 | }\r | |
410 | \r | |
411 | private final void collectPpiDatabase(PackageSurfaceArea spdDatabase)\r | |
412 | throws Exception {\r | |
413 | String pkgGuid;\r | |
414 | PpiDeclarations ppiDeclarations;\r | |
415 | \r | |
416 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
417 | ppiDeclarations = spdDatabase.getPpiDeclarations();\r | |
418 | \r | |
419 | if (ppiDeclarations != null) {\r | |
420 | Iterator<PpiDeclarations.Entry> itPpis;\r | |
421 | \r | |
422 | itPpis = ppiDeclarations.getEntryList().iterator();\r | |
423 | while (itPpis.hasNext()) {\r | |
424 | hashDbPpis.put(itPpis.next().getCName(), pkgGuid);\r | |
425 | }\r | |
426 | }\r | |
427 | \r | |
428 | }\r | |
429 | \r | |
430 | private final void collectProtocolDatabase(PackageSurfaceArea spdDatabase)\r | |
431 | throws Exception {\r | |
432 | String pkgGuid;\r | |
433 | ProtocolDeclarations protocolDeclarations;\r | |
434 | \r | |
435 | pkgGuid = spdDatabase.getSpdHeader().getGuidValue();\r | |
436 | protocolDeclarations = spdDatabase.getProtocolDeclarations();\r | |
437 | \r | |
438 | if (protocolDeclarations != null) {\r | |
439 | Iterator<ProtocolDeclarations.Entry> itProtocols;\r | |
440 | \r | |
441 | itProtocols = protocolDeclarations.getEntryList().iterator();\r | |
442 | while (itProtocols.hasNext()) {\r | |
443 | hashDbGuids.put(itProtocols.next().getCName(), pkgGuid);\r | |
444 | }\r | |
445 | }\r | |
446 | \r | |
447 | }\r | |
448 | \r | |
449 | private final void collectPackageDatabase(String packageFileName)\r | |
450 | throws Exception {\r | |
451 | XmlObject xmlPackage;\r | |
452 | PackageSurfaceArea spdDatabase;\r | |
453 | File pkgFile;\r | |
454 | \r | |
455 | pkgFile = new File(packageFileName);\r | |
456 | xmlPackage = XmlObject.Factory.parse(pkgFile);\r | |
457 | spdDatabase = ((PackageSurfaceAreaDocument) xmlPackage)\r | |
458 | .getPackageSurfaceArea();\r | |
459 | \r | |
460 | collectGuidDatabase(spdDatabase);\r | |
461 | collectProtocolDatabase(spdDatabase);\r | |
462 | collectPpiDatabase(spdDatabase);\r | |
463 | collectLibDataBase(spdDatabase, pkgFile.getParent());\r | |
464 | \r | |
465 | }\r | |
466 | \r | |
467 | public final void collectWorkSpaceDatabase() throws Exception {\r | |
468 | String databaseFileName;\r | |
469 | File databaseFile;\r | |
470 | XmlObject xmlDatabase;\r | |
471 | FrameworkDatabase frameworkDatabase;\r | |
472 | Iterator<DbPathAndFilename> packageFile;\r | |
473 | \r | |
474 | workspacePath = System.getenv("WORKSPACE");\r | |
475 | \r | |
476 | if (workspacePath == null) {\r | |
477 | String errorMessage = "Envivornment variable \"WORKSPACE\" is not set!";\r | |
478 | throw new Exception(errorMessage);\r | |
479 | }\r | |
480 | databaseFileName = workspacePath + File.separator + "Tools"\r | |
481 | + File.separator + "Conf" + File.separator\r | |
482 | + "FrameworkDatabase.db";\r | |
483 | System.out.println("Open " + databaseFileName);\r | |
484 | databaseFile = new File(databaseFileName);\r | |
485 | xmlDatabase = XmlObject.Factory.parse(databaseFile);\r | |
486 | frameworkDatabase = ((FrameworkDatabaseDocument) xmlDatabase)\r | |
487 | .getFrameworkDatabase();\r | |
488 | packageFile = frameworkDatabase.getPackageList().getFilenameList()\r | |
489 | .iterator();\r | |
490 | \r | |
491 | while (packageFile.hasNext()) {\r | |
492 | String packageFileName = packageFile.next().getStringValue();\r | |
493 | packageFileName = workspacePath + File.separator + packageFileName;\r | |
494 | packageFileName = convertToOsFilePath(packageFileName);\r | |
495 | \r | |
496 | System.out.println("Parsing: " + packageFileName);\r | |
497 | try {\r | |
498 | collectPackageDatabase(packageFileName);\r | |
499 | } catch (Exception e) {\r | |
500 | System.out.println("Error occured when opening "\r | |
501 | + packageFileName + e.getMessage());\r | |
502 | }\r | |
503 | }\r | |
504 | }\r | |
0dc8c589 | 505 | }\r |