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