]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/fpd/FpdParserTask.java
Update GlobalData, SPD, SurfaceAreaQuery to using EdkException.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / fpd / FpdParserTask.java
1 /** @file
2 This file is ANT task FpdParserTask.
3
4 FpdParserTask is used to parse FPD (Framework Platform Description) and generate
5 build.out.xml. It is for Package or Platform build use.
6
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
12
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.
15 **/
16 package org.tianocore.build.fpd;
17
18 import java.io.BufferedWriter;
19 import java.io.File;
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;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.Vector;
28
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;
34
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;
49
50 /**
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.
54
55 <p>The task sets global properties PLATFORM, PLATFORM_DIR, PLATFORM_RELATIVE_DIR
56 and BUILD_DIR. </p>
57
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>
60
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>
64
65 <p>The method parseFpdFile is also prepared for single module build. </p>
66
67 @since GenBuild 1.0
68 **/
69 public class FpdParserTask extends Task {
70
71 private File fpdFile = null;
72
73 PlatformIdentification platformId;
74
75 private String type;
76
77 ///
78 /// Mapping from modules identification to out put file name
79 ///
80 Map<FpdModuleIdentification, String> outfiles = new LinkedHashMap<FpdModuleIdentification, String>();
81
82 ///
83 /// Mapping from FV name to its modules
84 ///
85 Map<String, Set<FpdModuleIdentification>> fvs = new HashMap<String, Set<FpdModuleIdentification>>();
86
87 ///
88 /// FpdParserTask can specify some ANT properties.
89 ///
90 private Vector<Property> properties = new Vector<Property>();
91
92 SurfaceAreaQuery saq = null;
93
94 boolean isUnified = true;
95
96 /**
97 Public construct method. It is necessary for ANT task.
98 **/
99 public FpdParserTask() {
100 }
101
102 /**
103 ANT task's entry method. The main steps is described as following:
104
105 <ul>
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>
112 </ul>
113
114 @throws BuildException
115 Surface area is not valid.
116 **/
117 public void execute() throws BuildException {
118 //
119 // Parse FPD file
120 //
121 parseFpdFile();
122
123 //
124 // Prepare BUILD_DIR
125 //
126 isUnified = OutputManager.getInstance().prepareBuildDir(getProject());
127
128 //
129 // For every Target and ToolChain
130 //
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++) {
135 //
136 // Prepare FV_DIR
137 //
138 String ffsCommonDir = getProject().getProperty("BUILD_DIR") + File.separatorChar
139 + targetList[i] + "_"
140 + toolchainList[j];
141 File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
142 fvDir.mkdirs();
143 getProject().setProperty("FV_DIR", fvDir.getPath().replaceAll("(\\\\)", "/"));
144
145 //
146 // Gen Fv.inf files
147 //
148 genFvInfFiles(ffsCommonDir);
149 }
150 }
151
152 //
153 // Gen build.xml
154 //
155 PlatformBuildFileGenerator fileGenerator = new PlatformBuildFileGenerator(getProject(), outfiles, fvs, isUnified, saq);
156 fileGenerator.genBuildFile();
157
158 //
159 // Ant call ${PLATFORM}_build.xml
160 //
161 Ant ant = new Ant();
162 ant.setProject(getProject());
163 ant.setAntfile(platformId.getFpdFile().getParent() + File.separatorChar + platformId.getName() + "_build.xml");
164 ant.setTarget(type);
165 ant.setInheritAll(true);
166 ant.init();
167 ant.execute();
168 }
169
170 /**
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.
174
175 @throws BuildException
176 File write FV.inf files error.
177 **/
178 void genFvInfFiles(String ffsCommonDir) throws BuildException {
179 String[] validFv = saq.getFpdValidImageNames();
180 for (int i = 0; i < validFv.length; i++) {
181 //
182 // Get all global variables from FPD and set them to properties
183 //
184 String[][] globalVariables = saq.getFpdGlobalVariable();
185 for (int j = 0; j < globalVariables.length; j++) {
186 getProject().setProperty(globalVariables[j][0], globalVariables[j][1]);
187 }
188
189 getProject().setProperty("FV_FILENAME", validFv[i]);
190
191 File fvFile = new File(getProject().replaceProperties( getProject().getProperty("FV_DIR") + File.separatorChar + validFv[i] + ".inf"));
192 fvFile.getParentFile().mkdirs();
193
194 try {
195 FileWriter fw = new FileWriter(fvFile);
196 BufferedWriter bw = new BufferedWriter(fw);
197
198 //
199 // Options
200 //
201 String[][] options = saq.getFpdOptions(validFv[i]);
202 if (options.length > 0) {
203 bw.write("[options]");
204 bw.newLine();
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) {
209 str.append(' ');
210 }
211 str.append("= ");
212 str.append(options[j][1]);
213 bw.write(getProject().replaceProperties(str.toString()));
214 bw.newLine();
215 }
216 bw.newLine();
217 }
218
219 //
220 // Attributes;
221 //
222 String[][] attributes = saq.getFpdAttributes(validFv[i]);
223 if (attributes.length > 0) {
224 bw.write("[attributes]");
225 bw.newLine();
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) {
230 str.append(' ');
231 }
232 str.append("= ");
233 str.append(attributes[j][1]);
234 bw.write(getProject().replaceProperties(str.toString()));
235 bw.newLine();
236 }
237 bw.newLine();
238 }
239
240 //
241 // Components
242 //
243 String[][] components = saq.getFpdComponents(validFv[i]);
244 if (components.length > 0) {
245 bw.write("[components]");
246 bw.newLine();
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) {
251 str.append(' ');
252 }
253 str.append("= ");
254 str.append(components[j][1]);
255 bw.write(getProject().replaceProperties(str.toString()));
256 bw.newLine();
257 }
258 bw.newLine();
259 }
260
261 //
262 // Files
263 //
264 Set<FpdModuleIdentification> filesSet = fvs.get(validFv[i]);
265 if (filesSet != null) {
266 FpdModuleIdentification[] files = filesSet.toArray(new FpdModuleIdentification[filesSet.size()]);
267 bw.write("[files]");
268 bw.newLine();
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));
272 bw.newLine();
273 }
274 }
275 bw.flush();
276 bw.close();
277 fw.close();
278 } catch (Exception e) {
279 throw new BuildException("Generation of the FV file [" + fvFile.getPath() + "] failed!\n" + e.getMessage());
280 }
281 }
282 }
283 /**
284 This method is used for Single Module Build.
285
286
287 @throws BuildException
288 FPD file is not valid.
289 **/
290 public void parseFpdFile(File fpdFile) throws BuildException {
291 this.fpdFile = fpdFile;
292 parseFpdFile();
293 }
294
295 /**
296 Parse FPD file.
297
298 @throws BuildException
299 FPD file is not valid.
300 **/
301 void parseFpdFile() throws BuildException {
302 try {
303 XmlObject doc = XmlObject.Factory.parse(fpdFile);
304
305 if (!doc.validate()) {
306 throw new BuildException("Platform Surface Area file [" + fpdFile.getPath() + "] format is invalid!");
307 }
308
309 Map<String, XmlObject> map = new HashMap<String, XmlObject>();
310 map.put("PlatformSurfaceArea", doc);
311 saq = new SurfaceAreaQuery(map);
312
313 //
314 // Initialize
315 //
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("(\\\\)", "/"));
322
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("(\\\\)", "/"));
328 }
329
330 //
331 // Build mode. User-defined output dir.
332 //
333 String buildMode = saq.getFpdIntermediateDirectories();
334 String userDefinedOutputDir = saq.getFpdOutputDirectory();
335
336 OutputManager.getInstance().setup(userDefinedOutputDir, buildMode);
337
338 //
339 // TBD. Deal PCD and BuildOption related Info
340 //
341 GlobalData.setFpdBuildOptions(saq.getFpdBuildOptions());
342
343 GlobalData.setToolChainPlatformInfo(saq.getFpdToolChainInfo());
344
345 //
346 // Parse all list modules SA
347 //
348 parseModuleSAFiles();
349
350 //
351 // TBD. Deal PCD and BuildOption related Info
352 //
353 parseToolChainFamilyOptions();
354 parseToolChainOptions();
355
356 saq.push(map);
357
358 //
359 // Pcd Collection. Call CollectPCDAction to collect pcd info.
360 //
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());
365 }
366 }
367
368 /**
369 Parse all modules listed in FPD file.
370 **/
371 void parseModuleSAFiles() throws EdkException{
372 Map<FpdModuleIdentification, Map<String, XmlObject>> moduleSAs = saq.getFpdModules();
373
374 //
375 // For every Module lists in FPD file.
376 //
377 Set<FpdModuleIdentification> keys = moduleSAs.keySet();
378 Iterator iter = keys.iterator();
379 while (iter.hasNext()) {
380 FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
381
382 //
383 // Judge if Module is existed?
384 // TBD
385 GlobalData.registerFpdModuleSA(fpdModuleId, moduleSAs.get(fpdModuleId));
386
387 //
388 // Put fpdModuleId to the corresponding FV
389 //
390 saq.push(GlobalData.getDoc(fpdModuleId));
391 String fvBinding = saq.getModuleFvBindingKeyword();
392
393 fpdModuleId.setFvBinding(fvBinding);
394 updateFvs(fvBinding, fpdModuleId);
395
396 //
397 // Prepare for out put file name
398 //
399 ModuleIdentification moduleId = fpdModuleId.getModule();
400
401 String baseName = saq.getModuleOutputFileBasename();
402
403 if (baseName == null) {
404 baseName = moduleId.getName();
405 }
406 outfiles.put(fpdModuleId, fpdModuleId.getArch() + File.separatorChar
407 + moduleId.getGuid() + "-" + baseName
408 + getSuffix(moduleId.getModuleType()));
409
410 //
411 // parse module build options, if any
412 //
413 GlobalData.addModuleToolChainOption(fpdModuleId, parseModuleBuildOptions(false));
414 GlobalData.addModuleToolChainFamilyOption(fpdModuleId, parseModuleBuildOptions(true));
415 saq.pop();
416 }
417 }
418
419 ToolChainMap parseModuleBuildOptions(boolean toolChainFamilyFlag) throws EdkException {
420 String[][] options = saq.getModuleBuildOptions(toolChainFamilyFlag);
421 if (options == null || options.length == 0) {
422 return new ToolChainMap();
423 }
424 return parseOptions(options);
425 }
426
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();
431 }
432 return parseOptions(options);
433 }
434
435 private ToolChainMap parseOptions(String[][] options) throws EdkException {
436 ToolChainMap map = new ToolChainMap();
437 int flagIndex = ToolChainElement.ATTRIBUTE.value;
438
439 for (int i = 0; i < options.length; ++i) {
440 String flagString = options[i][flagIndex];
441 if (flagString == null) {
442 flagString = "";
443 }
444 options[i][flagIndex] = ToolChainAttribute.FLAGS + "";
445 map.put(options[i], flagString.trim());
446 }
447
448 return map;
449 }
450
451 private void parseToolChainFamilyOptions() throws EdkException {
452 GlobalData.setPlatformToolChainFamilyOption(parsePlatformBuildOptions(true));
453 }
454
455 private void parseToolChainOptions() throws EdkException {
456 GlobalData.setPlatformToolChainOption(parsePlatformBuildOptions(false));
457 }
458
459 /**
460 Add the current module to corresponding FV.
461
462 @param fvName current FV name
463 @param moduleName current module identification
464 **/
465 void updateFvs(String fvName, FpdModuleIdentification fpdModuleId) {
466 if (fvName == null || fvName.trim().length() == 0) {
467 fvName = "NULL";
468 }
469 String[] fvNameArray = fvName.split("[, \t]+");
470 for (int i = 0; i < fvNameArray.length; i++) {
471 //
472 // Put module to corresponding fvName
473 //
474 if (fvs.containsKey(fvNameArray[i])) {
475 Set<FpdModuleIdentification> set = fvs.get(fvNameArray[i]);
476 set.add(fpdModuleId);
477 } else {
478 Set<FpdModuleIdentification> set = new LinkedHashSet<FpdModuleIdentification>();
479 set.add(fpdModuleId);
480 fvs.put(fvNameArray[i], set);
481 }
482 }
483 }
484
485 /**
486 Get the suffix based on module type. Current relationship are listed:
487
488 <pre>
489 <b>ModuleType</b> <b>Suffix</b>
490 BASE .FFS
491 SEC .SEC
492 PEI_CORE .PEI
493 PEIM .PEI
494 DXE_CORE .DXE
495 DXE_DRIVER .DXE
496 DXE_RUNTIME_DRIVER .DXE
497 DXE_SAL_DRIVER .DXE
498 DXE_SMM_DRIVER .DXE
499 TOOL .FFS
500 UEFI_DRIVER .DXE
501 UEFI_APPLICATION .APP
502 USER_DEFINED .FFS
503 </pre>
504
505 @param moduleType module type
506 @return
507 @throws BuildException
508 If module type is null
509 **/
510 public static String getSuffix(String moduleType) throws BuildException {
511 if (moduleType == null) {
512 throw new BuildException("Module type is not specified.");
513 }
514
515 String[][] suffix = EdkDefinitions.ModuleTypeExtensions;
516
517 for (int i = 0; i < suffix.length; i++) {
518 if (suffix[i][0].equalsIgnoreCase(moduleType)) {
519 return suffix[i][1];
520 }
521 }
522 //
523 // Default is '.FFS'
524 //
525 return ".FFS";
526 }
527 /**
528 Add a property.
529
530 @param p property
531 **/
532 public void addProperty(Property p) {
533 properties.addElement(p);
534 }
535
536 public void setFpdFile(File fpdFile) {
537 this.fpdFile = fpdFile;
538 }
539
540 public void setType(String type) {
541 this.type = type;
542 }
543
544 public String getAllArchForModule(ModuleIdentification moduleId) {
545 String archs = "";
546 Iterator<FpdModuleIdentification> iter = outfiles.keySet().iterator();
547 while (iter.hasNext()) {
548 FpdModuleIdentification fpdModuleId = iter.next();
549
550 if (fpdModuleId.getModule().equals(moduleId)) {
551 archs += fpdModuleId.getArch() + " ";
552 }
553 }
554
555 return archs;
556 }
557 }