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