]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Adding new Logger instead of Ant's.
[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
af98370e 6\r
878ddf1f 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
af98370e 12\r
878ddf1f 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
11e7b0f6 19import java.util.ArrayList;\r
878ddf1f 20import java.util.List;\r
11e7b0f6 21import java.util.regex.Matcher;\r
22import java.util.regex.Pattern;\r
878ddf1f 23\r
2eb7d78d 24import org.tianocore.build.FrameworkBuildTask;\r
5f907e4a 25import org.tianocore.build.autogen.CommonDefinition;\r
878ddf1f 26import org.tianocore.build.global.GlobalData;\r
eece174a 27import org.tianocore.build.id.ModuleIdentification;\r
5f907e4a 28import org.tianocore.pcd.action.ActionMessage;\r
29import org.tianocore.pcd.action.BuildAction;\r
d14ebb43 30import org.tianocore.pcd.entity.MemoryDatabaseManager;\r
31import org.tianocore.pcd.entity.Token;\r
5f907e4a 32import org.tianocore.pcd.entity.UsageIdentification;\r
d14ebb43 33import org.tianocore.pcd.entity.UsageInstance;\r
34import org.tianocore.pcd.exception.BuildActionException;\r
878ddf1f 35\r
11eb278a 36/**\r
37 This class is to manage how to generate the PCD information into Autogen.c\r
38 and Autogen.h.\r
878ddf1f 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
af98370e 45\r
878ddf1f 46 ///\r
d14ebb43 47 /// The identification for a UsageInstance.\r
8840ad58 48 ///\r
af98370e 49 private UsageIdentification usageId;\r
50\r
8840ad58 51 ///\r
8840ad58 52 /// Whether current autogen is for building library used by current module.\r
af98370e 53 ///\r
8840ad58 54 private boolean isBuildUsedLibrary;\r
af98370e 55\r
5f907e4a 56 ///\r
57 /// One of PEI_PCD_DRIVER, DXE_PCD_DRIVER, NOT_PCD_DRIVER \r
58 /// \r
59 private CommonDefinition.PCD_DRIVER_TYPE pcdDriverType;\r
60\r
8840ad58 61 ///\r
878ddf1f 62 /// The generated string for header file.\r
63 ///\r
64 private String hAutoGenString;\r
af98370e 65\r
878ddf1f 66 ///\r
67 /// The generated string for C code file.\r
68 ///\r
ad82307c 69 private String cAutoGenString;\r
af98370e 70\r
ad82307c 71 ///\r
72 /// The name array of <PcdCoded> in a module.\r
af98370e 73 ///\r
eece174a 74 private String[] pcdNameArrayInMsa;\r
af98370e 75\r
2eb7d78d 76 private UsageIdentification parentId = null;\r
878ddf1f 77 /**\r
eece174a 78 Set parameter moduleId\r
af98370e 79\r
878ddf1f 80 @param moduleName the module name parameter.\r
81 **/\r
d14ebb43 82 public void setUsageId(UsageIdentification usageId) {\r
83 this.usageId = usageId;\r
8840ad58 84 }\r
85\r
5f907e4a 86 /**\r
87 Set paramter pcdDriverType\r
88 \r
89 @param pcdDriverType the driver type for PCD\r
90 **/\r
91 public void setPcdDriverType(CommonDefinition.PCD_DRIVER_TYPE pcdDriverType) {\r
92 this.pcdDriverType = pcdDriverType;\r
93 }\r
878ddf1f 94 /**\r
ad82307c 95 set isBuildUsedLibrary parameter.\r
af98370e 96\r
ad82307c 97 @param isBuildUsedLibrary\r
eece174a 98 **/\r
8840ad58 99 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {\r
100 this.isBuildUsedLibrary = isBuildUsedLibrary;\r
101 }\r
eece174a 102\r
ad82307c 103 /**\r
eece174a 104 set pcdNameArrayInMsa parameter.\r
af98370e 105\r
eece174a 106 @param pcdNameArrayInMsa\r
ad82307c 107 */\r
eece174a 108 public void setPcdNameArrayInMsa(String[] pcdNameArrayInMsa) {\r
109 this.pcdNameArrayInMsa = pcdNameArrayInMsa;\r
ad82307c 110 }\r
8840ad58 111\r
878ddf1f 112 /**\r
113 Get the output of generated string for header file.\r
af98370e 114\r
878ddf1f 115 @return the string of header file for PCD\r
116 **/\r
11eb278a 117 public String getHAutoGenString() {\r
878ddf1f 118 return hAutoGenString;\r
119 }\r
120\r
121 /**\r
122 Get the output of generated string for C Code file.\r
af98370e 123\r
878ddf1f 124 @return the string of C code file for PCD\r
125 **/\r
11eb278a 126 public String getCAutoGenString() {\r
878ddf1f 127 return cAutoGenString;\r
128 }\r
129\r
af98370e 130\r
878ddf1f 131 /**\r
eece174a 132 Construct function\r
136adffc 133\r
eece174a 134 This function mainly initialize some member variable.\r
af98370e 135\r
eece174a 136 @param moduleId the identification for module\r
137 @param arch the architecture for module\r
138 @param isBuildUsedLibary Is the current module library.\r
139 @param pcdNameArrayInMsa the pcd name array got from MSA file.\r
5f907e4a 140 @param pcdDriverType one of PEI_PCD_DRIVER, DXE_PCD_DRIVER,\r
141 NOT_PCD_DRIVER\r
eece174a 142 **/\r
af98370e 143 public PCDAutoGenAction(ModuleIdentification moduleId,\r
eece174a 144 String arch,\r
145 boolean isBuildUsedLibrary,\r
5f907e4a 146 String[] pcdNameArrayInMsa,\r
2eb7d78d 147 CommonDefinition.PCD_DRIVER_TYPE pcdDriverType,\r
148 ModuleIdentification parentId) {\r
eece174a 149 dbManager = null;\r
150 hAutoGenString = "";\r
151 cAutoGenString = "";\r
152\r
af98370e 153 setUsageId(new UsageIdentification(moduleId.getName(),\r
154 moduleId.getGuid(),\r
155 moduleId.getPackage().getName(),\r
156 moduleId.getPackage().getGuid(),\r
157 arch,\r
158 moduleId.getVersion(),\r
159 moduleId.getModuleType()));\r
2eb7d78d 160 if (parentId != null) {\r
161 this.parentId = new UsageIdentification(parentId.getName(),\r
162 parentId.getGuid(),\r
163 parentId.getPackage().getName(),\r
164 parentId.getPackage().getGuid(),\r
165 arch,\r
166 parentId.getVersion(),\r
167 parentId.getModuleType());\r
168 }\r
eece174a 169 setIsBuildUsedLibrary(isBuildUsedLibrary);\r
170 setPcdNameArrayInMsa(pcdNameArrayInMsa);\r
5f907e4a 171 setPcdDriverType(pcdDriverType);\r
eece174a 172 }\r
173\r
878ddf1f 174 /**\r
8b7bd455 175 Override function: check the parameter for action class.\r
af98370e 176\r
878ddf1f 177 @throws BuildActionException Bad parameter.\r
178 **/\r
af98370e 179 public void checkParameter() {\r
878ddf1f 180 }\r
181\r
182 /**\r
183 Core execution function for this action class.\r
af98370e 184\r
878ddf1f 185 All PCD information of this module comes from memory dabase. The collection\r
186 work should be done before this action execution.\r
af98370e 187 Currently, we should generated all PCD information(maybe all dynamic) as array\r
188 in Pei emulated driver for simulating PCD runtime database.\r
189\r
878ddf1f 190 @throws BuildActionException Failed to execute this aciton class.\r
191 **/\r
af98370e 192 public void performAction() {\r
193 ActionMessage.debug(this,\r
878ddf1f 194 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");\r
845fdeba 195\r
196 dbManager = GlobalData.getPCDMemoryDBManager();\r
197\r
198 if(dbManager.getDBSize() == 0) {\r
8840ad58 199 return;\r
845fdeba 200 }\r
201\r
202 ActionMessage.debug(this,\r
391dbbb1 203 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens.");\r
878ddf1f 204\r
8840ad58 205 generateAutogenForModule();\r
878ddf1f 206 }\r
207\r
208 /**\r
209 Generate the autogen string for a common module.\r
af98370e 210\r
878ddf1f 211 All PCD information of this module comes from memory dabase. The collection\r
212 work should be done before this action execution.\r
213 **/\r
214 private void generateAutogenForModule()\r
215 {\r
ad82307c 216 int index, index2;\r
217 List<UsageInstance> usageInstanceArray, usageContext;\r
11e7b0f6 218 String[] guidStringArray = null;\r
219 String guidStringCName = null;\r
220 String guidString = null;\r
d14ebb43 221 String moduleName = usageId.moduleName;\r
11e7b0f6 222 UsageInstance usageInstance = null;\r
a1eb6401 223 boolean found = false;\r
878ddf1f 224\r
a1eb6401 225 usageInstanceArray = null;\r
2eb7d78d 226 \r
227 if (FrameworkBuildTask.multithread) {\r
228 if (parentId == null) {\r
b6297711 229 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);\r
2eb7d78d 230 } else if ((pcdNameArrayInMsa != null) && (pcdNameArrayInMsa.length > 0)) {\r
231 usageContext = dbManager.getUsageInstanceArrayById(parentId);\r
ad82307c 232 //\r
2eb7d78d 233 // For building library package, although all module are library, but PCD entries of\r
234 // these library should be used to autogen.\r
af98370e 235 //\r
2eb7d78d 236 if (usageContext == null) {\r
237 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);\r
238 } else {\r
239 usageInstanceArray = new ArrayList<UsageInstance>();\r
240\r
241 //\r
242 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's\r
243 // <ModuleSA> in FPD file.\r
244 //\r
245 for (index = 0; index < pcdNameArrayInMsa.length; index++) {\r
246 found = false;\r
247 for (index2 = 0; index2 < usageContext.size(); index2 ++) {\r
248 if (pcdNameArrayInMsa[index].equalsIgnoreCase(usageContext.get(index2).parentToken.cName)) {\r
249 usageInstanceArray.add(usageContext.get(index2));\r
250 found = true;\r
251 break;\r
252 }\r
ad82307c 253 }\r
a1eb6401 254\r
2eb7d78d 255 if (!found) {\r
256 //\r
257 // All library's PCD should instanted in module's <ModuleSA> who\r
258 // use this library instance. If not, give errors.\r
259 //\r
260 throw new BuildActionException (String.format("Module %s using library instance %s; the PCD %s " +\r
261 "is required by this library instance, but can not be found " +\r
262 "in the %s's <ModuleSA> in the FPD file!",\r
263 MemoryDatabaseManager.CurrentModuleName,\r
264 moduleName,\r
265 pcdNameArrayInMsa[index],\r
266 MemoryDatabaseManager.CurrentModuleName\r
267 ));\r
268 }\r
269 }\r
270 }\r
271 }\r
272 } else {\r
273 if (!isBuildUsedLibrary) {\r
274 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);\r
275 MemoryDatabaseManager.UsageInstanceContext = usageInstanceArray;\r
276 MemoryDatabaseManager.CurrentModuleName = moduleName;\r
277 } else if ((pcdNameArrayInMsa != null) && (pcdNameArrayInMsa.length > 0)) {\r
278 usageContext = MemoryDatabaseManager.UsageInstanceContext;\r
279 //\r
280 // For building library package, although all module are library, but PCD entries of\r
281 // these library should be used to autogen.\r
282 //\r
283 if (usageContext == null) {\r
284 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);\r
285 } else {\r
286 usageInstanceArray = new ArrayList<UsageInstance>();\r
287 \r
288 //\r
289 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's\r
290 // <ModuleSA> in FPD file.\r
291 //\r
292 for (index = 0; index < pcdNameArrayInMsa.length; index++) {\r
293 found = false;\r
294 for (index2 = 0; index2 < usageContext.size(); index2 ++) {\r
295 if (pcdNameArrayInMsa[index].equalsIgnoreCase(usageContext.get(index2).parentToken.cName)) {\r
296 usageInstanceArray.add(usageContext.get(index2));\r
297 found = true;\r
298 break;\r
299 }\r
300 }\r
301 \r
302 if (!found) {\r
303 //\r
304 // All library's PCD should instanted in module's <ModuleSA> who\r
305 // use this library instance. If not, give errors.\r
306 //\r
307 throw new BuildActionException (String.format("Module %s using library instance %s; the PCD %s " +\r
308 "is required by this library instance, but can not be found " +\r
309 "in the %s's <ModuleSA> in the FPD file!",\r
310 MemoryDatabaseManager.CurrentModuleName,\r
311 moduleName,\r
312 pcdNameArrayInMsa[index],\r
313 MemoryDatabaseManager.CurrentModuleName\r
314 ));\r
315 }\r
a1eb6401 316 }\r
ad82307c 317 }\r
8840ad58 318 }\r
319 }\r
a1eb6401 320 if (usageInstanceArray == null) {\r
321 return;\r
322 }\r
323\r
11e7b0f6 324 //\r
325 // Generate all PCD entry for a module.\r
af98370e 326 //\r
878ddf1f 327 for(index = 0; index < usageInstanceArray.size(); index ++) {\r
eece174a 328 usageInstance = usageInstanceArray.get(index);\r
329 //\r
330 // Before generate any PCD information into autogen.h/autogen.c for a module,\r
331 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type\r
af98370e 332 // PCD in this module the token, they are all reference to TokenSpaceGuid\r
eece174a 333 // array.\r
af98370e 334 //\r
eece174a 335 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
336 guidStringArray = usageInstance.parentToken.tokenSpaceName.split("-");\r
af98370e 337 guidStringCName = "_gPcd_TokenSpaceGuid_" +\r
eece174a 338 usageInstance.parentToken.tokenSpaceName.replaceAll("-", "_");\r
339 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
340 guidStringArray[0],\r
341 guidStringArray[1],\r
342 guidStringArray[2],\r
343 (guidStringArray[3].substring(0, 2)),\r
344 (guidStringArray[3].substring(2, 4)),\r
345 (guidStringArray[4].substring(0, 2)),\r
346 (guidStringArray[4].substring(2, 4)),\r
347 (guidStringArray[4].substring(4, 6)),\r
348 (guidStringArray[4].substring(6, 8)),\r
349 (guidStringArray[4].substring(8, 10)),\r
350 (guidStringArray[4].substring(10, 12)));\r
af98370e 351\r
eece174a 352 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");\r
353 Matcher matcher = pattern.matcher(cAutoGenString + " ");\r
11e7b0f6 354 //\r
eece174a 355 // Find whether this guid array variable has been generated into autogen.c\r
356 // For different DyanmicEx pcd token who use same token space guid, the token space\r
357 // guid array should be only generated once.\r
af98370e 358 //\r
eece174a 359 if (!matcher.find()) {\r
af98370e 360 hAutoGenString += String.format("extern EFI_GUID %s;\r\n", guidStringCName);\r
eece174a 361 if (!isBuildUsedLibrary) {\r
362 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",\r
363 guidStringCName,\r
364 guidString);\r
af98370e 365 }\r
11e7b0f6 366 }\r
878ddf1f 367 }\r
eece174a 368\r
369 usageInstance.generateAutoGen(isBuildUsedLibrary);\r
370 //\r
371 // For every PCD entry for this module(usage instance), autogen string would\r
372 // be appand.\r
af98370e 373 //\r
eece174a 374 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";\r
375 cAutoGenString += usageInstance.getCAutogenStr();\r
878ddf1f 376 }\r
377\r
5f907e4a 378 if (pcdDriverType == CommonDefinition.PCD_DRIVER_TYPE.PEI_PCD_DRIVER) {\r
af98370e 379 hAutoGenString += MemoryDatabaseManager.PcdPeimHString;\r
380 cAutoGenString += MemoryDatabaseManager.PcdPeimCString;\r
5f907e4a 381 } else if (pcdDriverType == CommonDefinition.PCD_DRIVER_TYPE.DXE_PCD_DRIVER) {\r
af98370e 382 hAutoGenString += MemoryDatabaseManager.PcdDxeHString;\r
383 cAutoGenString += MemoryDatabaseManager.PcdDxeCString;\r
32648c62 384 }\r
878ddf1f 385 }\r
878ddf1f 386}\r