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
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.
12 package org
.tianocore
.build
;
15 import java
.util
.LinkedHashMap
;
17 import java
.io
.FileOutputStream
;
18 import java
.io
.OutputStreamWriter
;
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
;
30 import org
.apache
.tools
.ant
.BuildException
;
31 import org
.apache
.tools
.ant
.Project
;
32 import org
.tianocore
.build
.fpd
.FpdParserTask
;
33 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
34 import org
.tianocore
.build
.global
.PropertyManager
;
35 import org
.tianocore
.build
.id
.FpdModuleIdentification
;
36 import org
.tianocore
.build
.id
.ModuleIdentification
;
37 import org
.tianocore
.build
.id
.PackageIdentification
;
38 import org
.w3c
.dom
.Comment
;
39 import org
.w3c
.dom
.Document
;
40 import org
.w3c
.dom
.Element
;
41 import org
.w3c
.dom
.Node
;
43 public class ModuleBuildFileGenerator
{
46 /// Pass: TARGET, TOOLCHAIN, ARCH
47 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION
49 String
[] inheritProperties
= {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PLATFORM_FILE", "PACKAGE_GUID", "PACKAGE_VERSION"};
52 /// The information at the header of <em>build.xml</em>.
54 private String info
= "DO NOT EDIT \n"
55 + "This file is auto-generated by the build utility\n"
58 + "Auto-generated ANT build file for build EFI Modules and Platforms\n";
60 private FpdModuleIdentification fpdModuleId
;
62 private Project project
;
64 private String ffsKeyword
;
66 private String
[] includes
;
68 private SurfaceAreaQuery saq
= null;
70 public ModuleBuildFileGenerator(Project project
, String ffsKeyword
, FpdModuleIdentification fpdModuleId
, String
[] includes
, SurfaceAreaQuery saq
) {
71 this.project
= project
;
72 this.fpdModuleId
= fpdModuleId
;
73 this.ffsKeyword
= ffsKeyword
;
74 this.includes
= includes
;
79 The whole BaseName_build.xml is composed of seven part.
81 <li> ANT properties; </li>
82 <li> Dependent module (dependent library instances in most case); </li>
83 <li> Source files; </li>
84 <li> Sections if module is not library; </li>
85 <li> Output (different for library module and driver module); </li>
90 @throws BuildException
91 Error throws during BaseName_build.xml generating.
93 public void genBuildFile(String buildFilename
) throws BuildException
{
94 FfsProcess fp
= new FfsProcess();
95 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
97 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
98 Document document
= dombuilder
.newDocument();
99 Comment rootComment
= document
.createComment(info
);
102 // create root element and its attributes
104 Element root
= document
.createElement("project");
105 root
.setAttribute("name", fpdModuleId
.getModule().getName());
106 root
.setAttribute("default", "all");
107 root
.setAttribute("basedir", ".");
110 // element for External ANT tasks
112 root
.appendChild(document
.createComment("Apply external ANT tasks"));
113 Element ele
= document
.createElement("taskdef");
114 ele
.setAttribute("resource", "frameworktasks.tasks");
115 root
.appendChild(ele
);
116 ele
= document
.createElement("taskdef");
117 ele
.setAttribute("resource", "cpptasks.tasks");
118 root
.appendChild(ele
);
119 ele
= document
.createElement("typedef");
120 ele
.setAttribute("resource", "cpptasks.types");
121 root
.appendChild(ele
);
122 ele
= document
.createElement("taskdef");
123 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
124 root
.appendChild(ele
);
127 // Generate the default target,
128 // which depends on init, sections and output target
130 root
.appendChild(document
.createComment("Default target"));
131 ele
= document
.createElement("target");
132 ele
.setAttribute("name", "all");
133 ele
.setAttribute("depends", "libraries, sourcefiles, sections, output");
134 root
.appendChild(ele
);
137 // compile all source files
139 root
.appendChild(document
.createComment("Compile all dependency Library instances."));
140 ele
= document
.createElement("target");
141 ele
.setAttribute("name", "libraries");
144 // Parse all sourfiles but files specified in sections
146 if (!FrameworkBuildTask
.multithread
) {
147 applyLibraryInstance(document
, ele
);
149 root
.appendChild(ele
);
152 // compile all source files
154 root
.appendChild(document
.createComment("sourcefiles target"));
155 ele
= document
.createElement("target");
156 ele
.setAttribute("name", "sourcefiles");
159 // Parse all sourfiles but files specified in sections
161 applyCompileElement(document
, ele
);
162 root
.appendChild(ele
);
165 // generate the init target
166 // main purpose is create all nessary pathes
167 // generate the sections target
169 root
.appendChild(document
.createComment("sections target"));
170 ele
= document
.createElement("target");
171 ele
.setAttribute("name", "sections");
172 applySectionsElement(document
, ele
, fp
);
173 root
.appendChild(ele
);
176 // generate the output target
178 root
.appendChild(document
.createComment("output target"));
179 ele
= document
.createElement("target");
180 ele
.setAttribute("name", "output");
181 applyOutputElement(document
, ele
, fp
);
182 root
.appendChild(ele
);
186 // generate the clean target
188 root
.appendChild(document
.createComment("clean target"));
189 ele
= document
.createElement("target");
190 ele
.setAttribute("name", "clean");
191 applyCleanElement(document
, ele
);
192 root
.appendChild(ele
);
195 // generate the Clean All target
197 root
.appendChild(document
.createComment("Clean All target"));
198 ele
= document
.createElement("target");
199 ele
.setAttribute("name", "cleanall");
200 applyDeepcleanElement(document
, ele
);
201 root
.appendChild(ele
);
204 // add the root element to the document
206 document
.appendChild(rootComment
);
207 document
.appendChild(root
);
209 // Prepare the DOM document for writing
211 Source source
= new DOMSource(document
);
214 // Prepare the output file
216 File file
= new File(buildFilename
);
219 // generate all directory path
221 (new File(file
.getParent())).mkdirs();
222 FileOutputStream outputStream
= new FileOutputStream(file
);
223 Result result
= new StreamResult(new OutputStreamWriter(outputStream
));
226 // Write the DOM document to the file
228 Transformer xformer
= TransformerFactory
.newInstance().newTransformer();
229 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
230 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
231 xformer
.transform(source
, result
);
232 } catch (Exception ex
) {
233 throw new BuildException("Generating the module [" + fpdModuleId
.getModule().getName() + "] build.xml file failed!.\n" + ex
.getMessage());
238 Generate the clean elements for BaseName_build.xml.
240 @param document current BaseName_build.xml XML document
241 @param root Root element for current
243 private void applyCleanElement(Document document
, Node root
) {
245 // <delete includeemptydirs="true">
246 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
249 Element deleteEle
= document
.createElement("delete");
250 deleteEle
.setAttribute("includeemptydirs", "true");
251 Element filesetEle
= document
.createElement("fileset");
252 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
253 filesetEle
.setAttribute("includes", "**/*");
254 filesetEle
.setAttribute("excludes", "*.xml");
255 deleteEle
.appendChild(filesetEle
);
256 root
.appendChild(deleteEle
);
260 Generate the cleanall elements for BaseName_build.xml.
262 @param document current BaseName_build.xml XML document
263 @param root Root element for current
265 private void applyDeepcleanElement(Document document
, Node root
) {
267 // <delete includeemptydirs="true">
268 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
271 Element deleteEle
= document
.createElement("delete");
272 deleteEle
.setAttribute("includeemptydirs", "true");
273 Element filesetEle
= document
.createElement("fileset");
274 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
275 filesetEle
.setAttribute("includes", "**/*");
276 filesetEle
.setAttribute("excludes", "*.xml");
277 deleteEle
.appendChild(filesetEle
);
278 root
.appendChild(deleteEle
);
281 // <delete includeemptydirs="true">
282 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
285 deleteEle
= document
.createElement("delete");
286 deleteEle
.setAttribute("includeemptydirs", "true");
287 filesetEle
= document
.createElement("fileset");
288 filesetEle
.setAttribute("dir", "${DEST_DIR_DEBUG}");
289 filesetEle
.setAttribute("includes", "**/*");
290 deleteEle
.appendChild(filesetEle
);
291 root
.appendChild(deleteEle
);
295 Generate the dependent library instances elements for BaseName_build.xml.
297 @param document current BaseName_build.xml XML document
298 @param root Root element for current
300 private void applyLibraryInstance(Document document
, Node root
) {
301 ModuleIdentification
[] libinstances
= saq
.getLibraryInstance(fpdModuleId
.getArch());
302 for (int i
= 0; i
< libinstances
.length
; i
++) {
304 // Put package file path to module identification
306 PackageIdentification packageId
= libinstances
[i
].getPackage();
309 // Generate ANT script to build library instances
311 Element ele
= document
.createElement("GenBuild");
312 ele
.setAttribute("type", "build");
315 // Prepare pass down information
317 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
318 for (int j
= 0; j
< inheritProperties
.length
; j
++){
319 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
322 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
323 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
325 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
326 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
328 for (int j
= 0; j
< inheritProperties
.length
; j
++){
329 Element property
= document
.createElement("property");
330 property
.setAttribute("name", inheritProperties
[j
]);
331 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
332 ele
.appendChild(property
);
335 root
.appendChild(ele
);
340 Generate the build source files elements for BaseName_build.xml.
342 @param document current BaseName_build.xml XML document
343 @param root Root element for current
345 private void applyCompileElement(Document document
, Node root
) {
347 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
349 String
[][] sourceFiles
= saq
.getSourceFiles(fpdModuleId
.getArch());
351 FileProcess fileProcess
= new FileProcess();
352 fileProcess
.init(project
, includes
, document
);
355 // Initialize some properties by user
357 Element initEle
= document
.createElement("Build_Init");
358 root
.appendChild(initEle
);
360 String moduleDir
= project
.getProperty("MODULE_DIR");
362 // Parse all Unicode files
364 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
366 // Go through all source files. Add MODULE_DIR to preffix
368 File sourceFile
= new File(moduleDir
+ File
.separatorChar
+ sourceFiles
[i
][1]);
369 sourceFiles
[i
][1] = sourceFile
.getPath();
370 String filetype
= sourceFiles
[i
][0];
371 if (filetype
!= null) {
372 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, true);
374 fileProcess
.parseFile(sourceFiles
[i
][1], root
, true);
379 // If exist Unicode file
381 if (fileProcess
.isUnicodeExist()) {
382 Element ele
= document
.createElement("Build_Unicode_Database");
383 ele
.setAttribute("FILEPATH", ".");
384 ele
.setAttribute("FILENAME", "${BASE_NAME}");
385 Element includesEle
= document
.createElement("EXTRA.INC");
386 for (int i
= 0; i
< includes
.length
; i
++) {
387 Element includeEle
= document
.createElement("includepath");
388 includeEle
.setAttribute("path", includes
[i
]);
389 includesEle
.appendChild(includeEle
);
391 ele
.appendChild(includesEle
);
392 root
.appendChild(ele
);
396 // Parse AutoGen.c & AutoGen.h
398 if ( ! fpdModuleId
.getModule().getName().equalsIgnoreCase("Shell")) {
399 fileProcess
.parseFile(project
.getProperty("DEST_DIR_DEBUG") + File
.separatorChar
+ "AutoGen.c", root
, false);
403 // Parse all source files but Unicode files
405 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
406 String filetype
= sourceFiles
[i
][0];
407 if (filetype
!= null) {
408 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, false);
410 fileProcess
.parseFile(sourceFiles
[i
][1], root
, false);
415 // Initialize SOURCE_FILES for dependcy check use
418 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
419 str
+= " " + sourceFiles
[i
][1];
421 PropertyManager
.setProperty(project
, "SOURCE_FILES", str
.replaceAll("(\\\\)", "/"));
425 Generate the section elements for BaseName_build.xml. Library module will
428 @param document current BaseName_build.xml XML document
429 @param root Root element for current
431 private void applySectionsElement(Document document
, Node root
, FfsProcess fp
) {
432 if (fpdModuleId
.getModule().isLibrary()) {
435 if (fp
.initSections(ffsKeyword
, project
, fpdModuleId
)) {
436 String targetFilename
= fpdModuleId
.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask
.getSuffix(fpdModuleId
.getModule().getModuleType());
437 String
[] list
= fp
.getGenSectionElements(document
, "${BASE_NAME}", fpdModuleId
.getModule().getGuid(), targetFilename
);
439 for (int i
= 0; i
< list
.length
; i
++) {
440 Element ele
= document
.createElement(list
[i
]);
441 ele
.setAttribute("FILEPATH", ".");
442 ele
.setAttribute("FILENAME", "${BASE_NAME}");
443 root
.appendChild(ele
);
449 Generate the output elements for BaseName_build.xml. If module is library,
450 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
452 @param document current BaseName_build.xml XML document
453 @param root Root element for current
455 private void applyOutputElement(Document document
, Node root
, FfsProcess fp
) {
456 if (fpdModuleId
.getModule().isLibrary()) {
460 Element cc
= document
.createElement("Build_Library");
461 cc
.setAttribute("FILENAME", fpdModuleId
.getModule().getName());
462 root
.appendChild(cc
);
465 // if it is a module but library
468 if (fp
.getFfsNode() != null) {
469 root
.appendChild(fp
.getFfsNode());