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