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
;
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
.GlobalData
;
34 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
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 + "File auto-generated by build utility\n"
58 + "Auto-generated ANT build file for building of EFI Modules/Platforms\n";
60 private FpdModuleIdentification fpdModuleId
;
62 private Project project
;
64 private String ffsKeyword
;
66 public ModuleBuildFileGenerator(Project project
, String ffsKeyword
, FpdModuleIdentification fpdModuleId
) {
67 this.project
= project
;
68 this.fpdModuleId
= fpdModuleId
;
69 this.ffsKeyword
= ffsKeyword
;
73 The whole BaseName_build.xml is composed of seven part.
75 <li> ANT properties; </li>
76 <li> Dependent module (dependent library instances in most case); </li>
77 <li> Source files; </li>
78 <li> Sections if module is not library; </li>
79 <li> Output (different for library module and driver module); </li>
84 @throws BuildException
85 Error throws during BaseName_build.xml generating.
87 public void genBuildFile(String buildFilename
) throws BuildException
{
88 FfsProcess fp
= new FfsProcess();
89 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
91 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
92 Document document
= dombuilder
.newDocument();
93 Comment rootComment
= document
.createComment(info
);
96 // create root element and its attributes
98 Element root
= document
.createElement("project");
99 root
.setAttribute("name", fpdModuleId
.getModule().getName());
100 root
.setAttribute("default", "all");
101 root
.setAttribute("basedir", ".");
104 // element for External ANT tasks
106 root
.appendChild(document
.createComment("Apply external ANT tasks"));
107 Element ele
= document
.createElement("taskdef");
108 ele
.setAttribute("resource", "frameworktasks.tasks");
109 root
.appendChild(ele
);
110 ele
= document
.createElement("taskdef");
111 ele
.setAttribute("resource", "cpptasks.tasks");
112 root
.appendChild(ele
);
113 ele
= document
.createElement("typedef");
114 ele
.setAttribute("resource", "cpptasks.types");
115 root
.appendChild(ele
);
116 ele
= document
.createElement("taskdef");
117 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
118 root
.appendChild(ele
);
121 // Generate the default target,
122 // which depends on init, sections and output target
124 root
.appendChild(document
.createComment("Default target"));
125 ele
= document
.createElement("target");
126 ele
.setAttribute("name", "all");
127 ele
.setAttribute("depends", "libraries, sourcefiles, sections, output");
128 root
.appendChild(ele
);
131 // compile all source files
133 root
.appendChild(document
.createComment("Compile all dependency Library instances."));
134 ele
= document
.createElement("target");
135 ele
.setAttribute("name", "libraries");
138 // Parse all sourfiles but files specified in sections
140 applyLibraryInstance(document
, ele
);
141 root
.appendChild(ele
);
144 // compile all source files
146 root
.appendChild(document
.createComment("sourcefiles target"));
147 ele
= document
.createElement("target");
148 ele
.setAttribute("name", "sourcefiles");
151 // Parse all sourfiles but files specified in sections
153 applyCompileElement(document
, ele
);
154 root
.appendChild(ele
);
157 // generate the init target
158 // main purpose is create all nessary pathes
159 // generate the sections target
161 root
.appendChild(document
.createComment("sections target"));
162 ele
= document
.createElement("target");
163 ele
.setAttribute("name", "sections");
164 applySectionsElement(document
, ele
, fp
);
165 root
.appendChild(ele
);
168 // generate the output target
170 root
.appendChild(document
.createComment("output target"));
171 ele
= document
.createElement("target");
172 ele
.setAttribute("name", "output");
173 applyOutputElement(document
, ele
, fp
);
174 root
.appendChild(ele
);
178 // generate the clean target
180 root
.appendChild(document
.createComment("clean target"));
181 ele
= document
.createElement("target");
182 ele
.setAttribute("name", "clean");
183 applyCleanElement(document
, ele
);
184 root
.appendChild(ele
);
187 // generate the Clean All target
189 root
.appendChild(document
.createComment("Clean All target"));
190 ele
= document
.createElement("target");
191 ele
.setAttribute("name", "cleanall");
192 applyDeepcleanElement(document
, ele
);
193 root
.appendChild(ele
);
196 // add the root element to the document
198 document
.appendChild(rootComment
);
199 document
.appendChild(root
);
201 // Prepare the DOM document for writing
203 Source source
= new DOMSource(document
);
206 // Prepare the output file
208 File file
= new File(buildFilename
);
211 // generate all directory path
213 (new File(file
.getParent())).mkdirs();
214 Result result
= new StreamResult(file
);
217 // Write the DOM document to the file
219 Transformer xformer
= TransformerFactory
.newInstance().newTransformer();
220 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
221 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
222 xformer
.transform(source
, result
);
223 } catch (Exception ex
) {
224 throw new BuildException("Module [" + fpdModuleId
.getModule().getName() + "] generating build file failed.\n" + ex
.getMessage());
229 Generate the clean elements for BaseName_build.xml.
231 @param document current BaseName_build.xml XML document
232 @param root Root element for current
234 private void applyCleanElement(Document document
, Node root
) {
235 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
236 for (int i
= 0; i
< libinstances
.length
; i
++) {
238 // Put package file path to module identification
240 PackageIdentification packageId
= libinstances
[i
].getPackage();
243 // Generate ANT script to clean
245 Element ele
= document
.createElement("GenBuild");
246 ele
.setAttribute("type", "clean");
249 // Prepare pass down information
251 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
252 for (int j
= 0; j
< inheritProperties
.length
; j
++){
253 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
255 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
256 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
258 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
259 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
261 for (int j
= 0; j
< inheritProperties
.length
; j
++){
262 Element property
= document
.createElement("property");
263 property
.setAttribute("name", inheritProperties
[j
]);
264 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
265 ele
.appendChild(property
);
268 root
.appendChild(ele
);
271 // <delete includeemptydirs="true">
272 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
275 Element deleteEle
= document
.createElement("delete");
276 deleteEle
.setAttribute("includeemptydirs", "true");
277 Element filesetEle
= document
.createElement("fileset");
278 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
279 filesetEle
.setAttribute("includes", "**/*");
280 filesetEle
.setAttribute("excludes", "*.xml");
281 deleteEle
.appendChild(filesetEle
);
282 root
.appendChild(deleteEle
);
286 Generate the cleanall elements for BaseName_build.xml.
288 @param document current BaseName_build.xml XML document
289 @param root Root element for current
291 private void applyDeepcleanElement(Document document
, Node root
) {
292 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
293 for (int i
= 0; i
< libinstances
.length
; i
++) {
295 // Put package file path to module identification
297 PackageIdentification packageId
= libinstances
[i
].getPackage();
300 // Generate ANT script to clean
302 Element ele
= document
.createElement("GenBuild");
303 ele
.setAttribute("type", "cleanall");
306 // Prepare pass down information
308 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
309 for (int j
= 0; j
< inheritProperties
.length
; j
++){
310 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
313 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
314 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
316 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
317 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
319 for (int j
= 0; j
< inheritProperties
.length
; j
++){
320 Element property
= document
.createElement("property");
321 property
.setAttribute("name", inheritProperties
[j
]);
322 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
323 ele
.appendChild(property
);
326 root
.appendChild(ele
);
329 // <delete includeemptydirs="true">
330 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
333 Element deleteEle
= document
.createElement("delete");
334 deleteEle
.setAttribute("includeemptydirs", "true");
335 Element filesetEle
= document
.createElement("fileset");
336 filesetEle
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
337 filesetEle
.setAttribute("includes", "**/*");
338 filesetEle
.setAttribute("excludes", "*.xml");
339 deleteEle
.appendChild(filesetEle
);
340 root
.appendChild(deleteEle
);
343 // <delete includeemptydirs="true">
344 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
347 deleteEle
= document
.createElement("delete");
348 deleteEle
.setAttribute("includeemptydirs", "true");
349 filesetEle
= document
.createElement("fileset");
350 filesetEle
.setAttribute("dir", "${DEST_DIR_DEBUG}");
351 filesetEle
.setAttribute("includes", "**/*");
352 deleteEle
.appendChild(filesetEle
);
353 root
.appendChild(deleteEle
);
357 Generate the dependent library instances elements for BaseName_build.xml.
359 @param document current BaseName_build.xml XML document
360 @param root Root element for current
362 private void applyLibraryInstance(Document document
, Node root
) {
363 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
364 // String propertyLibs = "";
365 for (int i
= 0; i
< libinstances
.length
; i
++) {
367 // Put package file path to module identification
369 PackageIdentification packageId
= libinstances
[i
].getPackage();
372 // Generate ANT script to build library instances
374 Element ele
= document
.createElement("GenBuild");
375 ele
.setAttribute("type", "build");
376 // ele.setAttribute("inheritAll", "false");
379 // Prepare pass down information
381 Map
<String
, String
> passDownMap
= new LinkedHashMap
<String
, String
>();
382 for (int j
= 0; j
< inheritProperties
.length
; j
++){
383 passDownMap
.put(inheritProperties
[j
], "${" + inheritProperties
[j
] + "}");
386 passDownMap
.put("MODULE_GUID", libinstances
[i
].getGuid());
387 passDownMap
.put("MODULE_VERSION", libinstances
[i
].getVersion());
389 passDownMap
.put("PACKAGE_GUID", packageId
.getGuid());
390 passDownMap
.put("PACKAGE_VERSION", packageId
.getVersion());
392 for (int j
= 0; j
< inheritProperties
.length
; j
++){
393 Element property
= document
.createElement("property");
394 property
.setAttribute("name", inheritProperties
[j
]);
395 property
.setAttribute("value", passDownMap
.get(inheritProperties
[j
]));
396 ele
.appendChild(property
);
399 root
.appendChild(ele
);
400 // propertyLibs += " " + project.getProperty("BIN_DIR") + File.separatorChar + libinstances[i].getName() + ".lib";
402 // project.setProperty("LIBS", propertyLibs.replaceAll("(\\\\)", "/"));
406 Generate the build source files elements for BaseName_build.xml.
408 @param document current BaseName_build.xml XML document
409 @param root Root element for current
411 private void applyCompileElement(Document document
, Node root
) {
413 // Prepare the includes: PackageDependencies and Output debug direactory
415 Set
<String
> includes
= new LinkedHashSet
<String
>();
420 includes
.add("${WORKSPACE_DIR}");
425 includes
.add("${MODULE_DIR}");
426 includes
.add("${MODULE_DIR}" + File
.separatorChar
+ "${ARCH}");
429 // Packages in PackageDenpendencies
431 PackageIdentification
[] packageDependencies
= SurfaceAreaQuery
.getDependencePkg(fpdModuleId
.getArch());
432 for (int i
= 0; i
< packageDependencies
.length
; i
++) {
433 GlobalData
.refreshPackageIdentification(packageDependencies
[i
]);
434 File packageFile
= packageDependencies
[i
].getSpdFile();
435 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include");
436 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include" + File
.separatorChar
+ "${ARCH}");
440 // All Dependency Library Instance's PackageDependencies
442 ModuleIdentification
[] libinstances
= SurfaceAreaQuery
.getLibraryInstance(fpdModuleId
.getArch());
443 for (int i
= 0; i
< libinstances
.length
; i
++) {
444 SurfaceAreaQuery
.push(GlobalData
.getDoc(libinstances
[i
], fpdModuleId
.getArch()));
445 PackageIdentification
[] libraryPackageDependencies
= SurfaceAreaQuery
.getDependencePkg(fpdModuleId
.getArch());
446 for (int j
= 0; j
< libraryPackageDependencies
.length
; j
++) {
447 GlobalData
.refreshPackageIdentification(libraryPackageDependencies
[j
]);
448 File packageFile
= libraryPackageDependencies
[j
].getSpdFile();
449 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include");
450 includes
.add(packageFile
.getParent() + File
.separatorChar
+ "Include" + File
.separatorChar
+ "${ARCH}");
452 SurfaceAreaQuery
.pop();
457 // The package which the module belongs to
459 includes
.add(fpdModuleId
.getModule().getPackage().getPackageDir() + File
.separatorChar
+ "Include");
460 includes
.add(fpdModuleId
.getModule().getPackage().getPackageDir() + File
.separatorChar
+ "Include" + File
.separatorChar
+ "${ARCH}");
463 // Debug files output directory
465 includes
.add("${DEST_DIR_DEBUG}");
468 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
470 String
[][] sourceFiles
= SurfaceAreaQuery
.getSourceFiles(fpdModuleId
.getArch());
472 FileProcess fileProcess
= new FileProcess();
473 fileProcess
.init(project
, includes
, document
);
475 String moduleDir
= project
.getProperty("MODULE_DIR");
477 // Parse all Unicode files
479 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
481 // Go through all source files. Add MODULE_DIR to preffix
483 File sourceFile
= new File(moduleDir
+ File
.separatorChar
+ sourceFiles
[i
][1]);
484 sourceFiles
[i
][1] = sourceFile
.getPath();
485 String filetype
= sourceFiles
[i
][0];
486 if (filetype
!= null) {
487 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, true);
489 fileProcess
.parseFile(sourceFiles
[i
][1], root
, true);
494 // If exist Unicode file
496 if (fileProcess
.isUnicodeExist()) {
497 Element ele
= document
.createElement("Build_Unicode_Database");
498 ele
.setAttribute("FILEPATH", ".");
499 ele
.setAttribute("FILENAME", "${BASE_NAME}");
500 String
[] includePaths
= includes
.toArray(new String
[includes
.size()]);
501 Element includesEle
= document
.createElement("EXTRA.INC");
502 for (int i
= 0; i
< includePaths
.length
; i
++) {
503 Element includeEle
= document
.createElement("includepath");
504 includeEle
.setAttribute("path", includePaths
[i
]);
505 includesEle
.appendChild(includeEle
);
507 ele
.appendChild(includesEle
);
508 root
.appendChild(ele
);
512 // Parse AutoGen.c & AutoGen.h
514 if ( ! fpdModuleId
.getModule().getName().equalsIgnoreCase("Shell")) {
515 fileProcess
.parseFile(project
.getProperty("DEST_DIR_DEBUG") + File
.separatorChar
+ "AutoGen.c", root
, false);
519 // Parse all source files but Unicode files
521 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
522 String filetype
= sourceFiles
[i
][0];
523 if (filetype
!= null) {
524 fileProcess
.parseFile(sourceFiles
[i
][1], filetype
, root
, false);
526 fileProcess
.parseFile(sourceFiles
[i
][1], root
, false);
531 // Initialize SOURCE_FILES for dependcy check use
534 for (int i
= 0; i
< sourceFiles
.length
; i
++) {
535 str
+= " " + sourceFiles
[i
][1];
537 project
.setProperty("SOURCE_FILES", str
.replaceAll("(\\\\)", "/"));
541 Generate the section elements for BaseName_build.xml. Library module will
544 @param document current BaseName_build.xml XML document
545 @param root Root element for current
547 private void applySectionsElement(Document document
, Node root
, FfsProcess fp
) {
548 if (fpdModuleId
.getModule().isLibrary()) {
551 if (fp
.initSections(ffsKeyword
, project
, fpdModuleId
)) {
552 String targetFilename
= fpdModuleId
.getModule().getGuid() + "-" + fpdModuleId
.getModule().getName() + FpdParserTask
.getSuffix(fpdModuleId
.getModule().getModuleType());
553 String
[] list
= fp
.getGenSectionElements(document
, "${BASE_NAME}", fpdModuleId
.getModule().getGuid(), targetFilename
);
555 for (int i
= 0; i
< list
.length
; i
++) {
556 Element ele
= document
.createElement(list
[i
]);
557 ele
.setAttribute("FILEPATH", ".");
558 ele
.setAttribute("FILENAME", "${BASE_NAME}");
559 root
.appendChild(ele
);
565 Generate the output elements for BaseName_build.xml. If module is library,
566 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
568 @param document current BaseName_build.xml XML document
569 @param root Root element for current
571 private void applyOutputElement(Document document
, Node root
, FfsProcess fp
) {
572 if (fpdModuleId
.getModule().isLibrary()) {
576 Element cc
= document
.createElement("Build_Library");
577 cc
.setAttribute("FILENAME", fpdModuleId
.getModule().getName());
578 root
.appendChild(cc
);
581 // if it is a module but library
584 if (fp
.getFfsNode() != null) {
585 root
.appendChild(fp
.getFfsNode());