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