summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/MigrationTools/org/tianocore/migration/Database.java
blob: 5daae526f339b4bf3db8f02ac524ac288f141272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/** @file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.migration;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.xmlbeans.XmlObject;
import org.tianocore.DbPathAndFilename;
import org.tianocore.FrameworkDatabaseDocument;
import org.tianocore.PackageSurfaceAreaDocument;
import org.tianocore.FrameworkDatabaseDocument.FrameworkDatabase;
import org.tianocore.GuidDeclarationsDocument.GuidDeclarations;
import org.tianocore.LibraryClassDeclarationsDocument.LibraryClassDeclarations;
import org.tianocore.LibraryClassDeclarationsDocument.LibraryClassDeclarations.LibraryClass;
import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
import org.tianocore.PpiDeclarationsDocument.PpiDeclarations;
import org.tianocore.ProtocolDeclarationsDocument.ProtocolDeclarations;

public final class Database {
	private static final Database INSTANCE = Database.init();;

	Database(String path) {
		DatabasePath = path;

		try {
			// collectWorkSpaceDatabase();
			importPkgGuid("PkgGuid.csv");
			importDBLib("Library.csv");
			importDBGuid("Guid.csv", "Guid");
			importDBGuid("Ppi.csv", "Ppi");
			importDBGuid("Protocol.csv", "Protocol");
			importDBMacro("Macro.csv");
			importListR8Only();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public String DatabasePath;

	public Set<String> error = new HashSet<String>();

	public Set<String> r8only = new HashSet<String>();

	private Map<String, Guid> hashguid = new HashMap<String, Guid>();

	private Map<String, Func> hashfunc = new HashMap<String, Func>();

	private Map<String, Macro> hashmacro = new HashMap<String, Macro>();

	private Map<String, String> hashPkgGuid = new HashMap<String, String>();

	// -------------------------------------import------------------------------------------//
	private void importPkgGuid(String filename) throws Exception {
		BufferedReader rd = new BufferedReader(new FileReader(DatabasePath
				+ File.separator + filename));
		String line;
		String[] linecontext;

		if (rd.ready()) {
			System.out.println("Found " + filename
					+ ", Importing Package Guid Database.");
			//
			// Skip the title row.
			// 
			line = rd.readLine();
			while ((line = rd.readLine()) != null) {
				if (line.length() != 0) {
					linecontext = line.split(",");
					hashPkgGuid.put(linecontext[0], linecontext[1]);
				}
			}
		}
	}

	public Iterator<String> dumpAllPkgGuid() {
		return hashPkgGuid.values().iterator();
	}

	private void importDBLib(String filename) throws Exception {
		BufferedReader rd = new BufferedReader(new FileReader(DatabasePath
				+ File.separator + filename));
		String line;
		String[] linecontext;
		Func lf;

		if (rd.ready()) {
			System.out.println("Found " + filename
					+ ", Importing Library Database.");
			while ((line = rd.readLine()) != null) {
				if (line.length() != 0) {
					linecontext = line.split(",");
					lf = new Func(linecontext);
					hashfunc.put(lf.r8funcname, lf);
				}
			}
		}
	}

	private void importDBGuid(String filename, String type) throws Exception {
		BufferedReader rd = new BufferedReader(new FileReader(DatabasePath
				+ File.separator + filename));
		String line;
		String[] linecontext;
		Guid gu;

		if (rd.ready()) {
			System.out.println("Found " + filename + ", Importing " + type
					+ " Database.");
			while ((line = rd.readLine()) != null) {
				if (line.length() != 0) {
					linecontext = line.split(",");
					gu = new Guid(linecontext, type);
					hashguid.put(gu.r8name, gu);
				}
			}
		}
	}

	private void importDBMacro(String filename) throws Exception {
		BufferedReader rd = new BufferedReader(new FileReader(DatabasePath
				+ File.separator + filename));
		String line;
		String[] linecontext;
		Macro mc;

		if (rd.ready()) {
			System.out.println("Found " + filename
					+ ", Importing Macro Database.");
			while ((line = rd.readLine()) != null) {
				if (line.length() != 0) {
					linecontext = line.split(",");
					mc = new Macro(linecontext);
					hashmacro.put(mc.r8name, mc);
				}
			}
		}
	}

	private void importListR8Only() throws Exception {
		Pattern ptnr8only = Pattern.compile(
				"////#?(\\w*)?.*?R8_(.*?)\\s*\\(.*?////~", Pattern.DOTALL);
		String wholeline = Common.file2string(DatabasePath + File.separator
				+ "R8Lib.c");
		System.out
				.println("Found " + "R8Lib.c" + ", Importing R8Lib Database.");
		Matcher mtrr8only = ptnr8only.matcher(wholeline);
		while (mtrr8only.find()) {
			r8only.add(mtrr8only.group(2));
		}
	}

	// -------------------------------------import------------------------------------------//

	// -------------------------------------get------------------------------------------//

	public String getR9Lib(String r8funcname) {
		String temp = null;
		if (hashfunc.containsKey(r8funcname)) {
			temp = hashfunc.get(r8funcname).r9libname;
		}
		return temp;
	}

	public String getR9Func(String r8funcname) {
		String temp = null;
		if (hashfunc.containsKey(r8funcname)) {
			temp = hashfunc.get(r8funcname).r9funcname;
		}
		return temp;
	}

	public String getR9Macro(String r8macro) {
		return hashmacro.get(r8macro).r9name; // the verification job of if
												// the macro exists in the
												// database is done when
												// registering it
	}

	public String getR9Guidname(String r8Guid) {
		String temp = null;
		try {
			temp = hashguid.get(r8Guid).r9name;
		} catch (NullPointerException e) {
			error.add("getR9Guidname :" + r8Guid);
		}
		return temp;
	}

	public String getGuidType(String r8Guid) {
		String temp = null;
		try {
			temp = hashguid.get(r8Guid).type;
		} catch (NullPointerException e) {
			error.add("getR9Guidname :" + r8Guid);
		}
		return temp;
	}

	// -------------------------------------get------------------------------------------//

	// -------------------------------------has------------------------------------------//

	public boolean hasFunc(String r8lib) {
		return hashfunc.containsKey(r8lib);
	}

	public boolean hasGuid(String r8guid) {
		return hashguid.containsKey(r8guid);
	}

	public boolean hasMacro(String r8macro) {
		return hashmacro.containsKey(r8macro);
	}

	// -------------------------------------has------------------------------------------//

	// -------------------------------------init------------------------------------------//

	private static final Database init() {
		if (System.getenv("WORKSPACE") == null) {
			return new Database("C:" + File.separator + "tianocore"
					+ File.separator + "edk2" + File.separator + "Tools"
					+ File.separator + "Conf" + File.separator + "Migration");
		} else {
			return new Database(System.getenv("WORKSPACE") + File.separator
					+ "Tools" + File.separator + "Conf" + File.separator
					+ "Migration");
		}
	}

	public static final Database getInstance() {
		return INSTANCE;
	}

	private String workspacePath;

	private HashMap<String, String> hashDbGuids = new HashMap<String, String>();

	private HashMap<String, String> hashDbPpis = new HashMap<String, String>();

	private HashMap<String, String> hashDbProtocols = new HashMap<String, String>();

	private HashMap<String, String> hashDbLibSymbols = new HashMap<String, String>();

	private HashMap<String, String> hashDbLibFunctions = new HashMap<String, String>();

	private HashMap<String, String> hashDbLibExterns = new HashMap<String, String>();

	private final String regLibClassName = ".*\\W(\\w[\\w\\d]*)\\.h";

	private final Pattern ptnLibClassName = Pattern.compile(regLibClassName);

	private final String regLibSymbol = "#define\\s+(\\w[\\w\\d]*)";

	private final Pattern ptnLibSymbol = Pattern.compile(regLibSymbol);

	private final String regLibDataType = "[A-Z][A-Z0-9_]*\\s*\\**";

	private final String regLibFunction = regLibDataType
			+ "\\s*(?:EFIAPI)?\\s+" + "(\\w[\\w\\d]*)\\s*\\([^)]*\\)\\s*;";

	private Pattern ptnLibFunction = Pattern.compile(regLibFunction);

	private final String regLibExtern = "extern\\s+" + regLibDataType
			+ "\\s*(\\w[\\w\\d]*)";

	private final Pattern ptnLibExtern = Pattern.compile(regLibExtern);

	private final String convertToOsFilePath(String filePath) {
		return filePath.replace("/", File.separator).replace("\\",
				File.separator);
	}

	private final void collectLibHeaderFileInfo(String libHeaderFile,
			String pkgGuid) throws Exception {
		String fileContents;
		String libClassName;
		String libContainer;
		Matcher mtrLibClass;
		Matcher mtrLibSymbol;
		Matcher mtrLibFunction;
		Matcher mtrLibExtern;

		System.out.println("Parsing: " + libHeaderFile);
		mtrLibClass = ptnLibClassName.matcher(libHeaderFile);
		if (!mtrLibClass.matches()) {
			throw new Exception("Illegal libary header file");
		}
		libClassName = mtrLibClass.group(1);
		libContainer = libClassName + "@" + pkgGuid;

		fileContents = Common.file2string(libHeaderFile);
		mtrLibSymbol = ptnLibSymbol.matcher(fileContents);
		while (mtrLibSymbol.find()) {
			String libSymbol;
			String oldLibContainer;

			libSymbol = mtrLibSymbol.group(1);
			oldLibContainer = hashDbLibSymbols.put(libSymbol, libContainer);
			if (oldLibContainer != null) {
				String warnMessage;

				warnMessage = "Duplicated Lib Symbol:" + libSymbol + " Found. "
						+ "Later package will overide the previous one";
				System.out.println(warnMessage);
			}
		}

		mtrLibFunction = ptnLibFunction.matcher(fileContents);
		while (mtrLibFunction.find()) {
			String libFunction;
			String oldLibContainer;

			libFunction = mtrLibFunction.group(1);
			oldLibContainer = hashDbLibFunctions.put(libFunction, libContainer);
			if (oldLibContainer != null) {
				String warnMessage;

				warnMessage = "Duplicated Lib Function:" + libFunction
						+ " Found. "
						+ "Later package will overide the previous one";
				System.out.println(warnMessage);
			}
		}

		mtrLibExtern = ptnLibExtern.matcher(fileContents);
		while (mtrLibExtern.find()) {
			String libExtern;
			String oldLibContainer;

			libExtern = mtrLibExtern.group(1);
			oldLibContainer = hashDbLibExterns.put(libExtern, libContainer);
			if (oldLibContainer != null) {
				String warnMessage;

				warnMessage = "Duplicated Lib Extern:" + libExtern + " Found. "
						+ "Later package will overide the previous one";
				System.out.println(warnMessage);
			}
		}
	}

	private final void collectLibDataBase(PackageSurfaceArea spdDatabase,
			String pkgDirectory) throws Exception {
		String pkgGuid;
		LibraryClassDeclarations libClassDeclarations;

		pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
		libClassDeclarations = spdDatabase.getLibraryClassDeclarations();
		if (libClassDeclarations != null) {
			Iterator<LibraryClass> itLibClass;

			itLibClass = libClassDeclarations.getLibraryClassList().iterator();
			while (itLibClass.hasNext()) {
				String libHeaderFile;

				libHeaderFile = pkgDirectory + File.separator
						+ itLibClass.next().getIncludeHeader();
				libHeaderFile = convertToOsFilePath(libHeaderFile);
				try {
					collectLibHeaderFileInfo(libHeaderFile, pkgGuid);
				} catch (Exception e) {
					String errorMessage;

					errorMessage = "Error (" + e.getMessage()
							+ ")occurs when parsing " + libHeaderFile;
					System.out.println(errorMessage);
				}
			}
		}
	}

	private final void collectGuidDatabase(PackageSurfaceArea spdDatabase)
			throws Exception {
		String pkgGuid;
		GuidDeclarations guidDeclarations;

		pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
		guidDeclarations = spdDatabase.getGuidDeclarations();
		if (guidDeclarations != null) {
			Iterator<GuidDeclarations.Entry> itGuids;

			itGuids = guidDeclarations.getEntryList().iterator();
			while (itGuids.hasNext()) {
				hashDbGuids.put(itGuids.next().getCName(), pkgGuid);
			}
		}

	}

	private final void collectPpiDatabase(PackageSurfaceArea spdDatabase)
			throws Exception {
		String pkgGuid;
		PpiDeclarations ppiDeclarations;

		pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
		ppiDeclarations = spdDatabase.getPpiDeclarations();

		if (ppiDeclarations != null) {
			Iterator<PpiDeclarations.Entry> itPpis;

			itPpis = ppiDeclarations.getEntryList().iterator();
			while (itPpis.hasNext()) {
				hashDbPpis.put(itPpis.next().getCName(), pkgGuid);
			}
		}

	}

	private final void collectProtocolDatabase(PackageSurfaceArea spdDatabase)
			throws Exception {
		String pkgGuid;
		ProtocolDeclarations protocolDeclarations;

		pkgGuid = spdDatabase.getSpdHeader().getGuidValue();
		protocolDeclarations = spdDatabase.getProtocolDeclarations();

		if (protocolDeclarations != null) {
			Iterator<ProtocolDeclarations.Entry> itProtocols;

			itProtocols = protocolDeclarations.getEntryList().iterator();
			while (itProtocols.hasNext()) {
				hashDbGuids.put(itProtocols.next().getCName(), pkgGuid);
			}
		}

	}

	private final void collectPackageDatabase(String packageFileName)
			throws Exception {
		XmlObject xmlPackage;
		PackageSurfaceArea spdDatabase;
		File pkgFile;

		pkgFile = new File(packageFileName);
		xmlPackage = XmlObject.Factory.parse(pkgFile);
		spdDatabase = ((PackageSurfaceAreaDocument) xmlPackage)
				.getPackageSurfaceArea();

		collectGuidDatabase(spdDatabase);
		collectProtocolDatabase(spdDatabase);
		collectPpiDatabase(spdDatabase);
		collectLibDataBase(spdDatabase, pkgFile.getParent());

	}

	public final void collectWorkSpaceDatabase() throws Exception {
		String databaseFileName;
		File databaseFile;
		XmlObject xmlDatabase;
		FrameworkDatabase frameworkDatabase;
		Iterator<DbPathAndFilename> packageFile;

		workspacePath = System.getenv("WORKSPACE");

		if (workspacePath == null) {
			String errorMessage = "Envivornment variable \"WORKSPACE\" is not set!";
			throw new Exception(errorMessage);
		}
		databaseFileName = workspacePath + File.separator + "Tools"
				+ File.separator + "Conf" + File.separator
				+ "FrameworkDatabase.db";
		System.out.println("Open " + databaseFileName);
		databaseFile = new File(databaseFileName);
		xmlDatabase = XmlObject.Factory.parse(databaseFile);
		frameworkDatabase = ((FrameworkDatabaseDocument) xmlDatabase)
				.getFrameworkDatabase();
		packageFile = frameworkDatabase.getPackageList().getFilenameList()
				.iterator();

		while (packageFile.hasNext()) {
			String packageFileName = packageFile.next().getStringValue();
			packageFileName = workspacePath + File.separator + packageFileName;
			packageFileName = convertToOsFilePath(packageFileName);

			System.out.println("Parsing: " + packageFileName);
			try {
				collectPackageDatabase(packageFileName);
			} catch (Exception e) {
				System.out.println("Error occured when opening "
						+ packageFileName + e.getMessage());
			}
		}
	}
}