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
.UUID
;
23 import java
.util
.regex
.Matcher
;
24 import java
.util
.regex
.Pattern
;
26 import org
.tianocore
.build
.global
.GlobalData
;
27 import org
.tianocore
.build
.pcd
.entity
.MemoryDatabaseManager
;
28 import org
.tianocore
.build
.pcd
.entity
.Token
;
29 import org
.tianocore
.build
.pcd
.entity
.UsageInstance
;
30 import org
.tianocore
.build
.pcd
.exception
.BuildActionException
;
31 import org
.tianocore
.build
.pcd
.exception
.EntityException
;
33 /** This class is to manage how to generate the PCD information into Autogen.c and
36 public class PCDAutoGenAction
extends BuildAction
{
38 /// The reference of DBManager in GlobalData class.
40 private MemoryDatabaseManager dbManager
;
42 /// The name of module which is analysised currently.
44 private String moduleName
;
46 /// The Guid of module which is analyzed currently.
48 private UUID moduleGuid
;
50 /// The name of package whose module is analysized currently.
52 private String packageName
;
54 /// The Guid of package whose module is analyszed curretnly.
56 private UUID packageGuid
;
58 /// The arch of current module
62 /// The version of current module
64 private String version
;
66 /// Whether current autogen is for building library used by current module.
68 private boolean isBuildUsedLibrary
;
70 /// The generated string for header file.
72 private String hAutoGenString
;
74 /// The generated string for C code file.
76 private String cAutoGenString
;
78 /// The name array of <PcdCoded> in a module.
80 private String
[] pcdNameArray
;
82 Set parameter ModuleName
84 @param moduleName the module name parameter.
86 public void setModuleName(String moduleName
) {
87 this.moduleName
= moduleName
;
91 set the moduleGuid parameter.
95 public void setModuleGuid(UUID moduleGuid
) {
96 this.moduleGuid
= moduleGuid
;
100 set packageName parameter.
104 public void setPackageName(String packageName
) {
105 this.packageName
= packageName
;
109 set packageGuid parameter.
113 public void setPackageGuid(UUID packageGuid
) {
114 this.packageGuid
= packageGuid
;
122 public void setArch(String arch
) {
127 set version parameter
131 public void setVersion(String version
) {
132 this.version
= version
;
136 set isBuildUsedLibrary parameter.
138 @param isBuildUsedLibrary
140 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary
) {
141 this.isBuildUsedLibrary
= isBuildUsedLibrary
;
144 set pcdNameArray parameter.
148 public void setPcdNameArray(String
[] pcdNameArray
) {
149 this.pcdNameArray
= pcdNameArray
;
153 Get the output of generated string for header file.
155 @return the string of header file for PCD
157 public String
OutputH() {
158 return hAutoGenString
;
162 Get the output of generated string for C Code file.
164 @return the string of C code file for PCD
166 public String
OutputC() {
167 return cAutoGenString
;
173 This function mainly initialize some member variable.
175 @param moduleName Parameter of this action class.
176 @param isEmulatedPCDDriver Parameter of this action class.
178 public PCDAutoGenAction(String moduleName
,
184 boolean isBuildUsedLibrary
,
185 String
[] pcdNameArray
) {
190 setModuleName(moduleName
);
191 setModuleGuid(moduleGuid
);
192 setPackageName(packageName
);
193 setPackageGuid(packageGuid
);
194 setPcdNameArray(pcdNameArray
);
197 setIsBuildUsedLibrary(isBuildUsedLibrary
);
201 check the parameter for action class.
203 @throws BuildActionException Bad parameter.
205 void checkParameter() throws BuildActionException
{
210 Core execution function for this action class.
212 All PCD information of this module comes from memory dabase. The collection
213 work should be done before this action execution.
214 Currently, we should generated all PCD information(maybe all dynamic) as array
215 in Pei emulated driver for simulating PCD runtime database.
217 @throws BuildActionException Failed to execute this aciton class.
219 void performAction() throws BuildActionException
{
220 ActionMessage
.debug(this,
221 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
223 // Check the PCD memory database manager is valid.
225 if(GlobalData
.getPCDMemoryDBManager() == null) {
226 throw new BuildActionException("Memory database has not been initlizated!");
229 dbManager
= GlobalData
.getPCDMemoryDBManager();
231 if(dbManager
.getDBSize() == 0) {
235 ActionMessage
.debug(this,
236 "PCD memory database contains " + dbManager
.getDBSize() + " PCD tokens");
240 generateAutogenForModule();
244 Generate the autogen string for a common module.
246 All PCD information of this module comes from memory dabase. The collection
247 work should be done before this action execution.
249 private void generateAutogenForModule()
252 List
<UsageInstance
> usageInstanceArray
, usageContext
;
253 String
[] guidStringArray
= null;
254 String guidStringCName
= null;
255 String guidString
= null;
256 UsageInstance usageInstance
= null;
258 if (!isBuildUsedLibrary
) {
259 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(moduleName
,
265 dbManager
.UsageInstanceContext
= usageInstanceArray
;
266 dbManager
.CurrentModuleName
= moduleName
;
268 usageContext
= dbManager
.UsageInstanceContext
;
270 // For building MDE package, although all module are library, but PCD entries of
271 // these library should be used to autogen.
273 if (usageContext
== null) {
274 usageInstanceArray
= dbManager
.getUsageInstanceArrayByModuleName(moduleName
,
281 usageInstanceArray
= new ArrayList
<UsageInstance
>();
283 // Remove PCD entries which are not belong to this library.
285 for (index
= 0; index
< usageContext
.size(); index
++) {
286 if ((pcdNameArray
== null) || (pcdNameArray
.length
== 0)){
290 for (index2
= 0; index2
< pcdNameArray
.length
; index2
++) {
291 if (pcdNameArray
[index2
].equalsIgnoreCase(usageContext
.get(index
).parentToken
.cName
)) {
292 usageInstanceArray
.add(usageContext
.get(index
));
300 if(usageInstanceArray
.size() != 0) {
302 // Add "#include 'PcdLib.h'" for Header file
304 hAutoGenString
= "#include <MdePkg/Include/Library/PcdLib.h>\r\n";
308 // Generate all PCD entry for a module.
310 for(index
= 0; index
< usageInstanceArray
.size(); index
++) {
311 ActionMessage
.debug(this,
312 "Module " + moduleName
+ "'s PCD [" + Integer
.toHexString(index
) +
313 "]: " + usageInstanceArray
.get(index
).parentToken
.cName
);
315 usageInstance
= usageInstanceArray
.get(index
);
317 // Before generate any PCD information into autogen.h/autogen.c for a module,
318 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
319 // PCD in this module the token, they are all reference to TokenSpaceGuid
322 if (usageInstanceArray
.get(index
).modulePcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
323 guidStringArray
= usageInstance
.parentToken
.tokenSpaceName
.toString().split("-");
324 guidStringCName
= "_gPcd_TokenSpaceGuid_" +
325 usageInstance
.parentToken
.tokenSpaceName
.toString().replaceAll("-", "_");
326 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}}",
330 (guidStringArray
[3].substring(0, 2)),
331 (guidStringArray
[3].substring(2, 4)),
332 (guidStringArray
[4].substring(0, 2)),
333 (guidStringArray
[4].substring(2, 4)),
334 (guidStringArray
[4].substring(4, 6)),
335 (guidStringArray
[4].substring(6, 8)),
336 (guidStringArray
[4].substring(8, 10)),
337 (guidStringArray
[4].substring(10, 12)));
339 Pattern pattern
= Pattern
.compile("(" + guidStringCName
+ ")+?");
340 Matcher matcher
= pattern
.matcher(cAutoGenString
+ " ");
342 // Find whether this guid array variable has been generated into autogen.c
343 // For different DyanmicEx pcd token who use same token space guid, the token space
344 // guid array should be only generated once.
346 if (!matcher
.find()) {
347 hAutoGenString
+= String
.format("extern EFI_GUID %s;\r\n",
349 if (!isBuildUsedLibrary
) {
350 cAutoGenString
+= String
.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
357 usageInstance
.generateAutoGen(isBuildUsedLibrary
);
359 // For every PCD entry for this module(usage instance), autogen string would
362 hAutoGenString
+= usageInstance
.getHAutogenStr() + "\r\n";
363 cAutoGenString
+= usageInstance
.getCAutogenStr();
365 } catch(EntityException exp
) {
366 throw new BuildActionException("[PCD Autogen Error]: " + exp
.getMessage());
371 // Work around code, In furture following code should be modified that get
372 // these information from Uplevel Autogen tools.
374 if (moduleName
.equalsIgnoreCase("PcdPeim")) {
375 hAutoGenString
+= dbManager
.PcdPeimHString
;
376 cAutoGenString
+= dbManager
.PcdPeimCString
;
377 } else if (moduleName
.equalsIgnoreCase("PcdDxe")) {
378 hAutoGenString
+= dbManager
.PcdDxeHString
;
379 cAutoGenString
+= dbManager
.PcdDxeCString
;
382 ActionMessage
.debug(this,
383 "Module " + moduleName
+ "'s PCD header file:\r\n" + hAutoGenString
+ "\r\n"
385 ActionMessage
.debug(this,
386 "Module " + moduleName
+ "'s PCD C file:\r\n" + cAutoGenString
+ "\r\n"
393 @param argv paramter from command line
395 public static void main(String argv
[]) {
397 String WorkSpace
= "X:/edk2";
398 String logFilePath
= WorkSpace
+ "/EdkNt32Pkg/Nt32.fpd";
399 String
[] nameArray
= null;
402 // At first, CollectPCDAction should be invoked to collect
403 // all PCD information from SPD, MSA, FPD.
405 CollectPCDAction collectionAction
= new CollectPCDAction();
406 GlobalData
.initInfo("Tools" + File
.separator
+ "Conf" + File
.separator
+ "FrameworkDatabase.db",
410 collectionAction
.perform(WorkSpace
,
412 ActionMessage
.MAX_MESSAGE_LEVEL
);
413 } catch(Exception e
) {
418 // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c
420 PCDAutoGenAction autogenAction
= new PCDAutoGenAction("PcdPeim",
428 autogenAction
.execute();
430 System
.out
.println(autogenAction
.OutputH());
431 System
.out
.println("WQWQWQWQWQ");
432 System
.out
.println(autogenAction
.OutputC());