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
.build
.pcd
.entity
.MemoryDatabaseManager
;
33 import org
.tianocore
.build
.pcd
.entity
.Token
;
34 import org
.tianocore
.build
.pcd
.entity
.UsageInstance
;
35 import org
.tianocore
.build
.pcd
.exception
.BuildActionException
;
36 import org
.tianocore
.build
.pcd
.exception
.EntityException
;
38 /** This class is to manage how to generate the PCD information into Autogen.c and
41 public class PCDAutoGenAction
extends BuildAction
{
43 /// The reference of DBManager in GlobalData class.
45 private MemoryDatabaseManager dbManager
;
47 /// The identification for a module.
49 private ModuleIdentification moduleId
;
51 /// The arch of current module
55 /// Whether current autogen is for building library used by current module.
57 private boolean isBuildUsedLibrary
;
59 /// The generated string for header file.
61 private String hAutoGenString
;
63 /// The generated string for C code file.
65 private String cAutoGenString
;
67 /// The name array of <PcdCoded> in a module.
69 private String
[] pcdNameArrayInMsa
;
71 Set parameter moduleId
73 @param moduleName the module name parameter.
75 public void setModuleId(ModuleIdentification moduleId
) {
76 this.moduleId
= moduleId
;
84 public void setArch(String arch
) {
89 set isBuildUsedLibrary parameter.
91 @param isBuildUsedLibrary
93 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
94 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
98 set pcdNameArrayInMsa parameter.
100 @param pcdNameArrayInMsa
102 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
103 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
107 Get the output of generated string for header file.
109 @return the string of header file for PCD
111 public String
OutputH() {
112 return hAutoGenString
;
116 Get the output of generated string for C Code file.
118 @return the string of C code file for PCD
120 public String
OutputC() {
121 return cAutoGenString
;
128 This function mainly initialize some member variable.
130 @param moduleId the identification for module
131 @param arch the architecture for module
132 @param isBuildUsedLibary Is the current module library.
133 @param pcdNameArrayInMsa the pcd name array got from MSA file.
135 public PCDAutoGenAction(ModuleIdentification moduleId
,
137 boolean isBuildUsedLibrary
,
138 String
[] pcdNameArrayInMsa
) {
143 setModuleId(moduleId
);
145 setIsBuildUsedLibrary(isBuildUsedLibrary
);
146 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
150 check the parameter for action class.
152 @throws BuildActionException Bad parameter.
154 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 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
= moduleId
.getName();
204 UsageInstance usageInstance
= null;
205 boolean found
= false;
207 usageInstanceArray
= null;
208 if (!isBuildUsedLibrary
) {
209 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(moduleId
, arch
);
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(moduleId
, arch
);
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
) {