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