]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/tools/DefaultBuildFileGenerator.java
422d5038c20e53ee2ee40a8d0d61a0d43afeb18f
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / tools / DefaultBuildFileGenerator.java
1 /** @file
2 This file is an ANT task.
3
4 LibBuildFileGenerator task is used to generate module's build.xml file.
5
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
11
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.
14
15 **/
16 package org.tianocore.build.tools;
17
18 import java.io.File;
19 import java.util.Iterator;
20 import java.util.LinkedHashSet;
21 import java.util.Set;
22
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;
32
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;
40
41 public class DefaultBuildFileGenerator extends Task {
42
43 private Set<ModuleIdentification> modules = new LinkedHashSet<ModuleIdentification>();
44
45 private Set<PackageIdentification> packages = new LinkedHashSet<PackageIdentification>();
46
47 //
48 // <DefaultBuildFileGenerator mode="WORKSPACE | PACKAGE | MODULE">
49 // <PackageItem packageName="" packageGuid="" packageVersion="" />
50 // <ModuleItem moduleName="HelloWorld" moduleGuid="" moduleVersion="" packageName="" packageGuid="" packageVersion="" />
51 // </DefaultBuildFileGenerator>
52 //
53 private String mode = "MODULE";
54
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"
60 + "\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.";
63
64 /**
65 Public construct method. It is necessary for ANT task.
66 **/
67 public DefaultBuildFileGenerator () {
68 }
69
70 public void execute() throws BuildException {
71 //
72 // Global Data initialization
73 //
74 GlobalData.initInfo("Tools" + File.separatorChar + "Conf" + File.separatorChar + "FrameworkDatabase.db",
75 getProject().getProperty("WORKSPACE_DIR"), "tools_def.txt");
76
77 if (mode.equalsIgnoreCase("WORKSPACE")) {
78 modules.clear();
79 packages = GlobalData.getPackageList();
80 }
81 else if (mode.equalsIgnoreCase("PACKAGE")) {
82 modules.clear();
83 }
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));
89 }
90 }
91
92 Iterator iter = modules.iterator();
93 while (iter.hasNext()) {
94 ModuleIdentification moduleId = (ModuleIdentification)iter.next();
95 genBuildFile (moduleId);
96 }
97 }
98
99 private void genBuildFile(ModuleIdentification moduleId) {
100 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
101 try {
102 DocumentBuilder dombuilder = domfac.newDocumentBuilder();
103 Document document = dombuilder.newDocument();
104 //
105 // create root element and its attributes
106 //
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());
112
113 //
114 // element for External ANT tasks
115 //
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);
120
121 //
122 // <taskdef resource="net/sf/antcontrib/antlib.xml" />
123 //
124 ele = document.createElement("taskdef");
125 ele.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
126 root.appendChild(ele);
127
128 ele = document.createElement("property");
129 ele.setAttribute("environment", "env");
130 root.appendChild(ele);
131
132 ele = document.createElement("property");
133 ele.setAttribute("name", "WORKSPACE_DIR");
134 ele.setAttribute("value", "${env.WORKSPACE}");
135 root.appendChild(ele);
136
137 ele = document.createElement("property");
138 ele.setAttribute("name", "MSA_FILENAME");
139 ele.setAttribute("value", GlobalData.getMsaFile(moduleId).getName());
140 root.appendChild(ele);
141
142 ele = document.createElement("property");
143 ele.setAttribute("name", "BASE_NAME");
144 ele.setAttribute("value", moduleId.getName());
145 root.appendChild(ele);
146
147 //
148 // Don't change it!!
149 //
150 ele = document.createElement("import");
151 ele.setAttribute("file", "${WORKSPACE_DIR}/Tools/Conf/BuildMacro.xml");
152 root.appendChild(ele);
153
154 //
155 // <target name="all">
156 // <GenBuild msaFile="HelloWorld.msa"/>
157 // </target>
158 //
159 Element targetEle = document.createElement("target");
160 targetEle.setAttribute("name", "all");
161
162 ele = document.createElement("GenBuild");
163 ele.setAttribute("msaFile", "${MSA_FILENAME}");
164 targetEle.appendChild(ele);
165
166 root.appendChild(targetEle);
167
168 //
169 // <target name="clean">
170 // <OutputDirSetup msaFile="HelloWorld.msa"/>
171 // <if>
172 // <available file="${DEST_DIR_OUTPUT}/HelloWorld_build.xml"/>
173 // <then>
174 // <ant antfile="${DEST_DIR_OUTPUT}/HelloWorld_build.xml" target="clean"/>
175 // </then>
176 // </if>
177 // <delete dir="${DEST_DIR_OUTPUT}" excludes="*.xml"/>
178 // </target>
179 //
180 targetEle = document.createElement("target");
181 targetEle.setAttribute("name", "clean");
182
183 ele = document.createElement("OutputDirSetup");
184 ele.setAttribute("msaFile", "${MSA_FILENAME}");
185 targetEle.appendChild(ele);
186
187 ele = document.createElement("if");
188
189 Element availableEle = document.createElement("available");
190 availableEle.setAttribute("file", "${DEST_DIR_OUTPUT}/${BASE_NAME}_build.xml");
191 ele.appendChild(availableEle);
192
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);
200
201 ele = document.createElement("delete");
202 ele.setAttribute("dir", "${DEST_DIR_OUTPUT}");
203 ele.setAttribute("excludes", "*.xml");
204 targetEle.appendChild(ele);
205
206 root.appendChild(targetEle);
207
208 //
209 // <target name="cleanall">
210 // <OutputDirSetup msaFile="HelloWorld.msa"/>
211 // <if>
212 // <available file="${DEST_DIR_OUTPUT}/HelloWorld_build.xml"/>
213 // <then>
214 // <ant antfile="${DEST_DIR_OUTPUT}/HelloWorld_build.xml" target="cleanall"/>
215 // </then>
216 // </if>
217 // <delete dir="${DEST_DIR_OUTPUT}"/>
218 // <delete dir="${DEST_DIR_DEBUG}"/>
219 // <delete>
220 // <fileset dir="${BIN_DIR}" includes="**HelloWorld*"/>
221 // </delete>
222 // </target>
223 //
224 targetEle = document.createElement("target");
225 targetEle.setAttribute("name", "cleanall");
226
227 ele = document.createElement("OutputDirSetup");
228 ele.setAttribute("msaFile", "${MSA_FILENAME}");
229 targetEle.appendChild(ele);
230
231 ele = document.createElement("if");
232
233 availableEle = document.createElement("available");
234 availableEle.setAttribute("file", "${DEST_DIR_OUTPUT}/${BASE_NAME}_build.xml");
235 ele.appendChild(availableEle);
236
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);
244
245 ele = document.createElement("delete");
246 ele.setAttribute("dir", "${DEST_DIR_OUTPUT}");
247 targetEle.appendChild(ele);
248
249 ele = document.createElement("delete");
250 ele.setAttribute("dir", "${DEST_DIR_DEBUG}");
251 targetEle.appendChild(ele);
252
253 ele = document.createElement("delete");
254
255 Element filesetEle = document.createElement("fileset");
256 filesetEle.setAttribute("dir", "${BIN_DIR}");
257 filesetEle.setAttribute("includes", "**${BASE_NAME}*");
258 ele.appendChild(filesetEle);
259
260 targetEle.appendChild(ele);
261
262 root.appendChild(targetEle);
263
264
265 document.appendChild(root);
266
267 //
268 // Prepare the DOM document for writing
269 //
270 Source source = new DOMSource(document);
271
272 //
273 // Prepare the output file
274 //
275 String filename = GlobalData.getMsaFile(moduleId).getParent() + File.separatorChar + "build.xml";
276 File file = new File(getProject().replaceProperties(filename));
277
278 GlobalData.log.info("File generating - " + filename);
279 //
280 // generate all directory path
281 //
282 Result result = new StreamResult(file);
283
284 //
285 // Write the DOM document to the file
286 //
287 Transformer xformer = TransformerFactory.newInstance()
288 .newTransformer();
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);
294 }
295
296 }
297
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);
303 }
304
305 public void addConfiguredPackageItem(PackageItem packageItem) {
306 PackageIdentification packageId = new PackageIdentification(packageItem.getPackageName(), packageItem.getPackageGuid(), packageItem.getPackageVersion());
307 packages.add(packageId);
308 }
309
310 public void setMode(String mode) {
311 this.mode = mode;
312 }
313 }