2 This file is an ANT task.
4 LibBuildFileGenerator task is used to generate module's build.xml file.
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 package org
.tianocore
.build
.global
;
19 import java
.util
.HashMap
;
20 import java
.util
.Iterator
;
23 import java
.util
.Vector
;
25 import javax
.xml
.parsers
.DocumentBuilder
;
26 import javax
.xml
.parsers
.DocumentBuilderFactory
;
27 import javax
.xml
.transform
.OutputKeys
;
28 import javax
.xml
.transform
.Result
;
29 import javax
.xml
.transform
.Source
;
30 import javax
.xml
.transform
.Transformer
;
31 import javax
.xml
.transform
.TransformerFactory
;
32 import javax
.xml
.transform
.dom
.DOMSource
;
33 import javax
.xml
.transform
.stream
.StreamResult
;
35 import org
.apache
.tools
.ant
.BuildException
;
36 import org
.apache
.tools
.ant
.Task
;
37 import org
.apache
.xmlbeans
.XmlObject
;
38 import org
.tianocore
.MsaHeaderDocument
.MsaHeader
;
39 import org
.tianocore
.MsaLibHeaderDocument
.MsaLibHeader
;
40 import org
.w3c
.dom
.Document
;
41 import org
.w3c
.dom
.Element
;
44 This class <code>LibBuildFileGenerator</code> is an ANT task to generate
45 build.xml for each module. Here are two usages.
49 For one module (<b>bf</b> is LibBuildFileGenerator task name):
51 <bf buildFile="Application\HelloWorld\HelloWorld.msa" />
57 <bf recursive="true" />
64 public class LibBuildFileGenerator
extends Task
{
66 private File buildFile
;
68 private boolean recursive
= false;
70 private String license
= " Copyright (c) 2006, Intel Corporation \n"
71 + "All rights reserved. This program and the accompanying materials \n"
72 + "are licensed and made available under the terms and conditions of the BSD License \n"
73 + "which accompanies this distribution. The full text of the license may be found at \n"
74 + "http://opensource.org/licenses/bsd-license.php \n"
76 + "THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS, \n"
77 + "WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.";
79 private String base_name
;
81 private String module_relative_path
;
83 private File base_file
= new File(".");
86 Public construct method. It is necessary for ANT task.
88 public LibBuildFileGenerator () {
92 ANT task's entry point, will be called after init().
94 @throws BuildException
95 buildFile do not specify while recursive set to false
97 public void execute() throws BuildException
{
99 searchMsa(new File("."));
102 Map
<String
,File
> map
= new HashMap
<String
,File
>();
103 String basename
= buildFile
.getName();
104 int k
= basename
.lastIndexOf('.');
105 base_name
= basename
.substring(0, k
);
106 map
.put(base_name
, buildFile
);
112 Recursivly find all MSA files and record all modules.
114 @param path Package path
116 private void searchMsa(File path
){
117 File
[] files
= path
.listFiles();
118 Vector
<File
> vec
= new Vector
<File
>();
119 for(int i
=0; i
< files
.length
; i
++){
120 if (files
[i
].isFile()){
121 if(files
[i
].getName().endsWith(".msa")){
122 System
.out
.println("#" + files
[i
].getPath());
127 Map
<String
,File
> mapBasename
= new HashMap
<String
,File
>();
130 for ( int j
= 0 ; j
< vec
.size(); j
++){
131 if ( vec
.size() > 1){
132 System
.out
.println("##" + vec
.get(0));
134 File f
= (File
)vec
.get(j
);
135 SurfaceAreaParser surfaceAreaParser
= new SurfaceAreaParser();
136 Map
<String
, XmlObject
> map
= surfaceAreaParser
.parseFile(f
);
137 String baseName
= "";
138 XmlObject header
= null;
139 if ( (header
= map
.get("MsaHeader")) != null ){
140 baseName
= ((MsaHeader
)header
).getBaseName().getStringValue();
142 else if ( (header
= map
.get("MsaLibHeader")) != null){
143 baseName
= ((MsaLibHeader
)header
).getBaseName().getStringValue();
147 if ( base_name
== null || base_name
.length() > baseName
.length()){
148 base_name
= baseName
;
151 module_relative_path
= buildFile
.getParent().substring(base_file
.getPath().length() + 1);
154 module_relative_path
= ".";
157 mapBasename
.put(baseName
, f
);
159 genBuildFile(mapBasename
);
162 for(int i
=0; i
< files
.length
; i
++){
163 if (files
[i
].isDirectory()){
172 @param map All base name under one module directory
174 private void genBuildFile(Map map
) {
175 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
177 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
178 Document document
= dombuilder
.newDocument();
180 // create root element and its attributes
182 document
.appendChild(document
.createComment(license
));
183 Element root
= document
.createElement("project");
184 root
.setAttribute("default", base_name
);
185 root
.setAttribute("basedir", ".");
187 // element for External ANT tasks
189 root
.appendChild(document
.createComment("Apply external ANT tasks"));
190 Element ele
= document
.createElement("taskdef");
191 ele
.setAttribute("resource", "GenBuild.tasks");
192 root
.appendChild(ele
);
194 // <taskdef resource="net/sf/antcontrib/antlib.xml" />
196 ele
= document
.createElement("taskdef");
197 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
198 root
.appendChild(ele
);
200 ele
= document
.createElement("property");
201 ele
.setAttribute("environment", "env");
202 root
.appendChild(ele
);
204 ele
= document
.createElement("property");
205 ele
.setAttribute("name", "WORKSPACE_DIR");
206 ele
.setAttribute("value", "${env.WORKSPACE}");
207 root
.appendChild(ele
);
209 ele
= document
.createElement("import");
210 ele
.setAttribute("file", "${WORKSPACE_DIR}"+File
.separatorChar
+"Tools"+File
.separatorChar
+"Conf"+File
.separatorChar
+"BuildMacro.xml");
211 root
.appendChild(ele
);
213 root
.appendChild(document
.createComment("MODULE_RELATIVE PATH is relative to PACKAGE_DIR"));
214 ele
= document
.createElement("property");
215 ele
.setAttribute("name", "MODULE_RELATIVE_PATH");
216 ele
.setAttribute("value", module_relative_path
);
217 root
.appendChild(ele
);
219 ele
= document
.createElement("property");
220 ele
.setAttribute("name", "MODULE_DIR");
221 ele
.setAttribute("value", "${PACKAGE_DIR}" + File
.separatorChar
+ "${MODULE_RELATIVE_PATH}");
222 root
.appendChild(ele
);
224 ele
= document
.createElement("property");
225 ele
.setAttribute("name", "COMMON_FILE");
226 ele
.setAttribute("value", "${WORKSPACE_DIR}" + File
.separatorChar
+ "Tools"
227 + File
.separatorChar
+ "Conf" + File
.separatorChar
+ "Common.xml");
228 root
.appendChild(ele
);
231 // generate the buildfmd target
233 Set set
= map
.keySet();
234 Iterator iter
= set
.iterator();
235 while (iter
.hasNext()){
236 String bName
= (String
)iter
.next();
237 File msaFile
= (File
)map
.get(bName
);
238 String msaFilename
= "${MODULE_DIR}" + File
.separatorChar
+ msaFile
.getName();
239 String mbdFilename
= msaFilename
.substring(0 , msaFilename
.length() - 4) + ".mbd";
240 ele
= document
.createElement("target");
241 ele
.setAttribute("name", bName
);
242 Element target
= document
.createElement("GenBuild");
243 target
.setAttribute("msaFilename", msaFilename
);
244 target
.setAttribute("mbdFilename", mbdFilename
);
245 target
.setAttribute("baseName", bName
);
246 ele
.appendChild(target
);
247 root
.appendChild(ele
);
250 root
.appendChild(ele
);
254 ele
= document
.createElement("target");
255 ele
.setAttribute("name", "clean");
256 ele
.setAttribute("depends", base_name
+ "_clean");
257 root
.appendChild(ele
);
261 ele
= document
.createElement("target");
262 ele
.setAttribute("name", "cleanall");
263 ele
.setAttribute("depends", base_name
+ "_cleanall");
264 root
.appendChild(ele
);
266 // Every clean target for each BaseName
269 iter
= set
.iterator();
270 while (iter
.hasNext()){
271 String bName
= (String
)iter
.next();
272 File msaFile
= (File
)map
.get(bName
);
273 String msaFilename
= "${MODULE_DIR}" + File
.separatorChar
+ msaFile
.getName();
274 String mbdFilename
= msaFilename
.substring(0 , msaFilename
.length() - 4) + ".mbd";
276 ele
= document
.createElement("target");
277 ele
.setAttribute("name", bName
+ "_clean");
281 Element target
= document
.createElement("OutputDirSetup");
282 target
.setAttribute("msaFilename", msaFilename
);
283 target
.setAttribute("mbdFilename", mbdFilename
);
284 target
.setAttribute("baseName", bName
);
285 ele
.appendChild(target
);
287 // Call BaseName_build.xml clean
289 Element ifEle
= document
.createElement("if");
290 Element availableEle
= document
.createElement("available");
291 availableEle
.setAttribute("file", "${DEST_DIR_OUTPUT}" + File
.separatorChar
+ bName
+ "_build.xml");
292 ifEle
.appendChild(availableEle
);
293 Element elseEle
= document
.createElement("then");
295 Element moduleEle
= document
.createElement("ant");
296 moduleEle
.setAttribute("antfile", "${DEST_DIR_OUTPUT}" + File
.separatorChar
+ bName
+ "_build.xml");
297 moduleEle
.setAttribute("target", "clean");
299 elseEle
.appendChild(moduleEle
);
300 ifEle
.appendChild(elseEle
);
301 ele
.appendChild(ifEle
);
305 Element clean
= document
.createElement("delete");
306 clean
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
307 clean
.setAttribute("excludes", "*.xml");
308 ele
.appendChild(clean
);
310 root
.appendChild(ele
);
313 // Every Clean ALl target for each BaseName
316 iter
= set
.iterator();
317 while (iter
.hasNext()){
318 String bName
= (String
)iter
.next();
319 File msaFile
= (File
)map
.get(bName
);
320 String msaFilename
= "${MODULE_DIR}" + File
.separatorChar
+ msaFile
.getName();
321 String mbdFilename
= msaFilename
.substring(0 , msaFilename
.length() - 4) + ".mbd";
323 ele
= document
.createElement("target");
324 ele
.setAttribute("name", bName
+ "_cleanall");
328 Element target
= document
.createElement("OutputDirSetup");
329 target
.setAttribute("msaFilename", msaFilename
);
330 target
.setAttribute("mbdFilename", mbdFilename
);
331 target
.setAttribute("baseName", bName
);
332 ele
.appendChild(target
);
334 // Call BaseName_build.xml clean
336 Element ifEle
= document
.createElement("if");
337 Element availableEle
= document
.createElement("available");
338 availableEle
.setAttribute("file", "${DEST_DIR_OUTPUT}" + File
.separatorChar
+ bName
+ "_build.xml");
339 ifEle
.appendChild(availableEle
);
340 Element elseEle
= document
.createElement("then");
342 Element moduleEle
= document
.createElement("ant");
343 moduleEle
.setAttribute("antfile", "${DEST_DIR_OUTPUT}" + File
.separatorChar
+ bName
+ "_build.xml");
344 moduleEle
.setAttribute("target", "cleanall");
346 elseEle
.appendChild(moduleEle
);
347 ifEle
.appendChild(elseEle
);
348 ele
.appendChild(ifEle
);
352 Element clean
= document
.createElement("delete");
353 clean
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
354 ele
.appendChild(clean
);
356 clean
= document
.createElement("delete");
357 clean
.setAttribute("dir", "${DEST_DIR_DEBUG}");
358 ele
.appendChild(clean
);
360 clean
= document
.createElement("delete");
361 Element fileset
= document
.createElement("fileset");
362 fileset
.setAttribute("dir", "${BIN_DIR}");
363 fileset
.setAttribute("includes", "**" + bName
+ "*");
364 clean
.appendChild(fileset
);
365 ele
.appendChild(clean
);
367 root
.appendChild(ele
);
369 document
.appendChild(root
);
371 // Prepare the DOM document for writing
373 Source source
= new DOMSource(document
);
375 // Prepare the output file
377 String filename
= buildFile
.getParent() + File
.separatorChar
+ "build.xml";
378 File file
= new File(getProject().replaceProperties(filename
));
380 // generate all directory path
382 Result result
= new StreamResult(file
);
384 // Write the DOM document to the file
386 Transformer xformer
= TransformerFactory
.newInstance()
388 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
389 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
390 xformer
.transform(source
, result
);
391 } catch (Exception ex
) {
392 System
.out
.println("##" + ex
);
397 public File
getBuildFile() {
401 public void setBuildFile(File buildFile
) {
402 this.buildFile
= buildFile
;
405 public boolean isRecursive() {
409 public void setRecursive(boolean recursive
) {
410 this.recursive
= recursive
;