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
;
37 /** This class is to manage how to generate the PCD information into Autogen.c and
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
;
77 Set parameter moduleId
79 @param moduleName the module name parameter.
81 public void setUsageId(UsageIdentification usageId
) {
82 this.usageId
= usageId
;
86 Set paramter pcdDriverType
88 @param pcdDriverType the driver type for PCD
90 public void setPcdDriverType(CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
) {
91 this.pcdDriverType
= pcdDriverType
;
94 set isBuildUsedLibrary parameter.
96 @param isBuildUsedLibrary
98 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
99 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
103 set pcdNameArrayInMsa parameter.
105 @param pcdNameArrayInMsa
107 public void setPcdNameArrayInMsa(String
[] pcdNameArrayInMsa
) {
108 this.pcdNameArrayInMsa
= pcdNameArrayInMsa
;
112 Get the output of generated string for header file.
114 @return the string of header file for PCD
116 public String
OutputH() {
117 return hAutoGenString
;
121 Get the output of generated string for C Code file.
123 @return the string of C code file for PCD
125 public String
OutputC() {
126 return cAutoGenString
;
133 This function mainly initialize some member variable.
135 @param moduleId the identification for module
136 @param arch the architecture for module
137 @param isBuildUsedLibary Is the current module library.
138 @param pcdNameArrayInMsa the pcd name array got from MSA file.
139 @param pcdDriverType one of PEI_PCD_DRIVER, DXE_PCD_DRIVER,
142 public PCDAutoGenAction(ModuleIdentification moduleId
,
144 boolean isBuildUsedLibrary
,
145 String
[] pcdNameArrayInMsa
,
146 CommonDefinition
.PCD_DRIVER_TYPE pcdDriverType
) {
151 setUsageId(new UsageIdentification(moduleId
.getName(),
153 moduleId
.getPackage().getName(),
154 moduleId
.getPackage().getGuid(),
156 moduleId
.getVersion(),
157 moduleId
.getModuleType()));
158 setIsBuildUsedLibrary(isBuildUsedLibrary
);
159 setPcdNameArrayInMsa(pcdNameArrayInMsa
);
160 setPcdDriverType(pcdDriverType
);
164 Override function: check the parameter for action class.
166 @throws BuildActionException Bad parameter.
168 public void checkParameter() {
172 Core execution function for this action class.
174 All PCD information of this module comes from memory dabase. The collection
175 work should be done before this action execution.
176 Currently, we should generated all PCD information(maybe all dynamic) as array
177 in Pei emulated driver for simulating PCD runtime database.
179 @throws BuildActionException Failed to execute this aciton class.
181 public void performAction() {
182 ActionMessage
.debug(this,
183 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
185 dbManager
= GlobalData
.getPCDMemoryDBManager();
187 if(dbManager
.getDBSize() == 0) {
191 ActionMessage
.debug(this,
192 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens.");
194 generateAutogenForModule();
198 Generate the autogen string for a common module.
200 All PCD information of this module comes from memory dabase. The collection
201 work should be done before this action execution.
203 private void generateAutogenForModule()
206 List
<UsageInstance
> usageInstanceArray
, usageContext
;
207 String
[] guidStringArray
= null;
208 String guidStringCName
= null;
209 String guidString
= null;
210 String moduleName
= usageId
.moduleName
;
211 UsageInstance usageInstance
= null;
212 boolean found
= false;
214 usageInstanceArray
= null;
215 if (!isBuildUsedLibrary
) {
216 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
217 MemoryDatabaseManager
.UsageInstanceContext
= usageInstanceArray
;
218 MemoryDatabaseManager
.CurrentModuleName
= moduleName
;
219 } else if ((pcdNameArrayInMsa
!= null) && (pcdNameArrayInMsa
.length
> 0)) {
220 usageContext
= MemoryDatabaseManager
.UsageInstanceContext
;
222 // For building library package, although all module are library, but PCD entries of
223 // these library should be used to autogen.
225 if (usageContext
== null) {
226 usageInstanceArray
= dbManager
.getUsageInstanceArrayById(usageId
);
228 usageInstanceArray
= new ArrayList
<UsageInstance
>();
231 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
232 // <ModuleSA> in FPD file.
234 for (index
= 0; index
< pcdNameArrayInMsa
.length
; index
++) {
236 for (index2
= 0; index2
< usageContext
.size(); index2
++) {
237 if (pcdNameArrayInMsa
[index
].equalsIgnoreCase(usageContext
.get(index2
).parentToken
.cName
)) {
238 usageInstanceArray
.add(usageContext
.get(index2
));
246 // All library's PCD should instanted in module's <ModuleSA> who
247 // use this library instance. If not, give errors.
249 throw new BuildActionException (String
.format("Module %s using library instance %s; the PCD %s " +
250 "is required by this library instance, but can not be found " +
251 "in the %s's <ModuleSA> in the FPD file!",
252 MemoryDatabaseManager
.CurrentModuleName
,
254 pcdNameArrayInMsa
[index
],
255 MemoryDatabaseManager
.CurrentModuleName
262 if (usageInstanceArray
== null) {
267 // Generate all PCD entry for a module.
269 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
270 usageInstance
= usageInstanceArray
.get(index
);
272 // Before generate any PCD information into autogen.h/autogen.c for a module,
273 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
274 // PCD in this module the token, they are all reference to TokenSpaceGuid
277 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
278 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.split("-");
279 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
280 usageInstance
.parentToken
.tokenSpaceName
.replaceAll("-", "_");
281 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}}",
285 (guidStringArray
[3].substring(0, 2)),
286 (guidStringArray
[3].substring(2, 4)),
287 (guidStringArray
[4].substring(0, 2)),
288 (guidStringArray
[4].substring(2, 4)),
289 (guidStringArray
[4].substring(4, 6)),
290 (guidStringArray
[4].substring(6, 8)),
291 (guidStringArray
[4].substring(8, 10)),
292 (guidStringArray
[4].substring(10, 12)));
294 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
295 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
297 // Find whether this guid array variable has been generated into autogen.c
298 // For different DyanmicEx pcd token who use same token space guid, the token space
299 // guid array should be only generated once.
301 if (!matcher
.find()) {
302 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n", guidStringCName
);
303 if (!isBuildUsedLibrary
) {
304 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
311 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
313 // For every PCD entry for this module(usage instance), autogen string would
316 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
317 cAutoGenString
+= usageInstance
.getCAutogenStr();
321 // Work around code, In furture following code should be modified that get
322 // these information from Uplevel Autogen tools.
324 if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.PEI_PCD_DRIVER
) {
325 hAutoGenString
+= MemoryDatabaseManager
.PcdPeimHString
;
326 cAutoGenString
+= MemoryDatabaseManager
.PcdPeimCString
;
327 } else if (pcdDriverType
== CommonDefinition
.PCD_DRIVER_TYPE
.DXE_PCD_DRIVER
) {
328 hAutoGenString
+= MemoryDatabaseManager
.PcdDxeHString
;
329 cAutoGenString
+= MemoryDatabaseManager
.PcdDxeCString
;
336 @param argv paramter from command line
338 public static void main(String argv
[]) {
340 String WorkSpace
= "X:/edk2";
341 String logFilePath
= WorkSpace
+ "/EdkNt32Pkg/Nt32.fpd";
344 // At first, CollectPCDAction should be invoked to collect
345 // all PCD information from SPD, MSA, FPD.
347 PlatformPcdPreprocessActionForBuilding collectionAction
= new PlatformPcdPreprocessActionForBuilding();
348 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",
352 collectionAction
.perform(logFilePath
, ActionMessage
.MAX_MESSAGE_LEVEL
);
353 } catch(Exception e
) {