4 GlobalData provide initializing, instoring, querying and update global data.
5 It is a bridge to intercommunicate between multiple component, such as AutoGen,
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
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.
17 package org
.tianocore
.build
.global
;
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
;
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
;
44 GlobalData provide initializing, instoring, querying and update global data.
45 It is a bridge to intercommunicate between multiple component, such as AutoGen,
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>
53 public class GlobalData
{
56 /// means no surface area information for module
58 public static final int NO_SA
= 0;
63 public static final int ONLY_MSA
= 1;
66 /// means only Library MSA
68 public static final int ONLY_LIBMSA
= 2;
71 /// means both MSA and MBD
73 public static final int MSA_AND_MBD
= 3;
76 /// means both Library MSA and Library MBD
78 public static final int LIBMSA_AND_LIBMBD
= 4;
81 /// Be used to ensure Global data will be initialized only once.
83 public static boolean globalFlag
= false;
86 /// Record current WORKSPACE Directory
88 private static String workspaceDir
= "";
91 /// Two columns: Package Name (Key), Package Path(ralative to WORKSPACE)
93 private static final Map
<String
, String
> packageInfo
= new HashMap
<String
, String
>();
97 /// Key: Package Name, Value: SPD detail info
99 private static final Map
<String
, Spd
> spdTable
= new HashMap
<String
, Spd
>();
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)
107 private static final Map
<String
, String
[]> moduleInfo
= new HashMap
<String
, String
[]>();
110 /// List all libraries for current build module
111 /// Key: Library BaseName, Value: output library path+name
113 private static final Map
<String
, String
> libraries
= new HashMap
<String
, String
>();
116 /// Store every module's relative library instances BaseName
117 /// Key: Module BaseName, Value: All library instances module depends on.
119 private static final Map
<String
, Set
<String
> > moduleLibraryMap
= new HashMap
<String
, Set
<String
> >();
122 /// Key: Module BaseName, Value: original MSA info
124 private static final Map
<String
, Map
<String
, XmlObject
> > nativeMsa
= new HashMap
<String
, Map
<String
, XmlObject
> >();
127 /// Key: Module BaseName, Value: original MBD info
129 private static final Map
<String
, Map
<String
, XmlObject
> > nativeMbd
= new HashMap
<String
, Map
<String
, XmlObject
> >();
132 /// Two columns: Module Name or Base Name as Key
133 /// Value is a HashMap with overridden data from MSA/MBD or/and Platform
135 private static final Map
<String
, Map
<String
, XmlObject
> > parsedModules
= new HashMap
<String
, Map
<String
, XmlObject
> >();
138 /// List all built Module; Value is Module BaseName + Arch. TBD
140 private static final Set
<String
> builtModules
= new HashSet
<String
>();
143 /// Library instance information table which recored the library and it's
144 /// constructor and distructor function
146 private static final Map
<String
, String
[]> libInstanceInfo
= new HashMap
<String
, String
[]>();
149 /// PCD memory database stored all PCD information which collected from FPD,MSA and SPD.
151 private static final MemoryDatabaseManager pcdDbManager
= new MemoryDatabaseManager();
154 Query the module's absolute path with module base name.
156 @param moduleName the base name of the module
157 @return the absolute module path
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();
167 Query the module's absolute MSA file path with module base name.
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
174 private synchronized static String
getMsaFilename(String moduleName
) throws BuildException
{
175 String
[] info
= moduleInfo
.get(moduleName
);
177 throw new BuildException("Module base name [" + moduleName
+ "] can't found in all SPD.");
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();
185 Query the module's absolute MBD file path with module base name.
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
192 private synchronized static String
getMbdFilename(String moduleName
) throws BuildException
{
193 String
[] info
= moduleInfo
.get(moduleName
);
195 throw new BuildException("Info: Module base name [" + moduleName
+ "] can't found in all SPD.");
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";
203 Get the current WORKSPACE Directory.
204 @return current workspace directory
206 public synchronized static String
getWorkspacePath() {
211 Query package relative path to WORKSPACE_DIR with package name.
213 @param packageName the name of the package
214 @return the path relative to WORKSPACE_DIR
216 public synchronized static String
getPackagePath(String packageName
) {
217 return (String
) packageInfo
.get(packageName
);
221 Query package (which the module belongs to) relative path to WORSPACE_DIR.
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
226 public synchronized static String
getPackagePathForModule(String moduleName
) {
227 String
[] info
= moduleInfo
.get(moduleName
);
228 String packagePath
= (String
) packageInfo
.get(info
[1]);
233 Query the package name which the module belongs to with the module's base name.
235 @param moduleName the base name of the module
236 @return the package name which the module belongs to
238 public synchronized static String
getPackageNameForModule(String moduleName
) {
239 return moduleInfo
.get(moduleName
)[1];
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.
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
252 public synchronized static void initInfo(String workspaceDatabaseFile
, String workspaceDir
) throws BuildException
{
257 GlobalData
.workspaceDir
= workspaceDir
;
258 File dbFile
= new File(workspaceDir
+ File
.separatorChar
+ workspaceDatabaseFile
);
260 FrameworkDatabaseDocument db
= (FrameworkDatabaseDocument
) XmlObject
.Factory
.parse(dbFile
);
261 List
<PackageListDocument
.PackageList
.Package
> packages
= db
.getFrameworkDatabase().getPackageList()
263 Iterator iter
= packages
.iterator();
264 while (iter
.hasNext()) {
265 PackageListDocument
.PackageList
.Package packageItem
= (PackageListDocument
.PackageList
.Package
) iter
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
);
275 PackageSurfaceAreaDocument spdDoc
= (PackageSurfaceAreaDocument
) XmlObject
.Factory
.parse(spdFile
);
276 Spd spd
= new Spd(spdDoc
, path
);
277 spdTable
.put(name
, spd
);
280 } catch (Exception e
) {
281 throw new BuildException("Parse workspace Database [" + dbFile
.getPath() + "] Error.\n" + e
.getMessage());
286 Parse every MSA files, get base name from MSA Header. And record those
287 values to ModuleInfo.
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
294 private synchronized static void initPackageInfo(String packageFilename
, String packageName
) throws BuildException
{
295 File packageFile
= new File(packageFilename
);
297 PackageSurfaceAreaDocument spd
= (PackageSurfaceAreaDocument
) XmlObject
.Factory
.parse(packageFile
);
298 List
<FilenameDocument
.Filename
> msaFilenameList
;
300 List
<MsaFilesDocument
.MsaFiles
.MsaFile
> msasList
= spd
.getPackageSurfaceArea().getMsaFiles()
302 if (msasList
.size() == 0) {
303 msaFilenameList
= spd
.getPackageSurfaceArea().getMsaFiles().getFilenameList();
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());
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();
327 baseName
= ((MsaHeader
) header
).getModuleName();
329 } else if ((header
= map
.get("MsaLibHeader")) != null) {
330 baseName
= ((MsaLibHeader
) header
).getBaseName().getStringValue();
334 nativeMsa
.put(baseName
, map
);
335 String
[] info
= { filename
, packageName
};
336 moduleInfo
.put(baseName
, info
);
338 } catch (Exception e
) {
339 throw new BuildException("Parse package description file [" + packageFile
.getPath() + "] Error.\n"
345 Query the libraries which the module depends on.
347 @param moduleName the base name of the module
348 @return the libraries which the module depends on
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()]);
356 Register module's library list which it depends on for later use.
358 @param moduleName the base name of the module
359 @param libraryList the libraries which the module depends on
361 public synchronized static void addModuleLibrary(String moduleName
, String arch
, Set
<String
> libraryList
) {
362 moduleLibraryMap
.put(moduleName
+ "-" + arch
, libraryList
);
366 Query the library absolute file name with library name.
368 @param library the base name of the library
369 @return the library absolute file name
371 public synchronized static String
getLibrary(String library
, String arch
) {
372 return libraries
.get(library
+ "-" + arch
);
376 Register library absolute file name for later use.
378 @param library the base name of the library
379 @param resultPath the library absolute file name
381 public synchronized static void addLibrary(String library
, String arch
, String resultPath
) {
382 libraries
.put(library
+ "-" + arch
, resultPath
);
386 Whether the module with ARCH has built in the previous build.
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
392 public synchronized static boolean isModuleBuilt(String moduleName
, String arch
) {
393 return builtModules
.contains(moduleName
+ "-" + arch
);
397 Register the module with ARCH has built.
399 @param moduleName the base name of the module
400 @param arch current build ARCH
402 public synchronized static void registerBuiltModule(String moduleName
, String arch
) {
403 builtModules
.add(moduleName
+ "-" + arch
);
407 Whether the module's surface area has parsed in the previous build.
409 @param moduleName the base name of the module
410 @return true if the module's surface area has parsed in previous, otherwise
413 public synchronized static boolean isModuleParsed(String moduleName
) {
414 return parsedModules
.containsKey(moduleName
);
418 Query overrided module surface area information. If current is Package
419 or Platform build, also include the information from FPD file.
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>
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
429 public synchronized static Map
<String
, XmlObject
> getDoc(String moduleName
) throws BuildException
{
430 if (parsedModules
.containsKey(moduleName
)) {
431 return parsedModules
.get(moduleName
);
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
);
438 // IF IT IS A PALTFORM BUILD, OVERRIDE FROM PLATFORM
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
);
447 parsedModules
.put(moduleName
, map
);
453 Query the native MSA information with module base name.
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>
458 @param moduleName the base name of the module
459 @return the native MSA information
460 @throws BuildException
461 MSA file is not valid
463 public synchronized static Map
<String
, XmlObject
> getNativeMsa(String moduleName
) throws BuildException
{
464 if (nativeMsa
.containsKey(moduleName
)) {
465 return nativeMsa
.get(moduleName
);
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.");
472 SurfaceAreaParser surfaceAreaParser
= new SurfaceAreaParser();
473 Map
<String
, XmlObject
> map
= surfaceAreaParser
.parseFile(msaFile
);
474 nativeMsa
.put(moduleName
, map
);
479 Query the native MBD information with module base name.
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>
484 @param moduleName the base name of the module
485 @return the native MBD information
486 @throws BuildException
487 MBD file is not valid
489 public synchronized static Map
<String
, XmlObject
> getNativeMbd(String moduleName
) throws BuildException
{
490 if (nativeMbd
.containsKey(moduleName
)) {
491 return nativeMbd
.get(moduleName
);
493 String mbdFilename
= getMbdFilename(moduleName
);
494 File mbdFile
= new File(mbdFilename
);
495 if (!mbdFile
.exists()) {
497 //throw new BuildException("Info: Surface Area file [" + mbdFile.getPath() + "] can't found.");
499 SurfaceAreaParser surfaceAreaParser
= new SurfaceAreaParser();
500 Map
<String
, XmlObject
> map
= surfaceAreaParser
.parseFile(mbdFile
);
501 nativeMbd
.put(moduleName
, map
);
506 Register module overrided surface area information. If has existed, then update.
508 @param moduleName the base name of the module
509 @param map the overrided surface area information
511 public synchronized static void registerModule(String moduleName
, Map
<String
, XmlObject
> map
) {
512 parsedModules
.put(moduleName
, map
);
517 * @param protocolName
520 public synchronized static String
[] getProtocolInfoGuid(String protocolName
) {
521 Set set
= spdTable
.keySet();
522 Iterator iter
= set
.iterator();
523 String
[] cNameGuid
= null;
525 while (iter
.hasNext()) {
526 Spd spd
= (Spd
) spdTable
.get(iter
.next());
527 cNameGuid
= spd
.getProtocolNameGuidArray(protocolName
);
528 if (cNameGuid
!= null) {
535 public synchronized static String
[] getPpiInfoGuid(String ppiName
) {
536 Set set
= spdTable
.keySet();
537 Iterator iter
= set
.iterator();
538 String
[] cNameGuid
= null;
540 while (iter
.hasNext()) {
541 Spd spd
= (Spd
) spdTable
.get(iter
.next());
542 cNameGuid
= spd
.getPpiCnameGuidArray(ppiName
);
544 if (cNameGuid
!= null) {
556 public synchronized static String
[] getGuidInfoGuid(String guidName
) {
557 String
[] cNameGuid
= null;
558 Set set
= spdTable
.keySet();
559 Iterator iter
= set
.iterator();
561 while (iter
.hasNext()) {
562 Spd spd
= (Spd
) spdTable
.get(iter
.next());
563 cNameGuid
= spd
.getGuidNameArray(guidName
);
564 if (cNameGuid
!= null) {
571 public synchronized static String
getLibClassIncluder(String libName
) {
572 String libIncluder
= null;
573 Set set
= spdTable
.keySet();
574 Iterator iter
= set
.iterator();
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
);
585 packagePath
= packageName
;
587 if (libIncluder
!= null) {
588 libIncluder
= libIncluder
.replace('\\', File
.separatorChar
);
589 libIncluder
= libIncluder
.replace('/', File
.separatorChar
);
590 libIncluder
= packageName
+ File
.separatorChar
+ libIncluder
;
597 public synchronized static String
getModuleInfoByPackageName(String packageName
, String moduleType
) {
599 String includeFile
= null;
600 String includeStr
= "";
601 String cleanPath
= "";
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
);
613 if (cleanPath
.charAt(spd
.packagePath
.length() - 1) != File
.separatorChar
) {
614 cleanPath
= cleanPath
+ File
.separatorChar
;
616 includeStr
= includeStr
+ cleanPath
;
617 includeStr
= includeStr
+ includeFile
;
618 includeStr
= includeStr
+ ">\r\n";
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
;
629 libInstanceInfo
.put(libName
, libConsDes
);
632 public synchronized static boolean isHaveLibInstance(String libName
) {
633 return libInstanceInfo
.containsKey(libName
);
636 public synchronized static String
getLibInstanceConstructor(String libName
) {
637 String
[] libInstanceValue
;
638 libInstanceValue
= libInstanceInfo
.get(libName
);
639 if (libInstanceValue
!= null) {
640 return libInstanceValue
[0];
646 public synchronized static String
getLibInstanceDestructor(String libName
) {
647 String
[] libInstanceValue
;
648 libInstanceValue
= libInstanceInfo
.get(libName
);
649 if (libInstanceValue
!= null) {
650 return libInstanceValue
[1];
656 public synchronized static MemoryDatabaseManager
getPCDMemoryDBManager() {