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
.FrameworkBuildTask
;
25 import org
.tianocore
.build
.autogen
.CommonDefinition
;
26 import org
.tianocore
.build
.global
.GlobalData
;
27 import org
.tianocore
.build
.id
.ModuleIdentification
;
28 import org
.tianocore
.common
.logger
.EdkLog
;
29 import org
.tianocore
.pcd
.action
.BuildAction
;
30 import org
.tianocore
.pcd
.entity
.MemoryDatabaseManager
;
31 import org
.tianocore
.pcd
.entity
.Token
;
32 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
33 import org
.tianocore
.pcd
.entity
.UsageInstance
;
34 import org
.tianocore
.pcd
.exception
.BuildActionException
;
37 This class is to manage how to generate the PCD information into Autogen.c
40 public class PCDAutoGenAction
extends BuildAction
{
42 /// The reference of DBManager in GlobalData class.
44 private MemoryDatabaseManager dbManager
;
47 /// The identification for a UsageInstance.
49 private UsageIdentification usageId
;
52 /// Whether current autogen is for building library used by current module.
54 private boolean isBuildUsedLibrary
;
57 /// One of PEI_PCD_DRIVER, DXE_PCD_DRIVER, NOT_PCD_DRIVER
59 private CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
;
62 /// The generated string for header file.
64 private String hAutoGenString
;
67 /// The generated string for C code file.
69 private String cAutoGenString
;
72 /// The name array of <PcdCoded> in a module.
74 private String
[] pcdNameArrayInMsa
;
76 private UsageIdentification parentId
= null;
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
,
148 ModuleIdentification parentId
) {
153 setUsageId(new UsageIdentification(moduleId
.getName(),
155 moduleId
.getPackage().getName(),
156 moduleId
.getPackage().getGuid(),
158 moduleId
.getVersion(),
159 moduleId
.getModuleType()));
160 if (parentId
!= null) {
161 this.parentId
= new UsageIdentification(parentId
.getName(),
163 parentId
.getPackage().getName(),
164 parentId
.getPackage().getGuid(),
166 parentId
.getVersion(),
167 parentId
.getModuleType());
169 setIsBuildUsedLibrary(isBuildUsedLibrary
);
170 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
171 setPcdDriverType(pcdDriverType
);
175 Override function: check the parameter for action class.
177 @throws BuildActionException Bad parameter.
179 public void checkParameter() {
183 Core execution function for this action class.
185 All PCD information of this module comes from memory dabase. The collection
186 work should be done before this action execution.
187 Currently, we should generated all PCD information(maybe all dynamic) as array
188 in Pei emulated driver for simulating PCD runtime database.
190 @throws BuildActionException Failed to execute this aciton class.
192 public void performAction() {
193 EdkLog
.log(EdkLog
.EDK_DEBUG
, "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
195 dbManager
= GlobalData
.getPCDMemoryDBManager();
197 if(dbManager
.getDBSize() == 0) {
201 EdkLog
.log(EdkLog
.EDK_DEBUG
, "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens.");
203 generateAutogenForModule();
207 Generate the autogen string for a common module.
209 All PCD information of this module comes from memory dabase. The collection
210 work should be done before this action execution.
212 private void generateAutogenForModule()
215 List
<UsageInstance
> usageInstanceArray
, usageContext
;
216 String
[] guidStringArray
= null;
217 String guidStringCName
= null;
218 String guidString
= null;
219 String moduleName
= usageId
.moduleName
;
220 UsageInstance usageInstance
= null;
221 boolean found
= false;
223 usageInstanceArray
= null;
225 if (FrameworkBuildTask
.multithread
) {
226 if (parentId
== null) {
227 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
228 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
229 usageContext
= dbManager
.getUsageInstanceArrayById(parentId
);
231 // For building library package, although all module are library, but PCD entries of
232 // these library should be used to autogen.
234 if (usageContext
== null) {
235 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
237 usageInstanceArray
= new ArrayList
<UsageInstance
>();
240 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
241 // <ModuleSA> in FPD file.
243 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
245 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
246 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
247 usageInstanceArray
.add(usageContext
.get(index2
));
255 // All library's PCD should instanted in module's <ModuleSA> who
256 // use this library instance. If not, give errors.
258 throw new BuildActionException (String
.format("Module %s using library instance %s; the PCD %s " +
259 "is required by this library instance, but can not be found " +
260 "in the %s's <ModuleSA> in the FPD file!",
261 MemoryDatabaseManager
.CurrentModuleName
,
263 pcdNameArrayInMsa
[index
],
264 MemoryDatabaseManager
.CurrentModuleName
271 if (!isBuildUsedLibrary
) {
272 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
273 MemoryDatabaseManager
.UsageInstanceContext
= usageInstanceArray
;
274 MemoryDatabaseManager
.CurrentModuleName
= moduleName
;
275 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
276 usageContext
= MemoryDatabaseManager
.UsageInstanceContext
;
278 // For building library package, although all module are library, but PCD entries of
279 // these library should be used to autogen.
281 if (usageContext
== null) {
282 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
284 usageInstanceArray
= new ArrayList
<UsageInstance
>();
287 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
288 // <ModuleSA> in FPD file.
290 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
292 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
293 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
294 usageInstanceArray
.add(usageContext
.get(index2
));
302 // All library's PCD should instanted in module's <ModuleSA> who
303 // use this library instance. If not, give errors.
305 throw new BuildActionException (String
.format("Module %s using library instance %s; the PCD %s " +
306 "is required by this library instance, but can not be found " +
307 "in the %s's <ModuleSA> in the FPD file!",
308 MemoryDatabaseManager
.CurrentModuleName
,
310 pcdNameArrayInMsa
[index
],
311 MemoryDatabaseManager
.CurrentModuleName
318 if (usageInstanceArray
== null) {
323 // Generate all PCD entry for a module.
325 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
326 usageInstance
= usageInstanceArray
.get(index
);
328 // Before generate any PCD information into autogen.h/autogen.c for a module,
329 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
330 // PCD in this module the token, they are all reference to TokenSpaceGuid
333 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
334 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
335 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
336 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
337 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}}",
341 (guidStringArray
[3].substring(0, 2)),
342 (guidStringArray
[3].substring(2, 4)),
343 (guidStringArray
[4].substring(0, 2)),
344 (guidStringArray
[4].substring(2, 4)),
345 (guidStringArray
[4].substring(4, 6)),
346 (guidStringArray
[4].substring(6, 8)),
347 (guidStringArray
[4].substring(8, 10)),
348 (guidStringArray
[4].substring(10, 12)));
350 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
351 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
353 // Find whether this guid array variable has been generated into autogen.c
354 // For different DyanmicEx pcd token who use same token space guid, the token space
355 // guid array should be only generated once.
357 if (!matcher
.find()) {
358 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n", guidStringCName
);
359 if (!isBuildUsedLibrary
) {
360 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
367 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
369 // For every PCD entry for this module(usage instance), autogen string would
372 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
373 cAutoGenString
+= usageInstance
.getCAutogenStr();
376 if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.PEI_PCD_DRIVER
) {
377 hAutoGenString
+= MemoryDatabaseManager
.PcdPeimHString
;
378 cAutoGenString
+= MemoryDatabaseManager
.PcdPeimCString
;
379 } else if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.DXE_PCD_DRIVER
) {
380 hAutoGenString
+= MemoryDatabaseManager
.PcdDxeHString
;
381 cAutoGenString
+= MemoryDatabaseManager
.PcdDxeCString
;