]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Fix EDKT256: using unified logging mechanism in PcdAutogen tools.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / PCDAutoGenAction.java
1 /** @file
2 PCDAutoGenAction class.
3
4 This class is to manage how to generate the PCD information into Autogen.c and
5 Autogen.h.
6
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
12
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.
15
16 **/
17 package org.tianocore.build.pcd.action;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.tianocore.build.FrameworkBuildTask;
25 import org.tianocore.build.autogen.CommonDefinition;
26 import org.tianocore.build.global.GlobalData;
27 import org.tianocore.build.id.ModuleIdentification;
28 import org.tianocore.common.logger.EdkLog;
29 import org.tianocore.pcd.action.BuildAction;
30 import org.tianocore.pcd.entity.MemoryDatabaseManager;
31 import org.tianocore.pcd.entity.Token;
32 import org.tianocore.pcd.entity.UsageIdentification;
33 import org.tianocore.pcd.entity.UsageInstance;
34 import org.tianocore.pcd.exception.BuildActionException;
35
36 /**
37 This class is to manage how to generate the PCD information into Autogen.c
38 and Autogen.h.
39 **/
40 public class PCDAutoGenAction extends BuildAction {
41 ///
42 /// The reference of DBManager in GlobalData class.
43 ///
44 private MemoryDatabaseManager dbManager;
45
46 ///
47 /// The identification for a UsageInstance.
48 ///
49 private UsageIdentification usageId;
50
51 ///
52 /// Whether current autogen is for building library used by current module.
53 ///
54 private boolean isBuildUsedLibrary;
55
56 ///
57 /// One of PEI_PCD_DRIVER, DXE_PCD_DRIVER, NOT_PCD_DRIVER
58 ///
59 private CommonDefinition.PCD_DRIVER_TYPE pcdDriverType;
60
61 ///
62 /// The generated string for header file.
63 ///
64 private String hAutoGenString;
65
66 ///
67 /// The generated string for C code file.
68 ///
69 private String cAutoGenString;
70
71 ///
72 /// The name array of <PcdCoded> in a module.
73 ///
74 private String[] pcdNameArrayInMsa;
75
76 private UsageIdentification parentId = null;
77 /**
78 Set parameter moduleId
79
80 @param moduleName the module name parameter.
81 **/
82 public void setUsageId(UsageIdentification usageId) {
83 this.usageId = usageId;
84 }
85
86 /**
87 Set paramter pcdDriverType
88
89 @param pcdDriverType the driver type for PCD
90 **/
91 public void setPcdDriverType(CommonDefinition.PCD_DRIVER_TYPE pcdDriverType) {
92 this.pcdDriverType = pcdDriverType;
93 }
94 /**
95 set isBuildUsedLibrary parameter.
96
97 @param isBuildUsedLibrary
98 **/
99 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {
100 this.isBuildUsedLibrary = isBuildUsedLibrary;
101 }
102
103 /**
104 set pcdNameArrayInMsa parameter.
105
106 @param pcdNameArrayInMsa
107 */
108 public void setPcdNameArrayInMsa(String[] pcdNameArrayInMsa) {
109 this.pcdNameArrayInMsa = pcdNameArrayInMsa;
110 }
111
112 /**
113 Get the output of generated string for header file.
114
115 @return the string of header file for PCD
116 **/
117 public String getHAutoGenString() {
118 return hAutoGenString;
119 }
120
121 /**
122 Get the output of generated string for C Code file.
123
124 @return the string of C code file for PCD
125 **/
126 public String getCAutoGenString() {
127 return cAutoGenString;
128 }
129
130
131 /**
132 Construct function
133
134 This function mainly initialize some member variable.
135
136 @param moduleId the identification for module
137 @param arch the architecture for module
138 @param isBuildUsedLibary Is the current module library.
139 @param pcdNameArrayInMsa the pcd name array got from MSA file.
140 @param pcdDriverType one of PEI_PCD_DRIVER, DXE_PCD_DRIVER,
141 NOT_PCD_DRIVER
142 **/
143 public PCDAutoGenAction(ModuleIdentification moduleId,
144 String arch,
145 boolean isBuildUsedLibrary,
146 String[] pcdNameArrayInMsa,
147 CommonDefinition.PCD_DRIVER_TYPE pcdDriverType,
148 ModuleIdentification parentId) {
149 dbManager = null;
150 hAutoGenString = "";
151 cAutoGenString = "";
152
153 setUsageId(new UsageIdentification(moduleId.getName(),
154 moduleId.getGuid(),
155 moduleId.getPackage().getName(),
156 moduleId.getPackage().getGuid(),
157 arch,
158 moduleId.getVersion(),
159 moduleId.getModuleType()));
160 if (parentId != null) {
161 this.parentId = new UsageIdentification(parentId.getName(),
162 parentId.getGuid(),
163 parentId.getPackage().getName(),
164 parentId.getPackage().getGuid(),
165 arch,
166 parentId.getVersion(),
167 parentId.getModuleType());
168 }
169 setIsBuildUsedLibrary(isBuildUsedLibrary);
170 setPcdNameArrayInMsa(pcdNameArrayInMsa);
171 setPcdDriverType(pcdDriverType);
172 }
173
174 /**
175 Override function: check the parameter for action class.
176
177 @throws BuildActionException Bad parameter.
178 **/
179 public void checkParameter() {
180 }
181
182 /**
183 Core execution function for this action class.
184
185 All PCD information of this module comes from memory dabase. The collection
186 work should be done before this action execution.
187 Currently, we should generated all PCD information(maybe all dynamic) as array
188 in Pei emulated driver for simulating PCD runtime database.
189
190 @throws BuildActionException Failed to execute this aciton class.
191 **/
192 public void performAction() {
193 EdkLog.log(EdkLog.EDK_DEBUG, "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
194
195 dbManager = GlobalData.getPCDMemoryDBManager();
196
197 if(dbManager.getDBSize() == 0) {
198 return;
199 }
200
201 EdkLog.log(EdkLog.EDK_DEBUG, "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens.");
202
203 generateAutogenForModule();
204 }
205
206 /**
207 Generate the autogen string for a common module.
208
209 All PCD information of this module comes from memory dabase. The collection
210 work should be done before this action execution.
211 **/
212 private void generateAutogenForModule()
213 {
214 int index, index2;
215 List<UsageInstance> usageInstanceArray, usageContext;
216 String[] guidStringArray = null;
217 String guidStringCName = null;
218 String guidString = null;
219 String moduleName = usageId.moduleName;
220 UsageInstance usageInstance = null;
221 boolean found = false;
222
223 usageInstanceArray = null;
224
225 if (FrameworkBuildTask.multithread) {
226 if (parentId == null) {
227 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);
228 } else if ((pcdNameArrayInMsa != null) && (pcdNameArrayInMsa.length > 0)) {
229 usageContext = dbManager.getUsageInstanceArrayById(parentId);
230 //
231 // For building library package, although all module are library, but PCD entries of
232 // these library should be used to autogen.
233 //
234 if (usageContext == null) {
235 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);
236 } else {
237 usageInstanceArray = new ArrayList<UsageInstance>();
238
239 //
240 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
241 // <ModuleSA> in FPD file.
242 //
243 for (index = 0; index < pcdNameArrayInMsa.length; index++) {
244 found = false;
245 for (index2 = 0; index2 < usageContext.size(); index2 ++) {
246 if (pcdNameArrayInMsa[index].equalsIgnoreCase(usageContext.get(index2).parentToken.cName)) {
247 usageInstanceArray.add(usageContext.get(index2));
248 found = true;
249 break;
250 }
251 }
252
253 if (!found) {
254 //
255 // All library's PCD should instanted in module's <ModuleSA> who
256 // use this library instance. If not, give errors.
257 //
258 throw new BuildActionException (String.format("Module %s using library instance %s; the PCD %s " +
259 "is required by this library instance, but can not be found " +
260 "in the %s's <ModuleSA> in the FPD file!",
261 MemoryDatabaseManager.CurrentModuleName,
262 moduleName,
263 pcdNameArrayInMsa[index],
264 MemoryDatabaseManager.CurrentModuleName
265 ));
266 }
267 }
268 }
269 }
270 } else {
271 if (!isBuildUsedLibrary) {
272 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);
273 MemoryDatabaseManager.UsageInstanceContext = usageInstanceArray;
274 MemoryDatabaseManager.CurrentModuleName = moduleName;
275 } else if ((pcdNameArrayInMsa != null) && (pcdNameArrayInMsa.length > 0)) {
276 usageContext = MemoryDatabaseManager.UsageInstanceContext;
277 //
278 // For building library package, although all module are library, but PCD entries of
279 // these library should be used to autogen.
280 //
281 if (usageContext == null) {
282 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);
283 } else {
284 usageInstanceArray = new ArrayList<UsageInstance>();
285
286 //
287 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
288 // <ModuleSA> in FPD file.
289 //
290 for (index = 0; index < pcdNameArrayInMsa.length; index++) {
291 found = false;
292 for (index2 = 0; index2 < usageContext.size(); index2 ++) {
293 if (pcdNameArrayInMsa[index].equalsIgnoreCase(usageContext.get(index2).parentToken.cName)) {
294 usageInstanceArray.add(usageContext.get(index2));
295 found = true;
296 break;
297 }
298 }
299
300 if (!found) {
301 //
302 // All library's PCD should instanted in module's <ModuleSA> who
303 // use this library instance. If not, give errors.
304 //
305 throw new BuildActionException (String.format("Module %s using library instance %s; the PCD %s " +
306 "is required by this library instance, but can not be found " +
307 "in the %s's <ModuleSA> in the FPD file!",
308 MemoryDatabaseManager.CurrentModuleName,
309 moduleName,
310 pcdNameArrayInMsa[index],
311 MemoryDatabaseManager.CurrentModuleName
312 ));
313 }
314 }
315 }
316 }
317 }
318 if (usageInstanceArray == null) {
319 return;
320 }
321
322 //
323 // Generate all PCD entry for a module.
324 //
325 for(index = 0; index < usageInstanceArray.size(); index ++) {
326 usageInstance = usageInstanceArray.get(index);
327 //
328 // Before generate any PCD information into autogen.h/autogen.c for a module,
329 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
330 // PCD in this module the token, they are all reference to TokenSpaceGuid
331 // array.
332 //
333 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
334 guidStringArray = usageInstance.parentToken.tokenSpaceName.split("-");
335 guidStringCName = "_gPcd_TokenSpaceGuid_" +
336 usageInstance.parentToken.tokenSpaceName.replaceAll("-", "_");
337 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}}",
338 guidStringArray[0],
339 guidStringArray[1],
340 guidStringArray[2],
341 (guidStringArray[3].substring(0, 2)),
342 (guidStringArray[3].substring(2, 4)),
343 (guidStringArray[4].substring(0, 2)),
344 (guidStringArray[4].substring(2, 4)),
345 (guidStringArray[4].substring(4, 6)),
346 (guidStringArray[4].substring(6, 8)),
347 (guidStringArray[4].substring(8, 10)),
348 (guidStringArray[4].substring(10, 12)));
349
350 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");
351 Matcher matcher = pattern.matcher(cAutoGenString + " ");
352 //
353 // Find whether this guid array variable has been generated into autogen.c
354 // For different DyanmicEx pcd token who use same token space guid, the token space
355 // guid array should be only generated once.
356 //
357 if (!matcher.find()) {
358 hAutoGenString += String.format("extern EFI_GUID %s;\r\n", guidStringCName);
359 if (!isBuildUsedLibrary) {
360 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
361 guidStringCName,
362 guidString);
363 }
364 }
365 }
366
367 usageInstance.generateAutoGen(isBuildUsedLibrary);
368 //
369 // For every PCD entry for this module(usage instance), autogen string would
370 // be appand.
371 //
372 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";
373 cAutoGenString += usageInstance.getCAutogenStr();
374 }
375
376 if (pcdDriverType == CommonDefinition.PCD_DRIVER_TYPE.PEI_PCD_DRIVER) {
377 hAutoGenString += MemoryDatabaseManager.PcdPeimHString;
378 cAutoGenString += MemoryDatabaseManager.PcdPeimCString;
379 } else if (pcdDriverType == CommonDefinition.PCD_DRIVER_TYPE.DXE_PCD_DRIVER) {
380 hAutoGenString += MemoryDatabaseManager.PcdDxeHString;
381 cAutoGenString += MemoryDatabaseManager.PcdDxeCString;
382 }
383 }
384 }