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