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
.global
.GlobalData
;
26 import org
.tianocore
.build
.id
.ModuleIdentification
;
27 import org
.tianocore
.pcd
.entity
.MemoryDatabaseManager
;
28 import org
.tianocore
.pcd
.entity
.Token
;
29 import org
.tianocore
.pcd
.entity
.UsageInstance
;
30 import org
.tianocore
.pcd
.exception
.BuildActionException
;
31 import org
.tianocore
.pcd
.entity
.UsageIdentification
;
32 import org
.tianocore
.pcd
.action
.BuildAction
;
33 import org
.tianocore
.pcd
.action
.ActionMessage
;
35 /** This class is to manage how to generate the PCD information into Autogen.c and
38 public class PCDAutoGenAction
extends BuildAction
{
40 /// The reference of DBManager in GlobalData class.
42 private MemoryDatabaseManager dbManager
;
45 /// The identification for a UsageInstance.
47 private UsageIdentification usageId
;
50 /// Whether current autogen is for building library used by current module.
52 private boolean isBuildUsedLibrary
;
55 /// The generated string for header file.
57 private String hAutoGenString
;
60 /// The generated string for C code file.
62 private String cAutoGenString
;
65 /// The name array of <PcdCoded> in a module.
67 private String
[] pcdNameArrayInMsa
;
70 Set parameter moduleId
72 @param moduleName the module name parameter.
74 public void setUsageId(UsageIdentification usageId
) {
75 this.usageId
= usageId
;
79 set isBuildUsedLibrary parameter.
81 @param isBuildUsedLibrary
83 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
84 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
88 set pcdNameArrayInMsa parameter.
90 @param pcdNameArrayInMsa
92 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
93 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
97 Get the output of generated string for header file.
99 @return the string of header file for PCD
101 public String
OutputH() {
102 return hAutoGenString
;
106 Get the output of generated string for C Code file.
108 @return the string of C code file for PCD
110 public String
OutputC() {
111 return cAutoGenString
;
118 This function mainly initialize some member variable.
120 @param moduleId the identification for module
121 @param arch the architecture for module
122 @param isBuildUsedLibary Is the current module library.
123 @param pcdNameArrayInMsa the pcd name array got from MSA file.
125 public PCDAutoGenAction(ModuleIdentification moduleId
,
127 boolean isBuildUsedLibrary
,
128 String
[] pcdNameArrayInMsa
) {
133 setUsageId(new UsageIdentification(moduleId
.getName(),
135 moduleId
.getPackage().getName(),
136 moduleId
.getPackage().getGuid(),
138 moduleId
.getVersion(),
139 moduleId
.getModuleType()));
140 setIsBuildUsedLibrary(isBuildUsedLibrary
);
141 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
145 check the parameter for action class.
147 @throws BuildActionException Bad parameter.
149 public void checkParameter() {
153 Core execution function for this action class.
155 All PCD information of this module comes from memory dabase. The collection
156 work should be done before this action execution.
157 Currently, we should generated all PCD information(maybe all dynamic) as array
158 in Pei emulated driver for simulating PCD runtime database.
160 @throws BuildActionException Failed to execute this aciton class.
162 public void performAction() {
163 ActionMessage
.debug(this,
164 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
166 // Check the PCD memory database manager is valid.
168 if(GlobalData
.getPCDMemoryDBManager() == null) {
169 throw new BuildActionException("Memory database has not been initlizated!");
172 dbManager
= GlobalData
.getPCDMemoryDBManager();
174 if(dbManager
.getDBSize() == 0) {
178 ActionMessage
.debug(this,
179 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens");
181 generateAutogenForModule();
185 Generate the autogen string for a common module.
187 All PCD information of this module comes from memory dabase. The collection
188 work should be done before this action execution.
190 private void generateAutogenForModule()
193 List
<UsageInstance
> usageInstanceArray
, usageContext
;
194 String
[] guidStringArray
= null;
195 String guidStringCName
= null;
196 String guidString
= null;
197 String moduleName
= usageId
.moduleName
;
198 UsageInstance usageInstance
= null;
199 boolean found
= false;
201 usageInstanceArray
= null;
202 if (!isBuildUsedLibrary
) {
203 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(usageId
);
204 MemoryDatabaseManager
.UsageInstanceContext
= usageInstanceArray
;
205 MemoryDatabaseManager
.CurrentModuleName
= moduleName
;
206 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
207 usageContext
= MemoryDatabaseManager
.UsageInstanceContext
;
209 // For building library package, although all module are library, but PCD entries of
210 // these library should be used to autogen.
212 if (usageContext
== null) {
213 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(usageId
);
215 usageInstanceArray
= new ArrayList
<UsageInstance
>();
218 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
219 // <ModuleSA> in FPD file.
221 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
223 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
224 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
225 usageInstanceArray
.add(usageContext
.get(index2
));
233 // All library's PCD should instanted in module's <ModuleSA> who
234 // use this library instance. If not, give errors.
236 throw new BuildActionException (String
.format("[PCD Autogen Error] Module %s use library instance %s, the PCD %s " +
237 "is required by this library instance, but can not find " +
238 "it in the %s's <ModuleSA> in FPD file!",
239 MemoryDatabaseManager
.CurrentModuleName
,
241 pcdNameArrayInMsa
[index
],
242 MemoryDatabaseManager
.CurrentModuleName
249 if (usageInstanceArray
== null) {
254 // Generate all PCD entry for a module.
256 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
257 usageInstance
= usageInstanceArray
.get(index
);
259 // Before generate any PCD information into autogen.h/autogen.c for a module,
260 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
261 // PCD in this module the token, they are all reference to TokenSpaceGuid
264 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
265 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
266 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
267 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
268 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}}",
272 (guidStringArray
[3].substring(0, 2)),
273 (guidStringArray
[3].substring(2, 4)),
274 (guidStringArray
[4].substring(0, 2)),
275 (guidStringArray
[4].substring(2, 4)),
276 (guidStringArray
[4].substring(4, 6)),
277 (guidStringArray
[4].substring(6, 8)),
278 (guidStringArray
[4].substring(8, 10)),
279 (guidStringArray
[4].substring(10, 12)));
281 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
282 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
284 // Find whether this guid array variable has been generated into autogen.c
285 // For different DyanmicEx pcd token who use same token space guid, the token space
286 // guid array should be only generated once.
288 if (!matcher
.find()) {
289 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n", guidStringCName
);
290 if (!isBuildUsedLibrary
) {
291 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
298 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
300 // For every PCD entry for this module(usage instance), autogen string would
303 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
304 cAutoGenString
+= usageInstance
.getCAutogenStr();
308 // Work around code, In furture following code should be modified that get
309 // these information from Uplevel Autogen tools.
311 if (moduleName
.equalsIgnoreCase("PcdPeim")) {
312 hAutoGenString
+= MemoryDatabaseManager
.PcdPeimHString
;
313 cAutoGenString
+= MemoryDatabaseManager
.PcdPeimCString
;
314 } else if (moduleName
.equalsIgnoreCase("PcdDxe")) {
315 hAutoGenString
+= MemoryDatabaseManager
.PcdDxeHString
;
316 cAutoGenString
+= MemoryDatabaseManager
.PcdDxeCString
;
323 @param argv paramter from command line
325 public static void main(String argv
[]) {
327 String WorkSpace
= "X:/edk2";
328 String logFilePath
= WorkSpace
+ "/EdkNt32Pkg/Nt32.fpd";
331 // At first, CollectPCDAction should be invoked to collect
332 // all PCD information from SPD, MSA, FPD.
334 PlatformPcdPreprocessActionForBuilding collectionAction
= new PlatformPcdPreprocessActionForBuilding();
335 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",
339 collectionAction
.perform(WorkSpace
,
341 ActionMessage
.MAX_MESSAGE_LEVEL
);
342 } catch(Exception e
) {