]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/GenBuild/org/tianocore/build/fpd/FpdParserTask.java
Because Pcd entity, exception and some action package are shared by Building tools...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / fpd / FpdParserTask.java
... / ...
CommitLineData
1/** @file\r
2 This file is ANT task FpdParserTask. \r
3 \r
4 FpdParserTask is used to parse FPD (Framework Platform Description) and generate\r
5 build.out.xml. It is for Package or Platform build use. \r
6 \r
7 Copyright (c) 2006, Intel Corporation\r
8 All rights reserved. This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15 **/\r
16package org.tianocore.build.fpd;\r
17\r
18import java.io.BufferedWriter;\r
19import java.io.File;\r
20import java.io.FileWriter;\r
21import java.util.HashMap;\r
22import java.util.Iterator;\r
23import java.util.LinkedHashMap;\r
24import java.util.LinkedHashSet;\r
25import java.util.Map;\r
26import java.util.Set;\r
27import java.util.Vector;\r
28import java.util.TreeMap;\r
29\r
30import org.apache.tools.ant.BuildException;\r
31import org.apache.tools.ant.Task;\r
32import org.apache.tools.ant.taskdefs.Ant;\r
33import org.apache.tools.ant.taskdefs.Property;\r
34import org.apache.xmlbeans.XmlObject;\r
35\r
36import org.tianocore.build.global.GlobalData;\r
37import org.tianocore.build.global.OutputManager;\r
38import org.tianocore.build.global.SurfaceAreaQuery;\r
39import org.tianocore.build.id.FpdModuleIdentification;\r
40import org.tianocore.build.id.ModuleIdentification;\r
41import org.tianocore.build.id.PlatformIdentification;\r
42import org.tianocore.pcd.action.ActionMessage;\r
43import org.tianocore.build.pcd.action.CollectPCDAction;\r
44import org.tianocore.build.toolchain.ToolChainAttribute;\r
45import org.tianocore.build.toolchain.ToolChainElement;\r
46import org.tianocore.build.toolchain.ToolChainMap;\r
47import org.tianocore.exception.EdkException;\r
48\r
49/**\r
50 <code>FpdParserTask</code> is an ANT task. The main function is parsing Framework\r
51 Platform Descritpion (FPD) XML file and generating its ANT build script for \r
52 corresponding platform. \r
53\r
54 <p>The task sets global properties PLATFORM, PLATFORM_DIR, PLATFORM_RELATIVE_DIR\r
55 and BUILD_DIR. </p>\r
56 \r
57 <p>The task generates ${PLATFORM}_build.xml file which will be called by top level\r
58 build.xml. The task also generate Fv.inf files (File is for Tool GenFvImage) \r
59 and flash definition file (File is for Tool FlashMap) if necessary. </p>\r
60 \r
61 <p>FpdParserTask task stores all FPD information to GlobalData. And parse\r
62 tools definition file to set up compiler options for different Target and\r
63 different ToolChainTag. </p>\r
64 \r
65 <p>The method parseFpdFile is also prepared for single module build. </p>\r
66 \r
67 <p>The usage is (take NT32 Platform for example):</p>\r
68\r
69 <pre>\r
70 &lt;FPDParser platformName="Nt32" /&gt;\r
71 </pre>\r
72\r
73 <p>The task will initialize all information through parsing Framework Database, \r
74 SPD, Tool chain configuration files. </p>\r
75\r
76 @since GenBuild 1.0\r
77**/\r
78public class FpdParserTask extends Task {\r
79 \r
80 private String platformName;\r
81\r
82 private File fpdFile = null;\r
83 \r
84 private PlatformIdentification platformId;\r
85 \r
86 ///\r
87 /// \r
88 ///\r
89 private String type;\r
90 \r
91 ///\r
92 /// Mapping from modules identification to out put file name\r
93 ///\r
94 private Map<FpdModuleIdentification, String> outfiles = new LinkedHashMap<FpdModuleIdentification, String>();\r
95\r
96 ///\r
97 /// Mapping from FV name to its modules\r
98 ///\r
99 private Map<String, Set<FpdModuleIdentification>> fvs = new HashMap<String, Set<FpdModuleIdentification>>();\r
100\r
101 ///\r
102 /// Mapping from sequence number to FV names\r
103 ///\r
104 private Map<String, Set<String>> sequences = new TreeMap<String, Set<String>>();\r
105\r
106 ///\r
107 /// FpdParserTask can specify some ANT properties. \r
108 ///\r
109 private Vector<Property> properties = new Vector<Property>();\r
110 \r
111 private boolean isUnified = true;\r
112\r
113\r
114 /**\r
115 Public construct method. It is necessary for ANT task.\r
116 **/\r
117 public FpdParserTask() {\r
118 }\r
119\r
120 /**\r
121 ANT task's entry method. The main steps is described as following: \r
122 \r
123 <ul>\r
124 <li>Initialize global information (Framework DB, SPD files and all MSA files \r
125 listed in SPD). This step will execute only once in whole build process;</li>\r
126 <li>Parse specified FPD file; </li>\r
127 <li>Generate FV.inf files; </li>\r
128 <li>Generate PlatformName_build.xml file for Flatform build; </li>\r
129 <li>Collect PCD information. </li>\r
130 </ul>\r
131 \r
132 @throws BuildException\r
133 Surface area is not valid. \r
134 **/\r
135 public void execute() throws BuildException {\r
136 // Remove !!\r
137 if ( fpdFile == null) {\r
138 if (platformName == null) {\r
139 throw new BuildException("FpdParserTask parameter error. Please specify platform name or FPD file. ");\r
140 }\r
141 platformId = GlobalData.getPlatformByName(platformName);\r
142 fpdFile = platformId.getFpdFile();\r
143 }\r
144 \r
145 //\r
146 // Parse FPD file\r
147 //\r
148 parseFpdFile();\r
149 \r
150 //\r
151 // Prepare BUILD_DIR\r
152 //\r
153 isUnified = OutputManager.getInstance().prepareBuildDir(getProject());\r
154 \r
155 //\r
156 // Generate FDF (Flash Definition File) file\r
157 //\r
158\r
159 //\r
160 // For every Target and ToolChain\r
161 //\r
162 String[] targetList = GlobalData.getToolChainInfo().getTargets();\r
163 for (int i = 0; i < targetList.length; i++){\r
164 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();\r
165 for(int j = 0; j < toolchainList.length; j++){\r
166 //\r
167 // Prepare FV_DIR\r
168 //\r
169 String ffsCommonDir = getProject().getProperty("BUILD_DIR") + File.separatorChar \r
170 + targetList[i] + File.separatorChar \r
171 + toolchainList[j];\r
172 File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");\r
173 fvDir.mkdirs();\r
174 getProject().setProperty("FV_DIR", fvDir.getPath().replaceAll("(\\\\)", "/"));\r
175 \r
176 //\r
177 // Gen Fv.inf files\r
178 //\r
179 genFvInfFiles(ffsCommonDir);\r
180 }\r
181 }\r
182\r
183 //\r
184 // Gen build.xml\r
185 //\r
186 PlatformBuildFileGenerator fileGenerator = new PlatformBuildFileGenerator(getProject(), outfiles, fvs, sequences, isUnified);\r
187 fileGenerator.genBuildFile();\r
188 \r
189 //\r
190 // Ant call ${PLATFORM}_build.xml\r
191 //\r
192 \r
193 Ant ant = new Ant();\r
194 ant.setProject(getProject());\r
195 ant.setAntfile(platformId.getFpdFile().getParent() + File.separatorChar + platformId.getName() + "_build.xml");\r
196 ant.setTarget(type);\r
197 ant.setInheritAll(true);\r
198 ant.init();\r
199 ant.execute();\r
200 \r
201// GlobalData.log.info("Fpd build end. ");\r
202 }\r
203\r
204 /**\r
205 Generate Fv.inf files. The Fv.inf file is composed with four \r
206 parts: Options, Attributes, Components and Files. The Fv.inf files \r
207 will be under FV_DIR.\r
208 \r
209 @throws BuildException\r
210 File write FV.inf files error. \r
211 **/\r
212 private void genFvInfFiles(String ffsCommonDir) throws BuildException {\r
213 String[] validFv = SurfaceAreaQuery.getFpdValidImageNames();\r
214 for (int i = 0; i < validFv.length; i++) {\r
215 //\r
216 // Get all global variables from FPD and set them to properties\r
217 //\r
218 String[][] globalVariables = SurfaceAreaQuery.getFpdGlobalVariable();\r
219 for (int j = 0; j < globalVariables.length; j++) {\r
220 getProject().setProperty(globalVariables[j][0], globalVariables[j][1]);\r
221 }\r
222\r
223 getProject().setProperty("FV_FILENAME", validFv[i]);\r
224 \r
225 File fvFile = new File(getProject().replaceProperties( getProject().getProperty("FV_DIR") + File.separatorChar + validFv[i] + ".inf"));\r
226 fvFile.getParentFile().mkdirs();\r
227\r
228 try {\r
229 FileWriter fw = new FileWriter(fvFile);\r
230 BufferedWriter bw = new BufferedWriter(fw);\r
231 \r
232 //\r
233 // Options\r
234 //\r
235 String[][] options = SurfaceAreaQuery.getFpdOptions(validFv[i]);\r
236 if (options.length > 0) {\r
237 bw.write("[options]");\r
238 bw.newLine();\r
239 for (int j = 0; j < options.length; j++) {\r
240 StringBuffer str = new StringBuffer(100);\r
241 str.append(options[j][0]);\r
242 while (str.length() < 40) {\r
243 str.append(' ');\r
244 }\r
245 str.append("= ");\r
246 str.append(options[j][1]);\r
247 bw.write(getProject().replaceProperties(str.toString()));\r
248 bw.newLine();\r
249 }\r
250 bw.newLine();\r
251 }\r
252 \r
253 //\r
254 // Attributes;\r
255 //\r
256 String[][] attributes = SurfaceAreaQuery.getFpdAttributes(validFv[i]);\r
257 if (attributes.length > 0) {\r
258 bw.write("[attributes]");\r
259 bw.newLine();\r
260 for (int j = 0; j < attributes.length; j++) {\r
261 StringBuffer str = new StringBuffer(100);\r
262 str.append(attributes[j][0]);\r
263 while (str.length() < 40) {\r
264 str.append(' ');\r
265 }\r
266 str.append("= ");\r
267 str.append(attributes[j][1]);\r
268 bw.write(getProject().replaceProperties(str.toString()));\r
269 bw.newLine();\r
270 }\r
271 bw.newLine();\r
272 }\r
273 \r
274 //\r
275 // Components\r
276 //\r
277 String[][] components = SurfaceAreaQuery.getFpdComponents(validFv[i]);\r
278 if (components.length > 0) {\r
279 bw.write("[components]");\r
280 bw.newLine();\r
281 for (int j = 0; j < components.length; j++) {\r
282 StringBuffer str = new StringBuffer(100);\r
283 str.append(components[j][0]);\r
284 while (str.length() < 40) {\r
285 str.append(' ');\r
286 }\r
287 str.append("= ");\r
288 str.append(components[j][1]);\r
289 bw.write(getProject().replaceProperties(str.toString()));\r
290 bw.newLine();\r
291 }\r
292 bw.newLine();\r
293 }\r
294 \r
295 //\r
296 // Files\r
297 //\r
298 Set<FpdModuleIdentification> filesSet = fvs.get(validFv[i]);\r
299 if (filesSet != null) {\r
300 FpdModuleIdentification[] files = filesSet.toArray(new FpdModuleIdentification[filesSet.size()]);\r
301 bw.write("[files]");\r
302 bw.newLine();\r
303 for (int j = 0; j < files.length; j++) {\r
304 String str = ffsCommonDir + File.separatorChar + outfiles.get(files[j]);\r
305 bw.write(getProject().replaceProperties("EFI_FILE_NAME = " + str));\r
306 bw.newLine();\r
307 }\r
308 }\r
309 bw.flush();\r
310 bw.close();\r
311 fw.close();\r
312 } catch (Exception e) {\r
313 throw new BuildException("Generate FV file [" + fvFile.getPath() + "] failed. \n" + e.getMessage());\r
314 }\r
315 }\r
316 }\r
317 /**\r
318 This method is used for Single Module Build.\r
319 \r
320 \r
321 @throws BuildException\r
322 FPD file is not valid. \r
323 **/\r
324 public void parseFpdFile(File fpdFile) throws BuildException {\r
325 this.fpdFile = fpdFile;\r
326 parseFpdFile();\r
327 }\r
328\r
329 /**\r
330 Parse FPD file. \r
331 \r
332 @throws BuildException\r
333 FPD file is not valid. \r
334 **/\r
335 private void parseFpdFile() throws BuildException {\r
336 try {\r
337 XmlObject doc = XmlObject.Factory.parse(fpdFile);\r
338 \r
339 if (!doc.validate()) {\r
340 throw new BuildException("Platform Surface Area file [" + fpdFile.getPath() + "] is invalid.");\r
341 }\r
342 \r
343 Map<String, XmlObject> map = new HashMap<String, XmlObject>();\r
344 map.put("PlatformSurfaceArea", doc);\r
345 SurfaceAreaQuery.setDoc(map);\r
346\r
347 //\r
348 // Initialize\r
349 //\r
350 platformId = SurfaceAreaQuery.getFpdHeader();\r
351 platformId.setFpdFile(fpdFile);\r
352 getProject().setProperty("PLATFORM", platformId.getName());\r
353 getProject().setProperty("PLATFORM_FILE", platformId.getRelativeFpdFile().replaceAll("(\\\\)", "/"));\r
354 getProject().setProperty("PLATFORM_DIR", platformId.getFpdFile().getParent().replaceAll("(\\\\)", "/"));\r
355 getProject().setProperty("PLATFORM_RELATIVE_DIR", platformId.getPlatformRelativeDir().replaceAll("(\\\\)", "/"));\r
356\r
357 //\r
358 // Build mode. User-defined output dir. \r
359 //\r
360 String buildMode = SurfaceAreaQuery.getFpdIntermediateDirectories();\r
361 String userDefinedOutputDir = SurfaceAreaQuery.getFpdOutputDirectory();\r
362\r
363 OutputManager.getInstance().setup(userDefinedOutputDir, buildMode);\r
364\r
365 //\r
366 // TBD. Deal PCD and BuildOption related Info\r
367 //\r
368 GlobalData.setFpdBuildOptions(SurfaceAreaQuery.getFpdBuildOptions());\r
369 \r
370 GlobalData.setToolChainPlatformInfo(SurfaceAreaQuery.getFpdToolChainInfo());\r
371 \r
372 //\r
373 // Parse all list modules SA\r
374 //\r
375 parseModuleSAFiles();\r
376\r
377 //\r
378 // TBD. Deal PCD and BuildOption related Info\r
379 //\r
380 parseToolChainFamilyOptions();\r
381 parseToolChainOptions();\r
382\r
383 SurfaceAreaQuery.setDoc(map);\r
384 \r
385 //\r
386 // Pcd Collection. Call CollectPCDAction to collect pcd info.\r
387 //\r
388 try {\r
389 CollectPCDAction ca = new CollectPCDAction();\r
390 ca.perform(GlobalData.getWorkspacePath(),platformId.getFpdFile().getPath(),ActionMessage.NULL_MESSAGE_LEVEL);\r
391 } catch (Exception e){\r
392 throw new BuildException(e.getMessage());\r
393 }\r
394 } catch (Exception e) {\r
395 throw new BuildException("Load FPD file [" + fpdFile.getPath() + "] error. \n" + e.getMessage());\r
396 }\r
397 }\r
398\r
399\r
400 \r
401 /**\r
402 Parse all modules listed in FPD file. \r
403 **/\r
404 private void parseModuleSAFiles() throws EdkException{\r
405 Map<FpdModuleIdentification, Map<String, XmlObject>> moduleSAs = SurfaceAreaQuery.getFpdModules();\r
406\r
407 //\r
408 // For every Module lists in FPD file.\r
409 //\r
410 Set<FpdModuleIdentification> keys = moduleSAs.keySet();\r
411 Iterator iter = keys.iterator();\r
412 while (iter.hasNext()) {\r
413 FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();\r
414 \r
415 //\r
416 // Judge if Module is existed? \r
417 // TBD\r
418 \r
419 GlobalData.registerFpdModuleSA(fpdModuleId, moduleSAs.get(fpdModuleId));\r
420\r
421 //\r
422 // Put fpdModuleId to the corresponding FV\r
423 //\r
424 SurfaceAreaQuery.push(GlobalData.getDoc(fpdModuleId));\r
425 String fvBinding = SurfaceAreaQuery.getModuleFvBindingKeyword();\r
426 SurfaceAreaQuery.pop();\r
427\r
428 fpdModuleId.setFvBinding(fvBinding);\r
429 String fvSequence = fpdModuleId.getSequence();\r
430 updateFvs(fvSequence, fvBinding, fpdModuleId);\r
431 \r
432 //\r
433 // Prepare for out put file name\r
434 //\r
435 ModuleIdentification moduleId = fpdModuleId.getModule();\r
436 SurfaceAreaQuery.push(GlobalData.getDoc(fpdModuleId));\r
437 String baseName = SurfaceAreaQuery.getModuleOutputFileBasename();\r
438 SurfaceAreaQuery.pop();\r
439 if (baseName == null) {\r
440 baseName = moduleId.getName();\r
441 }\r
442 outfiles.put(fpdModuleId, fpdModuleId.getArch() + File.separatorChar \r
443 + moduleId.getGuid() + "-" + baseName \r
444 + getSuffix(moduleId.getModuleType()));\r
445\r
446 //\r
447 // parse module build options, if any\r
448 // \r
449 SurfaceAreaQuery.push(GlobalData.getDoc(fpdModuleId));\r
450 GlobalData.addModuleToolChainOption(fpdModuleId, parseModuleBuildOptions(false));\r
451 GlobalData.addModuleToolChainFamilyOption(fpdModuleId, parseModuleBuildOptions(true));\r
452 SurfaceAreaQuery.pop();\r
453 }\r
454 }\r
455\r
456 private ToolChainMap parseModuleBuildOptions(boolean toolChainFamilyFlag) throws EdkException {\r
457 String[][] options = SurfaceAreaQuery.getModuleBuildOptions(toolChainFamilyFlag);\r
458 if (options == null || options.length == 0) {\r
459 return null;\r
460 }\r
461 return parseOptions(options);\r
462 }\r
463 \r
464 private ToolChainMap parsePlatformBuildOptions(boolean toolChainFamilyFlag) throws EdkException {\r
465 String[][] options = SurfaceAreaQuery.getPlatformBuildOptions(toolChainFamilyFlag);\r
466 if (options == null || options.length == 0) {\r
467 return null;\r
468 }\r
469 return parseOptions(options);\r
470 }\r
471\r
472 private ToolChainMap parseOptions(String[][] options) throws EdkException {\r
473 ToolChainMap map = new ToolChainMap();\r
474 int flagIndex = ToolChainElement.ATTRIBUTE.value;\r
475\r
476 for (int i = 0; i < options.length; ++i) {\r
477 String flagString = options[i][flagIndex];\r
478 if (flagString == null) {\r
479 flagString = "";\r
480 }\r
481 options[i][flagIndex] = ToolChainAttribute.FLAGS + "";\r
482 map.put(options[i], flagString.trim());\r
483 }\r
484\r
485 return map;\r
486 }\r
487 \r
488 private void parseToolChainFamilyOptions() throws EdkException {\r
489 GlobalData.setPlatformToolChainFamilyOption(parsePlatformBuildOptions(true));\r
490 }\r
491\r
492 private void parseToolChainOptions() throws EdkException {\r
493 GlobalData.setPlatformToolChainOption(parsePlatformBuildOptions(false));\r
494 }\r
495\r
496 /**\r
497 Add the current module to corresponding FV. \r
498 \r
499 @param fvName current FV name\r
500 @param moduleName current module identification\r
501 **/\r
502 private void updateFvs(String fvSequence, String fvName, FpdModuleIdentification fpdModuleId) {\r
503 if (fvName == null || fvName.trim().length() == 0) {\r
504 fvName = "NULL";\r
505 }\r
506 String[] fvNameArray = fvName.split("[, \t]+");\r
507 for (int i = 0; i < fvNameArray.length; i++) {\r
508 //\r
509 // Put module to corresponding fvName\r
510 //\r
511 if (fvs.containsKey(fvNameArray[i])) {\r
512 Set<FpdModuleIdentification> set = fvs.get(fvNameArray[i]);\r
513 set.add(fpdModuleId);\r
514 }\r
515 else {\r
516 Set<FpdModuleIdentification> set = new LinkedHashSet<FpdModuleIdentification>();\r
517 set.add(fpdModuleId);\r
518 fvs.put(fvNameArray[i], set);\r
519 }\r
520 \r
521 //\r
522 // Put fvName to corresponding fvSequence\r
523 //\r
524 if (sequences.containsKey(fvSequence)) {\r
525 Set<String> set = sequences.get(fvSequence);\r
526 set.add(fvNameArray[i]);\r
527 }\r
528 else {\r
529 Set<String> set = new LinkedHashSet<String>();\r
530 set.add(fvNameArray[i]);\r
531 sequences.put(fvSequence, set);\r
532 }\r
533 }\r
534 }\r
535\r
536 /**\r
537 Get the suffix based on module type. Current relationship are listed: \r
538 \r
539 <pre>\r
540 <b>ModuleType</b> <b>Suffix</b>\r
541 BASE .FFS\r
542 SEC .SEC\r
543 PEI_CORE .PEI\r
544 PEIM .PEI\r
545 DXE_CORE .DXE\r
546 DXE_DRIVER .DXE\r
547 DXE_RUNTIME_DRIVER .DXE\r
548 DXE_SAL_DRIVER .DXE\r
549 DXE_SMM_DRIVER .DXE\r
550 TOOL .FFS\r
551 UEFI_DRIVER .DXE\r
552 UEFI_APPLICATION .APP\r
553 USER_DEFINED .FFS\r
554 </pre>\r
555 \r
556 @param moduleType module type\r
557 @return\r
558 @throws BuildException\r
559 If module type is null\r
560 **/\r
561 public static String getSuffix(String moduleType) throws BuildException {\r
562 if (moduleType == null) {\r
563 throw new BuildException("Module type is not specified.");\r
564 }\r
565\r
566 String[][] suffix = { { "BASE", ".FFS"},\r
567 { "SEC", ".SEC" }, { "PEI_CORE", ".PEI" }, \r
568 { "PEIM", ".PEI" }, { "DXE_CORE", ".DXE" },\r
569 { "DXE_DRIVER", ".DXE" }, { "DXE_RUNTIME_DRIVER", ".DXE" }, \r
570 { "DXE_SAL_DRIVER", ".DXE" }, { "DXE_SMM_DRIVER", ".DXE" }, \r
571 { "TOOL", ".FFS" }, { "UEFI_DRIVER", ".DXE" },\r
572 { "UEFI_APPLICATION", ".APP" }, { "USER_DEFINED", ".FFS" } };\r
573 \r
574 for (int i = 0; i < suffix.length; i++) {\r
575 if (suffix[i][0].equalsIgnoreCase(moduleType)) {\r
576 return suffix[i][1];\r
577 }\r
578 }\r
579 //\r
580 // Default is '.FFS'\r
581 //\r
582 return ".FFS";\r
583 }\r
584 /**\r
585 Add a property. \r
586 \r
587 @param p property\r
588 **/\r
589 public void addProperty(Property p) {\r
590 properties.addElement(p);\r
591 }\r
592\r
593 public void setPlatformName(String platformName) {\r
594 this.platformName = platformName;\r
595 }\r
596\r
597 public void setFpdFile(File fpdFile) {\r
598 this.fpdFile = fpdFile;\r
599 }\r
600\r
601 public void setType(String type) {\r
602 this.type = type;\r
603 }\r
604 \r
605\r
606}\r