- Fixed PVCS tracker 484 by merging the GenDll and GenEfi macro in BuildMacro.xml...
[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
91a1f0d7 366 // sourceFiles[][0] is FileType, \r
367 // [][1] is File name relative to Module_Dir, \r
368 // [][2] is ToolChainFamily\r
a29c47e0 369 //\r
83fba802 370 String[][] sourceFiles = saq.getSourceFiles(fpdModuleId.getArch());\r
a29c47e0 371\r
372 FileProcess fileProcess = new FileProcess();\r
373 fileProcess.init(project, includes, document);\r
a30ae9fa 374\r
375 boolean isBinaryModule = fpdModuleId.getModule().isBinary();\r
376 boolean isLibraryModule = fpdModuleId.getModule().isLibrary();\r
377\r
378 if (!isBinaryModule) {\r
379 //\r
380 // Initialize some properties by user\r
381 //\r
382 Element initEle = document.createElement("Build_Init");\r
383 Element initIncEle = document.createElement("EXTRA.INC");\r
384 for (int i = 0; i < includes.length; i++) {\r
385 Element includeEle = document.createElement("includepath");\r
386 includeEle.setAttribute("path", includes[i]);\r
387 initIncEle.appendChild(includeEle);\r
388 }\r
389 initEle.appendChild(initIncEle);\r
390 root.appendChild(initEle);\r
1549f516 391 }\r
a29c47e0 392\r
393 String moduleDir = project.getProperty("MODULE_DIR");\r
394 //\r
395 // Parse all Unicode files\r
396 //\r
397 for (int i = 0; i < sourceFiles.length; i++) {\r
398 //\r
399 // Go through all source files. Add MODULE_DIR to preffix\r
400 //\r
401 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);\r
402 sourceFiles[i][1] = sourceFile.getPath();\r
403 String filetype = sourceFiles[i][0];\r
404 if (filetype != null) {\r
91a1f0d7 405 fileProcess.parseFile(sourceFiles[i][1], filetype, sourceFiles[i][2], root, true);\r
a29c47e0 406 } else {\r
91a1f0d7 407 fileProcess.parseFile(sourceFiles[i][1], sourceFiles[i][2], root, true);\r
a29c47e0 408 }\r
409 }\r
410 \r
411 //\r
412 // If exist Unicode file\r
413 //\r
414 if (fileProcess.isUnicodeExist()) {\r
415 Element ele = document.createElement("Build_Unicode_Database");\r
416 ele.setAttribute("FILEPATH", ".");\r
417 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
a29c47e0 418 Element includesEle = document.createElement("EXTRA.INC");\r
bf3a7173 419 for (int i = 0; i < includes.length; i++) {\r
a29c47e0 420 Element includeEle = document.createElement("includepath");\r
bf3a7173 421 includeEle.setAttribute("path", includes[i]);\r
a29c47e0 422 includesEle.appendChild(includeEle);\r
423 }\r
424 ele.appendChild(includesEle);\r
425 root.appendChild(ele);\r
426 }\r
427\r
428 //\r
a30ae9fa 429 // Don't build AutoGen.c for library and binary module\r
a29c47e0 430 //\r
a30ae9fa 431 if (!isLibraryModule && !isBinaryModule) {\r
91a1f0d7 432 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", null, root, false);\r
a29c47e0 433 }\r
434 \r
435 //\r
436 // Parse all source files but Unicode files\r
437 //\r
438 for (int i = 0; i < sourceFiles.length; i++) {\r
439 String filetype = sourceFiles[i][0];\r
440 if (filetype != null) {\r
91a1f0d7 441 fileProcess.parseFile(sourceFiles[i][1], filetype, sourceFiles[i][2], root, false);\r
a29c47e0 442 } else {\r
91a1f0d7 443 fileProcess.parseFile(sourceFiles[i][1], sourceFiles[i][2], root, false);\r
a29c47e0 444 }\r
445 }\r
446 \r
447 //\r
448 // Initialize SOURCE_FILES for dependcy check use\r
449 //\r
450 String str = "";\r
451 for (int i = 0; i < sourceFiles.length; i++) {\r
452 str += " " + sourceFiles[i][1];\r
453 }\r
aca6c736 454 project.setProperty("SOURCE_FILES", str.replaceAll("(\\\\)", "/"));\r
a29c47e0 455 }\r
456\r
457 /**\r
458 Generate the section elements for BaseName_build.xml. Library module will\r
459 skip this process. \r
460 \r
461 @param document current BaseName_build.xml XML document\r
462 @param root Root element for current\r
463 **/\r
464 private void applySectionsElement(Document document, Node root, FfsProcess fp) {\r
465 if (fpdModuleId.getModule().isLibrary()) {\r
466 return ;\r
467 }\r
468 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {\r
196ad8d7 469 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());\r
a29c47e0 470 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);\r
471\r
472 for (int i = 0; i < list.length; i++) {\r
706c2ad4 473 String sectiontype = list[i];\r
474 if (sectiontype.equalsIgnoreCase("EFI_SECTION_RAW") && project.getProperty("MODULE_TYPE").equalsIgnoreCase("SEC")) {\r
475 sectiontype += "_SEC";\r
476 }\r
a30ae9fa 477 if ((sectiontype.equalsIgnoreCase("EFI_SECTION_PE32") || \r
478 sectiontype.equalsIgnoreCase("EFI_SECTION_TE"))\r
479 && !fpdModuleId.getModule().isLibrary()\r
480 && !fpdModuleId.getModule().isBinary()) {\r
481 //\r
482 // Generate code to generate efi file\r
483 // \r
484 Element ele = document.createElement("GenEfi");\r
485 ele.setAttribute("FILEPATH", ".");\r
486 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
487 root.appendChild(ele);\r
488 }\r
706c2ad4 489 Element ele = document.createElement(sectiontype);\r
a29c47e0 490 ele.setAttribute("FILEPATH", ".");\r
491 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
492 root.appendChild(ele);\r
493 }\r
494 }\r
495 }\r
496\r
497 /**\r
498 Generate the output elements for BaseName_build.xml. If module is library,\r
499 call the <em>LIB</em> command, else call the <em>GenFfs</em> command. \r
500 \r
501 @param document current BaseName_build.xml XML document\r
502 @param root Root element for current\r
503 **/\r
504 private void applyOutputElement(Document document, Node root, FfsProcess fp) {\r
505 if (fpdModuleId.getModule().isLibrary()) {\r
506 //\r
507 // call Lib command\r
508 //\r
509 Element cc = document.createElement("Build_Library");\r
510 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());\r
511 root.appendChild(cc);\r
512 }\r
513 //\r
514 // if it is a module but library\r
515 //\r
516 else {\r
517 if (fp.getFfsNode() != null) {\r
518 root.appendChild(fp.getFfsNode());\r
519 }\r
520 }\r
521 }\r
522\r
523}\r