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