Remove unnecessary exception caching.
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / pcd / action / PlatformPcdPreprocessActionForBuilding.java
CommitLineData
af98370e 1/** @file\r
2 PlatformPcdPreprocessActionForBuilding class.\r
3\r
4 This action class is to collect PCD information from MSA, SPD, FPD xml file.\r
5 This class will be used for wizard and build tools, So it can *not* inherit\r
6 from buildAction or wizardAction.\r
7\r
8Copyright (c) 2006, Intel Corporation\r
9All rights reserved. This program and the accompanying materials\r
10are licensed and made available under the terms and conditions of the BSD License\r
11which accompanies this distribution. The full text of the license may be found at\r
12http://opensource.org/licenses/bsd-license.php\r
13\r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18package org.tianocore.build.pcd.action;\r
19\r
20import java.io.File;\r
21import java.io.IOException;\r
af98370e 22import java.util.ArrayList;\r
23import java.util.Iterator;\r
24import java.util.List;\r
25import java.util.Map;\r
26\r
27import org.apache.xmlbeans.XmlException;\r
28import org.apache.xmlbeans.XmlObject;\r
29import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions;\r
e55d8a3c 30import org.tianocore.PcdBuildDefinitionDocument;\r
af98370e 31import org.tianocore.PlatformSurfaceAreaDocument;\r
e55d8a3c 32import org.tianocore.build.exception.PlatformPcdPreprocessBuildException;\r
af98370e 33import org.tianocore.build.global.GlobalData;\r
34import org.tianocore.build.id.FpdModuleIdentification;\r
e55d8a3c 35import org.tianocore.pcd.action.PlatformPcdPreprocessAction;\r
af98370e 36import org.tianocore.pcd.entity.MemoryDatabaseManager;\r
e55d8a3c 37import org.tianocore.pcd.entity.ModulePcdInfoFromFpd;\r
af98370e 38import org.tianocore.pcd.entity.Token;\r
39import org.tianocore.pcd.entity.UsageIdentification;\r
40import org.tianocore.pcd.exception.EntityException;\r
8b7bd455 41import org.tianocore.pcd.exception.PlatformPcdPreprocessException;\r
af98370e 42\r
43/**\r
44 This action class is to collect PCD information from MSA, SPD, FPD xml file.\r
45 This class will be used for wizard and build tools, So it can *not* inherit\r
46 from buildAction or UIAction.\r
47**/\r
48public class PlatformPcdPreprocessActionForBuilding extends PlatformPcdPreprocessAction {\r
af98370e 49 ///\r
11eb278a 50 /// FPD file path.\r
af98370e 51 ///\r
52 private String fpdFilePath;\r
53\r
af98370e 54 ///\r
55 /// Cache the fpd docment instance for private usage.\r
56 ///\r
57 private PlatformSurfaceAreaDocument fpdDocInstance;\r
58\r
af98370e 59 /**\r
60 Set FPDFileName parameter for this action class.\r
61\r
62 @param fpdFilePath fpd file path\r
63 **/\r
64 public void setFPDFilePath(String fpdFilePath) {\r
65 this.fpdFilePath = fpdFilePath;\r
66 }\r
67\r
68 /**\r
69 Common function interface for outer.\r
70\r
11eb278a 71 @param fpdFilePath The fpd file path of current build or processing.\r
af98370e 72\r
8b7bd455 73 @throws PlatformPreprocessBuildException \r
74 The exception of this function. Because it can *not* be predict\r
75 where the action class will be used. So only Exception can be throw.\r
af98370e 76\r
77 **/\r
e8c0c170 78 public void perform(String fpdFilePath) \r
8b7bd455 79 throws PlatformPcdPreprocessBuildException {\r
11eb278a 80 this.fpdFilePath = fpdFilePath;\r
af98370e 81 checkParameter();\r
82 execute();\r
af98370e 83 }\r
84\r
85 /**\r
86 Core execution function for this action class.\r
87\r
88 This function work flows will be:\r
89 1) Collect and prepocess PCD information from FPD file, all PCD\r
90 information will be stored into memory database.\r
91 2) Generate 3 strings for\r
92 a) All modules using Dynamic(Ex) PCD entry.(Token Number)\r
93 b) PEI PCDDatabase (C Structure) for PCD Service PEIM.\r
94 c) DXE PCD Database (C structure) for PCD Service DXE.\r
95\r
96\r
97 @throws EntityException Exception indicate failed to execute this action.\r
98\r
99 **/\r
8b7bd455 100 public void execute() throws PlatformPcdPreprocessBuildException {\r
391dbbb1 101 String errorMessageHeader = "Failed to initialize the Pcd memory database because: ";\r
e55d8a3c 102 String errorsForPreprocess = null;\r
103\r
af98370e 104 //\r
105 // Get memoryDatabaseManager instance from GlobalData.\r
8b7bd455 106 // The memoryDatabaseManager should be initialized as static variable\r
107 // in some Pre-process class.\r
af98370e 108 //\r
8b7bd455 109 setPcdDbManager(GlobalData.getPCDMemoryDBManager());\r
af98370e 110\r
111 //\r
112 // Collect all PCD information defined in FPD file.\r
113 // Evenry token defind in FPD will be created as an token into\r
114 // memory database.\r
115 //\r
8b7bd455 116 try {\r
117 initPcdMemoryDbWithPlatformInfo();\r
118 } catch (PlatformPcdPreprocessException exp) {\r
119 throw new PlatformPcdPreprocessBuildException(errorMessageHeader + exp.getMessage());\r
120 }\r
e55d8a3c 121 errorsForPreprocess = this.getErrorString();\r
122 if (errorsForPreprocess != null) {\r
123 throw new PlatformPcdPreprocessBuildException(errorMessageHeader + "\r\n" + errorsForPreprocess);\r
124 }\r
af98370e 125\r
126 //\r
127 // Generate for PEI, DXE PCD DATABASE's definition and initialization.\r
128 //\r
8b7bd455 129 try {\r
130 genPcdDatabaseSourceCode ();\r
131 } catch (EntityException exp) {\r
11eb278a 132 throw new PlatformPcdPreprocessBuildException(errorMessageHeader + "\r\n" + exp.getMessage());\r
8b7bd455 133 }\r
af98370e 134 }\r
135\r
136 /**\r
137 Override function: implementate the method of get Guid string information from SPD file.\r
138\r
139 @param guidCName Guid CName string.\r
140\r
20c5c53f 141 @return String Guid information from SPD file.\r
af98370e 142 **/\r
3fabe975 143 public String getGuidInfoFromSpd(String guidCName) {\r
144 return GlobalData.getGuidInfoFromCname(guidCName);\r
af98370e 145 }\r
146\r
147 /**\r
148 This function generates source code for PCD Database.\r
149\r
af98370e 150 @throws EntityException If the token does *not* exist in memory database.\r
151\r
152 **/\r
153 private void genPcdDatabaseSourceCode()\r
154 throws EntityException {\r
155 String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions();\r
156\r
157 ArrayList<Token> alPei = new ArrayList<Token> ();\r
158 ArrayList<Token> alDxe = new ArrayList<Token> ();\r
159\r
160 getPcdDbManager().getTwoPhaseDynamicRecordArray(alPei, alDxe);\r
161 PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0);\r
162 pcdPeiDatabase.genCode();\r
163 MemoryDatabaseManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString() +\r
164 PcdDatabase.getPcdPeiDatabaseDefinitions();\r
165 MemoryDatabaseManager.PcdPeimCString = pcdPeiDatabase.getCString();\r
166\r
167 PcdDatabase pcdDxeDatabase = new PcdDatabase(alDxe, "DXE", alPei.size());\r
168 pcdDxeDatabase.genCode();\r
169 MemoryDatabaseManager.PcdDxeHString = MemoryDatabaseManager.PcdPeimHString + pcdDxeDatabase.getHString() +\r
170 PcdDatabase.getPcdDxeDatabaseDefinitions();\r
171 MemoryDatabaseManager.PcdDxeCString = pcdDxeDatabase.getCString();\r
172 }\r
173\r
174 /**\r
175 Override function: Get component array from FPD.\r
176\r
177 This function maybe provided by some Global class.\r
178\r
8b7bd455 179 @return List<ModuleInfo> the component array.\r
180 @throws PlatformPcdPreprocessException get all modules in <ModuleSA> in FPD file.\r
af98370e 181\r
8b7bd455 182 **/\r
af98370e 183 public List<ModulePcdInfoFromFpd> getComponentsFromFpd()\r
8b7bd455 184 throws PlatformPcdPreprocessException {\r
af98370e 185 List<ModulePcdInfoFromFpd> allModules = new ArrayList<ModulePcdInfoFromFpd>();\r
186 Map<FpdModuleIdentification, XmlObject> pcdBuildDefinitions = null;\r
187 UsageIdentification usageId = null;\r
188\r
189 pcdBuildDefinitions = GlobalData.getFpdPcdBuildDefinitions();\r
190 if (pcdBuildDefinitions == null) {\r
191 return null;\r
192 }\r
193\r
194 //\r
195 // Loop map to retrieve all PCD build definition and Module id\r
196 //\r
197 Iterator item = pcdBuildDefinitions.keySet().iterator();\r
198 while (item.hasNext()){\r
199 FpdModuleIdentification id = (FpdModuleIdentification) item.next();\r
200 usageId = new UsageIdentification(id.getModule().getName(),\r
201 id.getModule().getGuid(),\r
202 id.getModule().getPackage().getName(),\r
203 id.getModule().getPackage().getGuid(),\r
204 id.getArch(),\r
205 id.getModule().getVersion(),\r
206 id.getModule().getModuleType());\r
e55d8a3c 207 allModules.add(\r
208 new ModulePcdInfoFromFpd(\r
209 usageId, \r
210 ((PcdBuildDefinitionDocument)pcdBuildDefinitions.get(id)).getPcdBuildDefinition()));\r
af98370e 211 }\r
212 return allModules;\r
213 }\r
214\r
215 /**\r
216 Override function: Verify the datum value according its datum size and datum type, this\r
217 function maybe moved to FPD verification tools in future.\r
218\r
8b7bd455 219 @param cName The token name\r
220 @param moduleName The module who use this PCD token\r
221 @param datum The PCD's datum\r
222 @param datumType The PCD's datum type\r
223 @param maxDatumSize The max size for PCD's Datum.\r
af98370e 224\r
8b7bd455 225 @return String exception strings.\r
af98370e 226 */\r
af98370e 227 public String verifyDatum(String cName,\r
228 String moduleName,\r
229 String datum,\r
230 Token.DATUM_TYPE datumType,\r
231 int maxDatumSize) {\r
4d1939b8 232 //\r
233 // In building system, datum should not be checked, the checking work\r
234 // should be done by wizard tools or PCD verification tools.\r
235 // \r
af98370e 236 return null;\r
237 }\r
238\r
239 /**\r
240 Override function: Get dynamic information for a dynamic PCD from <DynamicPcdBuildDefinition> seciton in FPD file.\r
241\r
242 This function should be implemented in GlobalData in future.\r
243\r
244 @param token The token instance which has hold module's PCD information\r
245 @param moduleName The name of module who will use this Dynamic PCD.\r
246\r
247 @return DynamicPcdBuildDefinitions.PcdBuildData\r
248 **/\r
249 public DynamicPcdBuildDefinitions.PcdBuildData getDynamicInfoFromFpd(Token token,\r
250 String moduleName)\r
8b7bd455 251 throws PlatformPcdPreprocessException {\r
af98370e 252 int index = 0;\r
253 String exceptionString = null;\r
254 String dynamicPrimaryKey = null;\r
255 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions = null;\r
256 List<DynamicPcdBuildDefinitions.PcdBuildData> dynamicPcdBuildDataArray = null;\r
20c5c53f 257 String tokenSpaceStrRet = null;\r
af98370e 258\r
259 //\r
260 // If FPD document is not be opened, open and initialize it.\r
261 // BUGBUG: The code should be moved into GlobalData in future.\r
262 //\r
263 if (fpdDocInstance == null) {\r
264 try {\r
265 fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
266 } catch(IOException ioE) {\r
8b7bd455 267 throw new PlatformPcdPreprocessException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
af98370e 268 } catch(XmlException xmlE) {\r
8b7bd455 269 throw new PlatformPcdPreprocessException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
af98370e 270 }\r
271 }\r
272\r
273 dynamicPcdBuildDefinitions = fpdDocInstance.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();\r
274 if (dynamicPcdBuildDefinitions == null) {\r
391dbbb1 275 exceptionString = String.format("[FPD file error] There are no <PcdDynamicBuildDescriptions> elements in FPD file but there are Dynamic type "+\r
276 "PCD entries %s in module %s!",\r
af98370e 277 token.cName,\r
278 moduleName);\r
e55d8a3c 279 putError(exceptionString);\r
280 return null;\r
af98370e 281 }\r
282\r
283 dynamicPcdBuildDataArray = dynamicPcdBuildDefinitions.getPcdBuildDataList();\r
284 for (index = 0; index < dynamicPcdBuildDataArray.size(); index ++) {\r
20c5c53f 285 tokenSpaceStrRet = getGuidInfoFromSpd(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuidCName());\r
af98370e 286\r
287 if (tokenSpaceStrRet == null) {\r
e55d8a3c 288 exceptionString = "Fail to get token space guid for token " + dynamicPcdBuildDataArray.get(index).getCName();\r
289 putError(exceptionString);\r
290 continue;\r
af98370e 291 }\r
292\r
293 dynamicPrimaryKey = Token.getPrimaryKeyString(dynamicPcdBuildDataArray.get(index).getCName(),\r
20c5c53f 294 tokenSpaceStrRet);\r
11eb278a 295 if (dynamicPrimaryKey.equals(token.getPrimaryKeyString())) {\r
af98370e 296 return dynamicPcdBuildDataArray.get(index);\r
297 }\r
298 }\r
299\r
300 return null;\r
301 }\r
302\r
303 /**\r
304 Override function: get all <DynamicPcdBuildDefinition> from FPD file.\r
305\r
8b7bd455 306 @return List<DynamicPcdBuildDefinitions.PcdBuildData> All DYNAMIC PCD list in <DynamicPcdBuildDefinitions> in FPD file.\r
307 @throws PlatformPcdPreprocessBuildException Failure to get dynamic information list.\r
308\r
af98370e 309 **/\r
310 public List<DynamicPcdBuildDefinitions.PcdBuildData>\r
311 getAllDynamicPcdInfoFromFpd()\r
8b7bd455 312 throws PlatformPcdPreprocessException {\r
af98370e 313 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions = null;\r
314\r
315 //\r
316 // Open fpd document to get <DynamicPcdBuildDefinition> Section.\r
317 // BUGBUG: the function should be move GlobalData in furture.\r
318 //\r
319 if (fpdDocInstance == null) {\r
320 try {\r
321 fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
322 } catch(IOException ioE) {\r
8b7bd455 323 throw new PlatformPcdPreprocessException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
af98370e 324 } catch(XmlException xmlE) {\r
8b7bd455 325 throw new PlatformPcdPreprocessException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
af98370e 326 }\r
327 }\r
328\r
329 dynamicPcdBuildDefinitions = fpdDocInstance.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();\r
330 if (dynamicPcdBuildDefinitions == null) {\r
331 return null;\r
332 }\r
333\r
334 return dynamicPcdBuildDefinitions.getPcdBuildDataList();\r
335 }\r
336\r
337 /**\r
338 check parameter for this action.\r
339\r
8b7bd455 340 @throws PlatformPcdPreprocessBuildException Bad parameter.\r
af98370e 341 **/\r
8b7bd455 342 private void checkParameter() throws PlatformPcdPreprocessBuildException {\r
af98370e 343 File file = null;\r
344\r
8b7bd455 345 if (fpdFilePath == null) {\r
11eb278a 346 throw new PlatformPcdPreprocessBuildException("FPDFileName should be empty for CollectPCDAtion!");\r
af98370e 347 }\r
348\r
8b7bd455 349 if (fpdFilePath.length() == 0) {\r
11eb278a 350 throw new PlatformPcdPreprocessBuildException("FPDFileName should be empty for CollectPCDAtion!");\r
af98370e 351 }\r
352\r
353 file = new File(fpdFilePath);\r
354\r
355 if(!file.exists()) {\r
8b7bd455 356 throw new PlatformPcdPreprocessBuildException("FPD File " + fpdFilePath + " does not exist!");\r
af98370e 357 }\r
358 }\r
af98370e 359}\r