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