]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/fpd/PlatformBuildFileGenerator.java
Modify the way of pcd warning message reporting to be a total in the end.
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / fpd / PlatformBuildFileGenerator.java
1 /** @file
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 **/
12 package org.tianocore.build.fpd;
13
14 import java.io.File;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.transform.OutputKeys;
23 import javax.xml.transform.Result;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.dom.DOMSource;
28 import javax.xml.transform.stream.StreamResult;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.tianocore.build.global.GlobalData;
33 import org.tianocore.build.global.SurfaceAreaQuery;
34 import org.tianocore.build.id.FpdModuleIdentification;
35 import org.tianocore.build.id.ModuleIdentification;
36 import org.w3c.dom.Comment;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.NamedNodeMap;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42
43 /**
44 class PlatformBuildFileGenerator is used to generate ${PLATFORM}_build.xml file.
45
46 @since GenBuild 1.0
47 **/
48 public class PlatformBuildFileGenerator {
49
50 ///
51 /// Mapping from modules identification to out put file name
52 ///
53 private Map<FpdModuleIdentification, String> outfiles;
54
55 ///
56 /// Mapping from FV name to its modules
57 ///
58 private Map<String, Set<FpdModuleIdentification>> fvs = new HashMap<String, Set<FpdModuleIdentification>>();
59
60
61 private boolean isUnified = true;
62
63 private SurfaceAreaQuery saq = null;
64
65 private File platformBuildFile = null;
66
67 private Project project;
68
69 private String info = "DO NOT EDIT \n"
70 + "This file is auto-generated by the build utility\n"
71 + "\n"
72 + "Abstract:\n"
73 + "Auto-generated ANT build file for building EFI Modules and Platforms\n";
74
75 public PlatformBuildFileGenerator(Project project, Map<FpdModuleIdentification, String> outfiles, Map<String, Set<FpdModuleIdentification>> fvs, boolean isUnified, SurfaceAreaQuery saq, String platformBuildFile){
76 this.project = project;
77 this.outfiles = outfiles;
78 this.isUnified = isUnified;
79 this.fvs = fvs;
80 this.saq = saq;
81 this.platformBuildFile = new File(platformBuildFile);
82 }
83
84 /**
85 Generate build.out.xml file.
86
87 @throws BuildException
88 build.out.xml XML document create error
89 **/
90 public void genBuildFile() throws BuildException {
91 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
92 try {
93 DocumentBuilder dombuilder = domfac.newDocumentBuilder();
94 Document document = dombuilder.newDocument();
95 Comment rootComment = document.createComment(info);
96 //
97 // create root element and its attributes
98 //
99 Element root = document.createElement("project");
100 root.setAttribute("name", project.getProperty("PLATFORM"));
101 root.setAttribute("default", "all");
102 root.setAttribute("basedir", ".");
103
104 //
105 // element for External ANT tasks
106 //
107 root.appendChild(document.createComment("Apply external ANT tasks"));
108 Element ele = document.createElement("taskdef");
109 ele.setAttribute("resource", "GenBuild.tasks");
110 root.appendChild(ele);
111
112 ele = document.createElement("taskdef");
113 ele.setAttribute("resource", "frameworktasks.tasks");
114 root.appendChild(ele);
115
116 ele = document.createElement("taskdef");
117 ele.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
118 root.appendChild(ele);
119
120 ele = document.createElement("property");
121 ele.setAttribute("environment", "env");
122 root.appendChild(ele);
123
124 //
125 // Default Target
126 //
127 root.appendChild(document.createComment("Default target"));
128 ele = document.createElement("target");
129 ele.setAttribute("name", "all");
130 ele.setAttribute("depends", "prebuild, modules, fvs, postbuild");
131 root.appendChild(ele);
132
133 //
134 // Modules and Fvs Target
135 //
136 applyModules(document, root);
137
138 applyFvs(document, root);
139
140 //
141 // Clean Target
142 //
143 applyClean(document, root);
144
145 //
146 // Deep Clean Target
147 //
148 applyCleanall(document, root);
149
150 //
151 // User Extension pre build
152 //
153 applyUserExtensionsPreBuild(document, root);
154
155 //
156 // User Extension Post build
157 //
158 applyUserExtensionsPostBuild(document, root);
159
160 document.appendChild(rootComment);
161 document.appendChild(root);
162 //
163 // Prepare the DOM document for writing
164 //
165 Source source = new DOMSource(document);
166 //
167 // generate all directory path
168 //
169 (new File(platformBuildFile.getParent())).mkdirs();
170 Result result = new StreamResult(platformBuildFile);
171 //
172 // Write the DOM document to the file
173 //
174 Transformer xformer = TransformerFactory.newInstance().newTransformer();
175 xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
176 xformer.setOutputProperty(OutputKeys.INDENT, "yes");
177 xformer.transform(source, result);
178 } catch (Exception ex) {
179 throw new BuildException("Generating platform build file [" + platformBuildFile.getPath() + "_build.xml] failed. \n" + ex.getMessage());
180 }
181 }
182
183 /**
184 1. Get All valid Fv Image Names in sequence
185 2. For each FV, get modules by sequences
186 3. Get other modules
187
188 @param document XML document
189 @param root Node
190 **/
191 private void applyModules(Document document, Node root) {
192 root.appendChild(document.createComment("Modules target"));
193 Element ele = document.createElement("target");
194 ele.setAttribute("name", "modules");
195
196 //
197 // Get all valid FV name
198 //
199 String[] validFv = saq.getFpdValidImageNames();
200
201 //
202 // For each valid FV, get all modules in sequence
203 //
204 for (int i = 0; i < validFv.length; i++) {
205 if (fvs.containsKey(validFv[i])) {
206 Set<FpdModuleIdentification> set = fvs.get(validFv[i]);
207 Iterator<FpdModuleIdentification> iter = set.iterator();
208 while (iter.hasNext()) {
209 FpdModuleIdentification fpdModuleId = iter.next();
210 applySingleModule(document, ele, fpdModuleId);
211 }
212 }
213 }
214
215 //
216 // Get all other modules
217 //
218 Iterator<String> fvsNameIter = fvs.keySet().iterator();
219
220 while (fvsNameIter.hasNext()) {
221 String fvName = fvsNameIter.next();
222 if (!isContain(validFv, fvName)) {
223 Set<FpdModuleIdentification> set = fvs.get(fvName);
224 Iterator iter = set.iterator();
225 while (iter.hasNext()) {
226 FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
227 applySingleModule(document, ele, fpdModuleId);
228 }
229 }
230 }
231
232 root.appendChild(ele);
233 }
234
235 private void applySingleModule(Document document, Node root, FpdModuleIdentification fpdModuleId) {
236 ModuleIdentification moduleId = fpdModuleId.getModule();
237 Element moduleEle = document.createElement("GenBuild");
238 moduleEle.setAttribute("type", "build");
239 //
240 // Inherit Properties.
241 //{"ARCH", "PACKAGE", "PACKAGE_GUID", "PACKAGE_VERSION", "MODULE_DIR"}
242 //
243
244 //
245 // ARCH
246 //
247 Element property = document.createElement("property");
248 property.setAttribute("name", "ARCH");
249 property.setAttribute("value", fpdModuleId.getArch());
250 moduleEle.appendChild(property);
251
252 //
253 // MODULE_GUID
254 //
255 property = document.createElement("property");
256 property.setAttribute("name", "MODULE_GUID");
257 property.setAttribute("value", moduleId.getGuid());
258 moduleEle.appendChild(property);
259
260 //
261 // MODULE_VERSION
262 //
263 property = document.createElement("property");
264 property.setAttribute("name", "MODULE_VERSION");
265 property.setAttribute("value", moduleId.getVersion());
266 moduleEle.appendChild(property);
267
268 //
269 // PACKAGE_GUID
270 //
271 property = document.createElement("property");
272 property.setAttribute("name", "PACKAGE_GUID");
273 property.setAttribute("value", moduleId.getPackage().getGuid());
274 moduleEle.appendChild(property);
275
276 //
277 // PACKAGE_VERSION
278 //
279 property = document.createElement("property");
280 property.setAttribute("name", "PACKAGE_VERSION");
281 property.setAttribute("value", moduleId.getPackage().getVersion());
282 moduleEle.appendChild(property);
283
284 root.appendChild(moduleEle);
285 }
286
287 private boolean isContain(String[] list, String item) {
288 for (int i = 0; i < list.length; i++) {
289 if (list[i].equalsIgnoreCase(item)) {
290 return true;
291 }
292 }
293 return false;
294 }
295
296 private void applyFvs(Document document, Node root) {
297 //
298 // FVS Target
299 //
300 root.appendChild(document.createComment("FVs target"));
301 Element ele = document.createElement("target");
302 ele.setAttribute("name", "fvs");
303
304 //
305 // For every Target and ToolChain
306 //
307 String[] targetList = GlobalData.getToolChainInfo().getTargets();
308 for (int i = 0; i < targetList.length; i++){
309 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
310 for(int j = 0; j < toolchainList.length; j++){
311 String fvOutputDir = project.getProperty("BUILD_DIR") + File.separatorChar
312 + targetList[i] + "_"
313 + toolchainList[j] + File.separatorChar + "FV";
314 String[] validFv = saq.getFpdValidImageNames();
315 for (int k = 0; k < validFv.length; k++) {
316 String inputFile = fvOutputDir + "" + File.separatorChar + validFv[k].toUpperCase() + ".inf";
317 Element fvEle = document.createElement("genfvimage");
318 fvEle.setAttribute("infFile", inputFile);
319 fvEle.setAttribute("outputDir", fvOutputDir);
320 ele.appendChild(fvEle);
321 }
322 }
323 }
324 root.appendChild(ele);
325 }
326
327 private void applyClean(Document document, Node root) {
328 //
329 // Clean Target
330 //
331 root.appendChild(document.createComment("Clean target"));
332 Element ele = document.createElement("target");
333 ele.setAttribute("name", "clean");
334
335 if (isUnified) {
336 Element cleanEle = document.createElement("delete");
337 cleanEle.setAttribute("includeemptydirs", "true");
338 Element filesetEle = document.createElement("fileset");
339 filesetEle.setAttribute("dir", project.getProperty("BUILD_DIR"));
340 filesetEle.setAttribute("includes", "**\\OUTPUT\\**");
341 cleanEle.appendChild(filesetEle);
342 ele.appendChild(cleanEle);
343 } else {
344 Set set = outfiles.keySet();
345 Iterator iter = set.iterator();
346 while (iter.hasNext()) {
347 FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
348 ModuleIdentification moduleId = fpdModuleId.getModule();
349
350 Element ifEle = document.createElement("if");
351 Element availableEle = document.createElement("available");
352 availableEle.setAttribute("file", moduleId.getMsaFile().getParent() + File.separatorChar
353 + "build.xml");
354 ifEle.appendChild(availableEle);
355 Element elseEle = document.createElement("then");
356
357 Element moduleEle = document.createElement("ant");
358 moduleEle.setAttribute("antfile", moduleId.getMsaFile().getParent() + File.separatorChar
359 + "build.xml");
360 moduleEle.setAttribute("target", "clean");
361 //
362 // Inherit Properties.
363 //{"ARCH", "PACKAGE", "PACKAGE_GUID", "PACKAGE_VERSION", "MODULE_DIR"}
364 //
365
366 //
367 // ARCH
368 //
369 Element property = document.createElement("property");
370 property.setAttribute("name", "ARCH");
371 property.setAttribute("value", fpdModuleId.getArch());
372 moduleEle.appendChild(property);
373
374 //
375 // PACKAGE
376 //
377 property = document.createElement("property");
378 property.setAttribute("name", "PACKAGE");
379 property.setAttribute("value", moduleId.getPackage().getName());
380 moduleEle.appendChild(property);
381
382 //
383 // PACKAGE_GUID
384 //
385 property = document.createElement("property");
386 property.setAttribute("name", "PACKAGE_GUID");
387 property.setAttribute("value", moduleId.getPackage().getGuid());
388 moduleEle.appendChild(property);
389
390 //
391 // PACKAGE_VERSION
392 //
393 property = document.createElement("property");
394 property.setAttribute("name", "PACKAGE_VERSION");
395 property.setAttribute("value", moduleId.getPackage().getVersion());
396 moduleEle.appendChild(property);
397
398 //
399 // MODULE_DIR
400 //
401 property = document.createElement("property");
402 property.setAttribute("name", "MODULE_DIR");
403 property.setAttribute("value", moduleId.getMsaFile().getParent());
404 moduleEle.appendChild(property);
405 elseEle.appendChild(moduleEle);
406 ifEle.appendChild(elseEle);
407 ele.appendChild(ifEle);
408 }
409 }
410 root.appendChild(ele);
411 }
412
413 private void applyCleanall(Document document, Node root) {
414 //
415 // Deep Clean Target
416 //
417 root.appendChild(document.createComment("Target: cleanall"));
418 Element ele = document.createElement("target");
419 ele.setAttribute("name", "cleanall");
420
421 if (isUnified) {
422 String[] targetList = GlobalData.getToolChainInfo().getTargets();
423 for (int i = 0; i < targetList.length; ++i) {
424 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
425 for(int j = 0; j < toolchainList.length; j++) {
426 Element cleanAllEle = document.createElement("delete");
427 cleanAllEle.setAttribute("dir", project.getProperty("BUILD_DIR") + File.separatorChar + targetList[i] + "_" + toolchainList[j]);
428 ele.appendChild(cleanAllEle);
429 }
430 }
431 } else {
432 Set set = outfiles.keySet();
433 Iterator iter = set.iterator();
434 while (iter.hasNext()) {
435 FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
436 ModuleIdentification moduleId = fpdModuleId.getModule();
437
438 Element ifEle = document.createElement("if");
439 Element availableEle = document.createElement("available");
440 availableEle.setAttribute("file", moduleId.getMsaFile().getParent() + File.separatorChar
441 + "build.xml");
442 ifEle.appendChild(availableEle);
443 Element elseEle = document.createElement("then");
444
445 Element moduleEle = document.createElement("ant");
446 moduleEle.setAttribute("antfile", moduleId.getMsaFile().getParent() + File.separatorChar
447 + "build.xml");
448 moduleEle.setAttribute("target", "cleanall");
449 //
450 // Inherit Properties.
451 //{"ARCH", "PACKAGE", "PACKAGE_GUID", "PACKAGE_VERSION", "MODULE_DIR"}
452 //
453
454 //
455 // ARCH
456 //
457 Element property = document.createElement("property");
458 property.setAttribute("name", "ARCH");
459 property.setAttribute("value", fpdModuleId.getArch());
460 moduleEle.appendChild(property);
461
462 //
463 // PACKAGE
464 //
465 property = document.createElement("property");
466 property.setAttribute("name", "PACKAGE");
467 property.setAttribute("value", moduleId.getPackage().getName());
468 moduleEle.appendChild(property);
469
470 //
471 // PACKAGE_GUID
472 //
473 property = document.createElement("property");
474 property.setAttribute("name", "PACKAGE_GUID");
475 property.setAttribute("value", moduleId.getPackage().getGuid());
476 moduleEle.appendChild(property);
477
478 //
479 // PACKAGE_VERSION
480 //
481 property = document.createElement("property");
482 property.setAttribute("name", "PACKAGE_VERSION");
483 property.setAttribute("value", moduleId.getPackage().getVersion());
484 moduleEle.appendChild(property);
485
486 //
487 // MODULE_DIR
488 //
489 property = document.createElement("property");
490 property.setAttribute("name", "MODULE_DIR");
491 property.setAttribute("value", moduleId.getMsaFile().getParent());
492 moduleEle.appendChild(property);
493 elseEle.appendChild(moduleEle);
494 ifEle.appendChild(elseEle);
495 ele.appendChild(ifEle);
496 }
497 }
498 root.appendChild(ele);
499 }
500
501 private void applyUserExtensionsPreBuild(Document document, Node root) {
502 //
503 // User Extensions
504 //
505 root.appendChild(document.createComment("Pre-Build Processing"));
506 Element ele = document.createElement("target");
507 ele.setAttribute("name", "prebuild");
508
509 Node node = saq.getFpdUserExtensionPreBuild();
510 if (node != null) {
511 //
512 // For every Target and ToolChain
513 //
514 String[] targetList = GlobalData.getToolChainInfo().getTargets();
515 for (int i = 0; i < targetList.length; i++){
516 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
517 for(int j = 0; j < toolchainList.length; j++){
518 //
519 // Prepare FV_DIR
520 //
521 String ffsCommonDir = project.getProperty("BUILD_DIR") + File.separatorChar
522 + targetList[i] + "_"
523 + toolchainList[j];
524 File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
525 Element fvEle = document.createElement("var");
526 fvEle.setAttribute("name", "FV_DIR");
527 fvEle.setAttribute("value", fvDir.getPath().replaceAll("(\\\\)", "/"));
528 ele.appendChild(fvEle);
529
530 Element targetDirEle = document.createElement("var");
531 targetDirEle.setAttribute("name", "TARGET_DIR");
532 targetDirEle.setAttribute("value", ffsCommonDir.replaceAll("(\\\\)", "/"));
533 ele.appendChild(targetDirEle);
534
535 NodeList childNodes = node.getChildNodes();
536 for (int k = 0; k < childNodes.getLength(); k++) {
537 Node childItem = childNodes.item(k);
538 if (childItem.getNodeType() == Node.ELEMENT_NODE) {
539 ele.appendChild(recursiveNode(childItem, document));
540 }
541 }
542 }
543 }
544 }
545
546 root.appendChild(ele);
547 }
548
549 private void applyUserExtensionsPostBuild(Document document, Node root) {
550 //
551 // User Extensions
552 //
553 root.appendChild(document.createComment("Post-Build Processing"));
554 Element ele = document.createElement("target");
555 ele.setAttribute("name", "postbuild");
556
557 Node node = saq.getFpdUserExtensionPostBuild();
558 if (node != null) {
559 //
560 // For every Target and ToolChain
561 //
562 String[] targetList = GlobalData.getToolChainInfo().getTargets();
563 for (int i = 0; i < targetList.length; i++){
564 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
565 for(int j = 0; j < toolchainList.length; j++){
566 //
567 // Prepare FV_DIR
568 //
569 String ffsCommonDir = project.getProperty("BUILD_DIR") + File.separatorChar
570 + targetList[i] + "_"
571 + toolchainList[j];
572 File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
573 Element fvEle = document.createElement("var");
574 fvEle.setAttribute("name", "FV_DIR");
575 fvEle.setAttribute("value", fvDir.getPath().replaceAll("(\\\\)", "/"));
576 ele.appendChild(fvEle);
577
578 Element targetDirEle = document.createElement("var");
579 targetDirEle.setAttribute("name", "TARGET_DIR");
580 targetDirEle.setAttribute("value", ffsCommonDir.replaceAll("(\\\\)", "/"));
581 ele.appendChild(targetDirEle);
582
583 NodeList childNodes = node.getChildNodes();
584 for (int k = 0; k < childNodes.getLength(); k++) {
585 Node childItem = childNodes.item(k);
586 if (childItem.getNodeType() == Node.ELEMENT_NODE) {
587 ele.appendChild(recursiveNode(childItem, document));
588 }
589 }
590
591 }
592 }
593 }
594
595 root.appendChild(ele);
596 }
597
598 private Element recursiveNode(Node node, Document document) {
599 Element root = document.createElement(node.getNodeName());
600 NamedNodeMap attr = node.getAttributes();
601 for (int i = 0; i < attr.getLength(); i++) {
602 Node attrItem = attr.item(i);
603 root.setAttribute(attrItem.getNodeName(), attrItem.getNodeValue());
604 }
605 NodeList childNodes = node.getChildNodes();
606 for (int i = 0; i < childNodes.getLength(); i++) {
607 Node childItem = childNodes.item(i);
608 if (childItem.getNodeType() == Node.ELEMENT_NODE) {
609 root.appendChild(recursiveNode(childItem, document));
610 }
611 else if (childItem.getNodeType() == Node.TEXT_NODE){
612 if (!childItem.getNodeValue().trim().equalsIgnoreCase("")) {
613 root.setTextContent(childItem.getNodeValue());
614 }
615 }
616 }
617 return root;
618 }
619 }