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
;
34 import org
.tianocore
.build
.exception
.PcdAutogenException
;
36 /** This class is to manage how to generate the PCD information into Autogen.c and
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 /// The generated string for header file.
58 private String hAutoGenString
;
61 /// The generated string for C code file.
63 private String cAutoGenString
;
66 /// The name array of <PcdCoded> in a module.
68 private String
[] pcdNameArrayInMsa
;
71 Set parameter moduleId
73 @param moduleName the module name parameter.
75 public void setUsageId(UsageIdentification usageId
) {
76 this.usageId
= usageId
;
80 set isBuildUsedLibrary parameter.
82 @param isBuildUsedLibrary
84 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
85 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
89 set pcdNameArrayInMsa parameter.
91 @param pcdNameArrayInMsa
93 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
94 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
98 Get the output of generated string for header file.
100 @return the string of header file for PCD
102 public String
OutputH() {
103 return hAutoGenString
;
107 Get the output of generated string for C Code file.
109 @return the string of C code file for PCD
111 public String
OutputC() {
112 return cAutoGenString
;
119 This function mainly initialize some member variable.
121 @param moduleId the identification for module
122 @param arch the architecture for module
123 @param isBuildUsedLibary Is the current module library.
124 @param pcdNameArrayInMsa the pcd name array got from MSA file.
126 public PCDAutoGenAction(ModuleIdentification moduleId
,
128 boolean isBuildUsedLibrary
,
129 String
[] pcdNameArrayInMsa
) {
134 setUsageId(new UsageIdentification(moduleId
.getName(),
136 moduleId
.getPackage().getName(),
137 moduleId
.getPackage().getGuid(),
139 moduleId
.getVersion(),
140 moduleId
.getModuleType()));
141 setIsBuildUsedLibrary(isBuildUsedLibrary
);
142 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
146 Override function: check the parameter for action class.
148 @throws BuildActionException Bad parameter.
150 public void checkParameter() {
154 Core execution function for this action class.
156 All PCD information of this module comes from memory dabase. The collection
157 work should be done before this action execution.
158 Currently, we should generated all PCD information(maybe all dynamic) as array
159 in Pei emulated driver for simulating PCD runtime database.
161 @throws BuildActionException Failed to execute this aciton class.
163 public void performAction() {
164 ActionMessage
.debug(this,
165 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
167 dbManager
= GlobalData
.getPCDMemoryDBManager();
169 if(dbManager
.getDBSize() == 0) {
173 ActionMessage
.debug(this,
174 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens");
176 generateAutogenForModule();
180 Generate the autogen string for a common module.
182 All PCD information of this module comes from memory dabase. The collection
183 work should be done before this action execution.
185 private void generateAutogenForModule()
188 List
<UsageInstance
> usageInstanceArray
, usageContext
;
189 String
[] guidStringArray
= null;
190 String guidStringCName
= null;
191 String guidString
= null;
192 String moduleName
= usageId
.moduleName
;
193 UsageInstance usageInstance
= null;
194 boolean found
= false;
196 usageInstanceArray
= null;
197 if (!isBuildUsedLibrary
) {
198 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(usageId
);
199 MemoryDatabaseManager
.UsageInstanceContext
= usageInstanceArray
;
200 MemoryDatabaseManager
.CurrentModuleName
= moduleName
;
201 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
202 usageContext
= MemoryDatabaseManager
.UsageInstanceContext
;
204 // For building library package, although all module are library, but PCD entries of
205 // these library should be used to autogen.
207 if (usageContext
== null) {
208 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(usageId
);
210 usageInstanceArray
= new ArrayList
<UsageInstance
>();
213 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
214 // <ModuleSA> in FPD file.
216 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
218 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
219 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
220 usageInstanceArray
.add(usageContext
.get(index2
));
228 // All library's PCD should instanted in module's <ModuleSA> who
229 // use this library instance. If not, give errors.
231 throw new BuildActionException (String
.format("Module %s use library instance %s, the PCD %s " +
232 "is required by this library instance, but can not find " +
233 "it in the %s's <ModuleSA> in FPD file!",
234 MemoryDatabaseManager
.CurrentModuleName
,
236 pcdNameArrayInMsa
[index
],
237 MemoryDatabaseManager
.CurrentModuleName
244 if (usageInstanceArray
== null) {
249 // Generate all PCD entry for a module.
251 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
252 usageInstance
= usageInstanceArray
.get(index
);
254 // Before generate any PCD information into autogen.h/autogen.c for a module,
255 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
256 // PCD in this module the token, they are all reference to TokenSpaceGuid
259 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
260 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
261 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
262 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
263 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}}",
267 (guidStringArray
[3].substring(0, 2)),
268 (guidStringArray
[3].substring(2, 4)),
269 (guidStringArray
[4].substring(0, 2)),
270 (guidStringArray
[4].substring(2, 4)),
271 (guidStringArray
[4].substring(4, 6)),
272 (guidStringArray
[4].substring(6, 8)),
273 (guidStringArray
[4].substring(8, 10)),
274 (guidStringArray
[4].substring(10, 12)));
276 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
277 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
279 // Find whether this guid array variable has been generated into autogen.c
280 // For different DyanmicEx pcd token who use same token space guid, the token space
281 // guid array should be only generated once.
283 if (!matcher
.find()) {
284 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n", guidStringCName
);
285 if (!isBuildUsedLibrary
) {
286 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
293 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
295 // For every PCD entry for this module(usage instance), autogen string would
298 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
299 cAutoGenString
+= usageInstance
.getCAutogenStr();
303 // Work around code, In furture following code should be modified that get
304 // these information from Uplevel Autogen tools.
306 if (moduleName
.equalsIgnoreCase("PcdPeim")) {
307 hAutoGenString
+= MemoryDatabaseManager
.PcdPeimHString
;
308 cAutoGenString
+= MemoryDatabaseManager
.PcdPeimCString
;
309 } else if (moduleName
.equalsIgnoreCase("PcdDxe")) {
310 hAutoGenString
+= MemoryDatabaseManager
.PcdDxeHString
;
311 cAutoGenString
+= MemoryDatabaseManager
.PcdDxeCString
;
318 @param argv paramter from command line
320 public static void main(String argv
[]) {
322 String WorkSpace
= "X:/edk2";
323 String logFilePath
= WorkSpace
+ "/EdkNt32Pkg/Nt32.fpd";
326 // At first, CollectPCDAction should be invoked to collect
327 // all PCD information from SPD, MSA, FPD.
329 PlatformPcdPreprocessActionForBuilding collectionAction
= new PlatformPcdPreprocessActionForBuilding();
330 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",
334 collectionAction
.perform(logFilePath
, ActionMessage
.MAX_MESSAGE_LEVEL
);
335 } catch(Exception e
) {