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
;
22 import java
.util
.regex
.Matcher
;
23 import java
.util
.regex
.Pattern
;
25 import org
.tianocore
.build
.autogen
.CommonDefinition
;
26 import org
.tianocore
.build
.exception
.PcdAutogenException
;
27 import org
.tianocore
.build
.global
.GlobalData
;
28 import org
.tianocore
.build
.id
.ModuleIdentification
;
29 import org
.tianocore
.pcd
.action
.ActionMessage
;
30 import org
.tianocore
.pcd
.action
.BuildAction
;
31 import org
.tianocore
.pcd
.entity
.MemoryDatabaseManager
;
32 import org
.tianocore
.pcd
.entity
.Token
;
33 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
34 import org
.tianocore
.pcd
.entity
.UsageInstance
;
35 import org
.tianocore
.pcd
.exception
.BuildActionException
;
38 This class is to manage how to generate the PCD information into Autogen.c
41 public class PCDAutoGenAction
extends BuildAction
{
43 /// The reference of DBManager in GlobalData class.
45 private MemoryDatabaseManager dbManager
;
48 /// The identification for a UsageInstance.
50 private UsageIdentification usageId
;
53 /// Whether current autogen is for building library used by current module.
55 private boolean isBuildUsedLibrary
;
58 /// One of PEI_PCD_DRIVER, DXE_PCD_DRIVER, NOT_PCD_DRIVER
60 private CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
;
63 /// The generated string for header file.
65 private String hAutoGenString
;
68 /// The generated string for C code file.
70 private String cAutoGenString
;
73 /// The name array of <PcdCoded> in a module.
75 private String
[] pcdNameArrayInMsa
;
78 Set parameter moduleId
80 @param moduleName the module name parameter.
82 public void setUsageId(UsageIdentification usageId
) {
83 this.usageId
= usageId
;
87 Set paramter pcdDriverType
89 @param pcdDriverType the driver type for PCD
91 public void setPcdDriverType(CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
) {
92 this.pcdDriverType
= pcdDriverType
;
95 set isBuildUsedLibrary parameter.
97 @param isBuildUsedLibrary
99 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
100 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
104 set pcdNameArrayInMsa parameter.
106 @param pcdNameArrayInMsa
108 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
109 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
113 Get the output of generated string for header file.
115 @return the string of header file for PCD
117 public String
getHAutoGenString() {
118 return hAutoGenString
;
122 Get the output of generated string for C Code file.
124 @return the string of C code file for PCD
126 public String
getCAutoGenString() {
127 return cAutoGenString
;
134 This function mainly initialize some member variable.
136 @param moduleId the identification for module
137 @param arch the architecture for module
138 @param isBuildUsedLibary Is the current module library.
139 @param pcdNameArrayInMsa the pcd name array got from MSA file.
140 @param pcdDriverType one of PEI_PCD_DRIVER, DXE_PCD_DRIVER,
143 public PCDAutoGenAction(ModuleIdentification moduleId
,
145 boolean isBuildUsedLibrary
,
146 String
[] pcdNameArrayInMsa
,
147 CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
) {
152 setUsageId(new UsageIdentification(moduleId
.getName(),
154 moduleId
.getPackage().getName(),
155 moduleId
.getPackage().getGuid(),
157 moduleId
.getVersion(),
158 moduleId
.getModuleType()));
159 setIsBuildUsedLibrary(isBuildUsedLibrary
);
160 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
161 setPcdDriverType(pcdDriverType
);
165 Override function: check the parameter for action class.
167 @throws BuildActionException Bad parameter.
169 public void checkParameter() {
173 Core execution function for this action class.
175 All PCD information of this module comes from memory dabase. The collection
176 work should be done before this action execution.
177 Currently, we should generated all PCD information(maybe all dynamic) as array
178 in Pei emulated driver for simulating PCD runtime database.
180 @throws BuildActionException Failed to execute this aciton class.
182 public void performAction() {
183 ActionMessage
.debug(this,
184 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
186 dbManager
= GlobalData
.getPCDMemoryDBManager();
188 if(dbManager
.getDBSize() == 0) {
192 ActionMessage
.debug(this,
193 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens.");
195 generateAutogenForModule();
199 Generate the autogen string for a common module.
201 All PCD information of this module comes from memory dabase. The collection
202 work should be done before this action execution.
204 private void generateAutogenForModule()
207 List
<UsageInstance
> usageInstanceArray
, usageContext
;
208 String
[] guidStringArray
= null;
209 String guidStringCName
= null;
210 String guidString
= null;
211 String moduleName
= usageId
.moduleName
;
212 UsageInstance usageInstance
= null;
213 boolean found
= false;
215 usageInstanceArray
= null;
216 if (!isBuildUsedLibrary
) {
217 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
218 MemoryDatabaseManager
.UsageInstanceContext
= usageInstanceArray
;
219 MemoryDatabaseManager
.CurrentModuleName
= moduleName
;
220 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
221 usageContext
= MemoryDatabaseManager
.UsageInstanceContext
;
223 // For building library package, although all module are library, but PCD entries of
224 // these library should be used to autogen.
226 if (usageContext
== null) {
227 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
229 usageInstanceArray
= new ArrayList
<UsageInstance
>();
232 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
233 // <ModuleSA> in FPD file.
235 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
237 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
238 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
239 usageInstanceArray
.add(usageContext
.get(index2
));
247 // All library's PCD should instanted in module's <ModuleSA> who
248 // use this library instance. If not, give errors.
250 throw new BuildActionException (String
.format("Module %s using library instance %s; the PCD %s " +
251 "is required by this library instance, but can not be found " +
252 "in the %s's <ModuleSA> in the FPD file!",
253 MemoryDatabaseManager
.CurrentModuleName
,
255 pcdNameArrayInMsa
[index
],
256 MemoryDatabaseManager
.CurrentModuleName
263 if (usageInstanceArray
== null) {
268 // Generate all PCD entry for a module.
270 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
271 usageInstance
= usageInstanceArray
.get(index
);
273 // Before generate any PCD information into autogen.h/autogen.c for a module,
274 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
275 // PCD in this module the token, they are all reference to TokenSpaceGuid
278 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
279 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
280 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
281 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
282 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}}",
286 (guidStringArray
[3].substring(0, 2)),
287 (guidStringArray
[3].substring(2, 4)),
288 (guidStringArray
[4].substring(0, 2)),
289 (guidStringArray
[4].substring(2, 4)),
290 (guidStringArray
[4].substring(4, 6)),
291 (guidStringArray
[4].substring(6, 8)),
292 (guidStringArray
[4].substring(8, 10)),
293 (guidStringArray
[4].substring(10, 12)));
295 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
296 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
298 // Find whether this guid array variable has been generated into autogen.c
299 // For different DyanmicEx pcd token who use same token space guid, the token space
300 // guid array should be only generated once.
302 if (!matcher
.find()) {
303 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n", guidStringCName
);
304 if (!isBuildUsedLibrary
) {
305 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
312 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
314 // For every PCD entry for this module(usage instance), autogen string would
317 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
318 cAutoGenString
+= usageInstance
.getCAutogenStr();
321 if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.PEI_PCD_DRIVER
) {
322 hAutoGenString
+= MemoryDatabaseManager
.PcdPeimHString
;
323 cAutoGenString
+= MemoryDatabaseManager
.PcdPeimCString
;
324 } else if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.DXE_PCD_DRIVER
) {
325 hAutoGenString
+= MemoryDatabaseManager
.PcdDxeHString
;
326 cAutoGenString
+= MemoryDatabaseManager
.PcdDxeCString
;