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
.definitions
.EdkDefinitions
;
36 import org
.tianocore
.common
.exception
.EdkException
;
37 import org
.tianocore
.pcd
.action
.ActionMessage
;
38 import org
.tianocore
.build
.FrameworkBuildTask
;
39 import org
.tianocore
.build
.global
.GlobalData
;
40 import org
.tianocore
.build
.global
.OutputManager
;
41 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
42 import org
.tianocore
.build
.id
.FpdModuleIdentification
;
43 import org
.tianocore
.build
.id
.ModuleIdentification
;
44 import org
.tianocore
.build
.id
.PlatformIdentification
;
45 import org
.tianocore
.build
.pcd
.action
.PlatformPcdPreprocessActionForBuilding
;
46 import org
.tianocore
.build
.toolchain
.ToolChainAttribute
;
47 import org
.tianocore
.build
.toolchain
.ToolChainElement
;
48 import org
.tianocore
.build
.toolchain
.ToolChainMap
;
51 <code>FpdParserTask</code> is an ANT task. The main function is parsing Framework
52 Platform Descritpion (FPD) XML file and generating its ANT build script for
53 corresponding platform.
55 <p>The task sets global properties PLATFORM, PLATFORM_DIR, PLATFORM_RELATIVE_DIR
58 <p>The task generates ${PLATFORM}_build.xml file which will be called by top level
59 build.xml. The task also generate Fv.inf files (File is for Tool GenFvImage). </p>
61 <p>FpdParserTask task stores all FPD information to GlobalData. And parse
62 tools definition file to set up compiler options for different Target and
63 different ToolChainTag. </p>
65 <p>The method parseFpdFile is also prepared for single module build. </p>
69 public class FpdParserTask
extends Task
{
71 private File fpdFile
= null;
73 PlatformIdentification platformId
;
78 /// Mapping from modules identification to out put file name
80 Map
<FpdModuleIdentification
, String
> outfiles
= new LinkedHashMap
<FpdModuleIdentification
, String
>();
83 /// Mapping from FV name to its modules
85 Map
<String
, Set
<FpdModuleIdentification
>> fvs
= new HashMap
<String
, Set
<FpdModuleIdentification
>>();
88 /// FpdParserTask can specify some ANT properties.
90 private Vector
<Property
> properties
= new Vector
<Property
>();
92 SurfaceAreaQuery saq
= null;
94 boolean isUnified
= true;
97 Public construct method. It is necessary for ANT task.
99 public FpdParserTask() {
103 ANT task's entry method. The main steps is described as following:
106 <li>Initialize global information (Framework DB, SPD files and all MSA files
107 listed in SPD). This step will execute only once in whole build process;</li>
108 <li>Parse specified FPD file; </li>
109 <li>Generate FV.inf files; </li>
110 <li>Generate PlatformName_build.xml file for Flatform build; </li>
111 <li>Collect PCD information. </li>
114 @throws BuildException
115 Surface area is not valid.
117 public void execute() throws BuildException
{
126 isUnified
= OutputManager
.getInstance().prepareBuildDir(getProject());
129 // For every Target and ToolChain
131 String
[] targetList
= GlobalData
.getToolChainInfo().getTargets();
132 for (int i
= 0; i
< targetList
.length
; i
++) {
133 String
[] toolchainList
= GlobalData
.getToolChainInfo().getTagnames();
134 for(int j
= 0; j
< toolchainList
.length
; j
++) {
138 String ffsCommonDir
= getProject().getProperty("BUILD_DIR") + File
.separatorChar
139 + targetList
[i
] + "_"
141 File fvDir
= new File(ffsCommonDir
+ File
.separatorChar
+ "FV");
143 getProject().setProperty("FV_DIR", fvDir
.getPath().replaceAll("(\\\\)", "/"));
148 genFvInfFiles(ffsCommonDir
);
155 PlatformBuildFileGenerator fileGenerator
= new PlatformBuildFileGenerator(getProject(), outfiles
, fvs
, isUnified
, saq
);
156 fileGenerator
.genBuildFile();
159 // Ant call ${PLATFORM}_build.xml
162 ant
.setProject(getProject());
163 ant
.setAntfile(platformId
.getFpdFile().getParent() + File
.separatorChar
+ platformId
.getName() + "_build.xml");
165 ant
.setInheritAll(true);
171 Generate Fv.inf files. The Fv.inf file is composed with four
172 parts: Options, Attributes, Components and Files. The Fv.inf files
173 will be under FV_DIR.
175 @throws BuildException
176 File write FV.inf files error.
178 void genFvInfFiles(String ffsCommonDir
) throws BuildException
{
179 String
[] validFv
= saq
.getFpdValidImageNames();
180 for (int i
= 0; i
< validFv
.length
; i
++) {
182 // Get all global variables from FPD and set them to properties
184 String
[][] globalVariables
= saq
.getFpdGlobalVariable();
185 for (int j
= 0; j
< globalVariables
.length
; j
++) {
186 getProject().setProperty(globalVariables
[j
][0], globalVariables
[j
][1]);
189 getProject().setProperty("FV_FILENAME", validFv
[i
]);
191 File fvFile
= new File(getProject().replaceProperties( getProject().getProperty("FV_DIR") + File
.separatorChar
+ validFv
[i
] + ".inf"));
192 fvFile
.getParentFile().mkdirs();
195 FileWriter fw
= new FileWriter(fvFile
);
196 BufferedWriter bw
= new BufferedWriter(fw
);
201 String
[][] options
= saq
.getFpdOptions(validFv
[i
]);
202 if (options
.length
> 0) {
203 bw
.write("[options]");
205 for (int j
= 0; j
< options
.length
; j
++) {
206 StringBuffer str
= new StringBuffer(100);
207 str
.append(options
[j
][0]);
208 while (str
.length() < 40) {
212 str
.append(options
[j
][1]);
213 bw
.write(getProject().replaceProperties(str
.toString()));
222 String
[][] attributes
= saq
.getFpdAttributes(validFv
[i
]);
223 if (attributes
.length
> 0) {
224 bw
.write("[attributes]");
226 for (int j
= 0; j
< attributes
.length
; j
++) {
227 StringBuffer str
= new StringBuffer(100);
228 str
.append(attributes
[j
][0]);
229 while (str
.length() < 40) {
233 str
.append(attributes
[j
][1]);
234 bw
.write(getProject().replaceProperties(str
.toString()));
243 String
[][] components
= saq
.getFpdComponents(validFv
[i
]);
244 if (components
.length
> 0) {
245 bw
.write("[components]");
247 for (int j
= 0; j
< components
.length
; j
++) {
248 StringBuffer str
= new StringBuffer(100);
249 str
.append(components
[j
][0]);
250 while (str
.length() < 40) {
254 str
.append(components
[j
][1]);
255 bw
.write(getProject().replaceProperties(str
.toString()));
264 Set
<FpdModuleIdentification
> filesSet
= fvs
.get(validFv
[i
]);
265 if (filesSet
!= null) {
266 FpdModuleIdentification
[] files
= filesSet
.toArray(new FpdModuleIdentification
[filesSet
.size()]);
269 for (int j
= 0; j
< files
.length
; j
++) {
270 String str
= ffsCommonDir
+ File
.separatorChar
+ outfiles
.get(files
[j
]);
271 bw
.write(getProject().replaceProperties("EFI_FILE_NAME = " + str
));
278 } catch (Exception e
) {
279 throw new BuildException("Generation of the FV file [" + fvFile
.getPath() + "] failed!\n" + e
.getMessage());
284 This method is used for Single Module Build.
287 @throws BuildException
288 FPD file is not valid.
290 public void parseFpdFile(File fpdFile
) throws BuildException
{
291 this.fpdFile
= fpdFile
;
298 @throws BuildException
299 FPD file is not valid.
301 void parseFpdFile() throws BuildException
{
303 XmlObject doc
= XmlObject
.Factory
.parse(fpdFile
);
305 if (!doc
.validate()) {
306 throw new BuildException("Platform Surface Area file [" + fpdFile
.getPath() + "] format is invalid!");
309 Map
<String
, XmlObject
> map
= new HashMap
<String
, XmlObject
>();
310 map
.put("PlatformSurfaceArea", doc
);
311 saq
= new SurfaceAreaQuery(map
);
316 platformId
= saq
.getFpdHeader();
317 platformId
.setFpdFile(fpdFile
);
318 getProject().setProperty("PLATFORM", platformId
.getName());
319 getProject().setProperty("PLATFORM_FILE", platformId
.getRelativeFpdFile().replaceAll("(\\\\)", "/"));
320 getProject().setProperty("PLATFORM_DIR", platformId
.getFpdFile().getParent().replaceAll("(\\\\)", "/"));
321 getProject().setProperty("PLATFORM_RELATIVE_DIR", platformId
.getPlatformRelativeDir().replaceAll("(\\\\)", "/"));
323 if( !FrameworkBuildTask
.multithread
) {
324 FrameworkBuildTask
.originalProperties
.put("PLATFORM", platformId
.getName());
325 FrameworkBuildTask
.originalProperties
.put("PLATFORM_FILE", platformId
.getRelativeFpdFile().replaceAll("(\\\\)", "/"));
326 FrameworkBuildTask
.originalProperties
.put("PLATFORM_DIR", platformId
.getFpdFile().getParent().replaceAll("(\\\\)", "/"));
327 FrameworkBuildTask
.originalProperties
.put("PLATFORM_RELATIVE_DIR", platformId
.getPlatformRelativeDir().replaceAll("(\\\\)", "/"));
331 // Build mode. User-defined output dir.
333 String buildMode
= saq
.getFpdIntermediateDirectories();
334 String userDefinedOutputDir
= saq
.getFpdOutputDirectory();
336 OutputManager
.getInstance().setup(userDefinedOutputDir
, buildMode
);
339 // TBD. Deal PCD and BuildOption related Info
341 GlobalData
.setFpdBuildOptions(saq
.getFpdBuildOptions());
343 GlobalData
.setToolChainPlatformInfo(saq
.getFpdToolChainInfo());
346 // Parse all list modules SA
348 parseModuleSAFiles();
351 // TBD. Deal PCD and BuildOption related Info
353 parseToolChainFamilyOptions();
354 parseToolChainOptions();
359 // Pcd Collection. Call CollectPCDAction to collect pcd info.
361 PlatformPcdPreprocessActionForBuilding ca
= new PlatformPcdPreprocessActionForBuilding();
362 ca
.perform(platformId
.getFpdFile().getPath(), ActionMessage
.NULL_MESSAGE_LEVEL
);
363 } catch (Exception e
) {
364 throw new BuildException("Parsing of the FPD file [" + fpdFile
.getPath() + "] failed!\n" + e
.getMessage());
369 Parse all modules listed in FPD file.
371 void parseModuleSAFiles() throws EdkException
{
372 Map
<FpdModuleIdentification
, Map
<String
, XmlObject
>> moduleSAs
= saq
.getFpdModules();
375 // For every Module lists in FPD file.
377 Set
<FpdModuleIdentification
> keys
= moduleSAs
.keySet();
378 Iterator iter
= keys
.iterator();
379 while (iter
.hasNext()) {
380 FpdModuleIdentification fpdModuleId
= (FpdModuleIdentification
) iter
.next();
383 // Judge if Module is existed?
385 GlobalData
.registerFpdModuleSA(fpdModuleId
, moduleSAs
.get(fpdModuleId
));
388 // Put fpdModuleId to the corresponding FV
390 saq
.push(GlobalData
.getDoc(fpdModuleId
));
391 String fvBinding
= saq
.getModuleFvBindingKeyword();
393 fpdModuleId
.setFvBinding(fvBinding
);
394 updateFvs(fvBinding
, fpdModuleId
);
397 // Prepare for out put file name
399 ModuleIdentification moduleId
= fpdModuleId
.getModule();
401 String baseName
= saq
.getModuleOutputFileBasename();
403 if (baseName
== null) {
404 baseName
= moduleId
.getName();
406 outfiles
.put(fpdModuleId
, fpdModuleId
.getArch() + File
.separatorChar
407 + moduleId
.getGuid() + "-" + baseName
408 + getSuffix(moduleId
.getModuleType()));
411 // parse module build options, if any
413 GlobalData
.addModuleToolChainOption(fpdModuleId
, parseModuleBuildOptions(false));
414 GlobalData
.addModuleToolChainFamilyOption(fpdModuleId
, parseModuleBuildOptions(true));
419 ToolChainMap
parseModuleBuildOptions(boolean toolChainFamilyFlag
) throws EdkException
{
420 String
[][] options
= saq
.getModuleBuildOptions(toolChainFamilyFlag
);
421 if (options
== null || options
.length
== 0) {
422 return new ToolChainMap();
424 return parseOptions(options
);
427 private ToolChainMap
parsePlatformBuildOptions(boolean toolChainFamilyFlag
) throws EdkException
{
428 String
[][] options
= saq
.getPlatformBuildOptions(toolChainFamilyFlag
);
429 if (options
== null || options
.length
== 0) {
430 return new ToolChainMap();
432 return parseOptions(options
);
435 private ToolChainMap
parseOptions(String
[][] options
) throws EdkException
{
436 ToolChainMap map
= new ToolChainMap();
437 int flagIndex
= ToolChainElement
.ATTRIBUTE
.value
;
439 for (int i
= 0; i
< options
.length
; ++i
) {
440 String flagString
= options
[i
][flagIndex
];
441 if (flagString
== null) {
444 options
[i
][flagIndex
] = ToolChainAttribute
.FLAGS
+ "";
445 map
.put(options
[i
], flagString
.trim());
451 private void parseToolChainFamilyOptions() throws EdkException
{
452 GlobalData
.setPlatformToolChainFamilyOption(parsePlatformBuildOptions(true));
455 private void parseToolChainOptions() throws EdkException
{
456 GlobalData
.setPlatformToolChainOption(parsePlatformBuildOptions(false));
460 Add the current module to corresponding FV.
462 @param fvName current FV name
463 @param moduleName current module identification
465 void updateFvs(String fvName
, FpdModuleIdentification fpdModuleId
) {
466 if (fvName
== null || fvName
.trim().length() == 0) {
469 String
[] fvNameArray
= fvName
.split("[, \t]+");
470 for (int i
= 0; i
< fvNameArray
.length
; i
++) {
472 // Put module to corresponding fvName
474 if (fvs
.containsKey(fvNameArray
[i
])) {
475 Set
<FpdModuleIdentification
> set
= fvs
.get(fvNameArray
[i
]);
476 set
.add(fpdModuleId
);
478 Set
<FpdModuleIdentification
> set
= new LinkedHashSet
<FpdModuleIdentification
>();
479 set
.add(fpdModuleId
);
480 fvs
.put(fvNameArray
[i
], set
);
486 Get the suffix based on module type. Current relationship are listed:
489 <b>ModuleType</b> <b>Suffix</b>
496 DXE_RUNTIME_DRIVER .DXE
501 UEFI_APPLICATION .APP
505 @param moduleType module type
507 @throws BuildException
508 If module type is null
510 public static String
getSuffix(String moduleType
) throws BuildException
{
511 if (moduleType
== null) {
512 throw new BuildException("Module type is not specified.");
515 String
[][] suffix
= EdkDefinitions
.ModuleTypeExtensions
;
517 for (int i
= 0; i
< suffix
.length
; i
++) {
518 if (suffix
[i
][0].equalsIgnoreCase(moduleType
)) {
532 public void addProperty(Property p
) {
533 properties
.addElement(p
);
536 public void setFpdFile(File fpdFile
) {
537 this.fpdFile
= fpdFile
;
540 public void setType(String type
) {
544 public String
getAllArchForModule(ModuleIdentification moduleId
) {
546 Iterator
<FpdModuleIdentification
> iter
= outfiles
.keySet().iterator();
547 while (iter
.hasNext()) {
548 FpdModuleIdentification fpdModuleId
= iter
.next();
550 if (fpdModuleId
.getModule().equals(moduleId
)) {
551 archs
+= fpdModuleId
.getArch() + " ";