]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/ModuleBuildFileGenerator.java
Backup original properties to avoid property overriding message. This change is for...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / ModuleBuildFileGenerator.java
1 /** @file
2
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
8
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.
11 **/
12 package org.tianocore.build;
13
14 import java.io.File;
15 import java.util.LinkedHashMap;
16 import java.util.Map;
17 import java.io.FileOutputStream;
18 import java.io.OutputStreamWriter;
19
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;
29
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.SurfaceAreaQuery;
34 import org.tianocore.build.id.FpdModuleIdentification;
35 import org.tianocore.build.id.ModuleIdentification;
36 import org.tianocore.build.id.PackageIdentification;
37 import org.w3c.dom.Comment;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
41
42 public class ModuleBuildFileGenerator {
43
44 ///
45 /// Pass: TARGET, TOOLCHAIN, ARCH
46 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION
47 ///
48 String[] inheritProperties = {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PACKAGE_GUID", "PACKAGE_VERSION"};
49
50 ///
51 /// The information at the header of <em>build.xml</em>.
52 ///
53 private String info = "DO NOT EDIT \n"
54 + "This file is auto-generated by the build utility\n"
55 + "\n"
56 + "Abstract:\n"
57 + "Auto-generated ANT build file for build EFI Modules and Platforms\n";
58
59 private FpdModuleIdentification fpdModuleId;
60
61 private Project project;
62
63 private String ffsKeyword;
64
65 private String[] includes;
66
67 private SurfaceAreaQuery saq = null;
68
69 public ModuleBuildFileGenerator(Project project, String ffsKeyword, FpdModuleIdentification fpdModuleId, String[] includes, SurfaceAreaQuery saq) {
70 this.project = project;
71 this.fpdModuleId = fpdModuleId;
72 this.ffsKeyword = ffsKeyword;
73 this.includes = includes;
74 this.saq = saq;
75 }
76
77 /**
78 The whole BaseName_build.xml is composed of seven part.
79 <ul>
80 <li> ANT properties; </li>
81 <li> Dependent module (dependent library instances in most case); </li>
82 <li> Source files; </li>
83 <li> Sections if module is not library; </li>
84 <li> Output (different for library module and driver module); </li>
85 <li> Clean; </li>
86 <li> Clean all. </li>
87 </ul>
88
89 @throws BuildException
90 Error throws during BaseName_build.xml generating.
91 **/
92 public void genBuildFile(String buildFilename) throws BuildException {
93 FfsProcess fp = new FfsProcess();
94 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
95 try {
96 DocumentBuilder dombuilder = domfac.newDocumentBuilder();
97 Document document = dombuilder.newDocument();
98 Comment rootComment = document.createComment(info);
99
100 //
101 // create root element and its attributes
102 //
103 Element root = document.createElement("project");
104 root.setAttribute("name", fpdModuleId.getModule().getName());
105 root.setAttribute("default", "all");
106 root.setAttribute("basedir", ".");
107
108 //
109 // element for External ANT tasks
110 //
111 root.appendChild(document.createComment("Apply external ANT tasks"));
112 Element ele = document.createElement("taskdef");
113 ele.setAttribute("resource", "frameworktasks.tasks");
114 root.appendChild(ele);
115 ele = document.createElement("taskdef");
116 ele.setAttribute("resource", "cpptasks.tasks");
117 root.appendChild(ele);
118 ele = document.createElement("typedef");
119 ele.setAttribute("resource", "cpptasks.types");
120 root.appendChild(ele);
121 ele = document.createElement("taskdef");
122 ele.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
123 root.appendChild(ele);
124
125 //
126 // Generate the default target,
127 // which depends on init, sections and output target
128 //
129 root.appendChild(document.createComment("Default target"));
130 ele = document.createElement("target");
131 ele.setAttribute("name", "all");
132 ele.setAttribute("depends", "libraries, sourcefiles, sections, output");
133 root.appendChild(ele);
134
135 //
136 // compile all source files
137 //
138 root.appendChild(document.createComment("Compile all dependency Library instances."));
139 ele = document.createElement("target");
140 ele.setAttribute("name", "libraries");
141
142 //
143 // Parse all sourfiles but files specified in sections
144 //
145 if (!FrameworkBuildTask.multithread) {
146 applyLibraryInstance(document, ele);
147 }
148 root.appendChild(ele);
149
150 //
151 // compile all source files
152 //
153 root.appendChild(document.createComment("sourcefiles target"));
154 ele = document.createElement("target");
155 ele.setAttribute("name", "sourcefiles");
156
157 //
158 // Parse all sourfiles but files specified in sections
159 //
160 applyCompileElement(document, ele);
161 root.appendChild(ele);
162
163 //
164 // generate the init target
165 // main purpose is create all nessary pathes
166 // generate the sections target
167 //
168 root.appendChild(document.createComment("sections target"));
169 ele = document.createElement("target");
170 ele.setAttribute("name", "sections");
171 applySectionsElement(document, ele, fp);
172 root.appendChild(ele);
173
174 //
175 // generate the output target
176 //
177 root.appendChild(document.createComment("output target"));
178 ele = document.createElement("target");
179 ele.setAttribute("name", "output");
180 applyOutputElement(document, ele, fp);
181 root.appendChild(ele);
182
183
184 //
185 // generate the clean target
186 //
187 root.appendChild(document.createComment("clean target"));
188 ele = document.createElement("target");
189 ele.setAttribute("name", "clean");
190 applyCleanElement(document, ele);
191 root.appendChild(ele);
192
193 //
194 // generate the Clean All target
195 //
196 root.appendChild(document.createComment("Clean All target"));
197 ele = document.createElement("target");
198 ele.setAttribute("name", "cleanall");
199 applyDeepcleanElement(document, ele);
200 root.appendChild(ele);
201
202 //
203 // add the root element to the document
204 //
205 document.appendChild(rootComment);
206 document.appendChild(root);
207 //
208 // Prepare the DOM document for writing
209 //
210 Source source = new DOMSource(document);
211
212 //
213 // Prepare the output file
214 //
215 File file = new File(buildFilename);
216
217 //
218 // generate all directory path
219 //
220 (new File(file.getParent())).mkdirs();
221 FileOutputStream outputStream = new FileOutputStream(file);
222 Result result = new StreamResult(new OutputStreamWriter(outputStream));
223
224 //
225 // Write the DOM document to the file
226 //
227 Transformer xformer = TransformerFactory.newInstance().newTransformer();
228 xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
229 xformer.setOutputProperty(OutputKeys.INDENT, "yes");
230 xformer.transform(source, result);
231 } catch (Exception ex) {
232 throw new BuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());
233 }
234 }
235
236 /**
237 Generate the clean elements for BaseName_build.xml.
238
239 @param document current BaseName_build.xml XML document
240 @param root Root element for current
241 **/
242 private void applyCleanElement(Document document, Node root) {
243 //
244 // <delete includeemptydirs="true">
245 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
246 // </delete>
247 //
248 Element deleteEle = document.createElement("delete");
249 deleteEle.setAttribute("includeemptydirs", "true");
250 Element filesetEle = document.createElement("fileset");
251 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");
252 filesetEle.setAttribute("includes", "**/*");
253 filesetEle.setAttribute("excludes", "*.xml");
254 deleteEle.appendChild(filesetEle);
255 root.appendChild(deleteEle);
256 }
257
258 /**
259 Generate the cleanall elements for BaseName_build.xml.
260
261 @param document current BaseName_build.xml XML document
262 @param root Root element for current
263 **/
264 private void applyDeepcleanElement(Document document, Node root) {
265 //
266 // <delete includeemptydirs="true">
267 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
268 // </delete>
269 //
270 Element deleteEle = document.createElement("delete");
271 deleteEle.setAttribute("includeemptydirs", "true");
272 Element filesetEle = document.createElement("fileset");
273 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");
274 filesetEle.setAttribute("includes", "**/*");
275 filesetEle.setAttribute("excludes", "*.xml");
276 deleteEle.appendChild(filesetEle);
277 root.appendChild(deleteEle);
278
279 //
280 // <delete includeemptydirs="true">
281 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
282 // </delete>
283 //
284 deleteEle = document.createElement("delete");
285 deleteEle.setAttribute("includeemptydirs", "true");
286 filesetEle = document.createElement("fileset");
287 filesetEle.setAttribute("dir", "${DEST_DIR_DEBUG}");
288 filesetEle.setAttribute("includes", "**/*");
289 deleteEle.appendChild(filesetEle);
290 root.appendChild(deleteEle);
291 }
292
293 /**
294 Generate the dependent library instances elements for BaseName_build.xml.
295
296 @param document current BaseName_build.xml XML document
297 @param root Root element for current
298 **/
299 private void applyLibraryInstance(Document document, Node root) {
300 ModuleIdentification[] libinstances = saq.getLibraryInstance(fpdModuleId.getArch());
301 for (int i = 0; i < libinstances.length; i++) {
302 //
303 // Put package file path to module identification
304 //
305 PackageIdentification packageId = libinstances[i].getPackage();
306
307 //
308 // Generate ANT script to build library instances
309 //
310 Element ele = document.createElement("GenBuild");
311 ele.setAttribute("type", "build");
312
313 //
314 // Prepare pass down information
315 //
316 Map<String, String> passDownMap = new LinkedHashMap<String, String>();
317 for (int j = 0; j < inheritProperties.length; j ++){
318 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");
319 }
320
321 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());
322 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());
323
324 passDownMap.put("PACKAGE_GUID", packageId.getGuid());
325 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());
326
327 for (int j = 0; j < inheritProperties.length; j ++){
328 Element property = document.createElement("property");
329 property.setAttribute("name", inheritProperties[j]);
330 property.setAttribute("value", passDownMap.get(inheritProperties[j]));
331 ele.appendChild(property);
332 }
333
334 root.appendChild(ele);
335 }
336 }
337
338 /**
339 Generate the build source files elements for BaseName_build.xml.
340
341 @param document current BaseName_build.xml XML document
342 @param root Root element for current
343 **/
344 private void applyCompileElement(Document document, Node root) {
345 //
346 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
347 //
348 String[][] sourceFiles = saq.getSourceFiles(fpdModuleId.getArch());
349
350 FileProcess fileProcess = new FileProcess();
351 fileProcess.init(project, includes, document);
352
353 //
354 // Initialize some properties by user
355 //
356 Element initEle = document.createElement("Build_Init");
357 root.appendChild(initEle);
358
359 String moduleDir = project.getProperty("MODULE_DIR");
360 //
361 // Parse all Unicode files
362 //
363 for (int i = 0; i < sourceFiles.length; i++) {
364 //
365 // Go through all source files. Add MODULE_DIR to preffix
366 //
367 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);
368 sourceFiles[i][1] = sourceFile.getPath();
369 String filetype = sourceFiles[i][0];
370 if (filetype != null) {
371 fileProcess.parseFile(sourceFiles[i][1], filetype, root, true);
372 } else {
373 fileProcess.parseFile(sourceFiles[i][1], root, true);
374 }
375 }
376
377 //
378 // If exist Unicode file
379 //
380 if (fileProcess.isUnicodeExist()) {
381 Element ele = document.createElement("Build_Unicode_Database");
382 ele.setAttribute("FILEPATH", ".");
383 ele.setAttribute("FILENAME", "${BASE_NAME}");
384 Element includesEle = document.createElement("EXTRA.INC");
385 for (int i = 0; i < includes.length; i++) {
386 Element includeEle = document.createElement("includepath");
387 includeEle.setAttribute("path", includes[i]);
388 includesEle.appendChild(includeEle);
389 }
390 ele.appendChild(includesEle);
391 root.appendChild(ele);
392 }
393
394 //
395 // Parse AutoGen.c & AutoGen.h
396 //
397 if ( ! fpdModuleId.getModule().getName().equalsIgnoreCase("Shell")) {
398 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", root, false);
399 }
400
401 //
402 // Parse all source files but Unicode files
403 //
404 for (int i = 0; i < sourceFiles.length; i++) {
405 String filetype = sourceFiles[i][0];
406 if (filetype != null) {
407 fileProcess.parseFile(sourceFiles[i][1], filetype, root, false);
408 } else {
409 fileProcess.parseFile(sourceFiles[i][1], root, false);
410 }
411 }
412
413 //
414 // Initialize SOURCE_FILES for dependcy check use
415 //
416 String str = "";
417 for (int i = 0; i < sourceFiles.length; i++) {
418 str += " " + sourceFiles[i][1];
419 }
420 project.setProperty("SOURCE_FILES", str.replaceAll("(\\\\)", "/"));
421 }
422
423 /**
424 Generate the section elements for BaseName_build.xml. Library module will
425 skip this process.
426
427 @param document current BaseName_build.xml XML document
428 @param root Root element for current
429 **/
430 private void applySectionsElement(Document document, Node root, FfsProcess fp) {
431 if (fpdModuleId.getModule().isLibrary()) {
432 return ;
433 }
434 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {
435 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());
436 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);
437
438 for (int i = 0; i < list.length; i++) {
439 Element ele = document.createElement(list[i]);
440 ele.setAttribute("FILEPATH", ".");
441 ele.setAttribute("FILENAME", "${BASE_NAME}");
442 root.appendChild(ele);
443 }
444 }
445 }
446
447 /**
448 Generate the output elements for BaseName_build.xml. If module is library,
449 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
450
451 @param document current BaseName_build.xml XML document
452 @param root Root element for current
453 **/
454 private void applyOutputElement(Document document, Node root, FfsProcess fp) {
455 if (fpdModuleId.getModule().isLibrary()) {
456 //
457 // call Lib command
458 //
459 Element cc = document.createElement("Build_Library");
460 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());
461 root.appendChild(cc);
462 }
463 //
464 // if it is a module but library
465 //
466 else {
467 if (fp.getFfsNode() != null) {
468 root.appendChild(fp.getFfsNode());
469 }
470 }
471 }
472
473 }