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