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
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.regex
.Matcher
;
22 import java
.util
.regex
.Pattern
;
24 import org
.tianocore
.build
.autogen
.CommonDefinition
;
25 import org
.tianocore
.build
.global
.GlobalData
;
26 import org
.tianocore
.build
.id
.ModuleIdentification
;
27 import org
.tianocore
.pcd
.action
.ActionMessage
;
28 import org
.tianocore
.pcd
.action
.BuildAction
;
29 import org
.tianocore
.pcd
.entity
.MemoryDatabaseManager
;
30 import org
.tianocore
.pcd
.entity
.Token
;
31 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
32 import org
.tianocore
.pcd
.entity
.UsageInstance
;
33 import org
.tianocore
.pcd
.exception
.BuildActionException
;
36 This class is to manage how to generate the PCD information into Autogen.c
39 public class PCDAutoGenAction
extends BuildAction
{
41 /// The reference of DBManager in GlobalData class.
43 private MemoryDatabaseManager dbManager
;
46 /// The identification for a UsageInstance.
48 private UsageIdentification usageId
;
51 /// Whether current autogen is for building library used by current module.
53 private boolean isBuildUsedLibrary
;
56 /// One of PEI_PCD_DRIVER, DXE_PCD_DRIVER, NOT_PCD_DRIVER
58 private CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
;
61 /// The generated string for header file.
63 private String hAutoGenString
;
66 /// The generated string for C code file.
68 private String cAutoGenString
;
71 /// The name array of <PcdCoded> in a module.
73 private String
[] pcdNameArrayInMsa
;
76 Set parameter moduleId
78 @param moduleName the module name parameter.
80 public void setUsageId(UsageIdentification usageId
) {
81 this.usageId
= usageId
;
85 Set paramter pcdDriverType
87 @param pcdDriverType the driver type for PCD
89 public void setPcdDriverType(CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
) {
90 this.pcdDriverType
= pcdDriverType
;
93 set isBuildUsedLibrary parameter.
95 @param isBuildUsedLibrary
97 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
98 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
102 set pcdNameArrayInMsa parameter.
104 @param pcdNameArrayInMsa
106 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
107 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
111 Get the output of generated string for header file.
113 @return the string of header file for PCD
115 public String
getHAutoGenString() {
116 return hAutoGenString
;
120 Get the output of generated string for C Code file.
122 @return the string of C code file for PCD
124 public String
getCAutoGenString() {
125 return cAutoGenString
;
132 This function mainly initialize some member variable.
134 @param moduleId the identification for module
135 @param arch the architecture for module
136 @param isBuildUsedLibary Is the current module library.
137 @param pcdNameArrayInMsa the pcd name array got from MSA file.
138 @param pcdDriverType one of PEI_PCD_DRIVER, DXE_PCD_DRIVER,
141 public PCDAutoGenAction(ModuleIdentification moduleId
,
143 boolean isBuildUsedLibrary
,
144 String
[] pcdNameArrayInMsa
,
145 CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
) {
150 setUsageId(new UsageIdentification(moduleId
.getName(),
152 moduleId
.getPackage().getName(),
153 moduleId
.getPackage().getGuid(),
155 moduleId
.getVersion(),
156 moduleId
.getModuleType()));
157 setIsBuildUsedLibrary(isBuildUsedLibrary
);
158 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
159 setPcdDriverType(pcdDriverType
);
163 Override function: check the parameter for action class.
165 @throws BuildActionException Bad parameter.
167 public void checkParameter() {
171 Core execution function for this action class.
173 All PCD information of this module comes from memory dabase. The collection
174 work should be done before this action execution.
175 Currently, we should generated all PCD information(maybe all dynamic) as array
176 in Pei emulated driver for simulating PCD runtime database.
178 @throws BuildActionException Failed to execute this aciton class.
180 public void performAction() {
181 ActionMessage
.debug(this,
182 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
184 dbManager
= GlobalData
.getPCDMemoryDBManager();
186 if(dbManager
.getDBSize() == 0) {
190 ActionMessage
.debug(this,
191 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens.");
193 generateAutogenForModule();
197 Generate the autogen string for a common module.
199 All PCD information of this module comes from memory dabase. The collection
200 work should be done before this action execution.
202 private void generateAutogenForModule()
205 List
<UsageInstance
> usageInstanceArray
, usageContext
;
206 String
[] guidStringArray
= null;
207 String guidStringCName
= null;
208 String guidString
= null;
209 String moduleName
= usageId
.moduleName
;
210 UsageInstance usageInstance
= null;
211 boolean found
= false;
213 usageInstanceArray
= null;
214 if (!isBuildUsedLibrary
) {
215 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
216 MemoryDatabaseManager
.UsageInstanceContext
= usageInstanceArray
;
217 MemoryDatabaseManager
.CurrentModuleName
= moduleName
;
218 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
219 usageContext
= MemoryDatabaseManager
.UsageInstanceContext
;
221 // For building library package, although all module are library, but PCD entries of
222 // these library should be used to autogen.
224 if (usageContext
== null) {
225 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
227 usageInstanceArray
= new ArrayList
<UsageInstance
>();
230 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
231 // <ModuleSA> in FPD file.
233 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
235 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
236 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
237 usageInstanceArray
.add(usageContext
.get(index2
));
245 // All library's PCD should instanted in module's <ModuleSA> who
246 // use this library instance. If not, give errors.
248 throw new BuildActionException (String
.format("Module %s using library instance %s; the PCD %s " +
249 "is required by this library instance, but can not be found " +
250 "in the %s's <ModuleSA> in the FPD file!",
251 MemoryDatabaseManager
.CurrentModuleName
,
253 pcdNameArrayInMsa
[index
],
254 MemoryDatabaseManager
.CurrentModuleName
261 if (usageInstanceArray
== null) {
266 // Generate all PCD entry for a module.
268 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
269 usageInstance
= usageInstanceArray
.get(index
);
271 // Before generate any PCD information into autogen.h/autogen.c for a module,
272 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
273 // PCD in this module the token, they are all reference to TokenSpaceGuid
276 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
277 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
278 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
279 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
280 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}}",
284 (guidStringArray
[3].substring(0, 2)),
285 (guidStringArray
[3].substring(2, 4)),
286 (guidStringArray
[4].substring(0, 2)),
287 (guidStringArray
[4].substring(2, 4)),
288 (guidStringArray
[4].substring(4, 6)),
289 (guidStringArray
[4].substring(6, 8)),
290 (guidStringArray
[4].substring(8, 10)),
291 (guidStringArray
[4].substring(10, 12)));
293 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
294 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
296 // Find whether this guid array variable has been generated into autogen.c
297 // For different DyanmicEx pcd token who use same token space guid, the token space
298 // guid array should be only generated once.
300 if (!matcher
.find()) {
301 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n", guidStringCName
);
302 if (!isBuildUsedLibrary
) {
303 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
310 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
312 // For every PCD entry for this module(usage instance), autogen string would
315 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
316 cAutoGenString
+= usageInstance
.getCAutogenStr();
319 if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.PEI_PCD_DRIVER
) {
320 hAutoGenString
+= MemoryDatabaseManager
.PcdPeimHString
;
321 cAutoGenString
+= MemoryDatabaseManager
.PcdPeimCString
;
322 } else if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.DXE_PCD_DRIVER
) {
323 hAutoGenString
+= MemoryDatabaseManager
.PcdDxeHString
;
324 cAutoGenString
+= MemoryDatabaseManager
.PcdDxeCString
;