]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/global/GlobalData.java
added the support for new schema and old schema at the same time
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / global / GlobalData.java
1 /** @file
2 GlobalData class.
3
4 GlobalData provide initializing, instoring, querying and update global data.
5 It is a bridge to intercommunicate between multiple component, such as AutoGen,
6 PCD and so on.
7
8 Copyright (c) 2006, Intel Corporation
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 **/
17 package org.tianocore.build.global;
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.xmlbeans.XmlObject;
30 import org.tianocore.FilenameDocument;
31 import org.tianocore.FilenameDocument.Filename;
32 import org.tianocore.FrameworkDatabaseDocument;
33 import org.tianocore.MsaFilesDocument;
34 import org.tianocore.MsaFilesDocument.MsaFiles.MsaFile;
35 import org.tianocore.MsaHeaderDocument.MsaHeader;
36 import org.tianocore.MsaLibHeaderDocument.MsaLibHeader;
37 import org.tianocore.PackageListDocument;
38 import org.tianocore.PackageSurfaceAreaDocument;
39 import org.tianocore.build.autogen.CommonDefinition;
40 import org.tianocore.build.fpd.FpdParserTask;
41 import org.tianocore.build.pcd.entity.MemoryDatabaseManager;
42
43 /**
44 GlobalData provide initializing, instoring, querying and update global data.
45 It is a bridge to intercommunicate between multiple component, such as AutoGen,
46 PCD and so on.
47
48 <p>Note that all global information are initialized incrementally. All data will
49 parse and record only it is necessary during build time. </p>
50
51 @since GenBuild 1.0
52 **/
53 public class GlobalData {
54
55 ///
56 /// means no surface area information for module
57 ///
58 public static final int NO_SA = 0;
59
60 ///
61 /// means only MSA
62 ///
63 public static final int ONLY_MSA = 1;
64
65 ///
66 /// means only Library MSA
67 ///
68 public static final int ONLY_LIBMSA = 2;
69
70 ///
71 /// means both MSA and MBD
72 ///
73 public static final int MSA_AND_MBD = 3;
74
75 ///
76 /// means both Library MSA and Library MBD
77 ///
78 public static final int LIBMSA_AND_LIBMBD = 4;
79
80 ///
81 /// Be used to ensure Global data will be initialized only once.
82 ///
83 public static boolean globalFlag = false;
84
85 ///
86 /// Record current WORKSPACE Directory
87 ///
88 private static String workspaceDir = "";
89
90 ///
91 /// Two columns: Package Name (Key), Package Path(ralative to WORKSPACE)
92 ///
93 private static final Map<String, String> packageInfo = new HashMap<String, String>();
94
95 ///
96 /// spdTable
97 /// Key: Package Name, Value: SPD detail info
98 ///
99 private static final Map<String, Spd> spdTable = new HashMap<String, Spd>();
100
101 ///
102 /// Three columns:
103 /// 1. Module Name | BaseName (Key)
104 /// 2. Module Path + Msa file name (relative to Package)
105 /// 3. Package Name (This module belong to which package)
106 ///
107 private static final Map<String, String[]> moduleInfo = new HashMap<String, String[]>();
108
109 ///
110 /// List all libraries for current build module
111 /// Key: Library BaseName, Value: output library path+name
112 ///
113 private static final Map<String, String> libraries = new HashMap<String, String>();
114
115 ///
116 /// Store every module's relative library instances BaseName
117 /// Key: Module BaseName, Value: All library instances module depends on.
118 ///
119 private static final Map<String, Set<String> > moduleLibraryMap = new HashMap<String, Set<String> >();
120
121 ///
122 /// Key: Module BaseName, Value: original MSA info
123 ///
124 private static final Map<String, Map<String, XmlObject> > nativeMsa = new HashMap<String, Map<String, XmlObject> >();
125
126 ///
127 /// Key: Module BaseName, Value: original MBD info
128 ///
129 private static final Map<String, Map<String, XmlObject> > nativeMbd = new HashMap<String, Map<String, XmlObject> >();
130
131 ///
132 /// Two columns: Module Name or Base Name as Key
133 /// Value is a HashMap with overridden data from MSA/MBD or/and Platform
134 ///
135 private static final Map<String, Map<String, XmlObject> > parsedModules = new HashMap<String, Map<String, XmlObject> >();
136
137 ///
138 /// List all built Module; Value is Module BaseName + Arch. TBD
139 ///
140 private static final Set<String> builtModules = new HashSet<String>();
141
142 ///
143 /// Library instance information table which recored the library and it's
144 /// constructor and distructor function
145 ///
146 private static final Map<String, String[]> libInstanceInfo = new HashMap<String, String[]>();
147
148 ///
149 /// PCD memory database stored all PCD information which collected from FPD,MSA and SPD.
150 ///
151 private static final MemoryDatabaseManager pcdDbManager = new MemoryDatabaseManager();
152
153 /**
154 Query the module's absolute path with module base name.
155
156 @param moduleName the base name of the module
157 @return the absolute module path
158 **/
159 public synchronized static String getModulePath(String moduleName) {
160 String[] info = moduleInfo.get(moduleName);
161 String packagePath = (String) packageInfo.get(info[1]);
162 File convertFile = new File(workspaceDir + File.separatorChar + packagePath + File.separatorChar + info[0]);
163 return convertFile.getParent();
164 }
165
166 /**
167 Query the module's absolute MSA file path with module base name.
168
169 @param moduleName the base name of the module
170 @return the absolute MSA file name
171 @throws BuildException
172 Base name is not registered in any SPD files
173 **/
174 private synchronized static String getMsaFilename(String moduleName) throws BuildException {
175 String[] info = moduleInfo.get(moduleName);
176 if (info == null) {
177 throw new BuildException("Module base name [" + moduleName + "] can't found in all SPD.");
178 }
179 String packagePath = (String) packageInfo.get(info[1]);
180 File convertFile = new File(workspaceDir + File.separatorChar + packagePath + File.separatorChar + info[0]);
181 return convertFile.getPath();
182 }
183
184 /**
185 Query the module's absolute MBD file path with module base name.
186
187 @param moduleName the base name of the module
188 @return the absolute MBD file name
189 @throws BuildException
190 Base name is not registered in any SPD files
191 **/
192 private synchronized static String getMbdFilename(String moduleName) throws BuildException {
193 String[] info = moduleInfo.get(moduleName);
194 if (info == null) {
195 throw new BuildException("Info: Module base name [" + moduleName + "] can't found in all SPD.");
196 }
197 String packagePath = (String) packageInfo.get(info[1]);
198 File convertFile = new File(workspaceDir + File.separatorChar + packagePath + File.separatorChar + info[0]);
199 return convertFile.getPath().substring(0, convertFile.getPath().length() - 4) + ".mbd";
200 }
201
202 /**
203 Get the current WORKSPACE Directory.
204 @return current workspace directory
205 **/
206 public synchronized static String getWorkspacePath() {
207 return workspaceDir;
208 }
209
210 /**
211 Query package relative path to WORKSPACE_DIR with package name.
212
213 @param packageName the name of the package
214 @return the path relative to WORKSPACE_DIR
215 **/
216 public synchronized static String getPackagePath(String packageName) {
217 return (String) packageInfo.get(packageName);
218 }
219
220 /**
221 Query package (which the module belongs to) relative path to WORSPACE_DIR.
222
223 @param moduleName the base name of the module
224 @return the relative path to WORKSPACE_DIR of the package which the module belongs to
225 **/
226 public synchronized static String getPackagePathForModule(String moduleName) {
227 String[] info = moduleInfo.get(moduleName);
228 String packagePath = (String) packageInfo.get(info[1]);
229 return packagePath;
230 }
231
232 /**
233 Query the package name which the module belongs to with the module's base name.
234
235 @param moduleName the base name of the module
236 @return the package name which the module belongs to
237 **/
238 public synchronized static String getPackageNameForModule(String moduleName) {
239 return moduleInfo.get(moduleName)[1];
240 }
241
242 /**
243 Parse framework database (DB) and all SPD files listed in DB to initialize
244 the environment for next build. This method will only be executed only once
245 in the whole build process.
246
247 @param workspaceDatabaseFile the file name of framework database
248 @param workspaceDir current workspace directory path
249 @throws BuildException
250 Framework Dababase or SPD or MSA file is not valid
251 **/
252 public synchronized static void initInfo(String workspaceDatabaseFile, String workspaceDir) throws BuildException {
253 if (globalFlag) {
254 return;
255 }
256 globalFlag = true;
257 GlobalData.workspaceDir = workspaceDir;
258 File dbFile = new File(workspaceDir + File.separatorChar + workspaceDatabaseFile);
259 try {
260 FrameworkDatabaseDocument db = (FrameworkDatabaseDocument) XmlObject.Factory.parse(dbFile);
261 List<PackageListDocument.PackageList.Package> packages = db.getFrameworkDatabase().getPackageList()
262 .getPackageList();
263 Iterator iter = packages.iterator();
264 while (iter.hasNext()) {
265 PackageListDocument.PackageList.Package packageItem = (PackageListDocument.PackageList.Package) iter
266 .next();
267 String name = packageItem.getPackageNameArray(0).getStringValue();
268 String path = packageItem.getPathArray(0).getStringValue();
269 packageInfo.put(name, path);
270 File spdFile = new File(workspaceDir + File.separatorChar + path + File.separatorChar + name + ".spd");
271 initPackageInfo(spdFile.getPath(), name);
272 //
273 // SPD Parse.
274 //
275 PackageSurfaceAreaDocument spdDoc = (PackageSurfaceAreaDocument) XmlObject.Factory.parse(spdFile);
276 Spd spd = new Spd(spdDoc, path);
277 spdTable.put(name, spd);
278
279 }
280 } catch (Exception e) {
281 throw new BuildException("Parse workspace Database [" + dbFile.getPath() + "] Error.\n" + e.getMessage());
282 }
283 }
284
285 /**
286 Parse every MSA files, get base name from MSA Header. And record those
287 values to ModuleInfo.
288
289 @param packageFilename the file name of the package
290 @param packageName the name of the package
291 @throws BuildException
292 SPD or MSA file is not valid
293 **/
294 private synchronized static void initPackageInfo(String packageFilename, String packageName) throws BuildException {
295 File packageFile = new File(packageFilename);
296 try {
297 PackageSurfaceAreaDocument spd = (PackageSurfaceAreaDocument) XmlObject.Factory.parse(packageFile);
298 List<FilenameDocument.Filename> msaFilenameList;
299
300 List<MsaFilesDocument.MsaFiles.MsaFile> msasList = spd.getPackageSurfaceArea().getMsaFiles()
301 .getMsaFileList();
302 if (msasList.size() == 0) {
303 msaFilenameList = spd.getPackageSurfaceArea().getMsaFiles().getFilenameList();
304 } else {
305 msaFilenameList = new ArrayList<FilenameDocument.Filename>(msasList.size());
306 Iterator msasIter = msasList.iterator();
307 while (msasIter.hasNext()) {
308 MsaFilesDocument.MsaFiles.MsaFile msaFile = (MsaFilesDocument.MsaFiles.MsaFile)msasIter.next();
309 msaFilenameList.add(msaFile.getFilename());
310 }
311 }
312
313 Iterator msaFilenameIter = msaFilenameList.iterator();
314 while (msaFilenameIter.hasNext()) {
315 FilenameDocument.Filename msaFilename = (FilenameDocument.Filename)msaFilenameIter.next();
316 String filename = msaFilename.getStringValue();
317 File msaFile = new File(workspaceDir + File.separatorChar + GlobalData.getPackagePath(packageName)
318 + File.separatorChar + filename);
319 SurfaceAreaParser surfaceAreaParser = new SurfaceAreaParser();
320 Map<String, XmlObject> map = surfaceAreaParser.parseFile(msaFile);
321 String baseName = "";
322 XmlObject header = null;
323 if ((header = map.get("MsaHeader")) != null) {
324 if (((MsaHeader) header).isSetBaseName()) {
325 baseName = ((MsaHeader) header).getBaseName().getStringValue();
326 } else {
327 baseName = ((MsaHeader) header).getModuleName();
328 }
329 } else if ((header = map.get("MsaLibHeader")) != null) {
330 baseName = ((MsaLibHeader) header).getBaseName().getStringValue();
331 } else {
332 continue;
333 }
334 nativeMsa.put(baseName, map);
335 String[] info = { filename, packageName };
336 moduleInfo.put(baseName, info);
337 }
338 } catch (Exception e) {
339 throw new BuildException("Parse package description file [" + packageFile.getPath() + "] Error.\n"
340 + e.getMessage());
341 }
342 }
343
344 /**
345 Query the libraries which the module depends on.
346
347 @param moduleName the base name of the module
348 @return the libraries which the module depends on
349 **/
350 public synchronized static String[] getModuleLibrary(String moduleName, String arch) {
351 Set<String> set = moduleLibraryMap.get(moduleName + "-" + arch);
352 return set.toArray(new String[set.size()]);
353 }
354
355 /**
356 Register module's library list which it depends on for later use.
357
358 @param moduleName the base name of the module
359 @param libraryList the libraries which the module depends on
360 **/
361 public synchronized static void addModuleLibrary(String moduleName, String arch, Set<String> libraryList) {
362 moduleLibraryMap.put(moduleName + "-" + arch, libraryList);
363 }
364
365 /**
366 Query the library absolute file name with library name.
367
368 @param library the base name of the library
369 @return the library absolute file name
370 **/
371 public synchronized static String getLibrary(String library, String arch) {
372 return libraries.get(library + "-" + arch);
373 }
374
375 /**
376 Register library absolute file name for later use.
377
378 @param library the base name of the library
379 @param resultPath the library absolute file name
380 **/
381 public synchronized static void addLibrary(String library, String arch, String resultPath) {
382 libraries.put(library + "-" + arch, resultPath);
383 }
384
385 /**
386 Whether the module with ARCH has built in the previous build.
387
388 @param moduleName the base name of the module
389 @param arch current build ARCH
390 @return true if the module has built in previous, otherwise return false
391 **/
392 public synchronized static boolean isModuleBuilt(String moduleName, String arch) {
393 return builtModules.contains(moduleName + "-" + arch);
394 }
395
396 /**
397 Register the module with ARCH has built.
398
399 @param moduleName the base name of the module
400 @param arch current build ARCH
401 **/
402 public synchronized static void registerBuiltModule(String moduleName, String arch) {
403 builtModules.add(moduleName + "-" + arch);
404 }
405
406 /**
407 Whether the module's surface area has parsed in the previous build.
408
409 @param moduleName the base name of the module
410 @return true if the module's surface area has parsed in previous, otherwise
411 return false
412 **/
413 public synchronized static boolean isModuleParsed(String moduleName) {
414 return parsedModules.containsKey(moduleName);
415 }
416
417 /**
418 Query overrided module surface area information. If current is Package
419 or Platform build, also include the information from FPD file.
420
421 <p>Note that surface area parsing is incremental. That means the method will
422 only to parse the MSA and MBD files when never parsed before. </p>
423
424 @param moduleName the base name of the module
425 @return the overrided module surface area information
426 @throws BuildException
427 MSA or MBD is not valid
428 **/
429 public synchronized static Map<String, XmlObject> getDoc(String moduleName) throws BuildException {
430 if (parsedModules.containsKey(moduleName)) {
431 return parsedModules.get(moduleName);
432 }
433 Map<String, XmlObject> msaMap = getNativeMsa(moduleName);
434 Map<String, XmlObject> mbdMap = getNativeMbd(moduleName);
435 OverrideProcess op = new OverrideProcess();
436 Map<String, XmlObject> map = op.override(mbdMap, msaMap);
437 //
438 // IF IT IS A PALTFORM BUILD, OVERRIDE FROM PLATFORM
439 //
440 if (FpdParserTask.platformBuildOptions != null) {
441 Map<String, XmlObject> platformMap = new HashMap<String, XmlObject>();
442 platformMap.put("BuildOptions", FpdParserTask.platformBuildOptions);
443 Map<String, XmlObject> overrideMap = op.override(platformMap, OverrideProcess.deal(map));
444 GlobalData.registerModule(moduleName, overrideMap);
445 return overrideMap;
446 } else {
447 parsedModules.put(moduleName, map);
448 return map;
449 }
450 }
451
452 /**
453 Query the native MSA information with module base name.
454
455 <p>Note that MSA parsing is incremental. That means the method will
456 only to parse the MSA files when never parsed before. </p>
457
458 @param moduleName the base name of the module
459 @return the native MSA information
460 @throws BuildException
461 MSA file is not valid
462 **/
463 public synchronized static Map<String, XmlObject> getNativeMsa(String moduleName) throws BuildException {
464 if (nativeMsa.containsKey(moduleName)) {
465 return nativeMsa.get(moduleName);
466 }
467 String msaFilename = getMsaFilename(moduleName);
468 File msaFile = new File(msaFilename);
469 if (!msaFile.exists()) {
470 throw new BuildException("Info: Surface Area file [" + msaFile.getPath() + "] can't found.");
471 }
472 SurfaceAreaParser surfaceAreaParser = new SurfaceAreaParser();
473 Map<String, XmlObject> map = surfaceAreaParser.parseFile(msaFile);
474 nativeMsa.put(moduleName, map);
475 return map;
476 }
477
478 /**
479 Query the native MBD information with module base name.
480
481 <p>Note that MBD parsing is incremental. That means the method will
482 only to parse the MBD files when never parsed before. </p>
483
484 @param moduleName the base name of the module
485 @return the native MBD information
486 @throws BuildException
487 MBD file is not valid
488 **/
489 public synchronized static Map<String, XmlObject> getNativeMbd(String moduleName) throws BuildException {
490 if (nativeMbd.containsKey(moduleName)) {
491 return nativeMbd.get(moduleName);
492 }
493 String mbdFilename = getMbdFilename(moduleName);
494 File mbdFile = new File(mbdFilename);
495 if (!mbdFile.exists()) {
496 return null;
497 //throw new BuildException("Info: Surface Area file [" + mbdFile.getPath() + "] can't found.");
498 }
499 SurfaceAreaParser surfaceAreaParser = new SurfaceAreaParser();
500 Map<String, XmlObject> map = surfaceAreaParser.parseFile(mbdFile);
501 nativeMbd.put(moduleName, map);
502 return map;
503 }
504
505 /**
506 Register module overrided surface area information. If has existed, then update.
507
508 @param moduleName the base name of the module
509 @param map the overrided surface area information
510 **/
511 public synchronized static void registerModule(String moduleName, Map<String, XmlObject> map) {
512 parsedModules.put(moduleName, map);
513 }
514
515 /**
516 *
517 * @param protocolName
518 * @return
519 */
520 public synchronized static String[] getProtocolInfoGuid(String protocolName) {
521 Set set = spdTable.keySet();
522 Iterator iter = set.iterator();
523 String[] cNameGuid = null;
524
525 while (iter.hasNext()) {
526 Spd spd = (Spd) spdTable.get(iter.next());
527 cNameGuid = spd.getProtocolNameGuidArray(protocolName);
528 if (cNameGuid != null) {
529 break;
530 }
531 }
532 return cNameGuid;
533 }
534
535 public synchronized static String[] getPpiInfoGuid(String ppiName) {
536 Set set = spdTable.keySet();
537 Iterator iter = set.iterator();
538 String[] cNameGuid = null;
539
540 while (iter.hasNext()) {
541 Spd spd = (Spd) spdTable.get(iter.next());
542 cNameGuid = spd.getPpiCnameGuidArray(ppiName);
543
544 if (cNameGuid != null) {
545 break;
546 }
547 }
548 return cNameGuid;
549 }
550
551 /**
552 *
553 * @param guidName
554 * @return
555 */
556 public synchronized static String[] getGuidInfoGuid(String guidName) {
557 String[] cNameGuid = null;
558 Set set = spdTable.keySet();
559 Iterator iter = set.iterator();
560
561 while (iter.hasNext()) {
562 Spd spd = (Spd) spdTable.get(iter.next());
563 cNameGuid = spd.getGuidNameArray(guidName);
564 if (cNameGuid != null) {
565 break;
566 }
567 }
568 return cNameGuid;
569 }
570
571 public synchronized static String getLibClassIncluder(String libName) {
572 String libIncluder = null;
573 Set set = spdTable.keySet();
574 Iterator iter = set.iterator();
575
576 while (iter.hasNext()) {
577 String packageName = (String) iter.next();
578 Spd spd = (Spd) spdTable.get(packageName);
579 libIncluder = spd.getLibClassIncluder(libName);
580 String packagePath = spd.packagePath;
581 if (packagePath != null) {
582 packagePath = packagePath.replace('\\', File.separatorChar);
583 packagePath = packagePath.replace('/', File.separatorChar);
584 } else {
585 packagePath = packageName;
586 }
587 if (libIncluder != null) {
588 libIncluder = libIncluder.replace('\\', File.separatorChar);
589 libIncluder = libIncluder.replace('/', File.separatorChar);
590 libIncluder = packageName + File.separatorChar + libIncluder;
591 break;
592 }
593 }
594 return libIncluder;
595 }
596
597 public synchronized static String getModuleInfoByPackageName(String packageName, String moduleType) {
598 Spd spd;
599 String includeFile = null;
600 String includeStr = "";
601 String cleanPath = "";
602
603 spd = (Spd) spdTable.get(packageName);
604 includeFile = spd.getModuleTypeIncluder(moduleType);
605 if (includeFile != null) {
606 includeFile = includeFile.replace('\\', File.separatorChar);
607 includeFile = includeFile.replace('/', File.separatorChar);
608 includeStr = CommonDefinition.include + " <" + includeStr;
609 cleanPath = spd.packagePath;
610 cleanPath = cleanPath.replace('\\', File.separatorChar);
611 cleanPath = cleanPath.replace('/', File.separatorChar);
612
613 if (cleanPath.charAt(spd.packagePath.length() - 1) != File.separatorChar) {
614 cleanPath = cleanPath + File.separatorChar;
615 }
616 includeStr = includeStr + cleanPath;
617 includeStr = includeStr + includeFile;
618 includeStr = includeStr + ">\r\n";
619 }
620
621 return includeStr;
622 }
623
624 public synchronized static void setLibInstanceInfo(String libName, String libConstructor, String libDesturctor) {
625 String[] libConsDes = new String[2];
626 libConsDes[0] = libConstructor;
627 libConsDes[1] = libDesturctor;
628
629 libInstanceInfo.put(libName, libConsDes);
630 }
631
632 public synchronized static boolean isHaveLibInstance(String libName) {
633 return libInstanceInfo.containsKey(libName);
634 }
635
636 public synchronized static String getLibInstanceConstructor(String libName) {
637 String[] libInstanceValue;
638 libInstanceValue = libInstanceInfo.get(libName);
639 if (libInstanceValue != null) {
640 return libInstanceValue[0];
641 } else {
642 return null;
643 }
644 }
645
646 public synchronized static String getLibInstanceDestructor(String libName) {
647 String[] libInstanceValue;
648 libInstanceValue = libInstanceInfo.get(libName);
649 if (libInstanceValue != null) {
650 return libInstanceValue[1];
651 } else {
652 return null;
653 }
654 }
655
656 public synchronized static MemoryDatabaseManager getPCDMemoryDBManager() {
657 return pcdDbManager;
658 }
659 }