2 CollectPCDAction class.
4 This action class is to collect PCD information from MSA, SPD, FPD xml file.
5 This class will be used for wizard and build tools, So it can *not* inherit
6 from buildAction or wizardAction.
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.
18 package org
.tianocore
.build
.pcd
.action
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.HashMap
;
24 import java
.util
.List
;
26 import java
.util
.UUID
;
28 import org
.apache
.xmlbeans
.XmlException
;
29 import org
.apache
.xmlbeans
.XmlObject
;
30 import org
.tianocore
.FrameworkPlatformDescriptionDocument
;
31 import org
.tianocore
.ModuleSADocument
;
32 import org
.tianocore
.PackageSurfaceAreaDocument
;
33 import org
.tianocore
.PcdBuildDeclarationsDocument
.PcdBuildDeclarations
.PcdBuildData
;
34 import org
.tianocore
.PcdDefinitionsDocument
.PcdDefinitions
;
35 import org
.tianocore
.build
.autogen
.CommonDefinition
;
36 import org
.tianocore
.build
.global
.GlobalData
;
37 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
38 import org
.tianocore
.build
.pcd
.action
.ActionMessage
;
39 import org
.tianocore
.build
.pcd
.entity
.MemoryDatabaseManager
;
40 import org
.tianocore
.build
.pcd
.entity
.SkuInstance
;
41 import org
.tianocore
.build
.pcd
.entity
.Token
;
42 import org
.tianocore
.build
.pcd
.entity
.UsageInstance
;
43 import org
.tianocore
.build
.pcd
.exception
.EntityException
;
45 /** This action class is to collect PCD information from MSA, SPD, FPD xml file.
46 This class will be used for wizard and build tools, So it can *not* inherit
47 from buildAction or UIAction.
49 public class CollectPCDAction
{
50 /// memoryDatabase hold all PCD information collected from SPD, MSA, FPD.
51 private MemoryDatabaseManager dbManager
;
53 /// Workspacepath hold the workspace information.
54 private String workspacePath
;
56 /// FPD file is the root file.
57 private String fpdFilePath
;
59 /// Message level for CollectPCDAction.
60 private int originalMessageLevel
;
63 Set WorkspacePath parameter for this action class.
65 @param workspacePath parameter for this action
67 public void setWorkspacePath(String workspacePath
) {
68 this.workspacePath
= workspacePath
;
72 Set action message level for CollectPcdAction tool.
74 The message should be restored when this action exit.
76 @param actionMessageLevel parameter for this action
78 public void setActionMessageLevel(int actionMessageLevel
) {
79 originalMessageLevel
= ActionMessage
.messageLevel
;
80 ActionMessage
.messageLevel
= actionMessageLevel
;
84 Set FPDFileName parameter for this action class.
86 @param fpdFilePath fpd file path
88 public void setFPDFilePath(String fpdFilePath
) {
89 this.fpdFilePath
= fpdFilePath
;
93 Common function interface for outer.
95 @param workspacePath The path of workspace of current build or analysis.
96 @param fpdFilePath The fpd file path of current build or analysis.
97 @param messageLevel The message level for this Action.
99 @throws Exception The exception of this function. Because it can *not* be predict
100 where the action class will be used. So only Exception can be throw.
103 public void perform(String workspacePath
, String fpdFilePath
,
104 int messageLevel
) throws Exception
{
105 setWorkspacePath(workspacePath
);
106 setFPDFilePath(fpdFilePath
);
107 setActionMessageLevel(messageLevel
);
110 ActionMessage
.messageLevel
= originalMessageLevel
;
114 Core execution function for this action class.
116 This function work flows will be:
117 1) Get all token's platform information from FPD, and create token object into memory database.
118 2) Get all token's module information from MSA, and create usage instance for every module's PCD entry.
119 3) Get all token's inherited information from MSA's library, and create usage instance
120 for module who consume this library and create usage instance for library for building.
121 4) Collect token's package information from SPD, update these information for token in memory
124 @throws EntityException Exception indicate failed to execute this action.
127 private void execute() throws EntityException
{
128 FrameworkPlatformDescriptionDocument fpdDoc
= null;
129 Object
[][] modulePCDArray
= null;
130 Map
<String
, XmlObject
> docMap
= null;
131 ModuleSADocument
.ModuleSA
[] moduleSAs
= null;
132 UsageInstance usageInstance
= null;
133 String packageName
= null;
134 String packageFullPath
= null;
136 int libraryIndex
= 0;
137 int pcdArrayIndex
= 0;
138 List
<String
> listLibraryInstance
= null;
139 String componentTypeStr
= null;
142 // Collect all PCD information defined in FPD file.
143 // Evenry token defind in FPD will be created as an token into
146 fpdDoc
= createTokenInDBFromFPD();
149 // Searching MSA and SPD document.
150 // The information of MSA will be used to create usage instance into database.
151 // The information of SPD will be used to update the token information in database.
154 HashMap
<String
, XmlObject
> map
= new HashMap
<String
, XmlObject
>();
155 map
.put("FrameworkPlatformDescription", fpdDoc
);
156 SurfaceAreaQuery
.setDoc(map
);
158 moduleSAs
= SurfaceAreaQuery
.getFpdModules();
159 for(index
= 0; index
< moduleSAs
.length
; index
++) {
161 // Get module document and use SurfaceAreaQuery to get PCD information
163 docMap
= GlobalData
.getDoc(moduleSAs
[index
].getModuleName());
164 SurfaceAreaQuery
.setDoc(docMap
);
165 modulePCDArray
= SurfaceAreaQuery
.getModulePCDTokenArray();
166 componentTypeStr
= SurfaceAreaQuery
.getComponentType();
168 GlobalData
.getPackageNameForModule(moduleSAs
[index
].getModuleName());
169 packageFullPath
= this.workspacePath
+ File
.separator
+
170 GlobalData
.getPackagePath(packageName
) +
171 packageName
+ ".spd";
173 if(modulePCDArray
!= null) {
175 // If current MSA contains <PCDs> information, then create usage
176 // instance for PCD information from MSA
178 for(pcdArrayIndex
= 0; pcdArrayIndex
< modulePCDArray
.length
;
181 createUsageInstanceFromMSA(moduleSAs
[index
].getModuleName(),
182 modulePCDArray
[pcdArrayIndex
]);
184 if(usageInstance
== null) {
188 // Get remaining PCD information from the package which this module belongs to
190 updateTokenBySPD(usageInstance
, packageFullPath
);
195 // Get inherit PCD information which inherit from library instance of this module.
197 listLibraryInstance
=
198 SurfaceAreaQuery
.getLibraryInstance(moduleSAs
[index
].getArch().toString(),
199 CommonDefinition
.AlwaysConsumed
);
200 if(listLibraryInstance
!= null) {
201 for(libraryIndex
= 0; libraryIndex
< listLibraryInstance
.size();
203 inheritPCDFromLibraryInstance(listLibraryInstance
.get(libraryIndex
),
204 moduleSAs
[index
].getModuleName(),
213 This function will collect inherit PCD information from library for a module.
215 This function will create two usage instance for inherited PCD token, one is
216 for module and another is for library.
217 For module, if it inherited a PCD token from library, this PCD token's value
218 should be instanced in module level, and belongs to module.
219 For library, it also need a usage instance for build.
221 @param libraryName The name of library instance.
222 @param moduleName The name of module.
223 @param packageName The name of package while module belongs to.
224 @param parentcomponentType The component type of module.
226 @throws EntityException If the token does *not* exist in memory database.
229 private void inheritPCDFromLibraryInstance(String libraryName
,
232 String parentcomponentType
)
233 throws EntityException
{
234 Map
<String
, XmlObject
> docMap
= null;
235 String primaryKeyString
= null;
236 Object
[][] libPcdDataArray
= null;
237 UUID nullUUID
= new UUID(0,0);
238 UUID platformUUID
= nullUUID
;
239 UUID tokenSpaceGuid
= null;
242 Token
.PCD_TYPE pcdType
= Token
.PCD_TYPE
.UNKNOWN
;
243 UsageInstance usageInstance
= null;
244 String packageFullPath
= null;
247 // Query PCD information from library's document.
249 docMap
= GlobalData
.getDoc(libraryName
);
250 SurfaceAreaQuery
.setDoc(docMap
);
251 libPcdDataArray
= SurfaceAreaQuery
.getModulePCDTokenArray();
253 if(libPcdDataArray
== null) {
257 for(tokenIndex
= 0; tokenIndex
< libPcdDataArray
.length
; tokenIndex
++) {
258 tokenSpaceGuid
=((UUID
)libPcdDataArray
[tokenIndex
][2] == null) ?
259 nullUUID
:(UUID
)libPcdDataArray
[tokenIndex
][2];
262 // Get token from memory database. The token must be created from FPD already.
264 primaryKeyString
= Token
.getPrimaryKeyString((String
)libPcdDataArray
[tokenIndex
][0],
269 if(dbManager
.isTokenInDatabase(primaryKeyString
)) {
270 token
= dbManager
.getTokenByKey(primaryKeyString
);
272 throw new EntityException("The PCD token " + primaryKeyString
+
273 " defined in module " + moduleName
+
274 " does not exist in FPD file!");
278 // Create usage instance for module.
280 pcdType
= Token
.getpcdTypeFromString((String
)libPcdDataArray
[tokenIndex
][1]);
281 usageInstance
= new UsageInstance(token
,
282 Token
.PCD_USAGE
.ALWAYS_CONSUMED
,
284 CommonDefinition
.getComponentType(parentcomponentType
),
285 libPcdDataArray
[tokenIndex
][3],
287 (String
) libPcdDataArray
[tokenIndex
][5],
292 if(Token
.PCD_USAGE
.UNKNOWN
== token
.isUsageInstanceExist(moduleName
)) {
293 token
.addUsageInstance(usageInstance
);
295 packageFullPath
= this.workspacePath
+ File
.separator
+
296 GlobalData
.getPackagePath(packageName
) +
297 packageName
+ ".spd";
298 updateTokenBySPD(usageInstance
, packageFullPath
);
302 // We need create second usage instance for inherited case, which
303 // add library as an usage instance, because when build a module, and
304 // if module inherited from base library, then build process will build
307 if(Token
.PCD_USAGE
.UNKNOWN
== token
.isUsageInstanceExist(libraryName
)) {
308 packageName
= GlobalData
.getPackageNameForModule(libraryName
);
309 usageInstance
= new UsageInstance(token
,
310 Token
.PCD_USAGE
.ALWAYS_CONSUMED
,
312 CommonDefinition
.ComponentTypeLibrary
,
313 libPcdDataArray
[tokenIndex
][3],
315 (String
)libPcdDataArray
[tokenIndex
][5],
320 token
.addUsageInstance(usageInstance
);
326 Create usage instance for PCD token defined in MSA document
328 A PCD token maybe used by many modules, and every module is one of usage
329 instance of this token. For ALWAY_CONSUMED, SOMETIMES_CONSUMED, it is
330 consumer type usage instance of this token, and for ALWAYS_PRODUCED,
331 SOMETIMES_PRODUCED, it is produce type usage instance.
333 @param moduleName The name of module
334 @param tokenInfoInMsa The PCD token information array retrieved from MSA.
336 @return UsageInstance The usage instance created in memroy database.
338 @throws EntityException If token did not exist in database yet.
341 private UsageInstance
createUsageInstanceFromMSA(String moduleName
,
342 Object
[] tokenInfoInMsa
)
343 throws EntityException
{
344 String packageName
= null;
345 UsageInstance usageInstance
= null;
346 UUID tokenSpaceGuid
= null;
347 UUID nullUUID
= new UUID(0,0);
348 String primaryKeyString
= null;
349 UUID platformTokenSpace
= nullUUID
;
351 Token
.PCD_TYPE pcdType
= Token
.PCD_TYPE
.UNKNOWN
;
352 Token
.PCD_USAGE pcdUsage
= Token
.PCD_USAGE
.UNKNOWN
;
354 tokenSpaceGuid
=((UUID
)tokenInfoInMsa
[2] == null) ? nullUUID
:(UUID
)tokenInfoInMsa
[2];
356 primaryKeyString
= Token
.getPrimaryKeyString((String
)tokenInfoInMsa
[0],
361 // Get token object from memory database firstly.
363 if(dbManager
.isTokenInDatabase(primaryKeyString
)) {
364 token
= dbManager
.getTokenByKey(primaryKeyString
);
366 throw new EntityException("The PCD token " + primaryKeyString
+ " defined in module " +
367 moduleName
+ " does not exist in FPD file!" );
369 pcdType
= Token
.getpcdTypeFromString((String
)tokenInfoInMsa
[1]);
370 pcdUsage
= Token
.getUsageFromString((String
)tokenInfoInMsa
[4]);
372 packageName
= GlobalData
.getPackageNameForModule(moduleName
);
374 if(Token
.PCD_USAGE
.UNKNOWN
!= token
.isUsageInstanceExist(moduleName
)) {
376 // BUGBUG: It should *not* throw exception here. Becaues in MdePkg.fpd,
377 // more than on BaseLib exist. But why? need confirmation.
379 //throw new EntityException(
380 // "In module " + moduleName + " exist more than one PCD token " + token.cName
382 ActionMessage
.warning(this,
383 "In module " + moduleName
+ " exist more than one PCD token " + token
.cName
389 // BUGBUG: following code could be enabled at current schema. Because
390 // current schema does not provide usage information.
392 // For FEATRURE_FLAG, FIXED_AT_BUILD, PATCH_IN_MODULE type PCD token, his
393 // usage is always ALWAYS_CONSUMED
395 //if((pcdType != Token.PCD_TYPE.DYNAMIC) &&
396 // (pcdType != Token.PCD_TYPE.DYNAMIC_EX)) {
397 pcdUsage
= Token
.PCD_USAGE
.ALWAYS_CONSUMED
;
400 usageInstance
= new UsageInstance(token
,
403 CommonDefinition
.getComponentType(SurfaceAreaQuery
.getComponentType()),
406 (String
) tokenInfoInMsa
[5],
413 // Use default value defined in MSA to update datum of token,
414 // if datum of token does not defined in FPD file.
416 if((token
.datum
== null) &&(tokenInfoInMsa
[3] != null)) {
417 token
.datum
= tokenInfoInMsa
[3];
420 token
.addUsageInstance(usageInstance
);
422 return usageInstance
;
426 Create token instance object into memory database, the token information
427 comes for FPD file. Normally, FPD file will contain all token platform
430 This fucntion should be executed at firsly before others collection work
431 such as searching token information from MSA, SPD.
433 @return FrameworkPlatformDescriptionDocument The FPD document instance for furture usage.
435 @throws EntityException Failed to parse FPD xml file.
438 private FrameworkPlatformDescriptionDocument
createTokenInDBFromFPD()
439 throws EntityException
{
440 XmlObject doc
= null;
441 FrameworkPlatformDescriptionDocument fpdDoc
= null;
443 List
<PcdBuildData
> pcdBuildDataArray
= new ArrayList
<PcdBuildData
>();
444 PcdBuildData pcdBuildData
= null;
446 UUID nullUUID
= new UUID(0,0);
447 UUID platformTokenSpace
= nullUUID
;
448 List skuDataArray
= new ArrayList();
449 SkuInstance skuInstance
= null;
453 // Get all tokens from FPD file and create token into database.
457 doc
= XmlObject
.Factory
.parse(new File(fpdFilePath
));
458 } catch(IOException ioE
) {
459 throw new EntityException("Can't find the FPD xml fle:" + fpdFilePath
);
460 } catch(XmlException xmlE
) {
461 throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath
);
465 // Get memoryDatabaseManager instance from GlobalData.
467 if((dbManager
= GlobalData
.getPCDMemoryDBManager()) == null) {
468 throw new EntityException("The instance of PCD memory database manager is null");
471 dbManager
= new MemoryDatabaseManager();
473 if(!(doc
instanceof FrameworkPlatformDescriptionDocument
)) {
474 throw new EntityException("File " + fpdFilePath
+
475 " is not a FrameworkPlatformDescriptionDocument");
478 fpdDoc
=(FrameworkPlatformDescriptionDocument
)doc
;
481 // Add all tokens in FPD into Memory Database.
484 fpdDoc
.getFrameworkPlatformDescription().getPcdBuildDeclarations().getPcdBuildDataList();
486 index
< fpdDoc
.getFrameworkPlatformDescription().getPcdBuildDeclarations().sizeOfPcdBuildDataArray();
488 pcdBuildData
= pcdBuildDataArray
.get(index
);
489 token
= new Token(pcdBuildData
.getCName(), new UUID(0, 0), new UUID(0, 0));
491 // BUGBUG: in FPD, <defaultValue> should be defined as <Value>
493 token
.datum
= pcdBuildData
.getDefaultValue();
494 token
.hiiEnabled
= pcdBuildData
.getHiiEnable();
495 token
.variableGuid
= Token
.getGUIDFromSchemaObject(pcdBuildData
.getVariableGuid());
496 token
.variableName
= pcdBuildData
.getVariableName();
497 token
.variableOffset
= Integer
.decode(pcdBuildData
.getDataOffset());
498 token
.skuEnabled
= pcdBuildData
.getSkuEnable();
499 token
.maxSkuCount
= Integer
.decode(pcdBuildData
.getMaxSku());
500 token
.skuId
= Integer
.decode(pcdBuildData
.getSkuId());
501 token
.skuDataArrayEnabled
= pcdBuildData
.getSkuDataArrayEnable();
502 token
.assignedtokenNumber
= Integer
.decode(pcdBuildData
.getToken().getStringValue());
503 skuDataArray
= pcdBuildData
.getSkuDataArray1();
504 token
.datumType
= Token
.getdatumTypeFromString(pcdBuildData
.getDatumType().toString());
506 if(skuDataArray
!= null) {
507 for(skuIndex
= 0; skuIndex
< skuDataArray
.size(); skuIndex
++) {
509 // BUGBUG: Now in current schema, The value is defined as String type,
510 // it is not correct, the type should be same as the datumType
512 skuInstance
= new SkuInstance(((PcdBuildData
.SkuData
)skuDataArray
.get(skuIndex
)).getId(),
513 ((PcdBuildData
.SkuData
)skuDataArray
.get(skuIndex
)).getValue());
514 token
.skuData
.add(skuInstance
);
518 if(dbManager
.isTokenInDatabase(Token
.getPrimaryKeyString(token
.cName
,
519 token
.tokenSpaceName
,
520 platformTokenSpace
))) {
522 // If found duplicate token, Should tool be hold?
524 ActionMessage
.warning(this,
525 "Token " + token
.cName
+ " exists in token database");
528 token
.pcdType
= Token
.getpcdTypeFromString(pcdBuildData
.getItemType().toString());
529 dbManager
.addTokenToDatabase(Token
.getPrimaryKeyString(token
.cName
,
530 token
.tokenSpaceName
,
539 Update PCD token in memory database by help information in SPD.
541 After create token from FPD and create usage instance from MSA, we should collect
542 PCD package level information from SPD and update token information in memory
545 @param usageInstance The usage instance defined in MSA and want to search in SPD.
546 @param packageFullPath The SPD file path.
548 @throws EntityException Failed to parse SPD xml file.
551 private void updateTokenBySPD(UsageInstance usageInstance
,
552 String packageFullPath
)
553 throws EntityException
{
554 PackageSurfaceAreaDocument pkgDoc
= null;
555 PcdDefinitions pcdDefinitions
= null;
556 List
<PcdDefinitions
.PcdEntry
> pcdEntryArray
= new ArrayList
<PcdDefinitions
.PcdEntry
>();
558 boolean isFoundInSpd
= false;
559 Token
.DATUM_TYPE datumType
= Token
.DATUM_TYPE
.UNKNOWN
;
562 pkgDoc
=(PackageSurfaceAreaDocument
)XmlObject
.Factory
.parse(new File(packageFullPath
));
563 } catch(IOException ioE
) {
564 throw new EntityException("Can't find the FPD xml fle:" + packageFullPath
);
565 } catch(XmlException xmlE
) {
566 throw new EntityException("Can't parse the FPD xml fle:" + packageFullPath
);
568 pcdDefinitions
= pkgDoc
.getPackageSurfaceArea().getPcdDefinitions();
570 // It is illege for SPD file does not contains any PCD information.
572 if (pcdDefinitions
== null) {
576 pcdEntryArray
= pcdDefinitions
.getPcdEntryList();
577 if (pcdEntryArray
== null) {
580 for(index
= 0; index
< pcdEntryArray
.size(); index
++) {
581 if(pcdEntryArray
.get(index
).getCName().equalsIgnoreCase(
582 usageInstance
.parentToken
.cName
)) {
585 // From SPD file , we can get following information.
586 // Token: Token number defined in package level.
587 // PcdItemType: This item does not single one. It means all supported item type.
588 // datumType: UINT8, UNIT16, UNIT32, UINT64, VOID*, BOOLEAN
589 // datumSize: The size of default value or maxmine size.
590 // defaultValue: This value is defined in package level.
591 // HelpText: The help text is provided in package level.
594 usageInstance
.parentToken
.tokenNumber
= Integer
.decode(pcdEntryArray
.get(index
).getToken());
596 if(pcdEntryArray
.get(index
).getDatumType() != null) {
597 datumType
= Token
.getdatumTypeFromString(
598 pcdEntryArray
.get(index
).getDatumType().toString());
599 if(usageInstance
.parentToken
.datumType
== Token
.DATUM_TYPE
.UNKNOWN
) {
600 usageInstance
.parentToken
.datumType
= datumType
;
602 if(datumType
!= usageInstance
.parentToken
.datumType
) {
603 throw new EntityException("Different datum types are defined for Token :" +
604 usageInstance
.parentToken
.cName
);
609 throw new EntityException("The datum type for token " + usageInstance
.parentToken
.cName
+
610 " is not defind in SPD file " + packageFullPath
);
613 usageInstance
.defaultValueInSPD
= pcdEntryArray
.get(index
).getDefaultValue();
614 usageInstance
.helpTextInSPD
= "Help Text in SPD";
617 // If token's datum is not valid, it indicate that datum is not provided
618 // in FPD and defaultValue is not provided in MSA, then use defaultValue
619 // in SPD as the datum of token.
621 if(usageInstance
.parentToken
.datum
== null) {
622 if(pcdEntryArray
.get(index
).getDefaultValue() != null) {
623 usageInstance
.parentToken
.datum
= pcdEntryArray
.get(index
).getDefaultValue();
625 throw new EntityException("FPD does not provide datum for token " + usageInstance
.parentToken
.cName
+
626 ", MSA and SPD also does not provide <defaultValue> for this token!");
634 check parameter for this action.
636 @throws EntityException Bad parameter.
638 private void checkParameter() throws EntityException
{
641 if((fpdFilePath
== null) ||(workspacePath
== null)) {
642 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
645 if(fpdFilePath
.length() == 0 || workspacePath
.length() == 0) {
646 throw new EntityException("WorkspacePath and FPDFileName should be blank for CollectPCDAtion!");
649 file
= new File(workspacePath
);
651 throw new EntityException("WorkpacePath " + workspacePath
+ " does not exist!");
654 file
= new File(fpdFilePath
);
657 throw new EntityException("FPD File " + fpdFilePath
+ " does not exist!");
664 @param argv parameter from command line
666 public static void main(String argv
[]) throws EntityException
{
667 CollectPCDAction ca
= new CollectPCDAction();
668 ca
.setWorkspacePath("G:/mdk");
669 ca
.setFPDFilePath("G:/mdk/EdkNt32Pkg/build/Nt32.fpd");
670 ca
.setActionMessageLevel(ActionMessage
.MAX_MESSAGE_LEVEL
);
671 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",