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
;
17 import java
.util
.regex
.*;
19 import org
.apache
.xmlbeans
.XmlObject
;
20 import org
.apache
.xmlbeans
.XmlObject
.Factory
;
21 import org
.tianocore
.DbPathAndFilename
;
22 import org
.tianocore
.FrameworkDatabaseDocument
;
23 import org
.tianocore
.FrameworkDatabaseDocument
.FrameworkDatabase
;
24 import org
.tianocore
.GuidDeclarationsDocument
.GuidDeclarations
;
25 import org
.tianocore
.PackageListDocument
.PackageList
;
26 import org
.tianocore
.PackageSurfaceAreaDocument
;
27 import org
.tianocore
.PackageSurfaceAreaDocument
.PackageSurfaceArea
;
28 import org
.tianocore
.PpiDeclarationsDocument
.PpiDeclarations
;
29 import org
.tianocore
.ProtocolDeclarationsDocument
.ProtocolDeclarations
;
30 import org
.tianocore
.LibraryClassDeclarationsDocument
.LibraryClassDeclarations
;
31 import org
.tianocore
.LibraryClassDeclarationsDocument
.LibraryClassDeclarations
.LibraryClass
;
33 public final class Database
{
34 private static final Database INSTANCE
= null;
36 Database(String path
) {
40 //collectWorkSpaceDatabase();
41 importPkgGuid("PkgGuid.csv");
42 importDBLib("Library.csv");
43 importDBGuid("Guid.csv", "Guid");
44 importDBGuid("Ppi.csv", "Ppi");
45 importDBGuid("Protocol.csv", "Protocol");
46 importDBMacro("Macro.csv");
48 } catch (Exception e
) {
49 System
.out
.println(e
.getMessage());
53 public String DatabasePath
;
54 public Set
<String
> error
= new HashSet
<String
>();
55 public Set
<String
> r8only
= new HashSet
<String
>();
57 private Map
<String
,Guid
> hashguid
= new HashMap
<String
,Guid
>();
58 private Map
<String
,Func
> hashfunc
= new HashMap
<String
,Func
>();
59 private Map
<String
,Macro
> hashmacro
= new HashMap
<String
,Macro
>();
60 private Map
<String
,String
> hashPkgGuid
= new HashMap
<String
,String
>();
63 //-------------------------------------import------------------------------------------//
64 private void importPkgGuid(String filename
) throws Exception
{
65 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
70 System
.out
.println("Found " + filename
+ ", Importing Package Guid Database.");
72 // Skip the title row.
75 while ((line
= rd
.readLine()) != null) {
76 if (line
.length() != 0) {
77 linecontext
= line
.split(",");
78 hashPkgGuid
.put(linecontext
[0], linecontext
[1]);
83 public Iterator
<String
> dumpAllPkgGuid() {
84 return hashPkgGuid
.values().iterator();
86 private void importDBLib(String filename
) throws Exception
{
87 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
93 System
.out
.println("Found " + filename
+ ", Importing Library Database.");
94 while ((line
= rd
.readLine()) != null) {
95 if (line
.length() != 0) {
96 linecontext
= line
.split(",");
97 lf
= new Func(linecontext
);
98 hashfunc
.put(lf
.r8funcname
,lf
);
104 private void importDBGuid(String filename
, String type
) throws Exception
{
105 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
107 String
[] linecontext
;
111 System
.out
.println("Found " + filename
+ ", Importing " + type
+ " Database.");
112 while ((line
= rd
.readLine()) != null) {
113 if (line
.length() != 0) {
114 linecontext
= line
.split(",");
115 gu
= new Guid(linecontext
, type
);
116 hashguid
.put(gu
.r8name
,gu
);
122 private void importDBMacro(String filename
) throws Exception
{
123 BufferedReader rd
= new BufferedReader(new FileReader(DatabasePath
+ File
.separator
+ filename
));
125 String
[] linecontext
;
129 System
.out
.println("Found " + filename
+ ", Importing Macro Database.");
130 while ((line
= rd
.readLine()) != null) {
131 if (line
.length() != 0) {
132 linecontext
= line
.split(",");
133 mc
= new Macro(linecontext
);
134 hashmacro
.put(mc
.r8name
,mc
);
140 private void importListR8Only() throws Exception
{
141 Pattern ptnr8only
= Pattern
.compile("////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern
.DOTALL
);
142 String wholeline
= Common
.file2string(DatabasePath
+ File
.separator
+ "R8Lib.c");
143 System
.out
.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");
144 Matcher mtrr8only
= ptnr8only
.matcher(wholeline
);
145 while (mtrr8only
.find()) {
146 r8only
.add(mtrr8only
.group(2));
150 //-------------------------------------import------------------------------------------//
152 //-------------------------------------get------------------------------------------//
154 public String
getR9Lib(String r8funcname
) {
156 if (hashfunc
.containsKey(r8funcname
)) {
157 temp
= hashfunc
.get(r8funcname
).r9libname
;
162 public String
getR9Func(String r8funcname
) {
164 if (hashfunc
.containsKey(r8funcname
)) {
165 temp
= hashfunc
.get(r8funcname
).r9funcname
;
170 public String
getR9Macro(String r8macro
) {
171 return hashmacro
.get(r8macro
).r9name
; // the verification job of if the macro exists in the database is done when registering it
174 public String
getR9Guidname(String r8Guid
) {
177 temp
= hashguid
.get(r8Guid
).r9name
;
178 } catch (NullPointerException e
) {
179 error
.add("getR9Guidname :" + r8Guid
);
184 public String
getGuidType(String r8Guid
) {
187 temp
= hashguid
.get(r8Guid
).type
;
188 } catch (NullPointerException e
) {
189 error
.add("getR9Guidname :" + r8Guid
);
194 //-------------------------------------get------------------------------------------//
196 //-------------------------------------has------------------------------------------//
198 public boolean hasFunc(String r8lib
) {
199 return hashfunc
.containsKey(r8lib
);
202 public boolean hasGuid(String r8guid
) {
203 return hashguid
.containsKey(r8guid
);
206 public boolean hasMacro(String r8macro
) {
207 return hashmacro
.containsKey(r8macro
);
210 //-------------------------------------has------------------------------------------//
212 //-------------------------------------init------------------------------------------//
214 private static final Database
init() {
215 if (System
.getenv("WORKSPACE") == null) {
216 return new Database("C:" + File
.separator
+ "tianocore" + File
.separator
+ "edk2" + File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ "Migration");
218 return new Database(System
.getenv("WORKSPACE") + File
.separator
+ "Tools" + File
.separator
+ "Conf" + File
.separator
+ "Migration");
222 public static final Database
getInstance() {
223 if (INSTANCE
== null) {
231 private static String workspacePath
;
232 private static HashMap
<String
, String
> hashDbGuids
= new HashMap
<String
, String
>();
233 private static HashMap
<String
, String
> hashDbPpis
= new HashMap
<String
, String
>();
234 private static HashMap
<String
, String
> hashDbProtocols
= new HashMap
<String
, String
>();
235 private static HashMap
<String
, String
> hashDbLibSymbols
= new HashMap
<String
, String
>();
236 private static HashMap
<String
, String
> hashDbLibFunctions
= new HashMap
<String
, String
>();
237 private static HashMap
<String
, String
> hashDbLibExterns
= new HashMap
<String
, String
>();
239 private static final String regLibClassName
= ".*\\W(\\w[\\w\\d]*)\\.h";
240 private static final Pattern ptnLibClassName
= Pattern
.compile(regLibClassName
);
242 private static final String regLibSymbol
= "#define\\s+(\\w[\\w\\d]*)";
243 private static final Pattern ptnLibSymbol
= Pattern
.compile(regLibSymbol
);
245 private static final String regLibDataType
= "[A-Z][A-Z0-9_]*\\s*\\**";
246 private static final String regLibFunction
= regLibDataType
+ "\\s*(?:EFIAPI)?\\s+" +
247 "(\\w[\\w\\d]*)\\s*\\([^)]*\\)\\s*;";
248 private static final Pattern ptnLibFunction
= Pattern
.compile(regLibFunction
);
250 private static final String regLibExtern
= "extern\\s+" + regLibDataType
+
251 "\\s*(\\w[\\w\\d]*)";
252 private static final Pattern ptnLibExtern
= Pattern
.compile(regLibExtern
);
254 private static final String
convertToOsFilePath(String filePath
) {
255 return filePath
.replace("/", File
.separator
).replace("\\", File
.separator
);
257 private static final void collectLibHeaderFileInfo(String libHeaderFile
, String pkgGuid
) throws Exception
{
262 Matcher mtrLibSymbol
;
263 Matcher mtrLibFunction
;
264 Matcher mtrLibExtern
;
266 System
.out
.println ("Parsing: " + libHeaderFile
);
267 mtrLibClass
= ptnLibClassName
.matcher(libHeaderFile
);
268 if (!mtrLibClass
.matches()) {
269 throw new Exception("Illegal libary header file");
271 libClassName
= mtrLibClass
.group(1);
272 libContainer
= libClassName
+ "@" + pkgGuid
;
274 fileContents
= Common
.file2string(libHeaderFile
);
275 mtrLibSymbol
= ptnLibSymbol
.matcher(fileContents
);
276 while (mtrLibSymbol
.find()) {
278 String oldLibContainer
;
280 libSymbol
= mtrLibSymbol
.group(1);
281 oldLibContainer
= hashDbLibSymbols
.put(libSymbol
, libContainer
);
282 if (oldLibContainer
!= null) {
285 warnMessage
= "Duplicated Lib Symbol:" + libSymbol
+ " Found. " +
286 "Later package will overide the previous one";
287 System
.out
.println(warnMessage
);
291 mtrLibFunction
= ptnLibFunction
.matcher(fileContents
);
292 while (mtrLibFunction
.find()) {
294 String oldLibContainer
;
296 libFunction
= mtrLibFunction
.group(1);
297 oldLibContainer
= hashDbLibFunctions
.put(libFunction
, libContainer
);
298 if (oldLibContainer
!= null) {
301 warnMessage
= "Duplicated Lib Function:" + libFunction
+ " Found. " +
302 "Later package will overide the previous one";
303 System
.out
.println(warnMessage
);
307 mtrLibExtern
= ptnLibExtern
.matcher(fileContents
);
308 while (mtrLibExtern
.find()) {
310 String oldLibContainer
;
312 libExtern
= mtrLibExtern
.group(1);
313 oldLibContainer
= hashDbLibExterns
.put(libExtern
, libContainer
);
314 if (oldLibContainer
!= null) {
317 warnMessage
= "Duplicated Lib Extern:" + libExtern
+ " Found. " +
318 "Later package will overide the previous one";
319 System
.out
.println(warnMessage
);
323 private static final void collectLibDataBase(PackageSurfaceArea spdDatabase
, String pkgDirectory
) throws Exception
{
325 LibraryClassDeclarations libClassDeclarations
;
327 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
328 libClassDeclarations
= spdDatabase
.getLibraryClassDeclarations();
329 if (libClassDeclarations
!= null) {
330 Iterator
<LibraryClass
> itLibClass
;
332 itLibClass
= libClassDeclarations
.getLibraryClassList().iterator();
333 while (itLibClass
.hasNext()) {
334 String libHeaderFile
;
336 libHeaderFile
= pkgDirectory
+ File
.separator
+
337 itLibClass
.next().getIncludeHeader();
338 libHeaderFile
= convertToOsFilePath (libHeaderFile
);
340 collectLibHeaderFileInfo(libHeaderFile
, pkgGuid
);
341 } catch (Exception e
) {
344 errorMessage
= "Error (" + e
.getMessage() + ")occurs when parsing " +
346 System
.out
.println(errorMessage
);
351 private static final void collectGuidDatabase(PackageSurfaceArea spdDatabase
) throws Exception
{
353 GuidDeclarations guidDeclarations
;
355 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
356 guidDeclarations
= spdDatabase
.getGuidDeclarations();
357 if (guidDeclarations
!= null) {
358 Iterator
<GuidDeclarations
.Entry
> itGuids
;
360 itGuids
= guidDeclarations
.getEntryList().iterator();
361 while (itGuids
.hasNext()) {
362 hashDbGuids
.put(itGuids
.next().getCName(), pkgGuid
);
368 private static final void collectPpiDatabase(PackageSurfaceArea spdDatabase
) throws Exception
{
370 PpiDeclarations ppiDeclarations
;
372 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
373 ppiDeclarations
= spdDatabase
.getPpiDeclarations();
375 if (ppiDeclarations
!= null) {
376 Iterator
<PpiDeclarations
.Entry
> itPpis
;
378 itPpis
= ppiDeclarations
.getEntryList().iterator();
379 while (itPpis
.hasNext()) {
380 hashDbPpis
.put(itPpis
.next().getCName(), pkgGuid
);
386 private static final void collectProtocolDatabase(PackageSurfaceArea spdDatabase
) throws Exception
{
388 ProtocolDeclarations protocolDeclarations
;
390 pkgGuid
= spdDatabase
.getSpdHeader().getGuidValue();
391 protocolDeclarations
= spdDatabase
.getProtocolDeclarations();
393 if (protocolDeclarations
!= null) {
394 Iterator
<ProtocolDeclarations
.Entry
> itProtocols
;
396 itProtocols
= protocolDeclarations
.getEntryList().iterator();
397 while (itProtocols
.hasNext()) {
398 hashDbGuids
.put(itProtocols
.next().getCName(), pkgGuid
);
404 private static final void collectPackageDatabase(String packageFileName
) throws Exception
{
405 XmlObject xmlPackage
;
406 PackageSurfaceArea spdDatabase
;
409 pkgFile
= new File(packageFileName
);
410 xmlPackage
= XmlObject
.Factory
.parse(pkgFile
);
411 spdDatabase
= ((PackageSurfaceAreaDocument
) xmlPackage
).getPackageSurfaceArea();
413 collectGuidDatabase(spdDatabase
);
414 collectProtocolDatabase(spdDatabase
);
415 collectPpiDatabase(spdDatabase
);
416 collectLibDataBase(spdDatabase
, pkgFile
.getParent());
420 public static final void collectWorkSpaceDatabase() throws Exception
{
421 String databaseFileName
;
423 XmlObject xmlDatabase
;
424 FrameworkDatabase frameworkDatabase
;
425 Iterator
<DbPathAndFilename
> packageFile
;
427 workspacePath
= System
.getenv("WORKSPACE");
429 if (workspacePath
== null) {
430 String errorMessage
= "Envivornment variable \"WORKSPACE\" is not set!";
431 throw new Exception(errorMessage
);
433 databaseFileName
= workspacePath
+ File
.separator
+
434 "Tools" + File
.separator
+
435 "Conf" + File
.separator
+
436 "FrameworkDatabase.db";
437 System
.out
.println("Open " + databaseFileName
);
438 databaseFile
= new File(databaseFileName
);
439 xmlDatabase
= XmlObject
.Factory
.parse(databaseFile
);
440 frameworkDatabase
= ((FrameworkDatabaseDocument
) xmlDatabase
).getFrameworkDatabase();
441 packageFile
= frameworkDatabase
.getPackageList().getFilenameList().iterator();
443 while (packageFile
.hasNext()) {
444 String packageFileName
= packageFile
.next().getStringValue();
445 packageFileName
= workspacePath
+ File
.separator
+ packageFileName
;
446 packageFileName
= convertToOsFilePath(packageFileName
);
448 System
.out
.println("Parsing: " + packageFileName
);
450 collectPackageDatabase(packageFileName
);
451 } catch (Exception e
) {
452 System
.out
.println("Error occured when opening " + packageFileName
+ e
.getMessage());