]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Clean up for debug message of PCD tools.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / PCDAutoGenAction.java
CommitLineData
878ddf1f 1/** @file\r
2 PCDAutoGenAction class.\r
3\r
4 This class is to manage how to generate the PCD information into Autogen.c and\r
5 Autogen.h.\r
6 \r
7Copyright (c) 2006, Intel Corporation\r
8All rights reserved. This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12 \r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17package org.tianocore.build.pcd.action;\r
18\r
19import java.io.File;\r
11e7b0f6 20import java.util.ArrayList;\r
878ddf1f 21import java.util.List;\r
136adffc 22import java.util.Map;\r
23import java.util.Set;\r
8840ad58 24import java.util.UUID;\r
11e7b0f6 25import java.util.regex.Matcher;\r
26import java.util.regex.Pattern;\r
878ddf1f 27\r
136adffc 28import org.apache.xmlbeans.XmlObject;\r
878ddf1f 29import org.tianocore.build.global.GlobalData;\r
136adffc 30import org.tianocore.build.global.SurfaceAreaQuery;\r
878ddf1f 31import org.tianocore.build.pcd.entity.MemoryDatabaseManager;\r
32import org.tianocore.build.pcd.entity.Token;\r
33import org.tianocore.build.pcd.entity.UsageInstance;\r
34import org.tianocore.build.pcd.exception.BuildActionException;\r
35import org.tianocore.build.pcd.exception.EntityException;\r
36\r
37/** This class is to manage how to generate the PCD information into Autogen.c and\r
38 Autogen.h.\r
39**/\r
40public class PCDAutoGenAction extends BuildAction {\r
41 ///\r
42 /// The reference of DBManager in GlobalData class.\r
43 ///\r
44 private MemoryDatabaseManager dbManager;\r
45 ///\r
46 /// The name of module which is analysised currently.\r
47 ///\r
48 private String moduleName;\r
49 ///\r
8840ad58 50 /// The Guid of module which is analyzed currently.\r
51 /// \r
52 private UUID moduleGuid;\r
53 ///\r
54 /// The name of package whose module is analysized currently.\r
55 /// \r
56 private String packageName;\r
57 ///\r
58 /// The Guid of package whose module is analyszed curretnly.\r
59 /// \r
60 private UUID packageGuid;\r
61 ///\r
62 /// The arch of current module\r
63 /// \r
64 private String arch;\r
65 ///\r
66 /// The version of current module\r
67 /// \r
68 private String version;\r
69 ///\r
8840ad58 70 /// Whether current autogen is for building library used by current module.\r
71 /// \r
72 private boolean isBuildUsedLibrary;\r
73 ///\r
878ddf1f 74 /// The generated string for header file.\r
75 ///\r
76 private String hAutoGenString;\r
77 ///\r
78 /// The generated string for C code file.\r
79 ///\r
ad82307c 80 private String cAutoGenString;\r
81 ///\r
82 /// The name array of <PcdCoded> in a module.\r
83 /// \r
84 private String[] pcdNameArray;\r
878ddf1f 85 /**\r
86 Set parameter ModuleName\r
87 \r
88 @param moduleName the module name parameter.\r
89 **/\r
90 public void setModuleName(String moduleName) {\r
91 this.moduleName = moduleName;\r
92 }\r
93\r
ad82307c 94 /**\r
95 set the moduleGuid parameter.\r
96 \r
97 @param moduleGuid\r
98 **/\r
8840ad58 99 public void setModuleGuid(UUID moduleGuid) {\r
100 this.moduleGuid = moduleGuid;\r
101 }\r
102\r
ad82307c 103 /**\r
104 set packageName parameter.\r
105 \r
106 @param packageName\r
107 **/\r
8840ad58 108 public void setPackageName(String packageName) {\r
109 this.packageName = packageName;\r
110 }\r
111\r
ad82307c 112 /**\r
113 set packageGuid parameter.\r
114 \r
115 @param packageGuid\r
116 **/\r
8840ad58 117 public void setPackageGuid(UUID packageGuid) {\r
118 this.packageGuid = packageGuid;\r
119 }\r
120\r
ad82307c 121 /**\r
122 set Arch parameter.\r
123 \r
124 @param arch\r
125 **/\r
8840ad58 126 public void setArch(String arch) {\r
127 this.arch = arch;\r
128 }\r
129\r
ad82307c 130 /**\r
131 set version parameter\r
132 \r
133 @param version\r
134 */\r
8840ad58 135 public void setVersion(String version) {\r
136 this.version = version;\r
137 }\r
138\r
878ddf1f 139 /**\r
ad82307c 140 set isBuildUsedLibrary parameter.\r
141 \r
142 @param isBuildUsedLibrary\r
143 */\r
8840ad58 144 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {\r
145 this.isBuildUsedLibrary = isBuildUsedLibrary;\r
146 }\r
ad82307c 147 /**\r
148 set pcdNameArray parameter.\r
149 \r
150 @param pcdNameArray\r
151 */\r
152 public void setPcdNameArray(String[] pcdNameArray) {\r
153 this.pcdNameArray = pcdNameArray;\r
154 }\r
8840ad58 155\r
878ddf1f 156 /**\r
157 Get the output of generated string for header file.\r
158 \r
159 @return the string of header file for PCD\r
160 **/\r
161 public String OutputH() {\r
162 return hAutoGenString;\r
163 }\r
164\r
165 /**\r
166 Get the output of generated string for C Code file.\r
167 \r
168 @return the string of C code file for PCD\r
169 **/\r
170 public String OutputC() {\r
171 return cAutoGenString;\r
172 }\r
173\r
136adffc 174// /**\r
175// Construct function\r
176// \r
177// This function mainly initialize some member variable.\r
178// \r
179// @param moduleName Parameter of this action class.\r
180// @param isEmulatedPCDDriver Parameter of this action class.\r
181// **/\r
182// public PCDAutoGenAction(String moduleName, \r
183// UUID moduleGuid, \r
184// String packageName,\r
185// UUID packageGuid,\r
186// String arch,\r
187// String version,\r
188// boolean isBuildUsedLibrary,\r
189// String[] pcdNameArray) {\r
190// dbManager = null;\r
191// hAutoGenString = "";\r
192// cAutoGenString = "";\r
193//\r
194// setModuleName(moduleName);\r
195// setModuleGuid(moduleGuid);\r
196// setPackageName(packageName);\r
197// setPackageGuid(packageGuid);\r
198// setPcdNameArray(pcdNameArray);\r
199// setArch(arch);\r
200// setVersion(version);\r
201// setIsBuildUsedLibrary(isBuildUsedLibrary);\r
202// }\r
203\r
204 \r
878ddf1f 205 /**\r
136adffc 206 Construct function\r
207\r
208 This function mainly initialize some member variable.\r
209 \r
210 @param moduleName Parameter of this action class.\r
211 @param isEmulatedPCDDriver Parameter of this action class.\r
212 **/\r
213 public PCDAutoGenAction(String moduleName, \r
214 String moduleGuidString, \r
215 String packageName,\r
216 String packageGuidString,\r
217 String arch,\r
218 String version,\r
219 boolean isBuildUsedLibrary,\r
220 String[] pcdNameArray) \r
221 throws BuildActionException {\r
222 dbManager = null;\r
223 hAutoGenString = "";\r
224 cAutoGenString = "";\r
225 try {\r
e39bb31c 226 setModuleName(moduleName);\r
227 setModuleGuid(translateSchemaStringToUUID(moduleGuidString));\r
228 setPackageName(packageName);\r
229 setPackageGuid(translateSchemaStringToUUID(packageGuidString));\r
230 setPcdNameArray(pcdNameArray);\r
231 setArch(arch);\r
232 setVersion(version);\r
233 setIsBuildUsedLibrary(isBuildUsedLibrary);\r
136adffc 234 } catch (EntityException e){\r
235 throw new BuildActionException(e.getMessage());\r
236 }\r
237 }\r
238\r
239 /**\r
240 Translate the schema string to UUID instance.\r
241 \r
242 In schema, the string of UUID is defined as following two types string:\r
243 1) GuidArrayType: pattern = 0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},(\r
244 )*0x[a-fA-F0-9]{1,4}(,( )*\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\})?\r
245 \r
246 2) GuidNamingConvention: pattern =\r
247 [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\r
248 \r
249 This function will convert string and create uuid instance.\r
250 \r
251 @param uuidString UUID string in XML file\r
252 \r
253 @return UUID UUID instance\r
254**/\r
255private UUID translateSchemaStringToUUID(String uuidString) \r
256 throws EntityException {\r
257 String temp;\r
258 String[] splitStringArray;\r
259 int index;\r
260 int chIndex;\r
261 int chLen;\r
262\r
263 if (uuidString == null) {\r
264 return null;\r
265 }\r
266\r
267 if (uuidString.length() == 0) {\r
268 return null;\r
269 }\r
270\r
271 if (uuidString.equals("0") ||\r
272 uuidString.equalsIgnoreCase("0x0")) {\r
273 return new UUID(0, 0);\r
274 }\r
275\r
276 uuidString = uuidString.replaceAll("\\{", "");\r
277 uuidString = uuidString.replaceAll("\\}", "");\r
278\r
279 //\r
280 // If the UUID schema string is GuidArrayType type then need translate \r
281 // to GuidNamingConvention type at first.\r
282 // \r
283 if ((uuidString.charAt(0) == '0') && ((uuidString.charAt(1) == 'x') || (uuidString.charAt(1) == 'X'))) {\r
284 splitStringArray = uuidString.split("," );\r
285 if (splitStringArray.length != 11) {\r
286 throw new EntityException ("[FPD file error] Wrong format for UUID string: " + uuidString);\r
287 }\r
288\r
289 //\r
290 // Remove blank space from these string and remove header string "0x"\r
291 // \r
292 for (index = 0; index < 11; index ++) {\r
293 splitStringArray[index] = splitStringArray[index].trim();\r
294 splitStringArray[index] = splitStringArray[index].substring(2, splitStringArray[index].length());\r
295 }\r
296\r
297 //\r
298 // Add heading '0' to normalize the string length\r
299 // \r
300 for (index = 3; index < 11; index ++) {\r
301 chLen = splitStringArray[index].length();\r
302 for (chIndex = 0; chIndex < 2 - chLen; chIndex ++) {\r
303 splitStringArray[index] = "0" + splitStringArray[index];\r
304 }\r
305 }\r
306\r
307 //\r
308 // construct the final GuidNamingConvention string\r
309 // \r
310 temp = String.format("%s-%s-%s-%s%s-%s%s%s%s%s%s",\r
311 splitStringArray[0],\r
312 splitStringArray[1],\r
313 splitStringArray[2],\r
314 splitStringArray[3],\r
315 splitStringArray[4],\r
316 splitStringArray[5],\r
317 splitStringArray[6],\r
318 splitStringArray[7],\r
319 splitStringArray[8],\r
320 splitStringArray[9],\r
321 splitStringArray[10]);\r
322 uuidString = temp;\r
323 }\r
324\r
325 return UUID.fromString(uuidString);\r
326 }\r
878ddf1f 327 \r
878ddf1f 328 /**\r
329 check the parameter for action class.\r
330 \r
331 @throws BuildActionException Bad parameter.\r
332 **/\r
333 void checkParameter() throws BuildActionException {\r
ad82307c 334 \r
878ddf1f 335 }\r
336\r
337 /**\r
338 Core execution function for this action class.\r
339 \r
340 All PCD information of this module comes from memory dabase. The collection\r
341 work should be done before this action execution.\r
342 Currently, we should generated all PCD information(maybe all dynamic) as array \r
343 in Pei emulated driver for simulating PCD runtime database. \r
344 \r
345 @throws BuildActionException Failed to execute this aciton class.\r
346 **/\r
347 void performAction() throws BuildActionException {\r
348 ActionMessage.debug(this, \r
349 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");\r
845fdeba 350 //\r
351 // Check the PCD memory database manager is valid.\r
352 //\r
353 if(GlobalData.getPCDMemoryDBManager() == null) {\r
354 throw new BuildActionException("Memory database has not been initlizated!");\r
355 }\r
356\r
357 dbManager = GlobalData.getPCDMemoryDBManager();\r
358\r
359 if(dbManager.getDBSize() == 0) {\r
8840ad58 360 return;\r
845fdeba 361 }\r
362\r
363 ActionMessage.debug(this,\r
364 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens");\r
878ddf1f 365\r
878ddf1f 366\r
8840ad58 367\r
368 generateAutogenForModule();\r
878ddf1f 369 }\r
370\r
371 /**\r
372 Generate the autogen string for a common module.\r
373 \r
374 All PCD information of this module comes from memory dabase. The collection\r
375 work should be done before this action execution.\r
376 **/\r
377 private void generateAutogenForModule()\r
378 {\r
ad82307c 379 int index, index2;\r
380 List<UsageInstance> usageInstanceArray, usageContext;\r
11e7b0f6 381 String[] guidStringArray = null;\r
382 String guidStringCName = null;\r
383 String guidString = null;\r
384 UsageInstance usageInstance = null;\r
878ddf1f 385\r
8840ad58 386 if (!isBuildUsedLibrary) {\r
387 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,\r
388 moduleGuid,\r
389 packageName,\r
390 packageGuid,\r
391 arch,\r
392 version);\r
393 dbManager.UsageInstanceContext = usageInstanceArray;\r
394 dbManager.CurrentModuleName = moduleName; \r
395 } else {\r
ad82307c 396 usageContext = dbManager.UsageInstanceContext;\r
8840ad58 397 //\r
398 // For building MDE package, although all module are library, but PCD entries of \r
399 // these library should be used to autogen.\r
400 // \r
ad82307c 401 if (usageContext == null) {\r
8840ad58 402 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,\r
403 moduleGuid,\r
404 packageName,\r
405 packageGuid,\r
406 arch,\r
407 version);\r
ad82307c 408 } else {\r
409 usageInstanceArray = new ArrayList<UsageInstance>();\r
410 //\r
411 // Remove PCD entries which are not belong to this library.\r
412 // \r
413 for (index = 0; index < usageContext.size(); index++) {\r
414 if ((pcdNameArray == null) || (pcdNameArray.length == 0)){\r
415 break;\r
416 }\r
417\r
418 for (index2 = 0; index2 < pcdNameArray.length; index2 ++) {\r
419 if (pcdNameArray[index2].equalsIgnoreCase(usageContext.get(index).parentToken.cName)) {\r
420 usageInstanceArray.add(usageContext.get(index));\r
421 break;\r
422 }\r
423 }\r
424 }\r
8840ad58 425 }\r
426 }\r
878ddf1f 427\r
428 if(usageInstanceArray.size() != 0) {\r
429 //\r
430 // Add "#include 'PcdLib.h'" for Header file\r
431 //\r
432 hAutoGenString = "#include <MdePkg/Include/Library/PcdLib.h>\r\n";\r
433 }\r
434\r
11e7b0f6 435 //\r
436 // Generate all PCD entry for a module.\r
437 // \r
878ddf1f 438 for(index = 0; index < usageInstanceArray.size(); index ++) {\r
439 ActionMessage.debug(this,\r
440 "Module " + moduleName + "'s PCD [" + Integer.toHexString(index) + \r
441 "]: " + usageInstanceArray.get(index).parentToken.cName);\r
442 try {\r
11e7b0f6 443 usageInstance = usageInstanceArray.get(index);\r
444 //\r
445 // Before generate any PCD information into autogen.h/autogen.c for a module,\r
446 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type\r
447 // PCD in this module the token, they are all reference to TokenSpaceGuid \r
448 // array.\r
449 // \r
450 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
451 guidStringArray = usageInstance.parentToken.tokenSpaceName.toString().split("-");\r
452 guidStringCName = "_gPcd_TokenSpaceGuid_" + \r
453 usageInstance.parentToken.tokenSpaceName.toString().replaceAll("-", "_");\r
454 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}}",\r
455 guidStringArray[0],\r
456 guidStringArray[1],\r
457 guidStringArray[2],\r
458 (guidStringArray[3].substring(0, 2)),\r
459 (guidStringArray[3].substring(2, 4)),\r
460 (guidStringArray[4].substring(0, 2)),\r
461 (guidStringArray[4].substring(2, 4)),\r
462 (guidStringArray[4].substring(4, 6)),\r
463 (guidStringArray[4].substring(6, 8)),\r
464 (guidStringArray[4].substring(8, 10)),\r
465 (guidStringArray[4].substring(10, 12)));\r
a49b34c0 466 \r
467 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");\r
468 Matcher matcher = pattern.matcher(cAutoGenString + " ");\r
469 //\r
470 // Find whether this guid array variable has been generated into autogen.c\r
471 // For different DyanmicEx pcd token who use same token space guid, the token space\r
472 // guid array should be only generated once.\r
473 // \r
474 if (!matcher.find()) {\r
475 hAutoGenString += String.format("extern EFI_GUID %s;\r\n",\r
476 guidStringCName);\r
477 if (!isBuildUsedLibrary) {\r
11e7b0f6 478 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",\r
479 guidStringCName,\r
480 guidString);\r
a49b34c0 481 } \r
11e7b0f6 482 }\r
483 }\r
484\r
485 usageInstance.generateAutoGen(isBuildUsedLibrary);\r
486 //\r
487 // For every PCD entry for this module(usage instance), autogen string would\r
488 // be appand.\r
489 // \r
490 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";\r
491 cAutoGenString += usageInstance.getCAutogenStr();\r
492\r
878ddf1f 493 } catch(EntityException exp) {\r
11e7b0f6 494 throw new BuildActionException("[PCD Autogen Error]: " + exp.getMessage());\r
878ddf1f 495 }\r
496 }\r
497\r
8840ad58 498 //\r
499 // Work around code, In furture following code should be modified that get \r
500 // these information from Uplevel Autogen tools.\r
501 // \r
32648c62 502 if (moduleName.equalsIgnoreCase("PcdPeim")) {\r
503 hAutoGenString += dbManager.PcdPeimHString;\r
504 cAutoGenString += dbManager.PcdPeimCString;\r
505 } else if (moduleName.equalsIgnoreCase("PcdDxe")) {\r
506 hAutoGenString += dbManager.PcdDxeHString;\r
507 cAutoGenString += dbManager.PcdDxeCString;\r
508 }\r
99d2c3c4 509\r
878ddf1f 510 ActionMessage.debug(this,\r
511 "Module " + moduleName + "'s PCD header file:\r\n" + hAutoGenString + "\r\n"\r
512 );\r
513 ActionMessage.debug(this,\r
514 "Module " + moduleName + "'s PCD C file:\r\n" + cAutoGenString + "\r\n"\r
515 );\r
516 }\r
517\r
878ddf1f 518 /**\r
519 Test case function\r
520\r
521 @param argv paramter from command line\r
522 **/\r
523 public static void main(String argv[]) {\r
99d2c3c4 524\r
58f1099f 525 String WorkSpace = "X:/edk2";\r
11e7b0f6 526 String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd";\r
ad82307c 527 String[] nameArray = null;\r
878ddf1f 528\r
529 //\r
530 // At first, CollectPCDAction should be invoked to collect\r
531 // all PCD information from SPD, MSA, FPD.\r
532 //\r
533 CollectPCDAction collectionAction = new CollectPCDAction();\r
534 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",\r
136adffc 535 WorkSpace,null);\r
878ddf1f 536\r
878ddf1f 537 try {\r
99d2c3c4 538 collectionAction.perform(WorkSpace, \r
878ddf1f 539 logFilePath,\r
540 ActionMessage.MAX_MESSAGE_LEVEL);\r
541 } catch(Exception e) {\r
542 e.printStackTrace();\r
543 }\r
544\r
545 //\r
546 // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c\r
547 //\r
136adffc 548// PCDAutoGenAction autogenAction = new PCDAutoGenAction("MonoStatusCode",\r
549// null,\r
550// null,\r
551// null,\r
552// "IA32",\r
553// null,\r
554// false,\r
555// nameArray);\r
556// autogenAction.execute();\r
557//\r
558// System.out.println(autogenAction.OutputH());\r
559// System.out.println("WQWQWQWQWQ");\r
560// System.out.println(autogenAction.OutputC());\r
878ddf1f 561 }\r
562}\r