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
.tools
;
19 import java
.util
.Iterator
;
20 import java
.util
.LinkedHashSet
;
23 import javax
.xml
.parsers
.DocumentBuilder
;
24 import javax
.xml
.parsers
.DocumentBuilderFactory
;
25 import javax
.xml
.transform
.OutputKeys
;
26 import javax
.xml
.transform
.Result
;
27 import javax
.xml
.transform
.Source
;
28 import javax
.xml
.transform
.Transformer
;
29 import javax
.xml
.transform
.TransformerFactory
;
30 import javax
.xml
.transform
.dom
.DOMSource
;
31 import javax
.xml
.transform
.stream
.StreamResult
;
33 import org
.apache
.tools
.ant
.BuildException
;
34 import org
.apache
.tools
.ant
.Task
;
35 import org
.tianocore
.build
.global
.GlobalData
;
36 import org
.tianocore
.build
.id
.ModuleIdentification
;
37 import org
.tianocore
.build
.id
.PackageIdentification
;
38 import org
.w3c
.dom
.Document
;
39 import org
.w3c
.dom
.Element
;
41 public class DefaultBuildFileGenerator
extends Task
{
43 private Set
<ModuleIdentification
> modules
= new LinkedHashSet
<ModuleIdentification
>();
45 private Set
<PackageIdentification
> packages
= new LinkedHashSet
<PackageIdentification
>();
48 // <DefaultBuildFileGenerator mode="WORKSPACE | PACKAGE | MODULE">
49 // <PackageItem packageName="" packageGuid="" packageVersion="" />
50 // <ModuleItem moduleName="HelloWorld" moduleGuid="" moduleVersion="" packageName="" packageGuid="" packageVersion="" />
51 // </DefaultBuildFileGenerator>
53 private String mode
= "MODULE";
55 private String license
= " Copyright (c) 2006, Intel Corporation \n"
56 + "All rights reserved. This program and the accompanying materials \n"
57 + "are licensed and made available under the terms and conditions of the BSD License \n"
58 + "which accompanies this distribution. The full text of the license may be found at \n"
59 + "http://opensource.org/licenses/bsd-license.php \n"
61 + "THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN \"AS IS\" BASIS, \n"
62 + "WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.";
65 Public construct method. It is necessary for ANT task.
67 public DefaultBuildFileGenerator () {
70 public void execute() throws BuildException
{
72 // Global Data initialization
74 GlobalData
.initInfo("Tools" + File
.separatorChar
+ "Conf" + File
.separatorChar
+ "FrameworkDatabase.db",
75 getProject().getProperty("WORKSPACE_DIR"), "tools_def.txt");
77 if (mode
.equalsIgnoreCase("WORKSPACE")) {
79 packages
= GlobalData
.getPackageList();
81 else if (mode
.equalsIgnoreCase("PACKAGE")) {
84 if (mode
.equalsIgnoreCase("WORKSPACE") || mode
.equalsIgnoreCase("PACKAGE")) {
85 Iterator iter
= packages
.iterator();
86 while (iter
.hasNext()) {
87 PackageIdentification packageId
= (PackageIdentification
)iter
.next();
88 modules
.addAll(GlobalData
.getModules(packageId
));
92 Iterator iter
= modules
.iterator();
93 while (iter
.hasNext()) {
94 ModuleIdentification moduleId
= (ModuleIdentification
)iter
.next();
95 genBuildFile (moduleId
);
99 private void genBuildFile(ModuleIdentification moduleId
) {
100 DocumentBuilderFactory domfac
= DocumentBuilderFactory
.newInstance();
102 DocumentBuilder dombuilder
= domfac
.newDocumentBuilder();
103 Document document
= dombuilder
.newDocument();
105 // create root element and its attributes
107 document
.appendChild(document
.createComment(license
));
108 Element root
= document
.createElement("project");
109 root
.setAttribute("default", "all");
110 root
.setAttribute("basedir", ".");
111 root
.setAttribute("name", moduleId
.getName());
114 // element for External ANT tasks
116 root
.appendChild(document
.createComment("Apply external ANT tasks"));
117 Element ele
= document
.createElement("taskdef");
118 ele
.setAttribute("resource", "GenBuild.tasks");
119 root
.appendChild(ele
);
122 // <taskdef resource="net/sf/antcontrib/antlib.xml" />
124 ele
= document
.createElement("taskdef");
125 ele
.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
126 root
.appendChild(ele
);
128 ele
= document
.createElement("property");
129 ele
.setAttribute("environment", "env");
130 root
.appendChild(ele
);
132 ele
= document
.createElement("property");
133 ele
.setAttribute("name", "WORKSPACE_DIR");
134 ele
.setAttribute("value", "${env.WORKSPACE}");
135 root
.appendChild(ele
);
137 ele
= document
.createElement("property");
138 ele
.setAttribute("name", "MSA_FILENAME");
139 ele
.setAttribute("value", GlobalData
.getMsaFile(moduleId
).getName());
140 root
.appendChild(ele
);
142 ele
= document
.createElement("property");
143 ele
.setAttribute("name", "BASE_NAME");
144 ele
.setAttribute("value", moduleId
.getName());
145 root
.appendChild(ele
);
150 ele
= document
.createElement("import");
151 ele
.setAttribute("file", "${WORKSPACE_DIR}/Tools/Conf/BuildMacro.xml");
152 root
.appendChild(ele
);
155 // <target name="all">
156 // <GenBuild msaFile="HelloWorld.msa"/>
159 Element targetEle
= document
.createElement("target");
160 targetEle
.setAttribute("name", "all");
162 ele
= document
.createElement("GenBuild");
163 ele
.setAttribute("msaFile", "${MSA_FILENAME}");
164 targetEle
.appendChild(ele
);
166 root
.appendChild(targetEle
);
169 // <target name="clean">
170 // <OutputDirSetup msaFile="HelloWorld.msa"/>
172 // <available file="${DEST_DIR_OUTPUT}/HelloWorld_build.xml"/>
174 // <ant antfile="${DEST_DIR_OUTPUT}/HelloWorld_build.xml" target="clean"/>
177 // <delete dir="${DEST_DIR_OUTPUT}" excludes="*.xml"/>
180 targetEle
= document
.createElement("target");
181 targetEle
.setAttribute("name", "clean");
183 ele
= document
.createElement("OutputDirSetup");
184 ele
.setAttribute("msaFile", "${MSA_FILENAME}");
185 targetEle
.appendChild(ele
);
187 ele
= document
.createElement("if");
189 Element availableEle
= document
.createElement("available");
190 availableEle
.setAttribute("file", "${DEST_DIR_OUTPUT}/${BASE_NAME}_build.xml");
191 ele
.appendChild(availableEle
);
193 Element thenEle
= document
.createElement("then");
194 Element antEle
= document
.createElement("ant");
195 antEle
.setAttribute("antfile", "${DEST_DIR_OUTPUT}/${BASE_NAME}_build.xml");
196 antEle
.setAttribute("target", "clean");
197 thenEle
.appendChild(antEle
);
198 ele
.appendChild(thenEle
);
199 targetEle
.appendChild(ele
);
201 ele
= document
.createElement("delete");
202 ele
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
203 ele
.setAttribute("excludes", "*.xml");
204 targetEle
.appendChild(ele
);
206 root
.appendChild(targetEle
);
209 // <target name="cleanall">
210 // <OutputDirSetup msaFile="HelloWorld.msa"/>
212 // <available file="${DEST_DIR_OUTPUT}/HelloWorld_build.xml"/>
214 // <ant antfile="${DEST_DIR_OUTPUT}/HelloWorld_build.xml" target="cleanall"/>
217 // <delete dir="${DEST_DIR_OUTPUT}"/>
218 // <delete dir="${DEST_DIR_DEBUG}"/>
220 // <fileset dir="${BIN_DIR}" includes="**HelloWorld*"/>
224 targetEle
= document
.createElement("target");
225 targetEle
.setAttribute("name", "cleanall");
227 ele
= document
.createElement("OutputDirSetup");
228 ele
.setAttribute("msaFile", "${MSA_FILENAME}");
229 targetEle
.appendChild(ele
);
231 ele
= document
.createElement("if");
233 availableEle
= document
.createElement("available");
234 availableEle
.setAttribute("file", "${DEST_DIR_OUTPUT}/${BASE_NAME}_build.xml");
235 ele
.appendChild(availableEle
);
237 thenEle
= document
.createElement("then");
238 antEle
= document
.createElement("ant");
239 antEle
.setAttribute("antfile", "${DEST_DIR_OUTPUT}/${BASE_NAME}_build.xml");
240 antEle
.setAttribute("target", "cleanall");
241 thenEle
.appendChild(antEle
);
242 ele
.appendChild(thenEle
);
243 targetEle
.appendChild(ele
);
245 ele
= document
.createElement("delete");
246 ele
.setAttribute("dir", "${DEST_DIR_OUTPUT}");
247 targetEle
.appendChild(ele
);
249 ele
= document
.createElement("delete");
250 ele
.setAttribute("dir", "${DEST_DIR_DEBUG}");
251 targetEle
.appendChild(ele
);
253 ele
= document
.createElement("delete");
255 Element filesetEle
= document
.createElement("fileset");
256 filesetEle
.setAttribute("dir", "${BIN_DIR}");
257 filesetEle
.setAttribute("includes", "**${BASE_NAME}*");
258 ele
.appendChild(filesetEle
);
260 targetEle
.appendChild(ele
);
262 root
.appendChild(targetEle
);
265 document
.appendChild(root
);
268 // Prepare the DOM document for writing
270 Source source
= new DOMSource(document
);
273 // Prepare the output file
275 String filename
= GlobalData
.getMsaFile(moduleId
).getParent() + File
.separatorChar
+ "build.xml";
276 File file
= new File(getProject().replaceProperties(filename
));
278 GlobalData
.log
.info("File generating - " + filename
);
280 // generate all directory path
282 Result result
= new StreamResult(file
);
285 // Write the DOM document to the file
287 Transformer xformer
= TransformerFactory
.newInstance()
289 xformer
.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
290 xformer
.setOutputProperty(OutputKeys
.INDENT
, "yes");
291 xformer
.transform(source
, result
);
292 } catch (Exception ex
) {
293 System
.out
.println("##" + ex
);
298 public void addConfiguredModuleItem(ModuleItem moduleItem
) {
299 PackageIdentification packageId
= new PackageIdentification(moduleItem
.getPackageName(), moduleItem
.getPackageGuid(), moduleItem
.getPackageVersion());
300 ModuleIdentification moduleId
= new ModuleIdentification(moduleItem
.getModuleName(), moduleItem
.getModuleGuid(), moduleItem
.getModuleVersion());
301 moduleId
.setPackage(packageId
);
302 modules
.add(moduleId
);
305 public void addConfiguredPackageItem(PackageItem packageItem
) {
306 PackageIdentification packageId
= new PackageIdentification(packageItem
.getPackageName(), packageItem
.getPackageGuid(), packageItem
.getPackageVersion());
307 packages
.add(packageId
);
310 public void setMode(String mode
) {