]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Because Pcd entity, exception and some action package are shared by Building tools...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / PCDAutoGenAction.java
... / ...
CommitLineData
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
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
12 \r
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
19import java.io.File;\r
20import java.util.ArrayList;\r
21import java.util.List;\r
22import java.util.Map;\r
23import java.util.Set;\r
24import java.util.UUID;\r
25import java.util.regex.Matcher;\r
26import java.util.regex.Pattern;\r
27\r
28import org.apache.xmlbeans.XmlObject;\r
29import org.tianocore.build.global.GlobalData;\r
30import org.tianocore.build.global.SurfaceAreaQuery;\r
31import org.tianocore.build.id.ModuleIdentification;\r
32import org.tianocore.pcd.entity.MemoryDatabaseManager;\r
33import org.tianocore.pcd.entity.Token;\r
34import org.tianocore.pcd.entity.UsageInstance;\r
35import org.tianocore.pcd.exception.BuildActionException;\r
36import org.tianocore.pcd.exception.EntityException;\r
37import org.tianocore.pcd.entity.UsageIdentification;\r
38import org.tianocore.pcd.action.BuildAction;\r
39import org.tianocore.pcd.action.ActionMessage;\r
40\r
41/** This class is to manage how to generate the PCD information into Autogen.c and\r
42 Autogen.h.\r
43**/\r
44public class PCDAutoGenAction extends BuildAction {\r
45 ///\r
46 /// The reference of DBManager in GlobalData class.\r
47 ///\r
48 private MemoryDatabaseManager dbManager;\r
49 ///\r
50 /// The identification for a UsageInstance.\r
51 /// \r
52 private UsageIdentification usageId;\r
53 ///\r
54 /// The arch of current module\r
55 /// \r
56 private String arch;\r
57 ///\r
58 /// Whether current autogen is for building library used by current module.\r
59 /// \r
60 private boolean isBuildUsedLibrary;\r
61 ///\r
62 /// The generated string for header file.\r
63 ///\r
64 private String hAutoGenString;\r
65 ///\r
66 /// The generated string for C code file.\r
67 ///\r
68 private String cAutoGenString;\r
69 ///\r
70 /// The name array of <PcdCoded> in a module.\r
71 /// \r
72 private String[] pcdNameArrayInMsa;\r
73 /**\r
74 Set parameter moduleId\r
75 \r
76 @param moduleName the module name parameter.\r
77 **/\r
78 public void setUsageId(UsageIdentification usageId) {\r
79 this.usageId = usageId;\r
80 }\r
81\r
82 /**\r
83 set isBuildUsedLibrary parameter.\r
84 \r
85 @param isBuildUsedLibrary\r
86 **/\r
87 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {\r
88 this.isBuildUsedLibrary = isBuildUsedLibrary;\r
89 }\r
90\r
91 /**\r
92 set pcdNameArrayInMsa parameter.\r
93 \r
94 @param pcdNameArrayInMsa\r
95 */\r
96 public void setPcdNameArrayInMsa(String[] pcdNameArrayInMsa) {\r
97 this.pcdNameArrayInMsa = pcdNameArrayInMsa;\r
98 }\r
99\r
100 /**\r
101 Get the output of generated string for header file.\r
102 \r
103 @return the string of header file for PCD\r
104 **/\r
105 public String OutputH() {\r
106 return hAutoGenString;\r
107 }\r
108\r
109 /**\r
110 Get the output of generated string for C Code file.\r
111 \r
112 @return the string of C code file for PCD\r
113 **/\r
114 public String OutputC() {\r
115 return cAutoGenString;\r
116 }\r
117\r
118 \r
119 /**\r
120 Construct function\r
121\r
122 This function mainly initialize some member variable.\r
123 \r
124 @param moduleId the identification for module\r
125 @param arch the architecture for module\r
126 @param isBuildUsedLibary Is the current module library.\r
127 @param pcdNameArrayInMsa the pcd name array got from MSA file.\r
128 **/\r
129 public PCDAutoGenAction(ModuleIdentification moduleId, \r
130 String arch,\r
131 boolean isBuildUsedLibrary,\r
132 String[] pcdNameArrayInMsa) {\r
133 UsageIdentification usageId = new UsageIdentification(moduleId.getName(), \r
134 moduleId.getGuid(), \r
135 moduleId.getPackage().getName(), \r
136 moduleId.getPackage().getGuid(), \r
137 arch, \r
138 moduleId.getVersion(), \r
139 moduleId.getModuleType());\r
140 dbManager = null;\r
141 hAutoGenString = "";\r
142 cAutoGenString = "";\r
143\r
144 setUsageId(usageId);\r
145 setIsBuildUsedLibrary(isBuildUsedLibrary);\r
146 setPcdNameArrayInMsa(pcdNameArrayInMsa);\r
147 }\r
148\r
149 /**\r
150 check the parameter for action class.\r
151 \r
152 @throws BuildActionException Bad parameter.\r
153 **/\r
154 public void checkParameter() throws BuildActionException {\r
155 \r
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 public void performAction() throws BuildActionException {\r
169 ActionMessage.debug(this, \r
170 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");\r
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
181 return;\r
182 }\r
183\r
184 ActionMessage.debug(this,\r
185 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens");\r
186\r
187 generateAutogenForModule();\r
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
198 int index, index2;\r
199 List<UsageInstance> usageInstanceArray, usageContext;\r
200 String[] guidStringArray = null;\r
201 String guidStringCName = null;\r
202 String guidString = null;\r
203 String moduleName = usageId.moduleName;\r
204 UsageInstance usageInstance = null;\r
205 boolean found = false;\r
206\r
207 usageInstanceArray = null;\r
208 if (!isBuildUsedLibrary) {\r
209 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(usageId);\r
210 dbManager.UsageInstanceContext = usageInstanceArray;\r
211 dbManager.CurrentModuleName = moduleName; \r
212 } else if ((pcdNameArrayInMsa != null) && (pcdNameArrayInMsa.length > 0)) {\r
213 usageContext = dbManager.UsageInstanceContext;\r
214 //\r
215 // For building library package, although all module are library, but PCD entries of \r
216 // these library should be used to autogen.\r
217 // \r
218 if (usageContext == null) {\r
219 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(usageId);\r
220 } else {\r
221 usageInstanceArray = new ArrayList<UsageInstance>();\r
222\r
223 //\r
224 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's \r
225 // <ModuleSA> in FPD file.\r
226 // \r
227 for (index = 0; index < pcdNameArrayInMsa.length; index++) {\r
228 found = false;\r
229 for (index2 = 0; index2 < usageContext.size(); index2 ++) {\r
230 if (pcdNameArrayInMsa[index].equalsIgnoreCase(usageContext.get(index2).parentToken.cName)) {\r
231 usageInstanceArray.add(usageContext.get(index2));\r
232 found = true;\r
233 break;\r
234 }\r
235 }\r
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
247 pcdNameArrayInMsa[index],\r
248 dbManager.CurrentModuleName\r
249 ));\r
250 }\r
251 }\r
252 }\r
253 }\r
254\r
255 if (usageInstanceArray == null) {\r
256 return;\r
257 }\r
258\r
259 //\r
260 // Generate all PCD entry for a module.\r
261 // \r
262 for(index = 0; index < usageInstanceArray.size(); index ++) {\r
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
289 //\r
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
293 // \r
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
302 }\r
303 }\r
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
312 }\r
313\r
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
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
325 }\r
326\r
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
333\r
334 String WorkSpace = "X:/edk2";\r
335 String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd";\r
336 String[] nameArray = null;\r
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
344 WorkSpace,null);\r
345\r
346 try {\r
347 collectionAction.perform(WorkSpace, \r
348 logFilePath,\r
349 ActionMessage.MAX_MESSAGE_LEVEL);\r
350 } catch(Exception e) {\r
351 e.printStackTrace();\r
352 }\r
353 }\r
354}\r