]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Modify autogen code for DynamicEx type PCD.
[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.io.File;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.UUID;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.xmlbeans.XmlObject;
29 import org.tianocore.build.global.GlobalData;
30 import org.tianocore.build.global.SurfaceAreaQuery;
31 import org.tianocore.build.pcd.entity.MemoryDatabaseManager;
32 import org.tianocore.build.pcd.entity.Token;
33 import org.tianocore.build.pcd.entity.UsageInstance;
34 import org.tianocore.build.pcd.exception.BuildActionException;
35 import org.tianocore.build.pcd.exception.EntityException;
36
37 /** This class is to manage how to generate the PCD information into Autogen.c and
38 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 /// The name of module which is analysised currently.
47 ///
48 private String moduleName;
49 ///
50 /// The Guid of module which is analyzed currently.
51 ///
52 private UUID moduleGuid;
53 ///
54 /// The name of package whose module is analysized currently.
55 ///
56 private String packageName;
57 ///
58 /// The Guid of package whose module is analyszed curretnly.
59 ///
60 private UUID packageGuid;
61 ///
62 /// The arch of current module
63 ///
64 private String arch;
65 ///
66 /// The version of current module
67 ///
68 private String version;
69 ///
70 /// Whether current autogen is for building library used by current module.
71 ///
72 private boolean isBuildUsedLibrary;
73 ///
74 /// The generated string for header file.
75 ///
76 private String hAutoGenString;
77 ///
78 /// The generated string for C code file.
79 ///
80 private String cAutoGenString;
81 ///
82 /// The name array of <PcdCoded> in a module.
83 ///
84 private String[] pcdNameArray;
85 /**
86 Set parameter ModuleName
87
88 @param moduleName the module name parameter.
89 **/
90 public void setModuleName(String moduleName) {
91 this.moduleName = moduleName;
92 }
93
94 /**
95 set the moduleGuid parameter.
96
97 @param moduleGuid
98 **/
99 public void setModuleGuid(UUID moduleGuid) {
100 this.moduleGuid = moduleGuid;
101 }
102
103 /**
104 set packageName parameter.
105
106 @param packageName
107 **/
108 public void setPackageName(String packageName) {
109 this.packageName = packageName;
110 }
111
112 /**
113 set packageGuid parameter.
114
115 @param packageGuid
116 **/
117 public void setPackageGuid(UUID packageGuid) {
118 this.packageGuid = packageGuid;
119 }
120
121 /**
122 set Arch parameter.
123
124 @param arch
125 **/
126 public void setArch(String arch) {
127 this.arch = arch;
128 }
129
130 /**
131 set version parameter
132
133 @param version
134 */
135 public void setVersion(String version) {
136 this.version = version;
137 }
138
139 /**
140 set isBuildUsedLibrary parameter.
141
142 @param isBuildUsedLibrary
143 */
144 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {
145 this.isBuildUsedLibrary = isBuildUsedLibrary;
146 }
147 /**
148 set pcdNameArray parameter.
149
150 @param pcdNameArray
151 */
152 public void setPcdNameArray(String[] pcdNameArray) {
153 this.pcdNameArray = pcdNameArray;
154 }
155
156 /**
157 Get the output of generated string for header file.
158
159 @return the string of header file for PCD
160 **/
161 public String OutputH() {
162 return hAutoGenString;
163 }
164
165 /**
166 Get the output of generated string for C Code file.
167
168 @return the string of C code file for PCD
169 **/
170 public String OutputC() {
171 return cAutoGenString;
172 }
173
174 /**
175 Construct function
176
177 This function mainly initialize some member variable.
178
179 @param moduleName Parameter of this action class.
180 @param isEmulatedPCDDriver Parameter of this action class.
181 **/
182 public PCDAutoGenAction(String moduleName,
183 UUID moduleGuid,
184 String packageName,
185 UUID packageGuid,
186 String arch,
187 String version,
188 boolean isBuildUsedLibrary,
189 String[] pcdNameArray) {
190 dbManager = null;
191 hAutoGenString = "";
192 cAutoGenString = "";
193
194 setModuleName(moduleName);
195 setModuleGuid(moduleGuid);
196 setPackageName(packageName);
197 setPackageGuid(packageGuid);
198 setPcdNameArray(pcdNameArray);
199 setArch(arch);
200 setVersion(version);
201 setIsBuildUsedLibrary(isBuildUsedLibrary);
202 }
203
204 /**
205 check the parameter for action class.
206
207 @throws BuildActionException Bad parameter.
208 **/
209 void checkParameter() throws BuildActionException {
210
211 }
212
213 /**
214 Core execution function for this action class.
215
216 All PCD information of this module comes from memory dabase. The collection
217 work should be done before this action execution.
218 Currently, we should generated all PCD information(maybe all dynamic) as array
219 in Pei emulated driver for simulating PCD runtime database.
220
221 @throws BuildActionException Failed to execute this aciton class.
222 **/
223 void performAction() throws BuildActionException {
224 ActionMessage.debug(this,
225 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
226 //
227 // Check the PCD memory database manager is valid.
228 //
229 if(GlobalData.getPCDMemoryDBManager() == null) {
230 throw new BuildActionException("Memory database has not been initlizated!");
231 }
232
233 dbManager = GlobalData.getPCDMemoryDBManager();
234
235 if(dbManager.getDBSize() == 0) {
236 return;
237 }
238
239 ActionMessage.debug(this,
240 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens");
241
242
243
244 generateAutogenForModule();
245 }
246
247 /**
248 Generate the autogen string for a common module.
249
250 All PCD information of this module comes from memory dabase. The collection
251 work should be done before this action execution.
252 **/
253 private void generateAutogenForModule()
254 {
255 int index, index2;
256 List<UsageInstance> usageInstanceArray, usageContext;
257 String[] guidStringArray = null;
258 String guidStringCName = null;
259 String guidString = null;
260 UsageInstance usageInstance = null;
261
262 if (!isBuildUsedLibrary) {
263 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,
264 moduleGuid,
265 packageName,
266 packageGuid,
267 arch,
268 version);
269 dbManager.UsageInstanceContext = usageInstanceArray;
270 dbManager.CurrentModuleName = moduleName;
271 } else {
272 usageContext = dbManager.UsageInstanceContext;
273 //
274 // For building MDE package, although all module are library, but PCD entries of
275 // these library should be used to autogen.
276 //
277 if (usageContext == null) {
278 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,
279 moduleGuid,
280 packageName,
281 packageGuid,
282 arch,
283 version);
284 } else {
285 usageInstanceArray = new ArrayList<UsageInstance>();
286 //
287 // Remove PCD entries which are not belong to this library.
288 //
289 for (index = 0; index < usageContext.size(); index++) {
290 if ((pcdNameArray == null) || (pcdNameArray.length == 0)){
291 break;
292 }
293
294 for (index2 = 0; index2 < pcdNameArray.length; index2 ++) {
295 if (pcdNameArray[index2].equalsIgnoreCase(usageContext.get(index).parentToken.cName)) {
296 usageInstanceArray.add(usageContext.get(index));
297 break;
298 }
299 }
300 }
301 }
302 }
303
304 if(usageInstanceArray.size() != 0) {
305 //
306 // Add "#include 'PcdLib.h'" for Header file
307 //
308 hAutoGenString = "#include <MdePkg/Include/Library/PcdLib.h>\r\n";
309 }
310
311 //
312 // Generate all PCD entry for a module.
313 //
314 for(index = 0; index < usageInstanceArray.size(); index ++) {
315 ActionMessage.debug(this,
316 "Module " + moduleName + "'s PCD [" + Integer.toHexString(index) +
317 "]: " + usageInstanceArray.get(index).parentToken.cName);
318 try {
319 usageInstance = usageInstanceArray.get(index);
320 //
321 // Before generate any PCD information into autogen.h/autogen.c for a module,
322 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
323 // PCD in this module the token, they are all reference to TokenSpaceGuid
324 // array.
325 //
326 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
327 guidStringArray = usageInstance.parentToken.tokenSpaceName.toString().split("-");
328 guidStringCName = "_gPcd_TokenSpaceGuid_" +
329 usageInstance.parentToken.tokenSpaceName.toString().replaceAll("-", "_");
330 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}}",
331 guidStringArray[0],
332 guidStringArray[1],
333 guidStringArray[2],
334 (guidStringArray[3].substring(0, 2)),
335 (guidStringArray[3].substring(2, 4)),
336 (guidStringArray[4].substring(0, 2)),
337 (guidStringArray[4].substring(2, 4)),
338 (guidStringArray[4].substring(4, 6)),
339 (guidStringArray[4].substring(6, 8)),
340 (guidStringArray[4].substring(8, 10)),
341 (guidStringArray[4].substring(10, 12)));
342 if (!isBuildUsedLibrary) {
343 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");
344 Matcher matcher = pattern.matcher(cAutoGenString + " ");
345 //
346 // Find whether this guid array variable has been generated into autogen.c
347 // For different DyanmicEx pcd token who use same token space guid, the token space
348 // guid array should be only generated once.
349 //
350 if (!matcher.find()) {
351 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
352 guidStringCName,
353 guidString);
354 }
355 }
356 }
357
358 usageInstance.generateAutoGen(isBuildUsedLibrary);
359 //
360 // For every PCD entry for this module(usage instance), autogen string would
361 // be appand.
362 //
363 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";
364 cAutoGenString += usageInstance.getCAutogenStr();
365
366 } catch(EntityException exp) {
367 throw new BuildActionException("[PCD Autogen Error]: " + exp.getMessage());
368 }
369 }
370
371 //
372 // Work around code, In furture following code should be modified that get
373 // these information from Uplevel Autogen tools.
374 //
375 if (moduleName.equalsIgnoreCase("PcdPeim")) {
376 hAutoGenString += dbManager.PcdPeimHString;
377 cAutoGenString += dbManager.PcdPeimCString;
378 } else if (moduleName.equalsIgnoreCase("PcdDxe")) {
379 hAutoGenString += dbManager.PcdDxeHString;
380 cAutoGenString += dbManager.PcdDxeCString;
381 }
382
383 ActionMessage.debug(this,
384 "Module " + moduleName + "'s PCD header file:\r\n" + hAutoGenString + "\r\n"
385 );
386 ActionMessage.debug(this,
387 "Module " + moduleName + "'s PCD C file:\r\n" + cAutoGenString + "\r\n"
388 );
389 }
390
391 /**
392 Test case function
393
394 @param argv paramter from command line
395 **/
396 public static void main(String argv[]) {
397
398 String WorkSpace = "M:/tianocore/edk2";
399 String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd";
400 String[] nameArray = null;
401
402 //
403 // At first, CollectPCDAction should be invoked to collect
404 // all PCD information from SPD, MSA, FPD.
405 //
406 CollectPCDAction collectionAction = new CollectPCDAction();
407 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",
408 WorkSpace);
409
410 try {
411 collectionAction.perform(WorkSpace,
412 logFilePath,
413 ActionMessage.MAX_MESSAGE_LEVEL);
414 } catch(Exception e) {
415 e.printStackTrace();
416 }
417
418 //
419 // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c
420 //
421 PCDAutoGenAction autogenAction = new PCDAutoGenAction("MonoStatusCode",
422 null,
423 null,
424 null,
425 "IA32",
426 null,
427 false,
428 nameArray);
429 autogenAction.execute();
430
431 System.out.println(autogenAction.OutputH());
432 System.out.println("WQWQWQWQWQ");
433 System.out.println(autogenAction.OutputC());
434 }
435 }