]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/GenBuild/org/tianocore/build/pcd/action/PlatformPcdPreprocessActionForBuilding.java
1. Update to just keep several line JAVA related msg; 2. Remove file PropertyManager...
[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
8b7bd455 142 @throws PlatformPcdPreprocessException\r
143 Fail to get Guid information from SPD file.\r
af98370e 144 **/\r
20c5c53f 145 public String getGuidInfoFromSpd(String guidCName) throws PlatformPcdPreprocessException {\r
146 String tokenSpaceStrRet = null;\r
af98370e 147 try {\r
148 tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(guidCName);\r
149 } catch ( Exception e ) {\r
391dbbb1 150 throw new PlatformPcdPreprocessException ("Failed to get Guid CName " + guidCName + " from the SPD file!");\r
af98370e 151 }\r
152 return tokenSpaceStrRet;\r
153 }\r
154\r
155 /**\r
156 This function generates source code for PCD Database.\r
157\r
af98370e 158 @throws EntityException If the token does *not* exist in memory database.\r
159\r
160 **/\r
161 private void genPcdDatabaseSourceCode()\r
162 throws EntityException {\r
163 String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions();\r
164\r
165 ArrayList<Token> alPei = new ArrayList<Token> ();\r
166 ArrayList<Token> alDxe = new ArrayList<Token> ();\r
167\r
168 getPcdDbManager().getTwoPhaseDynamicRecordArray(alPei, alDxe);\r
169 PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0);\r
170 pcdPeiDatabase.genCode();\r
171 MemoryDatabaseManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString() +\r
172 PcdDatabase.getPcdPeiDatabaseDefinitions();\r
173 MemoryDatabaseManager.PcdPeimCString = pcdPeiDatabase.getCString();\r
174\r
175 PcdDatabase pcdDxeDatabase = new PcdDatabase(alDxe, "DXE", alPei.size());\r
176 pcdDxeDatabase.genCode();\r
177 MemoryDatabaseManager.PcdDxeHString = MemoryDatabaseManager.PcdPeimHString + pcdDxeDatabase.getHString() +\r
178 PcdDatabase.getPcdDxeDatabaseDefinitions();\r
179 MemoryDatabaseManager.PcdDxeCString = pcdDxeDatabase.getCString();\r
180 }\r
181\r
182 /**\r
183 Override function: Get component array from FPD.\r
184\r
185 This function maybe provided by some Global class.\r
186\r
8b7bd455 187 @return List<ModuleInfo> the component array.\r
188 @throws PlatformPcdPreprocessException get all modules in <ModuleSA> in FPD file.\r
af98370e 189\r
8b7bd455 190 **/\r
af98370e 191 public List<ModulePcdInfoFromFpd> getComponentsFromFpd()\r
8b7bd455 192 throws PlatformPcdPreprocessException {\r
af98370e 193 List<ModulePcdInfoFromFpd> allModules = new ArrayList<ModulePcdInfoFromFpd>();\r
194 Map<FpdModuleIdentification, XmlObject> pcdBuildDefinitions = null;\r
195 UsageIdentification usageId = null;\r
196\r
197 pcdBuildDefinitions = GlobalData.getFpdPcdBuildDefinitions();\r
198 if (pcdBuildDefinitions == null) {\r
199 return null;\r
200 }\r
201\r
202 //\r
203 // Loop map to retrieve all PCD build definition and Module id\r
204 //\r
205 Iterator item = pcdBuildDefinitions.keySet().iterator();\r
206 while (item.hasNext()){\r
207 FpdModuleIdentification id = (FpdModuleIdentification) item.next();\r
208 usageId = new UsageIdentification(id.getModule().getName(),\r
209 id.getModule().getGuid(),\r
210 id.getModule().getPackage().getName(),\r
211 id.getModule().getPackage().getGuid(),\r
212 id.getArch(),\r
213 id.getModule().getVersion(),\r
214 id.getModule().getModuleType());\r
e55d8a3c 215 allModules.add(\r
216 new ModulePcdInfoFromFpd(\r
217 usageId, \r
218 ((PcdBuildDefinitionDocument)pcdBuildDefinitions.get(id)).getPcdBuildDefinition()));\r
af98370e 219 }\r
220 return allModules;\r
221 }\r
222\r
223 /**\r
224 Override function: Verify the datum value according its datum size and datum type, this\r
225 function maybe moved to FPD verification tools in future.\r
226\r
8b7bd455 227 @param cName The token name\r
228 @param moduleName The module who use this PCD token\r
229 @param datum The PCD's datum\r
230 @param datumType The PCD's datum type\r
231 @param maxDatumSize The max size for PCD's Datum.\r
af98370e 232\r
8b7bd455 233 @return String exception strings.\r
af98370e 234 */\r
af98370e 235 public String verifyDatum(String cName,\r
236 String moduleName,\r
237 String datum,\r
238 Token.DATUM_TYPE datumType,\r
239 int maxDatumSize) {\r
4d1939b8 240 //\r
241 // In building system, datum should not be checked, the checking work\r
242 // should be done by wizard tools or PCD verification tools.\r
243 // \r
af98370e 244 return null;\r
245 }\r
246\r
247 /**\r
248 Override function: Get dynamic information for a dynamic PCD from <DynamicPcdBuildDefinition> seciton in FPD file.\r
249\r
250 This function should be implemented in GlobalData in future.\r
251\r
252 @param token The token instance which has hold module's PCD information\r
253 @param moduleName The name of module who will use this Dynamic PCD.\r
254\r
255 @return DynamicPcdBuildDefinitions.PcdBuildData\r
256 **/\r
257 public DynamicPcdBuildDefinitions.PcdBuildData getDynamicInfoFromFpd(Token token,\r
258 String moduleName)\r
8b7bd455 259 throws PlatformPcdPreprocessException {\r
af98370e 260 int index = 0;\r
261 String exceptionString = null;\r
262 String dynamicPrimaryKey = null;\r
263 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions = null;\r
264 List<DynamicPcdBuildDefinitions.PcdBuildData> dynamicPcdBuildDataArray = null;\r
20c5c53f 265 String tokenSpaceStrRet = null;\r
af98370e 266\r
267 //\r
268 // If FPD document is not be opened, open and initialize it.\r
269 // BUGBUG: The code should be moved into GlobalData in future.\r
270 //\r
271 if (fpdDocInstance == null) {\r
272 try {\r
273 fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
274 } catch(IOException ioE) {\r
8b7bd455 275 throw new PlatformPcdPreprocessException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
af98370e 276 } catch(XmlException xmlE) {\r
8b7bd455 277 throw new PlatformPcdPreprocessException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
af98370e 278 }\r
279 }\r
280\r
281 dynamicPcdBuildDefinitions = fpdDocInstance.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();\r
282 if (dynamicPcdBuildDefinitions == null) {\r
391dbbb1 283 exceptionString = String.format("[FPD file error] There are no <PcdDynamicBuildDescriptions> elements in FPD file but there are Dynamic type "+\r
284 "PCD entries %s in module %s!",\r
af98370e 285 token.cName,\r
286 moduleName);\r
e55d8a3c 287 putError(exceptionString);\r
288 return null;\r
af98370e 289 }\r
290\r
291 dynamicPcdBuildDataArray = dynamicPcdBuildDefinitions.getPcdBuildDataList();\r
292 for (index = 0; index < dynamicPcdBuildDataArray.size(); index ++) {\r
20c5c53f 293 tokenSpaceStrRet = getGuidInfoFromSpd(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuidCName());\r
af98370e 294\r
295 if (tokenSpaceStrRet == null) {\r
e55d8a3c 296 exceptionString = "Fail to get token space guid for token " + dynamicPcdBuildDataArray.get(index).getCName();\r
297 putError(exceptionString);\r
298 continue;\r
af98370e 299 }\r
300\r
301 dynamicPrimaryKey = Token.getPrimaryKeyString(dynamicPcdBuildDataArray.get(index).getCName(),\r
20c5c53f 302 tokenSpaceStrRet);\r
11eb278a 303 if (dynamicPrimaryKey.equals(token.getPrimaryKeyString())) {\r
af98370e 304 return dynamicPcdBuildDataArray.get(index);\r
305 }\r
306 }\r
307\r
308 return null;\r
309 }\r
310\r
311 /**\r
312 Override function: get all <DynamicPcdBuildDefinition> from FPD file.\r
313\r
8b7bd455 314 @return List<DynamicPcdBuildDefinitions.PcdBuildData> All DYNAMIC PCD list in <DynamicPcdBuildDefinitions> in FPD file.\r
315 @throws PlatformPcdPreprocessBuildException Failure to get dynamic information list.\r
316\r
af98370e 317 **/\r
318 public List<DynamicPcdBuildDefinitions.PcdBuildData>\r
319 getAllDynamicPcdInfoFromFpd()\r
8b7bd455 320 throws PlatformPcdPreprocessException {\r
af98370e 321 DynamicPcdBuildDefinitions dynamicPcdBuildDefinitions = null;\r
322\r
323 //\r
324 // Open fpd document to get <DynamicPcdBuildDefinition> Section.\r
325 // BUGBUG: the function should be move GlobalData in furture.\r
326 //\r
327 if (fpdDocInstance == null) {\r
328 try {\r
329 fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
330 } catch(IOException ioE) {\r
8b7bd455 331 throw new PlatformPcdPreprocessException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
af98370e 332 } catch(XmlException xmlE) {\r
8b7bd455 333 throw new PlatformPcdPreprocessException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
af98370e 334 }\r
335 }\r
336\r
337 dynamicPcdBuildDefinitions = fpdDocInstance.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();\r
338 if (dynamicPcdBuildDefinitions == null) {\r
339 return null;\r
340 }\r
341\r
342 return dynamicPcdBuildDefinitions.getPcdBuildDataList();\r
343 }\r
344\r
345 /**\r
346 check parameter for this action.\r
347\r
8b7bd455 348 @throws PlatformPcdPreprocessBuildException Bad parameter.\r
af98370e 349 **/\r
8b7bd455 350 private void checkParameter() throws PlatformPcdPreprocessBuildException {\r
af98370e 351 File file = null;\r
352\r
8b7bd455 353 if (fpdFilePath == null) {\r
11eb278a 354 throw new PlatformPcdPreprocessBuildException("FPDFileName should be empty for CollectPCDAtion!");\r
af98370e 355 }\r
356\r
8b7bd455 357 if (fpdFilePath.length() == 0) {\r
11eb278a 358 throw new PlatformPcdPreprocessBuildException("FPDFileName should be empty for CollectPCDAtion!");\r
af98370e 359 }\r
360\r
361 file = new File(fpdFilePath);\r
362\r
363 if(!file.exists()) {\r
8b7bd455 364 throw new PlatformPcdPreprocessBuildException("FPD File " + fpdFilePath + " does not exist!");\r
af98370e 365 }\r
366 }\r
af98370e 367}\r