2 This file is ANT task FpdParserTask.
4 FpdParserTask is used to parse FPD (Framework Platform Description) and generate
5 build.out.xml. It is for Package or Platform build use.
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 package org
.tianocore
.build
.fpd
;
18 import java
.io
.BufferedWriter
;
20 import java
.io
.FileWriter
;
21 import java
.util
.HashMap
;
22 import java
.util
.Iterator
;
23 import java
.util
.LinkedHashMap
;
24 import java
.util
.LinkedHashSet
;
27 import java
.util
.Vector
;
29 import org
.apache
.tools
.ant
.BuildException
;
30 import org
.apache
.tools
.ant
.Task
;
31 import org
.apache
.tools
.ant
.taskdefs
.Ant
;
32 import org
.apache
.tools
.ant
.taskdefs
.Property
;
33 import org
.apache
.xmlbeans
.XmlObject
;
35 import org
.tianocore
.common
.exception
.EdkException
;
36 import org
.tianocore
.pcd
.action
.ActionMessage
;
37 import org
.tianocore
.build
.global
.GlobalData
;
38 import org
.tianocore
.build
.global
.OutputManager
;
39 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
40 import org
.tianocore
.build
.id
.FpdModuleIdentification
;
41 import org
.tianocore
.build
.id
.ModuleIdentification
;
42 import org
.tianocore
.build
.id
.PlatformIdentification
;
43 import org
.tianocore
.build
.pcd
.action
.PlatformPcdPreprocessActionForBuilding
;
44 import org
.tianocore
.build
.toolchain
.ToolChainAttribute
;
45 import org
.tianocore
.build
.toolchain
.ToolChainElement
;
46 import org
.tianocore
.build
.toolchain
.ToolChainMap
;
49 <code>FpdParserTask</code> is an ANT task. The main function is parsing Framework
50 Platform Descritpion (FPD) XML file and generating its ANT build script for
51 corresponding platform.
53 <p>The task sets global properties PLATFORM, PLATFORM_DIR, PLATFORM_RELATIVE_DIR
56 <p>The task generates ${PLATFORM}_build.xml file which will be called by top level
57 build.xml. The task also generate Fv.inf files (File is for Tool GenFvImage)
58 and flash definition file (File is for Tool FlashMap) if necessary. </p>
60 <p>FpdParserTask task stores all FPD information to GlobalData. And parse
61 tools definition file to set up compiler options for different Target and
62 different ToolChainTag. </p>
64 <p>The method parseFpdFile is also prepared for single module build. </p>
66 <p>The usage is (take NT32 Platform for example):</p>
69 <FPDParser platformName="Nt32" />
72 <p>The task will initialize all information through parsing Framework Database,
73 SPD, Tool chain configuration files. </p>
77 public class FpdParserTask
extends Task
{
79 private String platformName
;
81 private File fpdFile
= null;
83 private PlatformIdentification platformId
;
91 /// Mapping from modules identification to out put file name
93 private Map
<FpdModuleIdentification
, String
> outfiles
= new LinkedHashMap
<FpdModuleIdentification
, String
>();
96 /// Mapping from FV name to its modules
98 private Map
<String
, Set
<FpdModuleIdentification
>> fvs
= new HashMap
<String
, Set
<FpdModuleIdentification
>>();
101 /// FpdParserTask can specify some ANT properties.
103 private Vector
<Property
> properties
= new Vector
<Property
>();
105 private boolean isUnified
= true;
109 Public construct method. It is necessary for ANT task.
111 public FpdParserTask() {
115 ANT task's entry method. The main steps is described as following:
118 <li>Initialize global information (Framework DB, SPD files and all MSA files
119 listed in SPD). This step will execute only once in whole build process;</li>
120 <li>Parse specified FPD file; </li>
121 <li>Generate FV.inf files; </li>
122 <li>Generate PlatformName_build.xml file for Flatform build; </li>
123 <li>Collect PCD information. </li>
126 @throws BuildException
127 Surface area is not valid.
129 public void execute() throws BuildException
{
131 if ( fpdFile
== null) {
132 if (platformName
== null) {
133 throw new BuildException("FpdParserTask parameter error. Please specify either the platform name or FPD file!");
135 platformId
= GlobalData
.getPlatformByName(platformName
);
136 fpdFile
= platformId
.getFpdFile();
147 isUnified
= OutputManager
.getInstance().prepareBuildDir(getProject());
150 // For every Target and ToolChain
152 String
[] targetList
= GlobalData
.getToolChainInfo().getTargets();
153 for (int i
= 0; i
< targetList
.length
; i
++){
154 String
[] toolchainList
= GlobalData
.getToolChainInfo().getTagnames();
155 for(int j
= 0; j
< toolchainList
.length
; j
++){
159 String ffsCommonDir
= getProject().getProperty("BUILD_DIR") + File
.separatorChar
160 + targetList
[i
] + File
.separatorChar
162 File fvDir
= new File(ffsCommonDir
+ File
.separatorChar
+ "FV");
164 getProject().setProperty("FV_DIR", fvDir
.getPath().replaceAll("(\\\\)", "/"));
169 genFvInfFiles(ffsCommonDir
);
176 PlatformBuildFileGenerator fileGenerator
= new PlatformBuildFileGenerator(getProject(), outfiles
, isUnified
);
177 fileGenerator
.genBuildFile();
180 // Ant call ${PLATFORM}_build.xml
184 ant
.setProject(getProject());
185 ant
.setAntfile(platformId
.getFpdFile().getParent() + File
.separatorChar
+ platformId
.getName() + "_build.xml");
187 ant
.setInheritAll(true);
191 // GlobalData.log.info("Fpd build end. ");
195 Generate Fv.inf files. The Fv.inf file is composed with four
196 parts: Options, Attributes, Components and Files. The Fv.inf files
197 will be under FV_DIR.
199 @throws BuildException
200 File write FV.inf files error.
202 private void genFvInfFiles(String ffsCommonDir
) throws BuildException
{
203 String
[] validFv
= SurfaceAreaQuery
.getFpdValidImageNames();
204 for (int i
= 0; i
< validFv
.length
; i
++) {
206 // Get all global variables from FPD and set them to properties
208 String
[][] globalVariables
= SurfaceAreaQuery
.getFpdGlobalVariable();
209 for (int j
= 0; j
< globalVariables
.length
; j
++) {
210 getProject().setProperty(globalVariables
[j
][0], globalVariables
[j
][1]);
213 getProject().setProperty("FV_FILENAME", validFv
[i
]);
215 File fvFile
= new File(getProject().replaceProperties( getProject().getProperty("FV_DIR") + File
.separatorChar
+ validFv
[i
] + ".inf"));
216 fvFile
.getParentFile().mkdirs();
219 FileWriter fw
= new FileWriter(fvFile
);
220 BufferedWriter bw
= new BufferedWriter(fw
);
225 String
[][] options
= SurfaceAreaQuery
.getFpdOptions(validFv
[i
]);
226 if (options
.length
> 0) {
227 bw
.write("[options]");
229 for (int j
= 0; j
< options
.length
; j
++) {
230 StringBuffer str
= new StringBuffer(100);
231 str
.append(options
[j
][0]);
232 while (str
.length() < 40) {
236 str
.append(options
[j
][1]);
237 bw
.write(getProject().replaceProperties(str
.toString()));
246 String
[][] attributes
= SurfaceAreaQuery
.getFpdAttributes(validFv
[i
]);
247 if (attributes
.length
> 0) {
248 bw
.write("[attributes]");
250 for (int j
= 0; j
< attributes
.length
; j
++) {
251 StringBuffer str
= new StringBuffer(100);
252 str
.append(attributes
[j
][0]);
253 while (str
.length() < 40) {
257 str
.append(attributes
[j
][1]);
258 bw
.write(getProject().replaceProperties(str
.toString()));
267 String
[][] components
= SurfaceAreaQuery
.getFpdComponents(validFv
[i
]);
268 if (components
.length
> 0) {
269 bw
.write("[components]");
271 for (int j
= 0; j
< components
.length
; j
++) {
272 StringBuffer str
= new StringBuffer(100);
273 str
.append(components
[j
][0]);
274 while (str
.length() < 40) {
278 str
.append(components
[j
][1]);
279 bw
.write(getProject().replaceProperties(str
.toString()));
288 Set
<FpdModuleIdentification
> filesSet
= fvs
.get(validFv
[i
]);
289 if (filesSet
!= null) {
290 FpdModuleIdentification
[] files
= filesSet
.toArray(new FpdModuleIdentification
[filesSet
.size()]);
293 for (int j
= 0; j
< files
.length
; j
++) {
294 String str
= ffsCommonDir
+ File
.separatorChar
+ outfiles
.get(files
[j
]);
295 bw
.write(getProject().replaceProperties("EFI_FILE_NAME = " + str
));
302 } catch (Exception e
) {
303 throw new BuildException("Generation of the FV file [" + fvFile
.getPath() + "] failed!\n" + e
.getMessage());
308 This method is used for Single Module Build.
311 @throws BuildException
312 FPD file is not valid.
314 public void parseFpdFile(File fpdFile
) throws BuildException
{
315 this.fpdFile
= fpdFile
;
322 @throws BuildException
323 FPD file is not valid.
325 private void parseFpdFile() throws BuildException
{
327 XmlObject doc
= XmlObject
.Factory
.parse(fpdFile
);
329 if (!doc
.validate()) {
330 throw new BuildException("Platform Surface Area file [" + fpdFile
.getPath() + "] format is invalid!");
333 Map
<String
, XmlObject
> map
= new HashMap
<String
, XmlObject
>();
334 map
.put("PlatformSurfaceArea", doc
);
335 SurfaceAreaQuery
.setDoc(map
);
340 platformId
= SurfaceAreaQuery
.getFpdHeader();
341 platformId
.setFpdFile(fpdFile
);
342 getProject().setProperty("PLATFORM", platformId
.getName());
343 getProject().setProperty("PLATFORM_FILE", platformId
.getRelativeFpdFile().replaceAll("(\\\\)", "/"));
344 getProject().setProperty("PLATFORM_DIR", platformId
.getFpdFile().getParent().replaceAll("(\\\\)", "/"));
345 getProject().setProperty("PLATFORM_RELATIVE_DIR", platformId
.getPlatformRelativeDir().replaceAll("(\\\\)", "/"));
348 // Build mode. User-defined output dir.
350 String buildMode
= SurfaceAreaQuery
.getFpdIntermediateDirectories();
351 String userDefinedOutputDir
= SurfaceAreaQuery
.getFpdOutputDirectory();
353 OutputManager
.getInstance().setup(userDefinedOutputDir
, buildMode
);
356 // TBD. Deal PCD and BuildOption related Info
358 GlobalData
.setFpdBuildOptions(SurfaceAreaQuery
.getFpdBuildOptions());
360 GlobalData
.setToolChainPlatformInfo(SurfaceAreaQuery
.getFpdToolChainInfo());
363 // Parse all list modules SA
365 parseModuleSAFiles();
368 // TBD. Deal PCD and BuildOption related Info
370 parseToolChainFamilyOptions();
371 parseToolChainOptions();
373 SurfaceAreaQuery
.setDoc(map
);
376 // Pcd Collection. Call CollectPCDAction to collect pcd info.
378 PlatformPcdPreprocessActionForBuilding ca
= new PlatformPcdPreprocessActionForBuilding();
379 ca
.perform(platformId
.getFpdFile().getPath(), ActionMessage
.NULL_MESSAGE_LEVEL
);
380 } catch (Exception e
) {
381 throw new BuildException("Parsing of the FPD file [" + fpdFile
.getPath() + "] failed!\n" + e
.getMessage());
388 Parse all modules listed in FPD file.
390 private void parseModuleSAFiles() throws EdkException
{
391 Map
<FpdModuleIdentification
, Map
<String
, XmlObject
>> moduleSAs
= SurfaceAreaQuery
.getFpdModules();
394 // For every Module lists in FPD file.
396 Set
<FpdModuleIdentification
> keys
= moduleSAs
.keySet();
397 Iterator iter
= keys
.iterator();
398 while (iter
.hasNext()) {
399 FpdModuleIdentification fpdModuleId
= (FpdModuleIdentification
) iter
.next();
402 // Judge if Module is existed?
405 GlobalData
.registerFpdModuleSA(fpdModuleId
, moduleSAs
.get(fpdModuleId
));
408 // Put fpdModuleId to the corresponding FV
410 SurfaceAreaQuery
.push(GlobalData
.getDoc(fpdModuleId
));
411 String fvBinding
= SurfaceAreaQuery
.getModuleFvBindingKeyword();
412 SurfaceAreaQuery
.pop();
414 fpdModuleId
.setFvBinding(fvBinding
);
415 updateFvs(fvBinding
, fpdModuleId
);
418 // Prepare for out put file name
420 ModuleIdentification moduleId
= fpdModuleId
.getModule();
421 SurfaceAreaQuery
.push(GlobalData
.getDoc(fpdModuleId
));
422 String baseName
= SurfaceAreaQuery
.getModuleOutputFileBasename();
423 SurfaceAreaQuery
.pop();
424 if (baseName
== null) {
425 baseName
= moduleId
.getName();
427 outfiles
.put(fpdModuleId
, fpdModuleId
.getArch() + File
.separatorChar
428 + moduleId
.getGuid() + "-" + baseName
429 + getSuffix(moduleId
.getModuleType()));
432 // parse module build options, if any
434 SurfaceAreaQuery
.push(GlobalData
.getDoc(fpdModuleId
));
435 GlobalData
.addModuleToolChainOption(fpdModuleId
, parseModuleBuildOptions(false));
436 GlobalData
.addModuleToolChainFamilyOption(fpdModuleId
, parseModuleBuildOptions(true));
437 SurfaceAreaQuery
.pop();
441 private ToolChainMap
parseModuleBuildOptions(boolean toolChainFamilyFlag
) throws EdkException
{
442 String
[][] options
= SurfaceAreaQuery
.getModuleBuildOptions(toolChainFamilyFlag
);
443 if (options
== null || options
.length
== 0) {
446 return parseOptions(options
);
449 private ToolChainMap
parsePlatformBuildOptions(boolean toolChainFamilyFlag
) throws EdkException
{
450 String
[][] options
= SurfaceAreaQuery
.getPlatformBuildOptions(toolChainFamilyFlag
);
451 if (options
== null || options
.length
== 0) {
454 return parseOptions(options
);
457 private ToolChainMap
parseOptions(String
[][] options
) throws EdkException
{
458 ToolChainMap map
= new ToolChainMap();
459 int flagIndex
= ToolChainElement
.ATTRIBUTE
.value
;
461 for (int i
= 0; i
< options
.length
; ++i
) {
462 String flagString
= options
[i
][flagIndex
];
463 if (flagString
== null) {
466 options
[i
][flagIndex
] = ToolChainAttribute
.FLAGS
+ "";
467 map
.put(options
[i
], flagString
.trim());
473 private void parseToolChainFamilyOptions() throws EdkException
{
474 GlobalData
.setPlatformToolChainFamilyOption(parsePlatformBuildOptions(true));
477 private void parseToolChainOptions() throws EdkException
{
478 GlobalData
.setPlatformToolChainOption(parsePlatformBuildOptions(false));
482 Add the current module to corresponding FV.
484 @param fvName current FV name
485 @param moduleName current module identification
487 private void updateFvs(String fvName
, FpdModuleIdentification fpdModuleId
) {
488 if (fvName
== null || fvName
.trim().length() == 0) {
491 String
[] fvNameArray
= fvName
.split("[, \t]+");
492 for (int i
= 0; i
< fvNameArray
.length
; i
++) {
494 // Put module to corresponding fvName
496 if (fvs
.containsKey(fvNameArray
[i
])) {
497 Set
<FpdModuleIdentification
> set
= fvs
.get(fvNameArray
[i
]);
498 set
.add(fpdModuleId
);
501 Set
<FpdModuleIdentification
> set
= new LinkedHashSet
<FpdModuleIdentification
>();
502 set
.add(fpdModuleId
);
503 fvs
.put(fvNameArray
[i
], set
);
509 Get the suffix based on module type. Current relationship are listed:
512 <b>ModuleType</b> <b>Suffix</b>
519 DXE_RUNTIME_DRIVER .DXE
524 UEFI_APPLICATION .APP
528 @param moduleType module type
530 @throws BuildException
531 If module type is null
533 public static String
getSuffix(String moduleType
) throws BuildException
{
534 if (moduleType
== null) {
535 throw new BuildException("Module type is not specified.");
538 String
[][] suffix
= { { "BASE", ".FFS"},
539 { "SEC", ".SEC" }, { "PEI_CORE", ".PEI" },
540 { "PEIM", ".PEI" }, { "DXE_CORE", ".DXE" },
541 { "DXE_DRIVER", ".DXE" }, { "DXE_RUNTIME_DRIVER", ".DXE" },
542 { "DXE_SAL_DRIVER", ".DXE" }, { "DXE_SMM_DRIVER", ".DXE" },
543 { "TOOL", ".FFS" }, { "UEFI_DRIVER", ".DXE" },
544 { "UEFI_APPLICATION", ".APP" }, { "USER_DEFINED", ".FFS" } };
546 for (int i
= 0; i
< suffix
.length
; i
++) {
547 if (suffix
[i
][0].equalsIgnoreCase(moduleType
)) {
561 public void addProperty(Property p
) {
562 properties
.addElement(p
);
565 public void setPlatformName(String platformName
) {
566 this.platformName
= platformName
;
569 public void setFpdFile(File fpdFile
) {
570 this.fpdFile
= fpdFile
;
573 public void setType(String type
) {