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
.id
.FpdModuleIdentification
;
35 import org
.tianocore
.build
.id
.ModuleIdentification
;
36 import org
.tianocore
.build
.id
.PackageIdentification
;
37 import org
.w3c
.dom
.Comment
;
38 import org
.w3c
.dom
.Document
;
39 import org
.w3c
.dom
.Element
;
40 import org
.w3c
.dom
.Node
;
42 public class ModuleBuildFileGenerator
{
45 /// Pass: TARGET, TOOLCHAIN, ARCH
46 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION
48 String
[] inheritProperties
= {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PACKAGE_GUID", "PACKAGE_VERSION"};
51 /// The information at the header of <em>build.xml</em>.
53 private String info
= "DO NOT EDIT \n"
54 + "This file is auto-generated by the build utility\n"
57 + "Auto-generated ANT build file for build EFI Modules and Platforms\n";
59 private FpdModuleIdentification fpdModuleId
;
61 private Project project
;
63 private String ffsKeyword
;
65 private String
[] includes
;
67 private SurfaceAreaQuery saq
= null;
69 public ModuleBuildFileGenerator(Project project
, String ffsKeyword
, FpdModuleIdentification fpdModuleId
, String
[] includes
, SurfaceAreaQuery saq
) {
70 this.project
= project
;
71 this.fpdModuleId
= fpdModuleId
;
72 this.ffsKeyword
= ffsKeyword
;
73 this.includes
= includes
;
78 The whole BaseName_build.xml is composed of seven part.
80 <li> ANT properties; </li>
81 <li> Dependent module (dependent library instances in most case); </li>
82 <li> Source files; </li>
83 <li> Sections if module is not library; </li>
84 <li> Output (different for library module and driver module); </li>
89 @throws BuildException
90 Error throws during BaseName_build.xml generating.
92 public void genBuildFile(String buildFilename
) throws BuildException
{
93 FfsProcess fp
= new FfsProcess();
94 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
96 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
97 Document document
= dombuilder
.newDocument();
98 Comment rootComment
= document
.createComment(info
);
101 // create root element and its attributes
103 Element root
= document
.createElement("project");
104 root
.setAttribute("name", fpdModuleId
.getModule().getName());
105 root
.setAttribute("default", "all");
106 root
.setAttribute("basedir", ".");
109 // element for External ANT tasks
111 root
.appendChild(document
.createComment("Apply external ANT tasks"));
112 Element ele
= document
.createElement("taskdef");
113 ele
.setAttribute("resource", "frameworktasks.tasks");
114 root
.appendChild(ele
);
115 ele
= document
.createElement("taskdef");
116 ele
.setAttribute("resource", "cpptasks.tasks");
117 root
.appendChild(ele
);
118 ele
= document
.createElement("typedef");
119 ele
.setAttribute("resource", "cpptasks.types");
120 root
.appendChild(ele
);
121 ele
= document
.createElement("taskdef");
122 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
123 root
.appendChild(ele
);
126 // Generate the default target,
127 // which depends on init, sections and output target
129 root
.appendChild(document
.createComment("Default target"));
130 ele
= document
.createElement("target");
131 ele
.setAttribute("name", "all");
132 ele
.setAttribute("depends", "libraries, sourcefiles, sections, output");
133 root
.appendChild(ele
);
136 // compile all source files
138 root
.appendChild(document
.createComment("Compile all dependency Library instances."));
139 ele
= document
.createElement("target");
140 ele
.setAttribute("name", "libraries");
143 // Parse all sourfiles but files specified in sections
145 if (!FrameworkBuildTask
.multithread
) {
146 applyLibraryInstance(document
, ele
);
148 root
.appendChild(ele
);
151 // compile all source files
153 root
.appendChild(document
.createComment("sourcefiles target"));
154 ele
= document
.createElement("target");
155 ele
.setAttribute("name", "sourcefiles");
158 // Parse all sourfiles but files specified in sections
160 applyCompileElement(document
, ele
);
161 root
.appendChild(ele
);
164 // generate the init target
165 // main purpose is create all nessary pathes
166 // generate the sections target
168 root
.appendChild(document
.createComment("sections target"));
169 ele
= document
.createElement("target");
170 ele
.setAttribute("name", "sections");
171 applySectionsElement(document
, ele
, fp
);
172 root
.appendChild(ele
);
175 // generate the output target
177 root
.appendChild(document
.createComment("output target"));
178 ele
= document
.createElement("target");
179 ele
.setAttribute("name", "output");
180 applyOutputElement(document
, ele
, fp
);
181 root
.appendChild(ele
);
185 // generate the clean target
187 root
.appendChild(document
.createComment("clean target"));
188 ele
= document
.createElement("target");
189 ele
.setAttribute("name", "clean");
190 applyCleanElement(document
, ele
);
191 root
.appendChild(ele
);
194 // generate the Clean All target
196 root
.appendChild(document
.createComment("Clean All target"));
197 ele
= document
.createElement("target");
198 ele
.setAttribute("name", "cleanall");
199 applyDeepcleanElement(document
, ele
);
200 root
.appendChild(ele
);
203 // add the root element to the document
205 document
.appendChild(rootComment
);
206 document
.appendChild(root
);
208 // Prepare the DOM document for writing
210 Source source
= new DOMSource(document
);
213 // Prepare the output file
215 File file
= new File(buildFilename
);
218 // generate all directory path
220 (new File(file
.getParent())).mkdirs();
221 FileOutputStream outputStream
= new FileOutputStream(file
);
222 Result result
= new StreamResult(new OutputStreamWriter(outputStream
));
225 // Write the DOM document to the file
227 Transformer xformer
= TransformerFactory
.newInstance().newTransformer();
228 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
229 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
230 xformer
.transform(source
, result
);
231 } catch (Exception ex
) {
232 throw new BuildException("Generating the module [" + fpdModuleId
.getModule().getName() + "] build.xml file failed!.\n" + ex
.getMessage());
237 Generate the clean elements for BaseName_build.xml.
239 @param document current BaseName_build.xml XML document
240 @param root Root element for current
242 private void applyCleanElement(Document document
, Node root
) {
244 // <delete includeemptydirs="true">
245 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
248 Element deleteEle
= document
.createElement("delete");
249 deleteEle
.setAttribute("includeemptydirs", "true");
250 Element filesetEle
= document
.createElement("fileset");
251 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
252 filesetEle
.setAttribute("includes", "**/*");
253 filesetEle
.setAttribute("excludes", "*.xml");
254 deleteEle
.appendChild(filesetEle
);
255 root
.appendChild(deleteEle
);
259 Generate the cleanall elements for BaseName_build.xml.
261 @param document current BaseName_build.xml XML document
262 @param root Root element for current
264 private void applyDeepcleanElement(Document document
, Node root
) {
266 // <delete includeemptydirs="true">
267 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
270 Element deleteEle
= document
.createElement("delete");
271 deleteEle
.setAttribute("includeemptydirs", "true");
272 Element filesetEle
= document
.createElement("fileset");
273 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
274 filesetEle
.setAttribute("includes", "**/*");
275 filesetEle
.setAttribute("excludes", "*.xml");
276 deleteEle
.appendChild(filesetEle
);
277 root
.appendChild(deleteEle
);
280 // <delete includeemptydirs="true">
281 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
284 deleteEle
= document
.createElement("delete");
285 deleteEle
.setAttribute("includeemptydirs", "true");
286 filesetEle
= document
.createElement("fileset");
287 filesetEle
.setAttribute("dir", "${DEST_DIR_DEBUG}");
288 filesetEle
.setAttribute("includes", "**/*");
289 deleteEle
.appendChild(filesetEle
);
290 root
.appendChild(deleteEle
);
294 Generate the dependent library instances elements for BaseName_build.xml.
296 @param document current BaseName_build.xml XML document
297 @param root Root element for current
299 private void applyLibraryInstance(Document document
, Node root
) {
300 ModuleIdentification
[] libinstances
= saq
.getLibraryInstance(fpdModuleId
.getArch());
301 for (int i
= 0; i
< libinstances
.length
; i
++) {
303 // Put package file path to module identification
305 PackageIdentification packageId
= libinstances
[i
].getPackage();
308 // Generate ANT script to build library instances
310 Element ele
= document
.createElement("GenBuild");
311 ele
.setAttribute("type", "build");
314 // Prepare pass down information
316 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
317 for (int j
= 0; j
< inheritProperties
.length
; j
++){
318 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
321 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
322 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
324 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
325 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
327 for (int j
= 0; j
< inheritProperties
.length
; j
++){
328 Element property
= document
.createElement("property");
329 property
.setAttribute("name", inheritProperties
[j
]);
330 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
331 ele
.appendChild(property
);
334 root
.appendChild(ele
);
339 Generate the build source files elements for BaseName_build.xml.
341 @param document current BaseName_build.xml XML document
342 @param root Root element for current
344 private void applyCompileElement(Document document
, Node root
) {
346 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
348 String
[][] sourceFiles
= saq
.getSourceFiles(fpdModuleId
.getArch());
350 FileProcess fileProcess
= new FileProcess();
351 fileProcess
.init(project
, includes
, document
);
354 // Initialize some properties by user
356 Element initEle
= document
.createElement("Build_Init");
357 root
.appendChild(initEle
);
359 String moduleDir
= project
.getProperty("MODULE_DIR");
361 // Parse all Unicode files
363 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
365 // Go through all source files. Add MODULE_DIR to preffix
367 File sourceFile
= new File(moduleDir
+ File
.separatorChar
+ sourceFiles
[i
][1]);
368 sourceFiles
[i
][1] = sourceFile
.getPath();
369 String filetype
= sourceFiles
[i
][0];
370 if (filetype
!= null) {
371 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, true);
373 fileProcess
.parseFile(sourceFiles
[i
][1], root
, true);
378 // If exist Unicode file
380 if (fileProcess
.isUnicodeExist()) {
381 Element ele
= document
.createElement("Build_Unicode_Database");
382 ele
.setAttribute("FILEPATH", ".");
383 ele
.setAttribute("FILENAME", "${BASE_NAME}");
384 Element includesEle
= document
.createElement("EXTRA.INC");
385 for (int i
= 0; i
< includes
.length
; i
++) {
386 Element includeEle
= document
.createElement("includepath");
387 includeEle
.setAttribute("path", includes
[i
]);
388 includesEle
.appendChild(includeEle
);
390 ele
.appendChild(includesEle
);
391 root
.appendChild(ele
);
395 // Parse AutoGen.c & AutoGen.h
397 if ( ! fpdModuleId
.getModule().getName().equalsIgnoreCase("Shell")) {
398 fileProcess
.parseFile(project
.getProperty("DEST_DIR_DEBUG") + File
.separatorChar
+ "AutoGen.c", root
, false);
402 // Parse all source files but Unicode files
404 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
405 String filetype
= sourceFiles
[i
][0];
406 if (filetype
!= null) {
407 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, false);
409 fileProcess
.parseFile(sourceFiles
[i
][1], root
, false);
414 // Initialize SOURCE_FILES for dependcy check use
417 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
418 str
+= " " + sourceFiles
[i
][1];
420 project
.setProperty("SOURCE_FILES", str
.replaceAll("(\\\\)", "/"));
424 Generate the section elements for BaseName_build.xml. Library module will
427 @param document current BaseName_build.xml XML document
428 @param root Root element for current
430 private void applySectionsElement(Document document
, Node root
, FfsProcess fp
) {
431 if (fpdModuleId
.getModule().isLibrary()) {
434 if (fp
.initSections(ffsKeyword
, project
, fpdModuleId
)) {
435 String targetFilename
= fpdModuleId
.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask
.getSuffix(fpdModuleId
.getModule().getModuleType());
436 String
[] list
= fp
.getGenSectionElements(document
, "${BASE_NAME}", fpdModuleId
.getModule().getGuid(), targetFilename
);
438 for (int i
= 0; i
< list
.length
; i
++) {
439 Element ele
= document
.createElement(list
[i
]);
440 ele
.setAttribute("FILEPATH", ".");
441 ele
.setAttribute("FILENAME", "${BASE_NAME}");
442 root
.appendChild(ele
);
448 Generate the output elements for BaseName_build.xml. If module is library,
449 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
451 @param document current BaseName_build.xml XML document
452 @param root Root element for current
454 private void applyOutputElement(Document document
, Node root
, FfsProcess fp
) {
455 if (fpdModuleId
.getModule().isLibrary()) {
459 Element cc
= document
.createElement("Build_Library");
460 cc
.setAttribute("FILENAME", fpdModuleId
.getModule().getName());
461 root
.appendChild(cc
);
464 // if it is a module but library
467 if (fp
.getFfsNode() != null) {
468 root
.appendChild(fp
.getFfsNode());