Change some format and remove some unused codes.
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / ModuleBuildFileGenerator.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;\r
13\r
14import java.io.File;\r
15import java.util.LinkedHashMap;\r
a29c47e0 16import java.util.Map;\r
2eb7d78d 17import java.io.FileNotFoundException;\r
991d660b 18import java.io.FileOutputStream;\r
19import java.io.OutputStreamWriter;\r
a29c47e0 20\r
21import javax.xml.parsers.DocumentBuilder;\r
22import javax.xml.parsers.DocumentBuilderFactory;\r
2eb7d78d 23import javax.xml.parsers.ParserConfigurationException;\r
a29c47e0 24import javax.xml.transform.OutputKeys;\r
25import javax.xml.transform.Result;\r
26import javax.xml.transform.Source;\r
27import javax.xml.transform.Transformer;\r
2eb7d78d 28import javax.xml.transform.TransformerConfigurationException;\r
29import javax.xml.transform.TransformerException;\r
a29c47e0 30import javax.xml.transform.TransformerFactory;\r
31import javax.xml.transform.dom.DOMSource;\r
32import javax.xml.transform.stream.StreamResult;\r
33\r
34import org.apache.tools.ant.BuildException;\r
35import org.apache.tools.ant.Project;\r
2eb7d78d 36import org.tianocore.build.exception.GenBuildException;\r
a29c47e0 37import org.tianocore.build.fpd.FpdParserTask;\r
a29c47e0 38import org.tianocore.build.global.SurfaceAreaQuery;\r
39import org.tianocore.build.id.FpdModuleIdentification;\r
40import org.tianocore.build.id.ModuleIdentification;\r
41import org.tianocore.build.id.PackageIdentification;\r
892b0e7a 42import org.tianocore.common.exception.EdkException;\r
a29c47e0 43import org.w3c.dom.Comment;\r
44import org.w3c.dom.Document;\r
45import org.w3c.dom.Element;\r
46import org.w3c.dom.Node;\r
47\r
48public class ModuleBuildFileGenerator {\r
49\r
50 ///\r
51 /// Pass: TARGET, TOOLCHAIN, ARCH\r
52 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION\r
53 ///\r
aca6c736 54 String[] inheritProperties = {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PACKAGE_GUID", "PACKAGE_VERSION"};\r
a29c47e0 55\r
56 ///\r
57 /// The information at the header of <em>build.xml</em>.\r
58 ///\r
59 private String info = "DO NOT EDIT \n"\r
391dbbb1 60 + "This file is auto-generated by the build utility\n"\r
a29c47e0 61 + "\n"\r
62 + "Abstract:\n"\r
391dbbb1 63 + "Auto-generated ANT build file for build EFI Modules and Platforms\n";\r
a29c47e0 64\r
65 private FpdModuleIdentification fpdModuleId;\r
66 \r
67 private Project project;\r
68 \r
69 private String ffsKeyword;\r
70 \r
bf3a7173 71 private String[] includes;\r
72 \r
83fba802 73 private SurfaceAreaQuery saq = null;\r
74 \r
75 public ModuleBuildFileGenerator(Project project, String ffsKeyword, FpdModuleIdentification fpdModuleId, String[] includes, SurfaceAreaQuery saq) {\r
a29c47e0 76 this.project = project;\r
77 this.fpdModuleId = fpdModuleId;\r
78 this.ffsKeyword = ffsKeyword;\r
bf3a7173 79 this.includes = includes;\r
83fba802 80 this.saq = saq;\r
a29c47e0 81 }\r
82 \r
83 /**\r
84 The whole BaseName_build.xml is composed of seven part. \r
85 <ul>\r
86 <li> ANT properties; </li>\r
87 <li> Dependent module (dependent library instances in most case); </li>\r
88 <li> Source files; </li>\r
89 <li> Sections if module is not library; </li>\r
90 <li> Output (different for library module and driver module); </li>\r
91 <li> Clean; </li>\r
92 <li> Clean all. </li>\r
93 </ul>\r
94 \r
95 @throws BuildException\r
96 Error throws during BaseName_build.xml generating. \r
97 **/\r
2eb7d78d 98 public void genBuildFile(String buildFilename) throws GenBuildException, EdkException {\r
19bf6b15 99 FfsProcess fp = new FfsProcess();\r
a29c47e0 100 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();\r
101 try {\r
102 DocumentBuilder dombuilder = domfac.newDocumentBuilder();\r
103 Document document = dombuilder.newDocument();\r
104 Comment rootComment = document.createComment(info);\r
105 \r
106 //\r
107 // create root element and its attributes\r
108 //\r
109 Element root = document.createElement("project");\r
110 root.setAttribute("name", fpdModuleId.getModule().getName());\r
111 root.setAttribute("default", "all");\r
112 root.setAttribute("basedir", ".");\r
113 \r
114 //\r
115 // element for External ANT tasks\r
116 //\r
117 root.appendChild(document.createComment("Apply external ANT tasks"));\r
118 Element ele = document.createElement("taskdef");\r
119 ele.setAttribute("resource", "frameworktasks.tasks");\r
120 root.appendChild(ele);\r
121 ele = document.createElement("taskdef");\r
122 ele.setAttribute("resource", "cpptasks.tasks");\r
123 root.appendChild(ele);\r
124 ele = document.createElement("typedef");\r
125 ele.setAttribute("resource", "cpptasks.types");\r
126 root.appendChild(ele);\r
127 ele = document.createElement("taskdef");\r
128 ele.setAttribute("resource", "net/sf/antcontrib/antlib.xml");\r
129 root.appendChild(ele);\r
130\r
131 //\r
132 // Generate the default target,\r
133 // which depends on init, sections and output target\r
134 //\r
135 root.appendChild(document.createComment("Default target"));\r
136 ele = document.createElement("target");\r
137 ele.setAttribute("name", "all");\r
138 ele.setAttribute("depends", "libraries, sourcefiles, sections, output");\r
139 root.appendChild(ele);\r
140 \r
141 //\r
142 // compile all source files\r
143 //\r
144 root.appendChild(document.createComment("Compile all dependency Library instances."));\r
145 ele = document.createElement("target");\r
146 ele.setAttribute("name", "libraries");\r
147\r
148 //\r
149 // Parse all sourfiles but files specified in sections\r
150 //\r
19bf6b15 151 if (!FrameworkBuildTask.multithread) {\r
152 applyLibraryInstance(document, ele);\r
153 }\r
a29c47e0 154 root.appendChild(ele);\r
155\r
156 //\r
157 // compile all source files\r
158 //\r
159 root.appendChild(document.createComment("sourcefiles target"));\r
160 ele = document.createElement("target");\r
161 ele.setAttribute("name", "sourcefiles");\r
162 \r
163 //\r
164 // Parse all sourfiles but files specified in sections\r
165 //\r
166 applyCompileElement(document, ele);\r
167 root.appendChild(ele);\r
168\r
169 //\r
170 // generate the init target\r
171 // main purpose is create all nessary pathes\r
172 // generate the sections target\r
173 //\r
174 root.appendChild(document.createComment("sections target"));\r
175 ele = document.createElement("target");\r
176 ele.setAttribute("name", "sections");\r
177 applySectionsElement(document, ele, fp);\r
178 root.appendChild(ele);\r
179\r
180 //\r
181 // generate the output target\r
182 //\r
183 root.appendChild(document.createComment("output target"));\r
184 ele = document.createElement("target");\r
185 ele.setAttribute("name", "output");\r
186 applyOutputElement(document, ele, fp);\r
187 root.appendChild(ele);\r
188\r
189 \r
190 //\r
191 // generate the clean target\r
192 //\r
193 root.appendChild(document.createComment("clean target"));\r
194 ele = document.createElement("target");\r
195 ele.setAttribute("name", "clean");\r
196 applyCleanElement(document, ele);\r
197 root.appendChild(ele);\r
198 \r
199 //\r
200 // generate the Clean All target\r
201 //\r
202 root.appendChild(document.createComment("Clean All target"));\r
203 ele = document.createElement("target");\r
204 ele.setAttribute("name", "cleanall");\r
205 applyDeepcleanElement(document, ele);\r
206 root.appendChild(ele);\r
207 \r
208 //\r
209 // add the root element to the document\r
210 //\r
211 document.appendChild(rootComment);\r
212 document.appendChild(root);\r
213 //\r
214 // Prepare the DOM document for writing\r
215 //\r
216 Source source = new DOMSource(document);\r
217\r
218 //\r
219 // Prepare the output file\r
220 //\r
221 File file = new File(buildFilename);\r
222\r
223 //\r
224 // generate all directory path\r
225 //\r
226 (new File(file.getParent())).mkdirs();\r
991d660b 227 FileOutputStream outputStream = new FileOutputStream(file);\r
228 Result result = new StreamResult(new OutputStreamWriter(outputStream));\r
a29c47e0 229 \r
230 //\r
231 // Write the DOM document to the file\r
232 //\r
233 Transformer xformer = TransformerFactory.newInstance().newTransformer();\r
234 xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");\r
235 xformer.setOutputProperty(OutputKeys.INDENT, "yes");\r
236 xformer.transform(source, result);\r
2eb7d78d 237 } catch (ParserConfigurationException ex) {\r
238 GenBuildException e = new GenBuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());\r
239 e.setStackTrace(ex.getStackTrace());\r
240 throw e;\r
241 } catch (FileNotFoundException ex) {\r
242 GenBuildException e = new GenBuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());\r
243 e.setStackTrace(ex.getStackTrace());\r
244 throw e;\r
245 } catch (TransformerConfigurationException ex) {\r
246 GenBuildException e = new GenBuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());\r
247 e.setStackTrace(ex.getStackTrace());\r
248 throw e;\r
249 } catch (TransformerException ex) {\r
250 GenBuildException e = new GenBuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());\r
251 e.setStackTrace(ex.getStackTrace());\r
252 throw e;\r
a29c47e0 253 }\r
254 }\r
255\r
256 /**\r
257 Generate the clean elements for BaseName_build.xml. \r
258 \r
259 @param document current BaseName_build.xml XML document\r
260 @param root Root element for current\r
261 **/\r
262 private void applyCleanElement(Document document, Node root) {\r
a29c47e0 263 //\r
264 // <delete includeemptydirs="true">\r
265 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />\r
266 // </delete>\r
267 //\r
268 Element deleteEle = document.createElement("delete");\r
269 deleteEle.setAttribute("includeemptydirs", "true");\r
270 Element filesetEle = document.createElement("fileset");\r
271 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");\r
272 filesetEle.setAttribute("includes", "**/*");\r
273 filesetEle.setAttribute("excludes", "*.xml");\r
274 deleteEle.appendChild(filesetEle);\r
275 root.appendChild(deleteEle);\r
276 }\r
277\r
278 /**\r
279 Generate the cleanall elements for BaseName_build.xml. \r
280 \r
281 @param document current BaseName_build.xml XML document\r
282 @param root Root element for current\r
283 **/\r
284 private void applyDeepcleanElement(Document document, Node root) {\r
a29c47e0 285 //\r
286 // <delete includeemptydirs="true">\r
287 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />\r
288 // </delete>\r
289 //\r
290 Element deleteEle = document.createElement("delete");\r
291 deleteEle.setAttribute("includeemptydirs", "true");\r
292 Element filesetEle = document.createElement("fileset");\r
293 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");\r
294 filesetEle.setAttribute("includes", "**/*");\r
295 filesetEle.setAttribute("excludes", "*.xml");\r
296 deleteEle.appendChild(filesetEle);\r
297 root.appendChild(deleteEle);\r
298 \r
299 //\r
300 // <delete includeemptydirs="true">\r
301 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />\r
302 // </delete>\r
303 //\r
304 deleteEle = document.createElement("delete");\r
305 deleteEle.setAttribute("includeemptydirs", "true");\r
306 filesetEle = document.createElement("fileset");\r
307 filesetEle.setAttribute("dir", "${DEST_DIR_DEBUG}");\r
308 filesetEle.setAttribute("includes", "**/*");\r
309 deleteEle.appendChild(filesetEle);\r
310 root.appendChild(deleteEle);\r
311 }\r
312\r
313 /**\r
314 Generate the dependent library instances elements for BaseName_build.xml. \r
315 \r
316 @param document current BaseName_build.xml XML document\r
317 @param root Root element for current\r
318 **/\r
892b0e7a 319 private void applyLibraryInstance(Document document, Node root) throws EdkException {\r
83fba802 320 ModuleIdentification[] libinstances = saq.getLibraryInstance(fpdModuleId.getArch());\r
a29c47e0 321 for (int i = 0; i < libinstances.length; i++) {\r
322 //\r
323 // Put package file path to module identification\r
324 //\r
325 PackageIdentification packageId = libinstances[i].getPackage();\r
326 \r
327 //\r
328 // Generate ANT script to build library instances\r
329 //\r
330 Element ele = document.createElement("GenBuild");\r
331 ele.setAttribute("type", "build");\r
a29c47e0 332 \r
333 //\r
334 // Prepare pass down information\r
335 //\r
336 Map<String, String> passDownMap = new LinkedHashMap<String, String>();\r
337 for (int j = 0; j < inheritProperties.length; j ++){\r
338 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");\r
339 }\r
340 \r
341 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());\r
342 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());\r
343 \r
344 passDownMap.put("PACKAGE_GUID", packageId.getGuid());\r
345 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());\r
346 \r
347 for (int j = 0; j < inheritProperties.length; j ++){\r
348 Element property = document.createElement("property");\r
349 property.setAttribute("name", inheritProperties[j]);\r
350 property.setAttribute("value", passDownMap.get(inheritProperties[j]));\r
351 ele.appendChild(property);\r
352 }\r
353 \r
354 root.appendChild(ele);\r
a29c47e0 355 }\r
a29c47e0 356 }\r
a33f3dd1 357\r
a29c47e0 358 /**\r
359 Generate the build source files elements for BaseName_build.xml. \r
360 \r
361 @param document current BaseName_build.xml XML document\r
362 @param root Root element for current\r
363 **/\r
364 private void applyCompileElement(Document document, Node root) {\r
a29c47e0 365 //\r
366 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir\r
367 //\r
83fba802 368 String[][] sourceFiles = saq.getSourceFiles(fpdModuleId.getArch());\r
a29c47e0 369\r
370 FileProcess fileProcess = new FileProcess();\r
371 fileProcess.init(project, includes, document);\r
88f858a1 372 \r
373 //\r
374 // Initialize some properties by user\r
375 //\r
376 Element initEle = document.createElement("Build_Init");\r
1549f516 377 Element initIncEle = document.createElement("EXTRA.INC");\r
378 for (int i = 0; i < includes.length; i++) {\r
379 Element includeEle = document.createElement("includepath");\r
380 includeEle.setAttribute("path", includes[i]);\r
381 initIncEle.appendChild(includeEle);\r
382 }\r
383 initEle.appendChild(initIncEle);\r
88f858a1 384 root.appendChild(initEle);\r
a29c47e0 385\r
386 String moduleDir = project.getProperty("MODULE_DIR");\r
387 //\r
388 // Parse all Unicode files\r
389 //\r
390 for (int i = 0; i < sourceFiles.length; i++) {\r
391 //\r
392 // Go through all source files. Add MODULE_DIR to preffix\r
393 //\r
394 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);\r
395 sourceFiles[i][1] = sourceFile.getPath();\r
396 String filetype = sourceFiles[i][0];\r
397 if (filetype != null) {\r
398 fileProcess.parseFile(sourceFiles[i][1], filetype, root, true);\r
399 } else {\r
400 fileProcess.parseFile(sourceFiles[i][1], root, true);\r
401 }\r
402 }\r
403 \r
404 //\r
405 // If exist Unicode file\r
406 //\r
407 if (fileProcess.isUnicodeExist()) {\r
408 Element ele = document.createElement("Build_Unicode_Database");\r
409 ele.setAttribute("FILEPATH", ".");\r
410 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
a29c47e0 411 Element includesEle = document.createElement("EXTRA.INC");\r
bf3a7173 412 for (int i = 0; i < includes.length; i++) {\r
a29c47e0 413 Element includeEle = document.createElement("includepath");\r
bf3a7173 414 includeEle.setAttribute("path", includes[i]);\r
a29c47e0 415 includesEle.appendChild(includeEle);\r
416 }\r
417 ele.appendChild(includesEle);\r
418 root.appendChild(ele);\r
419 }\r
420\r
421 //\r
422 // Parse AutoGen.c & AutoGen.h\r
423 //\r
424 if ( ! fpdModuleId.getModule().getName().equalsIgnoreCase("Shell")) {\r
425 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", root, false);\r
426 }\r
427 \r
428 //\r
429 // Parse all source files but Unicode files\r
430 //\r
431 for (int i = 0; i < sourceFiles.length; i++) {\r
432 String filetype = sourceFiles[i][0];\r
433 if (filetype != null) {\r
434 fileProcess.parseFile(sourceFiles[i][1], filetype, root, false);\r
435 } else {\r
436 fileProcess.parseFile(sourceFiles[i][1], root, false);\r
437 }\r
438 }\r
439 \r
440 //\r
441 // Initialize SOURCE_FILES for dependcy check use\r
442 //\r
443 String str = "";\r
444 for (int i = 0; i < sourceFiles.length; i++) {\r
445 str += " " + sourceFiles[i][1];\r
446 }\r
aca6c736 447 project.setProperty("SOURCE_FILES", str.replaceAll("(\\\\)", "/"));\r
a29c47e0 448 }\r
449\r
450 /**\r
451 Generate the section elements for BaseName_build.xml. Library module will\r
452 skip this process. \r
453 \r
454 @param document current BaseName_build.xml XML document\r
455 @param root Root element for current\r
456 **/\r
457 private void applySectionsElement(Document document, Node root, FfsProcess fp) {\r
458 if (fpdModuleId.getModule().isLibrary()) {\r
459 return ;\r
460 }\r
461 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {\r
196ad8d7 462 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());\r
a29c47e0 463 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);\r
464\r
465 for (int i = 0; i < list.length; i++) {\r
466 Element ele = document.createElement(list[i]);\r
467 ele.setAttribute("FILEPATH", ".");\r
468 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
469 root.appendChild(ele);\r
470 }\r
471 }\r
472 }\r
473\r
474 /**\r
475 Generate the output elements for BaseName_build.xml. If module is library,\r
476 call the <em>LIB</em> command, else call the <em>GenFfs</em> command. \r
477 \r
478 @param document current BaseName_build.xml XML document\r
479 @param root Root element for current\r
480 **/\r
481 private void applyOutputElement(Document document, Node root, FfsProcess fp) {\r
482 if (fpdModuleId.getModule().isLibrary()) {\r
483 //\r
484 // call Lib command\r
485 //\r
486 Element cc = document.createElement("Build_Library");\r
487 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());\r
488 root.appendChild(cc);\r
489 }\r
490 //\r
491 // if it is a module but library\r
492 //\r
493 else {\r
494 if (fp.getFfsNode() != null) {\r
495 root.appendChild(fp.getFfsNode());\r
496 }\r
497 }\r
498 }\r
499\r
500}\r