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