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