]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/ModuleBuildFileGenerator.java
Change to new XML Schema.
[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", "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 ex.printStackTrace();
225 throw new BuildException("Module [" + fpdModuleId.getModule().getName() + "] generating build file failed.\n" + ex.getMessage());
226 }
227 }
228
229 /**
230 Generate the clean elements for BaseName_build.xml.
231
232 @param document current BaseName_build.xml XML document
233 @param root Root element for current
234 **/
235 private void applyCleanElement(Document document, Node root) {
236 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());
237 for (int i = 0; i < libinstances.length; i++) {
238 //
239 // Put package file path to module identification
240 //
241 PackageIdentification packageId = libinstances[i].getPackage();
242
243 //
244 // Generate ANT script to clean
245 //
246 Element ele = document.createElement("GenBuild");
247 ele.setAttribute("type", "clean");
248
249 //
250 // Prepare pass down information
251 //
252 Map<String, String> passDownMap = new LinkedHashMap<String, String>();
253 for (int j = 0; j < inheritProperties.length; j ++){
254 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");
255 }
256 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());
257 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());
258
259 passDownMap.put("PACKAGE_GUID", packageId.getGuid());
260 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());
261
262 for (int j = 0; j < inheritProperties.length; j ++){
263 Element property = document.createElement("property");
264 property.setAttribute("name", inheritProperties[j]);
265 property.setAttribute("value", passDownMap.get(inheritProperties[j]));
266 ele.appendChild(property);
267 }
268
269 root.appendChild(ele);
270 }
271 //
272 // <delete includeemptydirs="true">
273 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
274 // </delete>
275 //
276 Element deleteEle = document.createElement("delete");
277 deleteEle.setAttribute("includeemptydirs", "true");
278 Element filesetEle = document.createElement("fileset");
279 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");
280 filesetEle.setAttribute("includes", "**/*");
281 filesetEle.setAttribute("excludes", "*.xml");
282 deleteEle.appendChild(filesetEle);
283 root.appendChild(deleteEle);
284 }
285
286 /**
287 Generate the cleanall elements for BaseName_build.xml.
288
289 @param document current BaseName_build.xml XML document
290 @param root Root element for current
291 **/
292 private void applyDeepcleanElement(Document document, Node root) {
293 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());
294 for (int i = 0; i < libinstances.length; i++) {
295 //
296 // Put package file path to module identification
297 //
298 PackageIdentification packageId = libinstances[i].getPackage();
299
300 //
301 // Generate ANT script to clean
302 //
303 Element ele = document.createElement("GenBuild");
304 ele.setAttribute("type", "cleanall");
305
306 //
307 // Prepare pass down information
308 //
309 Map<String, String> passDownMap = new LinkedHashMap<String, String>();
310 for (int j = 0; j < inheritProperties.length; j ++){
311 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");
312 }
313
314 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());
315 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());
316
317 passDownMap.put("PACKAGE_GUID", packageId.getGuid());
318 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());
319
320 for (int j = 0; j < inheritProperties.length; j ++){
321 Element property = document.createElement("property");
322 property.setAttribute("name", inheritProperties[j]);
323 property.setAttribute("value", passDownMap.get(inheritProperties[j]));
324 ele.appendChild(property);
325 }
326
327 root.appendChild(ele);
328 }
329 //
330 // <delete includeemptydirs="true">
331 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />
332 // </delete>
333 //
334 Element deleteEle = document.createElement("delete");
335 deleteEle.setAttribute("includeemptydirs", "true");
336 Element filesetEle = document.createElement("fileset");
337 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");
338 filesetEle.setAttribute("includes", "**/*");
339 filesetEle.setAttribute("excludes", "*.xml");
340 deleteEle.appendChild(filesetEle);
341 root.appendChild(deleteEle);
342
343 //
344 // <delete includeemptydirs="true">
345 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />
346 // </delete>
347 //
348 deleteEle = document.createElement("delete");
349 deleteEle.setAttribute("includeemptydirs", "true");
350 filesetEle = document.createElement("fileset");
351 filesetEle.setAttribute("dir", "${DEST_DIR_DEBUG}");
352 filesetEle.setAttribute("includes", "**/*");
353 deleteEle.appendChild(filesetEle);
354 root.appendChild(deleteEle);
355 }
356
357 /**
358 Generate the dependent library instances elements for BaseName_build.xml.
359
360 @param document current BaseName_build.xml XML document
361 @param root Root element for current
362 **/
363 private void applyLibraryInstance(Document document, Node root) {
364 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());
365 // String propertyLibs = "";
366 for (int i = 0; i < libinstances.length; i++) {
367 //
368 // Put package file path to module identification
369 //
370 PackageIdentification packageId = libinstances[i].getPackage();
371
372 //
373 // Generate ANT script to build library instances
374 //
375 Element ele = document.createElement("GenBuild");
376 ele.setAttribute("type", "build");
377 // ele.setAttribute("inheritAll", "false");
378
379 //
380 // Prepare pass down information
381 //
382 Map<String, String> passDownMap = new LinkedHashMap<String, String>();
383 for (int j = 0; j < inheritProperties.length; j ++){
384 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");
385 }
386
387 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());
388 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());
389
390 passDownMap.put("PACKAGE_GUID", packageId.getGuid());
391 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());
392
393 for (int j = 0; j < inheritProperties.length; j ++){
394 Element property = document.createElement("property");
395 property.setAttribute("name", inheritProperties[j]);
396 property.setAttribute("value", passDownMap.get(inheritProperties[j]));
397 ele.appendChild(property);
398 }
399
400 root.appendChild(ele);
401 // propertyLibs += " " + project.getProperty("BIN_DIR") + File.separatorChar + libinstances[i].getName() + ".lib";
402 }
403 // project.setProperty("LIBS", propertyLibs.replaceAll("(\\\\)", "/"));
404 }
405
406 /**
407 Generate the build source files elements for BaseName_build.xml.
408
409 @param document current BaseName_build.xml XML document
410 @param root Root element for current
411 **/
412 private void applyCompileElement(Document document, Node root) {
413 //
414 // Prepare the includes: PackageDependencies and Output debug direactory
415 //
416 Set<String> includes = new LinkedHashSet<String>();
417
418 //
419 // WORKSPACE
420 //
421 includes.add("${WORKSPACE_DIR}");
422
423 //
424 // Module iteself
425 //
426 includes.add("${MODULE_DIR}");
427 includes.add("${MODULE_DIR}" + File.separatorChar + "${ARCH}");
428
429 //
430 // Packages in PackageDenpendencies
431 //
432 PackageIdentification[] packageDependencies = SurfaceAreaQuery.getDependencePkg(fpdModuleId.getArch());
433 for (int i = 0; i < packageDependencies.length; i++) {
434 GlobalData.refreshPackageIdentification(packageDependencies[i]);
435 File packageFile = packageDependencies[i].getSpdFile();
436 includes.add(packageFile.getParent() + File.separatorChar + "Include");
437 includes.add(packageFile.getParent() + File.separatorChar + "Include" + File.separatorChar + "${ARCH}");
438 }
439
440 //
441 // All Dependency Library Instance's PackageDependencies
442 //
443 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());
444 for (int i = 0; i < libinstances.length; i++) {
445 SurfaceAreaQuery.push(GlobalData.getDoc(libinstances[i], fpdModuleId.getArch()));
446 PackageIdentification[] libraryPackageDependencies = SurfaceAreaQuery.getDependencePkg(fpdModuleId.getArch());
447 for (int j = 0; j < libraryPackageDependencies.length; j++) {
448 GlobalData.refreshPackageIdentification(libraryPackageDependencies[j]);
449 File packageFile = libraryPackageDependencies[j].getSpdFile();
450 includes.add(packageFile.getParent() + File.separatorChar + "Include");
451 includes.add(packageFile.getParent() + File.separatorChar + "Include" + File.separatorChar + "${ARCH}");
452 }
453 SurfaceAreaQuery.pop();
454 }
455
456
457 //
458 // The package which the module belongs to
459 // TBD
460 includes.add(fpdModuleId.getModule().getPackage().getPackageDir() + File.separatorChar + "Include");
461 includes.add(fpdModuleId.getModule().getPackage().getPackageDir() + File.separatorChar + "Include" + File.separatorChar + "${ARCH}");
462
463 //
464 // Debug files output directory
465 //
466 includes.add("${DEST_DIR_DEBUG}");
467
468 //
469 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
470 //
471 String[][] sourceFiles = SurfaceAreaQuery.getSourceFiles(fpdModuleId.getArch());
472
473 FileProcess fileProcess = new FileProcess();
474 fileProcess.init(project, includes, document);
475
476 String moduleDir = project.getProperty("MODULE_DIR");
477 //
478 // Parse all Unicode files
479 //
480 for (int i = 0; i < sourceFiles.length; i++) {
481 //
482 // Go through all source files. Add MODULE_DIR to preffix
483 //
484 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);
485 sourceFiles[i][1] = sourceFile.getPath();
486 String filetype = sourceFiles[i][0];
487 if (filetype != null) {
488 fileProcess.parseFile(sourceFiles[i][1], filetype, root, true);
489 } else {
490 fileProcess.parseFile(sourceFiles[i][1], root, true);
491 }
492 }
493
494 //
495 // If exist Unicode file
496 //
497 if (fileProcess.isUnicodeExist()) {
498 Element ele = document.createElement("Build_Unicode_Database");
499 ele.setAttribute("FILEPATH", ".");
500 ele.setAttribute("FILENAME", "${BASE_NAME}");
501 String[] includePaths = includes.toArray(new String[includes.size()]);
502 Element includesEle = document.createElement("EXTRA.INC");
503 for (int i = 0; i < includePaths.length; i++) {
504 Element includeEle = document.createElement("includepath");
505 includeEle.setAttribute("path", includePaths[i]);
506 includesEle.appendChild(includeEle);
507 }
508 ele.appendChild(includesEle);
509 root.appendChild(ele);
510 }
511
512 //
513 // Parse AutoGen.c & AutoGen.h
514 //
515 if ( ! fpdModuleId.getModule().getName().equalsIgnoreCase("Shell")) {
516 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", root, false);
517 }
518
519 //
520 // Parse all source files but Unicode files
521 //
522 for (int i = 0; i < sourceFiles.length; i++) {
523 String filetype = sourceFiles[i][0];
524 if (filetype != null) {
525 fileProcess.parseFile(sourceFiles[i][1], filetype, root, false);
526 } else {
527 fileProcess.parseFile(sourceFiles[i][1], root, false);
528 }
529 }
530
531 //
532 // Initialize SOURCE_FILES for dependcy check use
533 //
534 String str = "";
535 for (int i = 0; i < sourceFiles.length; i++) {
536 str += " " + sourceFiles[i][1];
537 }
538 project.setProperty("SOURCE_FILES", str.replaceAll("(\\\\)", "/"));
539 }
540
541 /**
542 Generate the section elements for BaseName_build.xml. Library module will
543 skip this process.
544
545 @param document current BaseName_build.xml XML document
546 @param root Root element for current
547 **/
548 private void applySectionsElement(Document document, Node root, FfsProcess fp) {
549 if (fpdModuleId.getModule().isLibrary()) {
550 return ;
551 }
552 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {
553 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + fpdModuleId.getModule().getName() + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());
554 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);
555
556 for (int i = 0; i < list.length; i++) {
557 Element ele = document.createElement(list[i]);
558 ele.setAttribute("FILEPATH", ".");
559 ele.setAttribute("FILENAME", "${BASE_NAME}");
560 root.appendChild(ele);
561 }
562 }
563 }
564
565 /**
566 Generate the output elements for BaseName_build.xml. If module is library,
567 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
568
569 @param document current BaseName_build.xml XML document
570 @param root Root element for current
571 **/
572 private void applyOutputElement(Document document, Node root, FfsProcess fp) {
573 if (fpdModuleId.getModule().isLibrary()) {
574 //
575 // call Lib command
576 //
577 Element cc = document.createElement("Build_Library");
578 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());
579 root.appendChild(cc);
580 }
581 //
582 // if it is a module but library
583 //
584 else {
585 if (fp.getFfsNode() != null) {
586 root.appendChild(fp.getFfsNode());
587 }
588 }
589 }
590
591 }