3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 package org
.tianocore
.migration
;
15 import java
.io
.BufferedReader
;
17 import java
.io
.FileReader
;
18 import java
.util
.HashMap
;
19 import java
.util
.HashSet
;
20 import java
.util
.Iterator
;
23 import java
.util
.regex
.Matcher
;
24 import java
.util
.regex
.Pattern
;
26 import org
.apache
.xmlbeans
.XmlObject
;
27 import org
.tianocore
.DbPathAndFilename
;
28 import org
.tianocore
.FrameworkDatabaseDocument
;
29 import org
.tianocore
.PackageSurfaceAreaDocument
;
30 import org
.tianocore
.FrameworkDatabaseDocument
.FrameworkDatabase
;
31 import org
.tianocore
.GuidDeclarationsDocument
.GuidDeclarations
;
32 import org
.tianocore
.LibraryClassDeclarationsDocument
.LibraryClassDeclarations
;
33 import org
.tianocore
.LibraryClassDeclarationsDocument
.LibraryClassDeclarations
.LibraryClass
;
34 import org
.tianocore
.PackageSurfaceAreaDocument
.PackageSurfaceArea
;
35 import org
.tianocore
.PpiDeclarationsDocument
.PpiDeclarations
;
36 import org
.tianocore
.ProtocolDeclarationsDocument
.ProtocolDeclarations
;
38 public final class Database
{
39 private static final Database INSTANCE
= Database
.init();;
41 Database(String path
) {
45 // collectWorkSpaceDatabase();
46 importPkgGuid("PkgGuid.csv");
47 importDBLib("Library.csv");
48 importDBGuid("Guid.csv", "Guid");
49 importDBGuid("Ppi.csv", "Ppi");
50 importDBGuid("Protocol.csv", "Protocol");
51 importDBMacro("Macro.csv");
53 } catch (Exception e
) {
54 System
.out
.println(e
.getMessage());
58 public String DatabasePath
;
60 public Set
<String
> error
= new HashSet
<String
>();
62 public Set
<String
> r8only
= new HashSet
<String
>();
64 private Map
<String
, Guid
> hashguid
= new HashMap
<String
, Guid
>();
66 private Map
<String
, Func
> hashfunc
= new HashMap
<String
, Func
>();
68 private Map
<String
, Macro
> hashmacro
= new HashMap
<String
, Macro
>();
70 private Map
<String
, String
> hashPkgGuid
= new HashMap
<String
, String
>();
72 // -------------------------------------import------------------------------------------//
73 private void importPkgGuid(String filename
) throws Exception
{
74 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
75 + File
.separator
+ filename
));
80 System
.out
.println("Found " + filename
81 + ", Importing Package Guid Database.");
83 // Skip the title row.
86 while ((line
= rd
.readLine()) != null) {
87 if (line
.length() != 0) {
88 linecontext
= line
.split(",");
89 hashPkgGuid
.put(linecontext
[0], linecontext
[1]);
95 public Iterator
<String
> dumpAllPkgGuid() {
96 return hashPkgGuid
.values().iterator();
99 private void importDBLib(String filename
) throws Exception
{
100 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
101 + File
.separator
+ filename
));
103 String
[] linecontext
;
107 System
.out
.println("Found " + filename
108 + ", Importing Library Database.");
109 while ((line
= rd
.readLine()) != null) {
110 if (line
.length() != 0) {
111 linecontext
= line
.split(",");
112 lf
= new Func(linecontext
);
113 hashfunc
.put(lf
.r8funcname
, lf
);
119 private void importDBGuid(String filename
, String type
) throws Exception
{
120 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
121 + File
.separator
+ filename
));
123 String
[] linecontext
;
127 System
.out
.println("Found " + filename
+ ", Importing " + type
129 while ((line
= rd
.readLine()) != null) {
130 if (line
.length() != 0) {
131 linecontext
= line
.split(",");
132 gu
= new Guid(linecontext
, type
);
133 hashguid
.put(gu
.r8name
, gu
);
139 private void importDBMacro(String filename
) throws Exception
{
140 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
141 + File
.separator
+ filename
));
143 String
[] linecontext
;
147 System
.out
.println("Found " + filename
148 + ", Importing Macro Database.");
149 while ((line
= rd
.readLine()) != null) {
150 if (line
.length() != 0) {
151 linecontext
= line
.split(",");
152 mc
= new Macro(linecontext
);
153 hashmacro
.put(mc
.r8name
, mc
);
159 private void importListR8Only() throws Exception
{
160 Pattern ptnr8only
= Pattern
.compile(
161 "////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern
.DOTALL
);
162 String wholeline
= Common
.file2string(DatabasePath
+ File
.separator
165 .println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");
166 Matcher mtrr8only
= ptnr8only
.matcher(wholeline
);
167 while (mtrr8only
.find()) {
168 r8only
.add(mtrr8only
.group(2));
172 // -------------------------------------import------------------------------------------//
174 // -------------------------------------get------------------------------------------//
176 public String
getR9Lib(String r8funcname
) {
178 if (hashfunc
.containsKey(r8funcname
)) {
179 temp
= hashfunc
.get(r8funcname
).r9libname
;
184 public String
getR9Func(String r8funcname
) {
186 if (hashfunc
.containsKey(r8funcname
)) {
187 temp
= hashfunc
.get(r8funcname
).r9funcname
;
192 public String
getR9Macro(String r8macro
) {
193 return hashmacro
.get(r8macro
).r9name
; // the verification job of if
194 // the macro exists in the
195 // database is done when
199 public String
getR9Guidname(String r8Guid
) {
202 temp
= hashguid
.get(r8Guid
).r9name
;
203 } catch (NullPointerException e
) {
204 error
.add("getR9Guidname :" + r8Guid
);
209 public String
getGuidType(String r8Guid
) {
212 temp
= hashguid
.get(r8Guid
).type
;
213 } catch (NullPointerException e
) {
214 error
.add("getR9Guidname :" + r8Guid
);
219 // -------------------------------------get------------------------------------------//
221 // -------------------------------------has------------------------------------------//
223 public boolean hasFunc(String r8lib
) {
224 return hashfunc
.containsKey(r8lib
);
227 public boolean hasGuid(String r8guid
) {
228 return hashguid
.containsKey(r8guid
);
231 public boolean hasMacro(String r8macro
) {
232 return hashmacro
.containsKey(r8macro
);
235 // -------------------------------------has------------------------------------------//
237 // -------------------------------------init------------------------------------------//
239 private static final Database
init() {
240 if (System
.getenv("WORKSPACE") == null) {
241 return new Database("C:" + File
.separator
+ "tianocore"
242 + File
.separator
+ "edk2" + File
.separator
+ "Tools"
243 + File
.separator
+ "Conf" + File
.separator
+ "Migration");
245 return new Database(System
.getenv("WORKSPACE") + File
.separator
246 + "Tools" + File
.separator
+ "Conf" + File
.separator
251 public static final Database
getInstance() {
255 private String workspacePath
;
257 private HashMap
<String
, String
> hashDbGuids
= new HashMap
<String
, String
>();
259 private HashMap
<String
, String
> hashDbPpis
= new HashMap
<String
, String
>();
261 private HashMap
<String
, String
> hashDbProtocols
= new HashMap
<String
, String
>();
263 private HashMap
<String
, String
> hashDbLibSymbols
= new HashMap
<String
, String
>();
265 private HashMap
<String
, String
> hashDbLibFunctions
= new HashMap
<String
, String
>();
267 private HashMap
<String
, String
> hashDbLibExterns
= new HashMap
<String
, String
>();
269 private final String regLibClassName
= ".*\\W(\\w[\\w\\d]*)\\.h";
271 private final Pattern ptnLibClassName
= Pattern
.compile(regLibClassName
);
273 private final String regLibSymbol
= "#define\\s+(\\w[\\w\\d]*)";
275 private final Pattern ptnLibSymbol
= Pattern
.compile(regLibSymbol
);
277 private final String regLibDataType
= "[A-Z][A-Z0-9_]*\\s*\\**";
279 private final String regLibFunction
= regLibDataType
280 + "\\s*(?:EFIAPI)?\\s+" + "(\\w[\\w\\d]*)\\s*\\([^)]*\\)\\s*;";
282 private Pattern ptnLibFunction
= Pattern
.compile(regLibFunction
);
284 private final String regLibExtern
= "extern\\s+" + regLibDataType
285 + "\\s*(\\w[\\w\\d]*)";
287 private final Pattern ptnLibExtern
= Pattern
.compile(regLibExtern
);
289 private final String
convertToOsFilePath(String filePath
) {
290 return filePath
.replace("/", File
.separator
).replace("\\",
294 private final void collectLibHeaderFileInfo(String libHeaderFile
,
295 String pkgGuid
) throws Exception
{
300 Matcher mtrLibSymbol
;
301 Matcher mtrLibFunction
;
302 Matcher mtrLibExtern
;
304 System
.out
.println("Parsing: " + libHeaderFile
);
305 mtrLibClass
= ptnLibClassName
.matcher(libHeaderFile
);
306 if (!mtrLibClass
.matches()) {
307 throw new Exception("Illegal libary header file");
309 libClassName
= mtrLibClass
.group(1);
310 libContainer
= libClassName
+ "@" + pkgGuid
;
312 fileContents
= Common
.file2string(libHeaderFile
);
313 mtrLibSymbol
= ptnLibSymbol
.matcher(fileContents
);
314 while (mtrLibSymbol
.find()) {
316 String oldLibContainer
;
318 libSymbol
= mtrLibSymbol
.group(1);
319 oldLibContainer
= hashDbLibSymbols
.put(libSymbol
, libContainer
);
320 if (oldLibContainer
!= null) {
323 warnMessage
= "Duplicated Lib Symbol:" + libSymbol
+ " Found. "
324 + "Later package will overide the previous one";
325 System
.out
.println(warnMessage
);
329 mtrLibFunction
= ptnLibFunction
.matcher(fileContents
);
330 while (mtrLibFunction
.find()) {
332 String oldLibContainer
;
334 libFunction
= mtrLibFunction
.group(1);
335 oldLibContainer
= hashDbLibFunctions
.put(libFunction
, libContainer
);
336 if (oldLibContainer
!= null) {
339 warnMessage
= "Duplicated Lib Function:" + libFunction
341 + "Later package will overide the previous one";
342 System
.out
.println(warnMessage
);
346 mtrLibExtern
= ptnLibExtern
.matcher(fileContents
);
347 while (mtrLibExtern
.find()) {
349 String oldLibContainer
;
351 libExtern
= mtrLibExtern
.group(1);
352 oldLibContainer
= hashDbLibExterns
.put(libExtern
, libContainer
);
353 if (oldLibContainer
!= null) {
356 warnMessage
= "Duplicated Lib Extern:" + libExtern
+ " Found. "
357 + "Later package will overide the previous one";
358 System
.out
.println(warnMessage
);
363 private final void collectLibDataBase(PackageSurfaceArea spdDatabase
,
364 String pkgDirectory
) throws Exception
{
366 LibraryClassDeclarations libClassDeclarations
;
368 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
369 libClassDeclarations
= spdDatabase
.getLibraryClassDeclarations();
370 if (libClassDeclarations
!= null) {
371 Iterator
<LibraryClass
> itLibClass
;
373 itLibClass
= libClassDeclarations
.getLibraryClassList().iterator();
374 while (itLibClass
.hasNext()) {
375 String libHeaderFile
;
377 libHeaderFile
= pkgDirectory
+ File
.separator
378 + itLibClass
.next().getIncludeHeader();
379 libHeaderFile
= convertToOsFilePath(libHeaderFile
);
381 collectLibHeaderFileInfo(libHeaderFile
, pkgGuid
);
382 } catch (Exception e
) {
385 errorMessage
= "Error (" + e
.getMessage()
386 + ")occurs when parsing " + libHeaderFile
;
387 System
.out
.println(errorMessage
);
393 private final void collectGuidDatabase(PackageSurfaceArea spdDatabase
)
396 GuidDeclarations guidDeclarations
;
398 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
399 guidDeclarations
= spdDatabase
.getGuidDeclarations();
400 if (guidDeclarations
!= null) {
401 Iterator
<GuidDeclarations
.Entry
> itGuids
;
403 itGuids
= guidDeclarations
.getEntryList().iterator();
404 while (itGuids
.hasNext()) {
405 hashDbGuids
.put(itGuids
.next().getCName(), pkgGuid
);
411 private final void collectPpiDatabase(PackageSurfaceArea spdDatabase
)
414 PpiDeclarations ppiDeclarations
;
416 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
417 ppiDeclarations
= spdDatabase
.getPpiDeclarations();
419 if (ppiDeclarations
!= null) {
420 Iterator
<PpiDeclarations
.Entry
> itPpis
;
422 itPpis
= ppiDeclarations
.getEntryList().iterator();
423 while (itPpis
.hasNext()) {
424 hashDbPpis
.put(itPpis
.next().getCName(), pkgGuid
);
430 private final void collectProtocolDatabase(PackageSurfaceArea spdDatabase
)
433 ProtocolDeclarations protocolDeclarations
;
435 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
436 protocolDeclarations
= spdDatabase
.getProtocolDeclarations();
438 if (protocolDeclarations
!= null) {
439 Iterator
<ProtocolDeclarations
.Entry
> itProtocols
;
441 itProtocols
= protocolDeclarations
.getEntryList().iterator();
442 while (itProtocols
.hasNext()) {
443 hashDbGuids
.put(itProtocols
.next().getCName(), pkgGuid
);
449 private final void collectPackageDatabase(String packageFileName
)
451 XmlObject xmlPackage
;
452 PackageSurfaceArea spdDatabase
;
455 pkgFile
= new File(packageFileName
);
456 xmlPackage
= XmlObject
.Factory
.parse(pkgFile
);
457 spdDatabase
= ((PackageSurfaceAreaDocument
) xmlPackage
)
458 .getPackageSurfaceArea();
460 collectGuidDatabase(spdDatabase
);
461 collectProtocolDatabase(spdDatabase
);
462 collectPpiDatabase(spdDatabase
);
463 collectLibDataBase(spdDatabase
, pkgFile
.getParent());
467 public final void collectWorkSpaceDatabase() throws Exception
{
468 String databaseFileName
;
470 XmlObject xmlDatabase
;
471 FrameworkDatabase frameworkDatabase
;
472 Iterator
<DbPathAndFilename
> packageFile
;
474 workspacePath
= System
.getenv("WORKSPACE");
476 if (workspacePath
== null) {
477 String errorMessage
= "Envivornment variable \"WORKSPACE\" is not set!";
478 throw new Exception(errorMessage
);
480 databaseFileName
= workspacePath
+ File
.separator
+ "Tools"
481 + File
.separator
+ "Conf" + File
.separator
482 + "FrameworkDatabase.db";
483 System
.out
.println("Open " + databaseFileName
);
484 databaseFile
= new File(databaseFileName
);
485 xmlDatabase
= XmlObject
.Factory
.parse(databaseFile
);
486 frameworkDatabase
= ((FrameworkDatabaseDocument
) xmlDatabase
)
487 .getFrameworkDatabase();
488 packageFile
= frameworkDatabase
.getPackageList().getFilenameList()
491 while (packageFile
.hasNext()) {
492 String packageFileName
= packageFile
.next().getStringValue();
493 packageFileName
= workspacePath
+ File
.separator
+ packageFileName
;
494 packageFileName
= convertToOsFilePath(packageFileName
);
496 System
.out
.println("Parsing: " + packageFileName
);
498 collectPackageDatabase(packageFileName
);
499 } catch (Exception e
) {
500 System
.out
.println("Error occured when opening "
501 + packageFileName
+ e
.getMessage());