]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Use the address of guid array as parameter directly for _PCD_MODE_xx macro for Dynami...
[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
ad82307c 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
ad82307c 28import org.apache.xmlbeans.XmlObject;\r
878ddf1f 29import org.tianocore.build.global.GlobalData;\r
ad82307c 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
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
ad82307c 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
8840ad58 190 dbManager = null;\r
191 hAutoGenString = "";\r
192 cAutoGenString = "";\r
193\r
878ddf1f 194 setModuleName(moduleName);\r
8840ad58 195 setModuleGuid(moduleGuid);\r
196 setPackageName(packageName);\r
197 setPackageGuid(packageGuid);\r
ad82307c 198 setPcdNameArray(pcdNameArray);\r
8840ad58 199 setArch(arch);\r
200 setVersion(version);\r
201 setIsBuildUsedLibrary(isBuildUsedLibrary);\r
878ddf1f 202 }\r
203\r
204 /**\r
205 check the parameter for action class.\r
206 \r
207 @throws BuildActionException Bad parameter.\r
208 **/\r
209 void checkParameter() throws BuildActionException {\r
ad82307c 210 \r
878ddf1f 211 }\r
212\r
213 /**\r
214 Core execution function for this action class.\r
215 \r
216 All PCD information of this module comes from memory dabase. The collection\r
217 work should be done before this action execution.\r
218 Currently, we should generated all PCD information(maybe all dynamic) as array \r
219 in Pei emulated driver for simulating PCD runtime database. \r
220 \r
221 @throws BuildActionException Failed to execute this aciton class.\r
222 **/\r
223 void performAction() throws BuildActionException {\r
224 ActionMessage.debug(this, \r
225 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");\r
845fdeba 226 //\r
227 // Check the PCD memory database manager is valid.\r
228 //\r
229 if(GlobalData.getPCDMemoryDBManager() == null) {\r
230 throw new BuildActionException("Memory database has not been initlizated!");\r
231 }\r
232\r
233 dbManager = GlobalData.getPCDMemoryDBManager();\r
234\r
235 if(dbManager.getDBSize() == 0) {\r
8840ad58 236 return;\r
845fdeba 237 }\r
238\r
239 ActionMessage.debug(this,\r
240 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens");\r
878ddf1f 241\r
878ddf1f 242\r
8840ad58 243\r
244 generateAutogenForModule();\r
878ddf1f 245 }\r
246\r
247 /**\r
248 Generate the autogen string for a common module.\r
249 \r
250 All PCD information of this module comes from memory dabase. The collection\r
251 work should be done before this action execution.\r
252 **/\r
253 private void generateAutogenForModule()\r
254 {\r
ad82307c 255 int index, index2;\r
256 List<UsageInstance> usageInstanceArray, usageContext;\r
11e7b0f6 257 String[] guidStringArray = null;\r
258 String guidStringCName = null;\r
259 String guidString = null;\r
260 UsageInstance usageInstance = null;\r
878ddf1f 261\r
8840ad58 262 if (!isBuildUsedLibrary) {\r
263 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,\r
264 moduleGuid,\r
265 packageName,\r
266 packageGuid,\r
267 arch,\r
268 version);\r
269 dbManager.UsageInstanceContext = usageInstanceArray;\r
270 dbManager.CurrentModuleName = moduleName; \r
271 } else {\r
ad82307c 272 usageContext = dbManager.UsageInstanceContext;\r
8840ad58 273 //\r
274 // For building MDE package, although all module are library, but PCD entries of \r
275 // these library should be used to autogen.\r
276 // \r
ad82307c 277 if (usageContext == null) {\r
8840ad58 278 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,\r
279 moduleGuid,\r
280 packageName,\r
281 packageGuid,\r
282 arch,\r
283 version);\r
ad82307c 284 } else {\r
285 usageInstanceArray = new ArrayList<UsageInstance>();\r
286 //\r
287 // Remove PCD entries which are not belong to this library.\r
288 // \r
289 for (index = 0; index < usageContext.size(); index++) {\r
290 if ((pcdNameArray == null) || (pcdNameArray.length == 0)){\r
291 break;\r
292 }\r
293\r
294 for (index2 = 0; index2 < pcdNameArray.length; index2 ++) {\r
295 if (pcdNameArray[index2].equalsIgnoreCase(usageContext.get(index).parentToken.cName)) {\r
296 usageInstanceArray.add(usageContext.get(index));\r
297 break;\r
298 }\r
299 }\r
300 }\r
8840ad58 301 }\r
302 }\r
878ddf1f 303\r
304 if(usageInstanceArray.size() != 0) {\r
305 //\r
306 // Add "#include 'PcdLib.h'" for Header file\r
307 //\r
308 hAutoGenString = "#include <MdePkg/Include/Library/PcdLib.h>\r\n";\r
309 }\r
310\r
11e7b0f6 311 //\r
312 // Generate all PCD entry for a module.\r
313 // \r
878ddf1f 314 for(index = 0; index < usageInstanceArray.size(); index ++) {\r
315 ActionMessage.debug(this,\r
316 "Module " + moduleName + "'s PCD [" + Integer.toHexString(index) + \r
317 "]: " + usageInstanceArray.get(index).parentToken.cName);\r
318 try {\r
11e7b0f6 319 usageInstance = usageInstanceArray.get(index);\r
320 //\r
321 // Before generate any PCD information into autogen.h/autogen.c for a module,\r
322 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type\r
323 // PCD in this module the token, they are all reference to TokenSpaceGuid \r
324 // array.\r
325 // \r
326 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
327 guidStringArray = usageInstance.parentToken.tokenSpaceName.toString().split("-");\r
328 guidStringCName = "_gPcd_TokenSpaceGuid_" + \r
329 usageInstance.parentToken.tokenSpaceName.toString().replaceAll("-", "_");\r
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}}",\r
331 guidStringArray[0],\r
332 guidStringArray[1],\r
333 guidStringArray[2],\r
334 (guidStringArray[3].substring(0, 2)),\r
335 (guidStringArray[3].substring(2, 4)),\r
336 (guidStringArray[4].substring(0, 2)),\r
337 (guidStringArray[4].substring(2, 4)),\r
338 (guidStringArray[4].substring(4, 6)),\r
339 (guidStringArray[4].substring(6, 8)),\r
340 (guidStringArray[4].substring(8, 10)),\r
341 (guidStringArray[4].substring(10, 12)));\r
a49b34c0 342 \r
343 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");\r
344 Matcher matcher = pattern.matcher(cAutoGenString + " ");\r
345 //\r
346 // Find whether this guid array variable has been generated into autogen.c\r
347 // For different DyanmicEx pcd token who use same token space guid, the token space\r
348 // guid array should be only generated once.\r
349 // \r
350 if (!matcher.find()) {\r
351 hAutoGenString += String.format("extern EFI_GUID %s;\r\n",\r
352 guidStringCName);\r
353 if (!isBuildUsedLibrary) {\r
11e7b0f6 354 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",\r
355 guidStringCName,\r
356 guidString);\r
a49b34c0 357 } \r
11e7b0f6 358 }\r
359 }\r
360\r
361 usageInstance.generateAutoGen(isBuildUsedLibrary);\r
362 //\r
363 // For every PCD entry for this module(usage instance), autogen string would\r
364 // be appand.\r
365 // \r
366 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";\r
367 cAutoGenString += usageInstance.getCAutogenStr();\r
368\r
878ddf1f 369 } catch(EntityException exp) {\r
11e7b0f6 370 throw new BuildActionException("[PCD Autogen Error]: " + exp.getMessage());\r
878ddf1f 371 }\r
372 }\r
373\r
8840ad58 374 //\r
375 // Work around code, In furture following code should be modified that get \r
376 // these information from Uplevel Autogen tools.\r
377 // \r
32648c62 378 if (moduleName.equalsIgnoreCase("PcdPeim")) {\r
379 hAutoGenString += dbManager.PcdPeimHString;\r
380 cAutoGenString += dbManager.PcdPeimCString;\r
381 } else if (moduleName.equalsIgnoreCase("PcdDxe")) {\r
382 hAutoGenString += dbManager.PcdDxeHString;\r
383 cAutoGenString += dbManager.PcdDxeCString;\r
384 }\r
99d2c3c4 385\r
878ddf1f 386 ActionMessage.debug(this,\r
387 "Module " + moduleName + "'s PCD header file:\r\n" + hAutoGenString + "\r\n"\r
388 );\r
389 ActionMessage.debug(this,\r
390 "Module " + moduleName + "'s PCD C file:\r\n" + cAutoGenString + "\r\n"\r
391 );\r
392 }\r
393\r
878ddf1f 394 /**\r
395 Test case function\r
396\r
397 @param argv paramter from command line\r
398 **/\r
399 public static void main(String argv[]) {\r
99d2c3c4 400\r
11e7b0f6 401 String WorkSpace = "M:/tianocore/edk2";\r
402 String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd";\r
ad82307c 403 String[] nameArray = null;\r
878ddf1f 404\r
405 //\r
406 // At first, CollectPCDAction should be invoked to collect\r
407 // all PCD information from SPD, MSA, FPD.\r
408 //\r
409 CollectPCDAction collectionAction = new CollectPCDAction();\r
410 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",\r
99d2c3c4 411 WorkSpace);\r
878ddf1f 412\r
878ddf1f 413 try {\r
99d2c3c4 414 collectionAction.perform(WorkSpace, \r
878ddf1f 415 logFilePath,\r
416 ActionMessage.MAX_MESSAGE_LEVEL);\r
417 } catch(Exception e) {\r
418 e.printStackTrace();\r
419 }\r
420\r
421 //\r
422 // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c\r
423 //\r
11e7b0f6 424 PCDAutoGenAction autogenAction = new PCDAutoGenAction("MonoStatusCode",\r
8840ad58 425 null,\r
426 null,\r
427 null,\r
11e7b0f6 428 "IA32",\r
8840ad58 429 null,\r
430 false,\r
ad82307c 431 nameArray);\r
32648c62 432 autogenAction.execute();\r
99d2c3c4 433\r
32648c62 434 System.out.println(autogenAction.OutputH());\r
435 System.out.println("WQWQWQWQWQ");\r
436 System.out.println(autogenAction.OutputC());\r
878ddf1f 437 }\r
438}\r