2 PCDAutoGenAction class.
4 This class is to manage how to generate the PCD information into Autogen.c and
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 package org
.tianocore
.build
.pcd
.action
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
24 import java
.util
.UUID
;
25 import java
.util
.regex
.Matcher
;
26 import java
.util
.regex
.Pattern
;
28 import org
.apache
.xmlbeans
.XmlObject
;
29 import org
.tianocore
.build
.global
.GlobalData
;
30 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
31 import org
.tianocore
.build
.id
.ModuleIdentification
;
32 import org
.tianocore
.pcd
.entity
.MemoryDatabaseManager
;
33 import org
.tianocore
.pcd
.entity
.Token
;
34 import org
.tianocore
.pcd
.entity
.UsageInstance
;
35 import org
.tianocore
.pcd
.exception
.BuildActionException
;
36 import org
.tianocore
.pcd
.exception
.EntityException
;
37 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
38 import org
.tianocore
.pcd
.action
.BuildAction
;
39 import org
.tianocore
.pcd
.action
.ActionMessage
;
41 /** This class is to manage how to generate the PCD information into Autogen.c and
44 public class PCDAutoGenAction
extends BuildAction
{
46 /// The reference of DBManager in GlobalData class.
48 private MemoryDatabaseManager dbManager
;
50 /// The identification for a UsageInstance.
52 private UsageIdentification usageId
;
54 /// The arch of current module
58 /// Whether current autogen is for building library used by current module.
60 private boolean isBuildUsedLibrary
;
62 /// The generated string for header file.
64 private String hAutoGenString
;
66 /// The generated string for C code file.
68 private String cAutoGenString
;
70 /// The name array of <PcdCoded> in a module.
72 private String
[] pcdNameArrayInMsa
;
74 Set parameter moduleId
76 @param moduleName the module name parameter.
78 public void setUsageId(UsageIdentification usageId
) {
79 this.usageId
= usageId
;
83 set isBuildUsedLibrary parameter.
85 @param isBuildUsedLibrary
87 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
88 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
92 set pcdNameArrayInMsa parameter.
94 @param pcdNameArrayInMsa
96 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
97 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
101 Get the output of generated string for header file.
103 @return the string of header file for PCD
105 public String
OutputH() {
106 return hAutoGenString
;
110 Get the output of generated string for C Code file.
112 @return the string of C code file for PCD
114 public String
OutputC() {
115 return cAutoGenString
;
122 This function mainly initialize some member variable.
124 @param moduleId the identification for module
125 @param arch the architecture for module
126 @param isBuildUsedLibary Is the current module library.
127 @param pcdNameArrayInMsa the pcd name array got from MSA file.
129 public PCDAutoGenAction(ModuleIdentification moduleId
,
131 boolean isBuildUsedLibrary
,
132 String
[] pcdNameArrayInMsa
) {
133 UsageIdentification usageId
= new UsageIdentification(moduleId
.getName(),
135 moduleId
.getPackage().getName(),
136 moduleId
.getPackage().getGuid(),
138 moduleId
.getVersion(),
139 moduleId
.getModuleType());
145 setIsBuildUsedLibrary(isBuildUsedLibrary
);
146 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
150 check the parameter for action class.
152 @throws BuildActionException Bad parameter.
154 public void checkParameter() throws BuildActionException
{
159 Core execution function for this action class.
161 All PCD information of this module comes from memory dabase. The collection
162 work should be done before this action execution.
163 Currently, we should generated all PCD information(maybe all dynamic) as array
164 in Pei emulated driver for simulating PCD runtime database.
166 @throws BuildActionException Failed to execute this aciton class.
168 public void performAction() throws BuildActionException
{
169 ActionMessage
.debug(this,
170 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
172 // Check the PCD memory database manager is valid.
174 if(GlobalData
.getPCDMemoryDBManager() == null) {
175 throw new BuildActionException("Memory database has not been initlizated!");
178 dbManager
= GlobalData
.getPCDMemoryDBManager();
180 if(dbManager
.getDBSize() == 0) {
184 ActionMessage
.debug(this,
185 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens");
187 generateAutogenForModule();
191 Generate the autogen string for a common module.
193 All PCD information of this module comes from memory dabase. The collection
194 work should be done before this action execution.
196 private void generateAutogenForModule()
199 List
<UsageInstance
> usageInstanceArray
, usageContext
;
200 String
[] guidStringArray
= null;
201 String guidStringCName
= null;
202 String guidString
= null;
203 String moduleName
= usageId
.moduleName
;
204 UsageInstance usageInstance
= null;
205 boolean found
= false;
207 usageInstanceArray
= null;
208 if (!isBuildUsedLibrary
) {
209 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(usageId
);
210 dbManager
.UsageInstanceContext
= usageInstanceArray
;
211 dbManager
.CurrentModuleName
= moduleName
;
212 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
213 usageContext
= dbManager
.UsageInstanceContext
;
215 // For building library package, although all module are library, but PCD entries of
216 // these library should be used to autogen.
218 if (usageContext
== null) {
219 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(usageId
);
221 usageInstanceArray
= new ArrayList
<UsageInstance
>();
224 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
225 // <ModuleSA> in FPD file.
227 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
229 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
230 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
231 usageInstanceArray
.add(usageContext
.get(index2
));
239 // All library's PCD should instanted in module's <ModuleSA> who
240 // use this library instance. If not, give errors.
242 throw new BuildActionException (String
.format("[PCD Autogen Error] Module %s use library instance %s, the PCD %s " +
243 "is required by this library instance, but can not find " +
244 "it in the %s's <ModuleSA> in FPD file!",
245 dbManager
.CurrentModuleName
,
247 pcdNameArrayInMsa
[index
],
248 dbManager
.CurrentModuleName
255 if (usageInstanceArray
== null) {
260 // Generate all PCD entry for a module.
262 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
263 usageInstance
= usageInstanceArray
.get(index
);
265 // Before generate any PCD information into autogen.h/autogen.c for a module,
266 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
267 // PCD in this module the token, they are all reference to TokenSpaceGuid
270 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
271 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
272 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
273 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
274 guidString
= String
.format("{ 0x%s, 0x%s, 0x%s, {0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s}}",
278 (guidStringArray
[3].substring(0, 2)),
279 (guidStringArray
[3].substring(2, 4)),
280 (guidStringArray
[4].substring(0, 2)),
281 (guidStringArray
[4].substring(2, 4)),
282 (guidStringArray
[4].substring(4, 6)),
283 (guidStringArray
[4].substring(6, 8)),
284 (guidStringArray
[4].substring(8, 10)),
285 (guidStringArray
[4].substring(10, 12)));
287 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
288 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
290 // Find whether this guid array variable has been generated into autogen.c
291 // For different DyanmicEx pcd token who use same token space guid, the token space
292 // guid array should be only generated once.
294 if (!matcher
.find()) {
295 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n",
297 if (!isBuildUsedLibrary
) {
298 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
305 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
307 // For every PCD entry for this module(usage instance), autogen string would
310 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
311 cAutoGenString
+= usageInstance
.getCAutogenStr();
315 // Work around code, In furture following code should be modified that get
316 // these information from Uplevel Autogen tools.
318 if (moduleName
.equalsIgnoreCase("PcdPeim")) {
319 hAutoGenString
+= dbManager
.PcdPeimHString
;
320 cAutoGenString
+= dbManager
.PcdPeimCString
;
321 } else if (moduleName
.equalsIgnoreCase("PcdDxe")) {
322 hAutoGenString
+= dbManager
.PcdDxeHString
;
323 cAutoGenString
+= dbManager
.PcdDxeCString
;
330 @param argv paramter from command line
332 public static void main(String argv
[]) {
334 String WorkSpace
= "X:/edk2";
335 String logFilePath
= WorkSpace
+ "/EdkNt32Pkg/Nt32.fpd";
336 String
[] nameArray
= null;
339 // At first, CollectPCDAction should be invoked to collect
340 // all PCD information from SPD, MSA, FPD.
342 CollectPCDAction collectionAction
= new CollectPCDAction();
343 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",
347 collectionAction
.perform(WorkSpace
,
349 ActionMessage
.MAX_MESSAGE_LEVEL
);
350 } catch(Exception e
) {