]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/ModuleBuildFileGenerator.java
Remove static from SurfaceAreaQuery.java for preparing parallel build.(1)
[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(saq);
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 applyLibraryInstance(document, ele);
147 root.appendChild(ele);
148
149 //
150 // compile all source files
151 //
152 root.appendChild(document.createComment("sourcefiles target"));
153 ele = document.createElement("target");
154 ele.setAttribute("name", "sourcefiles");
155
156 //
157 // Parse all sourfiles but files specified in sections
158 //
159 applyCompileElement(document, ele);
160 root.appendChild(ele);
161
162 //
163 // generate the init target
164 // main purpose is create all nessary pathes
165 // generate the sections target
166 //
167 root.appendChild(document.createComment("sections target"));
168 ele = document.createElement("target");
169 ele.setAttribute("name", "sections");
170 applySectionsElement(document, ele, fp);
171 root.appendChild(ele);
172
173 //
174 // generate the output target
175 //
176 root.appendChild(document.createComment("output target"));
177 ele = document.createElement("target");
178 ele.setAttribute("name", "output");
179 applyOutputElement(document, ele, fp);
180 root.appendChild(ele);
181
182
183 //
184 // generate the clean target
185 //
186 root.appendChild(document.createComment("clean target"));
187 ele = document.createElement("target");
188 ele.setAttribute("name", "clean");
189 applyCleanElement(document, ele);
190 root.appendChild(ele);
191
192 //
193 // generate the Clean All target
194 //
195 root.appendChild(document.createComment("Clean All target"));
196 ele = document.createElement("target");
197 ele.setAttribute("name", "cleanall");
198 applyDeepcleanElement(document, ele);
199 root.appendChild(ele);
200
201 //
202 // add the root element to the document
203 //
204 document.appendChild(rootComment);
205 document.appendChild(root);
206 //
207 // Prepare the DOM document for writing
208 //
209 Source source = new DOMSource(document);
210
211 //
212 // Prepare the output file
213 //
214 File file = new File(buildFilename);
215
216 //
217 // generate all directory path
218 //
219 (new File(file.getParent())).mkdirs();
220 FileOutputStream outputStream = new FileOutputStream(file);
221 Result result = new StreamResult(new OutputStreamWriter(outputStream));
222
223 //
224 // Write the DOM document to the file
225 //
226 Transformer xformer = TransformerFactory.newInstance().newTransformer();
227 xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
228 xformer.setOutputProperty(OutputKeys.INDENT, "yes");
229 xformer.transform(source, result);
230 } catch (Exception ex) {
231 throw new BuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());
232 }
233 }
234
235 /**
236 Generate the clean elements for BaseName_build.xml.
237
238 @param document current BaseName_build.xml XML document
239 @param root Root element for current
240 **/
241 private void applyCleanElement(Document document, Node root) {
242 //
243 // <delete includeemptydirs="true">
244 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
245 // </delete>
246 //
247 Element deleteEle = document.createElement("delete");
248 deleteEle.setAttribute("includeemptydirs", "true");
249 Element filesetEle = document.createElement("fileset");
250 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");
251 filesetEle.setAttribute("includes", "**/*");
252 filesetEle.setAttribute("excludes", "*.xml");
253 deleteEle.appendChild(filesetEle);
254 root.appendChild(deleteEle);
255 }
256
257 /**
258 Generate the cleanall elements for BaseName_build.xml.
259
260 @param document current BaseName_build.xml XML document
261 @param root Root element for current
262 **/
263 private void applyDeepcleanElement(Document document, Node root) {
264 //
265 // <delete includeemptydirs="true">
266 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
267 // </delete>
268 //
269 Element deleteEle = document.createElement("delete");
270 deleteEle.setAttribute("includeemptydirs", "true");
271 Element filesetEle = document.createElement("fileset");
272 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");
273 filesetEle.setAttribute("includes", "**/*");
274 filesetEle.setAttribute("excludes", "*.xml");
275 deleteEle.appendChild(filesetEle);
276 root.appendChild(deleteEle);
277
278 //
279 // <delete includeemptydirs="true">
280 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
281 // </delete>
282 //
283 deleteEle = document.createElement("delete");
284 deleteEle.setAttribute("includeemptydirs", "true");
285 filesetEle = document.createElement("fileset");
286 filesetEle.setAttribute("dir", "${DEST_DIR_DEBUG}");
287 filesetEle.setAttribute("includes", "**/*");
288 deleteEle.appendChild(filesetEle);
289 root.appendChild(deleteEle);
290 }
291
292 /**
293 Generate the dependent library instances elements for BaseName_build.xml.
294
295 @param document current BaseName_build.xml XML document
296 @param root Root element for current
297 **/
298 private void applyLibraryInstance(Document document, Node root) {
299 ModuleIdentification[] libinstances = saq.getLibraryInstance(fpdModuleId.getArch());
300 for (int i = 0; i < libinstances.length; i++) {
301 //
302 // Put package file path to module identification
303 //
304 PackageIdentification packageId = libinstances[i].getPackage();
305
306 //
307 // Generate ANT script to build library instances
308 //
309 Element ele = document.createElement("GenBuild");
310 ele.setAttribute("type", "build");
311
312 //
313 // Prepare pass down information
314 //
315 Map<String, String> passDownMap = new LinkedHashMap<String, String>();
316 for (int j = 0; j < inheritProperties.length; j ++){
317 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");
318 }
319
320 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());
321 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());
322
323 passDownMap.put("PACKAGE_GUID", packageId.getGuid());
324 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());
325
326 for (int j = 0; j < inheritProperties.length; j ++){
327 Element property = document.createElement("property");
328 property.setAttribute("name", inheritProperties[j]);
329 property.setAttribute("value", passDownMap.get(inheritProperties[j]));
330 ele.appendChild(property);
331 }
332
333 root.appendChild(ele);
334 }
335 }
336
337 /**
338 Generate the build source files elements for BaseName_build.xml.
339
340 @param document current BaseName_build.xml XML document
341 @param root Root element for current
342 **/
343 private void applyCompileElement(Document document, Node root) {
344 //
345 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
346 //
347 String[][] sourceFiles = saq.getSourceFiles(fpdModuleId.getArch());
348
349 FileProcess fileProcess = new FileProcess();
350 fileProcess.init(project, includes, document);
351
352 //
353 // Initialize some properties by user
354 //
355 Element initEle = document.createElement("Build_Init");
356 root.appendChild(initEle);
357
358 String moduleDir = project.getProperty("MODULE_DIR");
359 //
360 // Parse all Unicode files
361 //
362 for (int i = 0; i < sourceFiles.length; i++) {
363 //
364 // Go through all source files. Add MODULE_DIR to preffix
365 //
366 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);
367 sourceFiles[i][1] = sourceFile.getPath();
368 String filetype = sourceFiles[i][0];
369 if (filetype != null) {
370 fileProcess.parseFile(sourceFiles[i][1], filetype, root, true);
371 } else {
372 fileProcess.parseFile(sourceFiles[i][1], root, true);
373 }
374 }
375
376 //
377 // If exist Unicode file
378 //
379 if (fileProcess.isUnicodeExist()) {
380 Element ele = document.createElement("Build_Unicode_Database");
381 ele.setAttribute("FILEPATH", ".");
382 ele.setAttribute("FILENAME", "${BASE_NAME}");
383 Element includesEle = document.createElement("EXTRA.INC");
384 for (int i = 0; i < includes.length; i++) {
385 Element includeEle = document.createElement("includepath");
386 includeEle.setAttribute("path", includes[i]);
387 includesEle.appendChild(includeEle);
388 }
389 ele.appendChild(includesEle);
390 root.appendChild(ele);
391 }
392
393 //
394 // Parse AutoGen.c & AutoGen.h
395 //
396 if ( ! fpdModuleId.getModule().getName().equalsIgnoreCase("Shell")) {
397 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", root, false);
398 }
399
400 //
401 // Parse all source files but Unicode files
402 //
403 for (int i = 0; i < sourceFiles.length; i++) {
404 String filetype = sourceFiles[i][0];
405 if (filetype != null) {
406 fileProcess.parseFile(sourceFiles[i][1], filetype, root, false);
407 } else {
408 fileProcess.parseFile(sourceFiles[i][1], root, false);
409 }
410 }
411
412 //
413 // Initialize SOURCE_FILES for dependcy check use
414 //
415 String str = "";
416 for (int i = 0; i < sourceFiles.length; i++) {
417 str += " " + sourceFiles[i][1];
418 }
419 PropertyManager.setProperty(project, "SOURCE_FILES", str.replaceAll("(\\\\)", "/"));
420 }
421
422 /**
423 Generate the section elements for BaseName_build.xml. Library module will
424 skip this process.
425
426 @param document current BaseName_build.xml XML document
427 @param root Root element for current
428 **/
429 private void applySectionsElement(Document document, Node root, FfsProcess fp) {
430 if (fpdModuleId.getModule().isLibrary()) {
431 return ;
432 }
433 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {
434 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());
435 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);
436
437 for (int i = 0; i < list.length; i++) {
438 Element ele = document.createElement(list[i]);
439 ele.setAttribute("FILEPATH", ".");
440 ele.setAttribute("FILENAME", "${BASE_NAME}");
441 root.appendChild(ele);
442 }
443 }
444 }
445
446 /**
447 Generate the output elements for BaseName_build.xml. If module is library,
448 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
449
450 @param document current BaseName_build.xml XML document
451 @param root Root element for current
452 **/
453 private void applyOutputElement(Document document, Node root, FfsProcess fp) {
454 if (fpdModuleId.getModule().isLibrary()) {
455 //
456 // call Lib command
457 //
458 Element cc = document.createElement("Build_Library");
459 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());
460 root.appendChild(cc);
461 }
462 //
463 // if it is a module but library
464 //
465 else {
466 if (fp.getFfsNode() != null) {
467 root.appendChild(fp.getFfsNode());
468 }
469 }
470 }
471
472 }