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
.global
.PropertyManager
;
38 import org
.tianocore
.build
.id
.FpdModuleIdentification
;
39 import org
.tianocore
.build
.id
.ModuleIdentification
;
40 import org
.tianocore
.build
.id
.PackageIdentification
;
41 import org
.w3c
.dom
.Comment
;
42 import org
.w3c
.dom
.Document
;
43 import org
.w3c
.dom
.Element
;
44 import org
.w3c
.dom
.Node
;
46 public class ModuleBuildFileGenerator
{
49 /// Pass: TARGET, TOOLCHAIN, ARCH
50 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION
52 String
[] inheritProperties
= {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PLATFORM_FILE", "PACKAGE_GUID", "PACKAGE_VERSION"};
55 /// The information at the header of <em>build.xml</em>.
57 private String info
= "DO NOT EDIT \n"
58 + "This file is auto-generated by the build utility\n"
61 + "Auto-generated ANT build file for build EFI Modules and Platforms\n";
63 private FpdModuleIdentification fpdModuleId
;
65 private Project project
;
67 private String ffsKeyword
;
69 public ModuleBuildFileGenerator(Project project
, String ffsKeyword
, FpdModuleIdentification fpdModuleId
) {
70 this.project
= project
;
71 this.fpdModuleId
= fpdModuleId
;
72 this.ffsKeyword
= ffsKeyword
;
76 The whole BaseName_build.xml is composed of seven part.
78 <li> ANT properties; </li>
79 <li> Dependent module (dependent library instances in most case); </li>
80 <li> Source files; </li>
81 <li> Sections if module is not library; </li>
82 <li> Output (different for library module and driver module); </li>
87 @throws BuildException
88 Error throws during BaseName_build.xml generating.
90 public void genBuildFile(String buildFilename
) throws BuildException
{
91 FfsProcess fp
= new FfsProcess();
92 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
94 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
95 Document document
= dombuilder
.newDocument();
96 Comment rootComment
= document
.createComment(info
);
99 // create root element and its attributes
101 Element root
= document
.createElement("project");
102 root
.setAttribute("name", fpdModuleId
.getModule().getName());
103 root
.setAttribute("default", "all");
104 root
.setAttribute("basedir", ".");
107 // element for External ANT tasks
109 root
.appendChild(document
.createComment("Apply external ANT tasks"));
110 Element ele
= document
.createElement("taskdef");
111 ele
.setAttribute("resource", "frameworktasks.tasks");
112 root
.appendChild(ele
);
113 ele
= document
.createElement("taskdef");
114 ele
.setAttribute("resource", "cpptasks.tasks");
115 root
.appendChild(ele
);
116 ele
= document
.createElement("typedef");
117 ele
.setAttribute("resource", "cpptasks.types");
118 root
.appendChild(ele
);
119 ele
= document
.createElement("taskdef");
120 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
121 root
.appendChild(ele
);
124 // Generate the default target,
125 // which depends on init, sections and output target
127 root
.appendChild(document
.createComment("Default target"));
128 ele
= document
.createElement("target");
129 ele
.setAttribute("name", "all");
130 ele
.setAttribute("depends", "libraries, sourcefiles, sections, output");
131 root
.appendChild(ele
);
134 // compile all source files
136 root
.appendChild(document
.createComment("Compile all dependency Library instances."));
137 ele
= document
.createElement("target");
138 ele
.setAttribute("name", "libraries");
141 // Parse all sourfiles but files specified in sections
143 applyLibraryInstance(document
, ele
);
144 root
.appendChild(ele
);
147 // compile all source files
149 root
.appendChild(document
.createComment("sourcefiles target"));
150 ele
= document
.createElement("target");
151 ele
.setAttribute("name", "sourcefiles");
154 // Parse all sourfiles but files specified in sections
156 applyCompileElement(document
, ele
);
157 root
.appendChild(ele
);
160 // generate the init target
161 // main purpose is create all nessary pathes
162 // generate the sections target
164 root
.appendChild(document
.createComment("sections target"));
165 ele
= document
.createElement("target");
166 ele
.setAttribute("name", "sections");
167 applySectionsElement(document
, ele
, fp
);
168 root
.appendChild(ele
);
171 // generate the output target
173 root
.appendChild(document
.createComment("output target"));
174 ele
= document
.createElement("target");
175 ele
.setAttribute("name", "output");
176 applyOutputElement(document
, ele
, fp
);
177 root
.appendChild(ele
);
181 // generate the clean target
183 root
.appendChild(document
.createComment("clean target"));
184 ele
= document
.createElement("target");
185 ele
.setAttribute("name", "clean");
186 applyCleanElement(document
, ele
);
187 root
.appendChild(ele
);
190 // generate the Clean All target
192 root
.appendChild(document
.createComment("Clean All target"));
193 ele
= document
.createElement("target");
194 ele
.setAttribute("name", "cleanall");
195 applyDeepcleanElement(document
, ele
);
196 root
.appendChild(ele
);
199 // add the root element to the document
201 document
.appendChild(rootComment
);
202 document
.appendChild(root
);
204 // Prepare the DOM document for writing
206 Source source
= new DOMSource(document
);
209 // Prepare the output file
211 File file
= new File(buildFilename
);
214 // generate all directory path
216 (new File(file
.getParent())).mkdirs();
217 FileOutputStream outputStream
= new FileOutputStream(file
);
218 Result result
= new StreamResult(new OutputStreamWriter(outputStream
));
221 // Write the DOM document to the file
223 Transformer xformer
= TransformerFactory
.newInstance().newTransformer();
224 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
225 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
226 xformer
.transform(source
, result
);
227 } catch (Exception ex
) {
228 throw new BuildException("Generating the module [" + fpdModuleId
.getModule().getName() + "] build.xml file failed!.\n" + ex
.getMessage());
233 Generate the clean elements for BaseName_build.xml.
235 @param document current BaseName_build.xml XML document
236 @param root Root element for current
238 private void applyCleanElement(Document document
, Node root
) {
240 // <delete includeemptydirs="true">
241 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
244 Element deleteEle
= document
.createElement("delete");
245 deleteEle
.setAttribute("includeemptydirs", "true");
246 Element filesetEle
= document
.createElement("fileset");
247 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
248 filesetEle
.setAttribute("includes", "**/*");
249 filesetEle
.setAttribute("excludes", "*.xml");
250 deleteEle
.appendChild(filesetEle
);
251 root
.appendChild(deleteEle
);
255 Generate the cleanall elements for BaseName_build.xml.
257 @param document current BaseName_build.xml XML document
258 @param root Root element for current
260 private void applyDeepcleanElement(Document document
, Node root
) {
262 // <delete includeemptydirs="true">
263 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
266 Element deleteEle
= document
.createElement("delete");
267 deleteEle
.setAttribute("includeemptydirs", "true");
268 Element filesetEle
= document
.createElement("fileset");
269 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
270 filesetEle
.setAttribute("includes", "**/*");
271 filesetEle
.setAttribute("excludes", "*.xml");
272 deleteEle
.appendChild(filesetEle
);
273 root
.appendChild(deleteEle
);
276 // <delete includeemptydirs="true">
277 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
280 deleteEle
= document
.createElement("delete");
281 deleteEle
.setAttribute("includeemptydirs", "true");
282 filesetEle
= document
.createElement("fileset");
283 filesetEle
.setAttribute("dir", "${DEST_DIR_DEBUG}");
284 filesetEle
.setAttribute("includes", "**/*");
285 deleteEle
.appendChild(filesetEle
);
286 root
.appendChild(deleteEle
);
290 Generate the dependent library instances elements for BaseName_build.xml.
292 @param document current BaseName_build.xml XML document
293 @param root Root element for current
295 private void applyLibraryInstance(Document document
, Node root
) {
296 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
297 for (int i
= 0; i
< libinstances
.length
; i
++) {
299 // Put package file path to module identification
301 PackageIdentification packageId
= libinstances
[i
].getPackage();
304 // Generate ANT script to build library instances
306 Element ele
= document
.createElement("GenBuild");
307 ele
.setAttribute("type", "build");
310 // Prepare pass down information
312 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
313 for (int j
= 0; j
< inheritProperties
.length
; j
++){
314 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
317 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
318 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
320 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
321 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
323 for (int j
= 0; j
< inheritProperties
.length
; j
++){
324 Element property
= document
.createElement("property");
325 property
.setAttribute("name", inheritProperties
[j
]);
326 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
327 ele
.appendChild(property
);
330 root
.appendChild(ele
);
335 Return the name of the directory that corresponds to the architecture.
336 This is a translation from the XML Schema tag to a directory that
337 corresponds to our directory name coding convention.
340 private String
archDir(String arch
) {
341 return arch
.replaceFirst("X64", "x64")
342 .replaceFirst("IPF", "Ipf")
343 .replaceFirst("IA32", "Ia32")
344 .replaceFirst("ARM", "Arm")
345 .replaceFirst("EBC", "Ebc");
349 Generate the build source files elements for BaseName_build.xml.
351 @param document current BaseName_build.xml XML document
352 @param root Root element for current
354 private void applyCompileElement(Document document
, Node root
) {
356 // Prepare the includes: PackageDependencies and Output debug direactory
358 Set
<String
> includes
= new LinkedHashSet
<String
>();
359 String arch
= project
.getProperty("ARCH");
364 includes
.add("${WORKSPACE_DIR}" + File
.separatorChar
);
369 includes
.add("${MODULE_DIR}");
370 includes
.add("${MODULE_DIR}" + File
.separatorChar
+ archDir(arch
));
373 // Packages in PackageDenpendencies
375 PackageIdentification
[] packageDependencies
= SurfaceAreaQuery
.getDependencePkg(fpdModuleId
.getArch());
376 for (int i
= 0; i
< packageDependencies
.length
; i
++) {
377 GlobalData
.refreshPackageIdentification(packageDependencies
[i
]);
378 File packageFile
= packageDependencies
[i
].getSpdFile();
379 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include");
380 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include" + File
.separatorChar
+ archDir(arch
));
384 // All Dependency Library Instance's PackageDependencies
386 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
387 for (int i
= 0; i
< libinstances
.length
; i
++) {
388 SurfaceAreaQuery
.push(GlobalData
.getDoc(libinstances
[i
], fpdModuleId
.getArch()));
389 PackageIdentification
[] libraryPackageDependencies
= SurfaceAreaQuery
.getDependencePkg(fpdModuleId
.getArch());
390 for (int j
= 0; j
< libraryPackageDependencies
.length
; j
++) {
391 GlobalData
.refreshPackageIdentification(libraryPackageDependencies
[j
]);
392 File packageFile
= libraryPackageDependencies
[j
].getSpdFile();
393 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include");
394 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include" + File
.separatorChar
+ archDir(arch
));
396 SurfaceAreaQuery
.pop();
401 // The package which the module belongs to
403 includes
.add(fpdModuleId
.getModule().getPackage().getPackageDir() + File
.separatorChar
+ "Include");
404 includes
.add(fpdModuleId
.getModule().getPackage().getPackageDir() + File
.separatorChar
+ "Include" + File
.separatorChar
+ archDir(arch
));
407 // Debug files output directory
409 includes
.add("${DEST_DIR_DEBUG}");
412 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
414 String
[][] sourceFiles
= SurfaceAreaQuery
.getSourceFiles(fpdModuleId
.getArch());
416 FileProcess fileProcess
= new FileProcess();
417 fileProcess
.init(project
, includes
, document
);
420 // Initialize some properties by user
422 Element initEle
= document
.createElement("Build_Init");
423 root
.appendChild(initEle
);
425 String moduleDir
= project
.getProperty("MODULE_DIR");
427 // Parse all Unicode files
429 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
431 // Go through all source files. Add MODULE_DIR to preffix
433 File sourceFile
= new File(moduleDir
+ File
.separatorChar
+ sourceFiles
[i
][1]);
434 sourceFiles
[i
][1] = sourceFile
.getPath();
435 String filetype
= sourceFiles
[i
][0];
436 if (filetype
!= null) {
437 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, true);
439 fileProcess
.parseFile(sourceFiles
[i
][1], root
, true);
444 // If exist Unicode file
446 if (fileProcess
.isUnicodeExist()) {
447 Element ele
= document
.createElement("Build_Unicode_Database");
448 ele
.setAttribute("FILEPATH", ".");
449 ele
.setAttribute("FILENAME", "${BASE_NAME}");
450 String
[] includePaths
= includes
.toArray(new String
[includes
.size()]);
451 Element includesEle
= document
.createElement("EXTRA.INC");
452 for (int i
= 0; i
< includePaths
.length
; i
++) {
453 Element includeEle
= document
.createElement("includepath");
454 includeEle
.setAttribute("path", includePaths
[i
]);
455 includesEle
.appendChild(includeEle
);
457 ele
.appendChild(includesEle
);
458 root
.appendChild(ele
);
462 // Parse AutoGen.c & AutoGen.h
464 if ( ! fpdModuleId
.getModule().getName().equalsIgnoreCase("Shell")) {
465 fileProcess
.parseFile(project
.getProperty("DEST_DIR_DEBUG") + File
.separatorChar
+ "AutoGen.c", root
, false);
469 // Parse all source files but Unicode files
471 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
472 String filetype
= sourceFiles
[i
][0];
473 if (filetype
!= null) {
474 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, false);
476 fileProcess
.parseFile(sourceFiles
[i
][1], root
, false);
481 // Initialize SOURCE_FILES for dependcy check use
484 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
485 str
+= " " + sourceFiles
[i
][1];
487 PropertyManager
.setProperty(project
, "SOURCE_FILES", str
.replaceAll("(\\\\)", "/"));
491 Generate the section elements for BaseName_build.xml. Library module will
494 @param document current BaseName_build.xml XML document
495 @param root Root element for current
497 private void applySectionsElement(Document document
, Node root
, FfsProcess fp
) {
498 if (fpdModuleId
.getModule().isLibrary()) {
501 if (fp
.initSections(ffsKeyword
, project
, fpdModuleId
)) {
502 String targetFilename
= fpdModuleId
.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask
.getSuffix(fpdModuleId
.getModule().getModuleType());
503 String
[] list
= fp
.getGenSectionElements(document
, "${BASE_NAME}", fpdModuleId
.getModule().getGuid(), targetFilename
);
505 for (int i
= 0; i
< list
.length
; i
++) {
506 Element ele
= document
.createElement(list
[i
]);
507 ele
.setAttribute("FILEPATH", ".");
508 ele
.setAttribute("FILENAME", "${BASE_NAME}");
509 root
.appendChild(ele
);
515 Generate the output elements for BaseName_build.xml. If module is library,
516 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
518 @param document current BaseName_build.xml XML document
519 @param root Root element for current
521 private void applyOutputElement(Document document
, Node root
, FfsProcess fp
) {
522 if (fpdModuleId
.getModule().isLibrary()) {
526 Element cc
= document
.createElement("Build_Library");
527 cc
.setAttribute("FILENAME", fpdModuleId
.getModule().getName());
528 root
.appendChild(cc
);
531 // if it is a module but library
534 if (fp
.getFfsNode() != null) {
535 root
.appendChild(fp
.getFfsNode());