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