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