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
;
16 import java
.util
.LinkedHashSet
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.OutputStreamWriter
;
22 import javax
.xml
.parsers
.DocumentBuilder
;
23 import javax
.xml
.parsers
.DocumentBuilderFactory
;
24 import javax
.xml
.transform
.OutputKeys
;
25 import javax
.xml
.transform
.Result
;
26 import javax
.xml
.transform
.Source
;
27 import javax
.xml
.transform
.Transformer
;
28 import javax
.xml
.transform
.TransformerFactory
;
29 import javax
.xml
.transform
.dom
.DOMSource
;
30 import javax
.xml
.transform
.stream
.StreamResult
;
32 import org
.apache
.tools
.ant
.BuildException
;
33 import org
.apache
.tools
.ant
.Project
;
34 import org
.tianocore
.build
.fpd
.FpdParserTask
;
35 import org
.tianocore
.build
.global
.GlobalData
;
36 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
37 import org
.tianocore
.build
.id
.FpdModuleIdentification
;
38 import org
.tianocore
.build
.id
.ModuleIdentification
;
39 import org
.tianocore
.build
.id
.PackageIdentification
;
40 import org
.w3c
.dom
.Comment
;
41 import org
.w3c
.dom
.Document
;
42 import org
.w3c
.dom
.Element
;
43 import org
.w3c
.dom
.Node
;
45 public class ModuleBuildFileGenerator
{
48 /// Pass: TARGET, TOOLCHAIN, ARCH
49 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION
51 String
[] inheritProperties
= {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PLATFORM_FILE", "PACKAGE_GUID", "PACKAGE_VERSION"};
54 /// The information at the header of <em>build.xml</em>.
56 private String info
= "DO NOT EDIT \n"
57 + "File auto-generated by build utility\n"
60 + "Auto-generated ANT build file for building of EFI Modules/Platforms\n";
62 private FpdModuleIdentification fpdModuleId
;
64 private Project project
;
66 private String ffsKeyword
;
68 public ModuleBuildFileGenerator(Project project
, String ffsKeyword
, FpdModuleIdentification fpdModuleId
) {
69 this.project
= project
;
70 this.fpdModuleId
= fpdModuleId
;
71 this.ffsKeyword
= ffsKeyword
;
75 The whole BaseName_build.xml is composed of seven part.
77 <li> ANT properties; </li>
78 <li> Dependent module (dependent library instances in most case); </li>
79 <li> Source files; </li>
80 <li> Sections if module is not library; </li>
81 <li> Output (different for library module and driver module); </li>
86 @throws BuildException
87 Error throws during BaseName_build.xml generating.
89 public void genBuildFile(String buildFilename
) throws BuildException
{
90 FfsProcess fp
= new FfsProcess();
91 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
93 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
94 Document document
= dombuilder
.newDocument();
95 Comment rootComment
= document
.createComment(info
);
98 // create root element and its attributes
100 Element root
= document
.createElement("project");
101 root
.setAttribute("name", fpdModuleId
.getModule().getName());
102 root
.setAttribute("default", "all");
103 root
.setAttribute("basedir", ".");
106 // element for External ANT tasks
108 root
.appendChild(document
.createComment("Apply external ANT tasks"));
109 Element ele
= document
.createElement("taskdef");
110 ele
.setAttribute("resource", "frameworktasks.tasks");
111 root
.appendChild(ele
);
112 ele
= document
.createElement("taskdef");
113 ele
.setAttribute("resource", "cpptasks.tasks");
114 root
.appendChild(ele
);
115 ele
= document
.createElement("typedef");
116 ele
.setAttribute("resource", "cpptasks.types");
117 root
.appendChild(ele
);
118 ele
= document
.createElement("taskdef");
119 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
120 root
.appendChild(ele
);
123 // Generate the default target,
124 // which depends on init, sections and output target
126 root
.appendChild(document
.createComment("Default target"));
127 ele
= document
.createElement("target");
128 ele
.setAttribute("name", "all");
129 ele
.setAttribute("depends", "libraries, sourcefiles, sections, output");
130 root
.appendChild(ele
);
133 // compile all source files
135 root
.appendChild(document
.createComment("Compile all dependency Library instances."));
136 ele
= document
.createElement("target");
137 ele
.setAttribute("name", "libraries");
140 // Parse all sourfiles but files specified in sections
142 applyLibraryInstance(document
, ele
);
143 root
.appendChild(ele
);
146 // compile all source files
148 root
.appendChild(document
.createComment("sourcefiles target"));
149 ele
= document
.createElement("target");
150 ele
.setAttribute("name", "sourcefiles");
153 // Parse all sourfiles but files specified in sections
155 applyCompileElement(document
, ele
);
156 root
.appendChild(ele
);
159 // generate the init target
160 // main purpose is create all nessary pathes
161 // generate the sections target
163 root
.appendChild(document
.createComment("sections target"));
164 ele
= document
.createElement("target");
165 ele
.setAttribute("name", "sections");
166 applySectionsElement(document
, ele
, fp
);
167 root
.appendChild(ele
);
170 // generate the output target
172 root
.appendChild(document
.createComment("output target"));
173 ele
= document
.createElement("target");
174 ele
.setAttribute("name", "output");
175 applyOutputElement(document
, ele
, fp
);
176 root
.appendChild(ele
);
180 // generate the clean target
182 root
.appendChild(document
.createComment("clean target"));
183 ele
= document
.createElement("target");
184 ele
.setAttribute("name", "clean");
185 applyCleanElement(document
, ele
);
186 root
.appendChild(ele
);
189 // generate the Clean All target
191 root
.appendChild(document
.createComment("Clean All target"));
192 ele
= document
.createElement("target");
193 ele
.setAttribute("name", "cleanall");
194 applyDeepcleanElement(document
, ele
);
195 root
.appendChild(ele
);
198 // add the root element to the document
200 document
.appendChild(rootComment
);
201 document
.appendChild(root
);
203 // Prepare the DOM document for writing
205 Source source
= new DOMSource(document
);
208 // Prepare the output file
210 File file
= new File(buildFilename
);
213 // generate all directory path
215 (new File(file
.getParent())).mkdirs();
216 FileOutputStream outputStream
= new FileOutputStream(file
);
217 Result result
= new StreamResult(new OutputStreamWriter(outputStream
));
220 // Write the DOM document to the file
222 Transformer xformer
= TransformerFactory
.newInstance().newTransformer();
223 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
224 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
225 xformer
.transform(source
, result
);
226 } catch (Exception ex
) {
227 throw new BuildException("Module [" + fpdModuleId
.getModule().getName() + "] generating build file failed.\n" + ex
.getMessage());
232 Generate the clean elements for BaseName_build.xml.
234 @param document current BaseName_build.xml XML document
235 @param root Root element for current
237 private void applyCleanElement(Document document
, Node root
) {
239 // <delete includeemptydirs="true">
240 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
243 Element deleteEle
= document
.createElement("delete");
244 deleteEle
.setAttribute("includeemptydirs", "true");
245 Element filesetEle
= document
.createElement("fileset");
246 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
247 filesetEle
.setAttribute("includes", "**/*");
248 filesetEle
.setAttribute("excludes", "*.xml");
249 deleteEle
.appendChild(filesetEle
);
250 root
.appendChild(deleteEle
);
254 Generate the cleanall elements for BaseName_build.xml.
256 @param document current BaseName_build.xml XML document
257 @param root Root element for current
259 private void applyDeepcleanElement(Document document
, Node root
) {
261 // <delete includeemptydirs="true">
262 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
265 Element deleteEle
= document
.createElement("delete");
266 deleteEle
.setAttribute("includeemptydirs", "true");
267 Element filesetEle
= document
.createElement("fileset");
268 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
269 filesetEle
.setAttribute("includes", "**/*");
270 filesetEle
.setAttribute("excludes", "*.xml");
271 deleteEle
.appendChild(filesetEle
);
272 root
.appendChild(deleteEle
);
275 // <delete includeemptydirs="true">
276 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
279 deleteEle
= document
.createElement("delete");
280 deleteEle
.setAttribute("includeemptydirs", "true");
281 filesetEle
= document
.createElement("fileset");
282 filesetEle
.setAttribute("dir", "${DEST_DIR_DEBUG}");
283 filesetEle
.setAttribute("includes", "**/*");
284 deleteEle
.appendChild(filesetEle
);
285 root
.appendChild(deleteEle
);
289 Generate the dependent library instances elements for BaseName_build.xml.
291 @param document current BaseName_build.xml XML document
292 @param root Root element for current
294 private void applyLibraryInstance(Document document
, Node root
) {
295 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
296 for (int i
= 0; i
< libinstances
.length
; i
++) {
298 // Put package file path to module identification
300 PackageIdentification packageId
= libinstances
[i
].getPackage();
303 // Generate ANT script to build library instances
305 Element ele
= document
.createElement("GenBuild");
306 ele
.setAttribute("type", "build");
309 // Prepare pass down information
311 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
312 for (int j
= 0; j
< inheritProperties
.length
; j
++){
313 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
316 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
317 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
319 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
320 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
322 for (int j
= 0; j
< inheritProperties
.length
; j
++){
323 Element property
= document
.createElement("property");
324 property
.setAttribute("name", inheritProperties
[j
]);
325 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
326 ele
.appendChild(property
);
329 root
.appendChild(ele
);
334 Return the name of the directory that corresponds to the architecture.
335 This is a translation from the XML Schema tag to a directory that
336 corresponds to our directory name coding convention.
339 private String
archDir(String arch
) {
340 return arch
.replaceFirst("X64", "x64")
341 .replaceFirst("IPF", "Ipf")
342 .replaceFirst("IA32", "Ia32")
343 .replaceFirst("ARM", "Arm")
344 .replaceFirst("EBC", "Ebc");
348 Generate the build source files elements for BaseName_build.xml.
350 @param document current BaseName_build.xml XML document
351 @param root Root element for current
353 private void applyCompileElement(Document document
, Node root
) {
355 // Prepare the includes: PackageDependencies and Output debug direactory
357 Set
<String
> includes
= new LinkedHashSet
<String
>();
358 String arch
= project
.getProperty("ARCH");
363 includes
.add("${WORKSPACE_DIR}" + File
.separatorChar
);
368 includes
.add("${MODULE_DIR}");
369 includes
.add("${MODULE_DIR}" + File
.separatorChar
+ archDir(arch
));
372 // Packages in PackageDenpendencies
374 PackageIdentification
[] packageDependencies
= SurfaceAreaQuery
.getDependencePkg(fpdModuleId
.getArch());
375 for (int i
= 0; i
< packageDependencies
.length
; i
++) {
376 GlobalData
.refreshPackageIdentification(packageDependencies
[i
]);
377 File packageFile
= packageDependencies
[i
].getSpdFile();
378 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include");
379 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include" + File
.separatorChar
+ archDir(arch
));
383 // All Dependency Library Instance's PackageDependencies
385 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
386 for (int i
= 0; i
< libinstances
.length
; i
++) {
387 SurfaceAreaQuery
.push(GlobalData
.getDoc(libinstances
[i
], fpdModuleId
.getArch()));
388 PackageIdentification
[] libraryPackageDependencies
= SurfaceAreaQuery
.getDependencePkg(fpdModuleId
.getArch());
389 for (int j
= 0; j
< libraryPackageDependencies
.length
; j
++) {
390 GlobalData
.refreshPackageIdentification(libraryPackageDependencies
[j
]);
391 File packageFile
= libraryPackageDependencies
[j
].getSpdFile();
392 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include");
393 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include" + File
.separatorChar
+ archDir(arch
));
395 SurfaceAreaQuery
.pop();
400 // The package which the module belongs to
402 includes
.add(fpdModuleId
.getModule().getPackage().getPackageDir() + File
.separatorChar
+ "Include");
403 includes
.add(fpdModuleId
.getModule().getPackage().getPackageDir() + File
.separatorChar
+ "Include" + File
.separatorChar
+ archDir(arch
));
406 // Debug files output directory
408 includes
.add("${DEST_DIR_DEBUG}");
411 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
413 String
[][] sourceFiles
= SurfaceAreaQuery
.getSourceFiles(fpdModuleId
.getArch());
415 FileProcess fileProcess
= new FileProcess();
416 fileProcess
.init(project
, includes
, document
);
418 String moduleDir
= project
.getProperty("MODULE_DIR");
420 // Parse all Unicode files
422 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
424 // Go through all source files. Add MODULE_DIR to preffix
426 File sourceFile
= new File(moduleDir
+ File
.separatorChar
+ sourceFiles
[i
][1]);
427 sourceFiles
[i
][1] = sourceFile
.getPath();
428 String filetype
= sourceFiles
[i
][0];
429 if (filetype
!= null) {
430 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, true);
432 fileProcess
.parseFile(sourceFiles
[i
][1], root
, true);
437 // If exist Unicode file
439 if (fileProcess
.isUnicodeExist()) {
440 Element ele
= document
.createElement("Build_Unicode_Database");
441 ele
.setAttribute("FILEPATH", ".");
442 ele
.setAttribute("FILENAME", "${BASE_NAME}");
443 String
[] includePaths
= includes
.toArray(new String
[includes
.size()]);
444 Element includesEle
= document
.createElement("EXTRA.INC");
445 for (int i
= 0; i
< includePaths
.length
; i
++) {
446 Element includeEle
= document
.createElement("includepath");
447 includeEle
.setAttribute("path", includePaths
[i
]);
448 includesEle
.appendChild(includeEle
);
450 ele
.appendChild(includesEle
);
451 root
.appendChild(ele
);
455 // Parse AutoGen.c & AutoGen.h
457 if ( ! fpdModuleId
.getModule().getName().equalsIgnoreCase("Shell")) {
458 fileProcess
.parseFile(project
.getProperty("DEST_DIR_DEBUG") + File
.separatorChar
+ "AutoGen.c", root
, false);
462 // Parse all source files but Unicode files
464 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
465 String filetype
= sourceFiles
[i
][0];
466 if (filetype
!= null) {
467 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, false);
469 fileProcess
.parseFile(sourceFiles
[i
][1], root
, false);
474 // Initialize SOURCE_FILES for dependcy check use
477 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
478 str
+= " " + sourceFiles
[i
][1];
480 project
.setProperty("SOURCE_FILES", str
.replaceAll("(\\\\)", "/"));
484 Generate the section elements for BaseName_build.xml. Library module will
487 @param document current BaseName_build.xml XML document
488 @param root Root element for current
490 private void applySectionsElement(Document document
, Node root
, FfsProcess fp
) {
491 if (fpdModuleId
.getModule().isLibrary()) {
494 if (fp
.initSections(ffsKeyword
, project
, fpdModuleId
)) {
495 String targetFilename
= fpdModuleId
.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask
.getSuffix(fpdModuleId
.getModule().getModuleType());
496 String
[] list
= fp
.getGenSectionElements(document
, "${BASE_NAME}", fpdModuleId
.getModule().getGuid(), targetFilename
);
498 for (int i
= 0; i
< list
.length
; i
++) {
499 Element ele
= document
.createElement(list
[i
]);
500 ele
.setAttribute("FILEPATH", ".");
501 ele
.setAttribute("FILENAME", "${BASE_NAME}");
502 root
.appendChild(ele
);
508 Generate the output elements for BaseName_build.xml. If module is library,
509 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
511 @param document current BaseName_build.xml XML document
512 @param root Root element for current
514 private void applyOutputElement(Document document
, Node root
, FfsProcess fp
) {
515 if (fpdModuleId
.getModule().isLibrary()) {
519 Element cc
= document
.createElement("Build_Library");
520 cc
.setAttribute("FILENAME", fpdModuleId
.getModule().getName());
521 root
.appendChild(cc
);
524 // if it is a module but library
527 if (fp
.getFfsNode() != null) {
528 root
.appendChild(fp
.getFfsNode());