]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
1. PcdUsage is added to PCD entry in schema so wizard tool is also updated to support...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / PCDAutoGenAction.java
... / ...
CommitLineData
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
20import java.util.ArrayList;\r
21import java.util.List;\r
22import java.util.Map;\r
23import java.util.Set;\r
24import java.util.UUID;\r
25import java.util.regex.Matcher;\r
26import java.util.regex.Pattern;\r
27\r
28import org.apache.xmlbeans.XmlObject;\r
29import org.tianocore.build.global.GlobalData;\r
30import org.tianocore.build.global.SurfaceAreaQuery;\r
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
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
70 /// Whether current autogen is for building library used by current module.\r
71 /// \r
72 private boolean isBuildUsedLibrary;\r
73 ///\r
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
80 private String cAutoGenString;\r
81 ///\r
82 /// The name array of <PcdCoded> in a module.\r
83 /// \r
84 private String[] pcdNameArray;\r
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
94 /**\r
95 set the moduleGuid parameter.\r
96 \r
97 @param moduleGuid\r
98 **/\r
99 public void setModuleGuid(UUID moduleGuid) {\r
100 this.moduleGuid = moduleGuid;\r
101 }\r
102\r
103 /**\r
104 set packageName parameter.\r
105 \r
106 @param packageName\r
107 **/\r
108 public void setPackageName(String packageName) {\r
109 this.packageName = packageName;\r
110 }\r
111\r
112 /**\r
113 set packageGuid parameter.\r
114 \r
115 @param packageGuid\r
116 **/\r
117 public void setPackageGuid(UUID packageGuid) {\r
118 this.packageGuid = packageGuid;\r
119 }\r
120\r
121 /**\r
122 set Arch parameter.\r
123 \r
124 @param arch\r
125 **/\r
126 public void setArch(String arch) {\r
127 this.arch = arch;\r
128 }\r
129\r
130 /**\r
131 set version parameter\r
132 \r
133 @param version\r
134 */\r
135 public void setVersion(String version) {\r
136 this.version = version;\r
137 }\r
138\r
139 /**\r
140 set isBuildUsedLibrary parameter.\r
141 \r
142 @param isBuildUsedLibrary\r
143 */\r
144 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {\r
145 this.isBuildUsedLibrary = isBuildUsedLibrary;\r
146 }\r
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
155\r
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
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
205 /**\r
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
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
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
327 \r
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
334 \r
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
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
360 return;\r
361 }\r
362\r
363 ActionMessage.debug(this,\r
364 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens");\r
365\r
366\r
367\r
368 generateAutogenForModule();\r
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
379 int index, index2;\r
380 List<UsageInstance> usageInstanceArray, usageContext;\r
381 String[] guidStringArray = null;\r
382 String guidStringCName = null;\r
383 String guidString = null;\r
384 UsageInstance usageInstance = null;\r
385\r
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
396 usageContext = dbManager.UsageInstanceContext;\r
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
401 if (usageContext == null) {\r
402 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,\r
403 moduleGuid,\r
404 packageName,\r
405 packageGuid,\r
406 arch,\r
407 version);\r
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
425 }\r
426 }\r
427\r
428 //\r
429 // Generate all PCD entry for a module.\r
430 // \r
431 for(index = 0; index < usageInstanceArray.size(); index ++) {\r
432 ActionMessage.debug(this,\r
433 "Module " + moduleName + "'s PCD [" + Integer.toHexString(index) + \r
434 "]: " + usageInstanceArray.get(index).parentToken.cName);\r
435 try {\r
436 usageInstance = usageInstanceArray.get(index);\r
437 //\r
438 // Before generate any PCD information into autogen.h/autogen.c for a module,\r
439 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type\r
440 // PCD in this module the token, they are all reference to TokenSpaceGuid \r
441 // array.\r
442 // \r
443 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
444 guidStringArray = usageInstance.parentToken.tokenSpaceName.toString().split("-");\r
445 guidStringCName = "_gPcd_TokenSpaceGuid_" + \r
446 usageInstance.parentToken.tokenSpaceName.toString().replaceAll("-", "_");\r
447 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
448 guidStringArray[0],\r
449 guidStringArray[1],\r
450 guidStringArray[2],\r
451 (guidStringArray[3].substring(0, 2)),\r
452 (guidStringArray[3].substring(2, 4)),\r
453 (guidStringArray[4].substring(0, 2)),\r
454 (guidStringArray[4].substring(2, 4)),\r
455 (guidStringArray[4].substring(4, 6)),\r
456 (guidStringArray[4].substring(6, 8)),\r
457 (guidStringArray[4].substring(8, 10)),\r
458 (guidStringArray[4].substring(10, 12)));\r
459 \r
460 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");\r
461 Matcher matcher = pattern.matcher(cAutoGenString + " ");\r
462 //\r
463 // Find whether this guid array variable has been generated into autogen.c\r
464 // For different DyanmicEx pcd token who use same token space guid, the token space\r
465 // guid array should be only generated once.\r
466 // \r
467 if (!matcher.find()) {\r
468 hAutoGenString += String.format("extern EFI_GUID %s;\r\n",\r
469 guidStringCName);\r
470 if (!isBuildUsedLibrary) {\r
471 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",\r
472 guidStringCName,\r
473 guidString);\r
474 } \r
475 }\r
476 }\r
477\r
478 usageInstance.generateAutoGen(isBuildUsedLibrary);\r
479 //\r
480 // For every PCD entry for this module(usage instance), autogen string would\r
481 // be appand.\r
482 // \r
483 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";\r
484 cAutoGenString += usageInstance.getCAutogenStr();\r
485\r
486 } catch(EntityException exp) {\r
487 throw new BuildActionException("[PCD Autogen Error]: " + exp.getMessage());\r
488 }\r
489 }\r
490\r
491 //\r
492 // Work around code, In furture following code should be modified that get \r
493 // these information from Uplevel Autogen tools.\r
494 // \r
495 if (moduleName.equalsIgnoreCase("PcdPeim")) {\r
496 hAutoGenString += dbManager.PcdPeimHString;\r
497 cAutoGenString += dbManager.PcdPeimCString;\r
498 } else if (moduleName.equalsIgnoreCase("PcdDxe")) {\r
499 hAutoGenString += dbManager.PcdDxeHString;\r
500 cAutoGenString += dbManager.PcdDxeCString;\r
501 }\r
502\r
503 ActionMessage.debug(this,\r
504 "Module " + moduleName + "'s PCD header file:\r\n" + hAutoGenString + "\r\n"\r
505 );\r
506 ActionMessage.debug(this,\r
507 "Module " + moduleName + "'s PCD C file:\r\n" + cAutoGenString + "\r\n"\r
508 );\r
509 }\r
510\r
511 /**\r
512 Test case function\r
513\r
514 @param argv paramter from command line\r
515 **/\r
516 public static void main(String argv[]) {\r
517\r
518 String WorkSpace = "X:/edk2";\r
519 String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd";\r
520 String[] nameArray = null;\r
521\r
522 //\r
523 // At first, CollectPCDAction should be invoked to collect\r
524 // all PCD information from SPD, MSA, FPD.\r
525 //\r
526 CollectPCDAction collectionAction = new CollectPCDAction();\r
527 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",\r
528 WorkSpace,null);\r
529\r
530 try {\r
531 collectionAction.perform(WorkSpace, \r
532 logFilePath,\r
533 ActionMessage.MAX_MESSAGE_LEVEL);\r
534 } catch(Exception e) {\r
535 e.printStackTrace();\r
536 }\r
537\r
538 //\r
539 // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c\r
540 //\r
541// PCDAutoGenAction autogenAction = new PCDAutoGenAction("MonoStatusCode",\r
542// null,\r
543// null,\r
544// null,\r
545// "IA32",\r
546// null,\r
547// false,\r
548// nameArray);\r
549// autogenAction.execute();\r
550//\r
551// System.out.println(autogenAction.OutputH());\r
552// System.out.println("WQWQWQWQWQ");\r
553// System.out.println(autogenAction.OutputC());\r
554 }\r
555}\r