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