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