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(saq
);
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 applyLibraryInstance(document
, ele
);
147 root
.appendChild(ele
);
150 // compile all source files
152 root
.appendChild(document
.createComment("sourcefiles target"));
153 ele
= document
.createElement("target");
154 ele
.setAttribute("name", "sourcefiles");
157 // Parse all sourfiles but files specified in sections
159 applyCompileElement(document
, ele
);
160 root
.appendChild(ele
);
163 // generate the init target
164 // main purpose is create all nessary pathes
165 // generate the sections target
167 root
.appendChild(document
.createComment("sections target"));
168 ele
= document
.createElement("target");
169 ele
.setAttribute("name", "sections");
170 applySectionsElement(document
, ele
, fp
);
171 root
.appendChild(ele
);
174 // generate the output target
176 root
.appendChild(document
.createComment("output target"));
177 ele
= document
.createElement("target");
178 ele
.setAttribute("name", "output");
179 applyOutputElement(document
, ele
, fp
);
180 root
.appendChild(ele
);
184 // generate the clean target
186 root
.appendChild(document
.createComment("clean target"));
187 ele
= document
.createElement("target");
188 ele
.setAttribute("name", "clean");
189 applyCleanElement(document
, ele
);
190 root
.appendChild(ele
);
193 // generate the Clean All target
195 root
.appendChild(document
.createComment("Clean All target"));
196 ele
= document
.createElement("target");
197 ele
.setAttribute("name", "cleanall");
198 applyDeepcleanElement(document
, ele
);
199 root
.appendChild(ele
);
202 // add the root element to the document
204 document
.appendChild(rootComment
);
205 document
.appendChild(root
);
207 // Prepare the DOM document for writing
209 Source source
= new DOMSource(document
);
212 // Prepare the output file
214 File file
= new File(buildFilename
);
217 // generate all directory path
219 (new File(file
.getParent())).mkdirs();
220 FileOutputStream outputStream
= new FileOutputStream(file
);
221 Result result
= new StreamResult(new OutputStreamWriter(outputStream
));
224 // Write the DOM document to the file
226 Transformer xformer
= TransformerFactory
.newInstance().newTransformer();
227 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
228 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
229 xformer
.transform(source
, result
);
230 } catch (Exception ex
) {
231 throw new BuildException("Generating the module [" + fpdModuleId
.getModule().getName() + "] build.xml file failed!.\n" + ex
.getMessage());
236 Generate the clean elements for BaseName_build.xml.
238 @param document current BaseName_build.xml XML document
239 @param root Root element for current
241 private void applyCleanElement(Document document
, Node root
) {
243 // <delete includeemptydirs="true">
244 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
247 Element deleteEle
= document
.createElement("delete");
248 deleteEle
.setAttribute("includeemptydirs", "true");
249 Element filesetEle
= document
.createElement("fileset");
250 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
251 filesetEle
.setAttribute("includes", "**/*");
252 filesetEle
.setAttribute("excludes", "*.xml");
253 deleteEle
.appendChild(filesetEle
);
254 root
.appendChild(deleteEle
);
258 Generate the cleanall elements for BaseName_build.xml.
260 @param document current BaseName_build.xml XML document
261 @param root Root element for current
263 private void applyDeepcleanElement(Document document
, Node root
) {
265 // <delete includeemptydirs="true">
266 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
269 Element deleteEle
= document
.createElement("delete");
270 deleteEle
.setAttribute("includeemptydirs", "true");
271 Element filesetEle
= document
.createElement("fileset");
272 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
273 filesetEle
.setAttribute("includes", "**/*");
274 filesetEle
.setAttribute("excludes", "*.xml");
275 deleteEle
.appendChild(filesetEle
);
276 root
.appendChild(deleteEle
);
279 // <delete includeemptydirs="true">
280 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
283 deleteEle
= document
.createElement("delete");
284 deleteEle
.setAttribute("includeemptydirs", "true");
285 filesetEle
= document
.createElement("fileset");
286 filesetEle
.setAttribute("dir", "${DEST_DIR_DEBUG}");
287 filesetEle
.setAttribute("includes", "**/*");
288 deleteEle
.appendChild(filesetEle
);
289 root
.appendChild(deleteEle
);
293 Generate the dependent library instances elements for BaseName_build.xml.
295 @param document current BaseName_build.xml XML document
296 @param root Root element for current
298 private void applyLibraryInstance(Document document
, Node root
) {
299 ModuleIdentification
[] libinstances
= saq
.getLibraryInstance(fpdModuleId
.getArch());
300 for (int i
= 0; i
< libinstances
.length
; i
++) {
302 // Put package file path to module identification
304 PackageIdentification packageId
= libinstances
[i
].getPackage();
307 // Generate ANT script to build library instances
309 Element ele
= document
.createElement("GenBuild");
310 ele
.setAttribute("type", "build");
313 // Prepare pass down information
315 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
316 for (int j
= 0; j
< inheritProperties
.length
; j
++){
317 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
320 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
321 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
323 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
324 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
326 for (int j
= 0; j
< inheritProperties
.length
; j
++){
327 Element property
= document
.createElement("property");
328 property
.setAttribute("name", inheritProperties
[j
]);
329 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
330 ele
.appendChild(property
);
333 root
.appendChild(ele
);
338 Generate the build source files elements for BaseName_build.xml.
340 @param document current BaseName_build.xml XML document
341 @param root Root element for current
343 private void applyCompileElement(Document document
, Node root
) {
345 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
347 String
[][] sourceFiles
= saq
.getSourceFiles(fpdModuleId
.getArch());
349 FileProcess fileProcess
= new FileProcess();
350 fileProcess
.init(project
, includes
, document
);
353 // Initialize some properties by user
355 Element initEle
= document
.createElement("Build_Init");
356 root
.appendChild(initEle
);
358 String moduleDir
= project
.getProperty("MODULE_DIR");
360 // Parse all Unicode files
362 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
364 // Go through all source files. Add MODULE_DIR to preffix
366 File sourceFile
= new File(moduleDir
+ File
.separatorChar
+ sourceFiles
[i
][1]);
367 sourceFiles
[i
][1] = sourceFile
.getPath();
368 String filetype
= sourceFiles
[i
][0];
369 if (filetype
!= null) {
370 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, true);
372 fileProcess
.parseFile(sourceFiles
[i
][1], root
, true);
377 // If exist Unicode file
379 if (fileProcess
.isUnicodeExist()) {
380 Element ele
= document
.createElement("Build_Unicode_Database");
381 ele
.setAttribute("FILEPATH", ".");
382 ele
.setAttribute("FILENAME", "${BASE_NAME}");
383 Element includesEle
= document
.createElement("EXTRA.INC");
384 for (int i
= 0; i
< includes
.length
; i
++) {
385 Element includeEle
= document
.createElement("includepath");
386 includeEle
.setAttribute("path", includes
[i
]);
387 includesEle
.appendChild(includeEle
);
389 ele
.appendChild(includesEle
);
390 root
.appendChild(ele
);
394 // Parse AutoGen.c & AutoGen.h
396 if ( ! fpdModuleId
.getModule().getName().equalsIgnoreCase("Shell")) {
397 fileProcess
.parseFile(project
.getProperty("DEST_DIR_DEBUG") + File
.separatorChar
+ "AutoGen.c", root
, false);
401 // Parse all source files but Unicode files
403 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
404 String filetype
= sourceFiles
[i
][0];
405 if (filetype
!= null) {
406 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, false);
408 fileProcess
.parseFile(sourceFiles
[i
][1], root
, false);
413 // Initialize SOURCE_FILES for dependcy check use
416 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
417 str
+= " " + sourceFiles
[i
][1];
419 PropertyManager
.setProperty(project
, "SOURCE_FILES", str
.replaceAll("(\\\\)", "/"));
423 Generate the section elements for BaseName_build.xml. Library module will
426 @param document current BaseName_build.xml XML document
427 @param root Root element for current
429 private void applySectionsElement(Document document
, Node root
, FfsProcess fp
) {
430 if (fpdModuleId
.getModule().isLibrary()) {
433 if (fp
.initSections(ffsKeyword
, project
, fpdModuleId
)) {
434 String targetFilename
= fpdModuleId
.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask
.getSuffix(fpdModuleId
.getModule().getModuleType());
435 String
[] list
= fp
.getGenSectionElements(document
, "${BASE_NAME}", fpdModuleId
.getModule().getGuid(), targetFilename
);
437 for (int i
= 0; i
< list
.length
; i
++) {
438 Element ele
= document
.createElement(list
[i
]);
439 ele
.setAttribute("FILEPATH", ".");
440 ele
.setAttribute("FILENAME", "${BASE_NAME}");
441 root
.appendChild(ele
);
447 Generate the output elements for BaseName_build.xml. If module is library,
448 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
450 @param document current BaseName_build.xml XML document
451 @param root Root element for current
453 private void applyOutputElement(Document document
, Node root
, FfsProcess fp
) {
454 if (fpdModuleId
.getModule().isLibrary()) {
458 Element cc
= document
.createElement("Build_Library");
459 cc
.setAttribute("FILENAME", fpdModuleId
.getModule().getName());
460 root
.appendChild(cc
);
463 // if it is a module but library
466 if (fp
.getFfsNode() != null) {
467 root
.appendChild(fp
.getFfsNode());