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