]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/ModuleBuildFileGenerator.java
Fixes for Linux builds.
[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 Return the name of the directory that corresponds to the architecture.
407 This is a translation from the XML Schema tag to a directory that
408 corresponds to our directory name coding convention.
409
410 **/
411 private String archDir(String arch) {
412 return arch.replaceFirst("X64", "x64")
413 .replaceFirst("IPF", "Ipf")
414 .replaceFirst("IA32", "Ia32")
415 .replaceFirst("ARM", "Arm")
416 .replaceFirst("EBC", "Ebc");
417 }
418
419 /**
420 Generate the build source files elements for BaseName_build.xml.
421
422 @param document current BaseName_build.xml XML document
423 @param root Root element for current
424 **/
425 private void applyCompileElement(Document document, Node root) {
426 //
427 // Prepare the includes: PackageDependencies and Output debug direactory
428 //
429 Set<String> includes = new LinkedHashSet<String>();
430 String arch = project.getProperty("ARCH");
431
432 //
433 // WORKSPACE
434 //
435 includes.add("${WORKSPACE_DIR}");
436
437 //
438 // Module iteself
439 //
440 includes.add("${MODULE_DIR}");
441 includes.add("${MODULE_DIR}" + File.separatorChar + archDir(arch));
442
443 //
444 // Packages in PackageDenpendencies
445 //
446 PackageIdentification[] packageDependencies = SurfaceAreaQuery.getDependencePkg(fpdModuleId.getArch());
447 for (int i = 0; i < packageDependencies.length; i++) {
448 GlobalData.refreshPackageIdentification(packageDependencies[i]);
449 File packageFile = packageDependencies[i].getSpdFile();
450 includes.add(packageFile.getParent() + File.separatorChar + "Include");
451 includes.add(packageFile.getParent() + File.separatorChar + "Include" + File.separatorChar + archDir(arch));
452 }
453
454 //
455 // All Dependency Library Instance's PackageDependencies
456 //
457 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());
458 for (int i = 0; i < libinstances.length; i++) {
459 SurfaceAreaQuery.push(GlobalData.getDoc(libinstances[i], fpdModuleId.getArch()));
460 PackageIdentification[] libraryPackageDependencies = SurfaceAreaQuery.getDependencePkg(fpdModuleId.getArch());
461 for (int j = 0; j < libraryPackageDependencies.length; j++) {
462 GlobalData.refreshPackageIdentification(libraryPackageDependencies[j]);
463 File packageFile = libraryPackageDependencies[j].getSpdFile();
464 includes.add(packageFile.getParent() + File.separatorChar + "Include");
465 includes.add(packageFile.getParent() + File.separatorChar + "Include" + File.separatorChar + archDir(arch));
466 }
467 SurfaceAreaQuery.pop();
468 }
469
470
471 //
472 // The package which the module belongs to
473 // TBD
474 includes.add(fpdModuleId.getModule().getPackage().getPackageDir() + File.separatorChar + "Include");
475 includes.add(fpdModuleId.getModule().getPackage().getPackageDir() + File.separatorChar + "Include" + File.separatorChar + archDir(arch));
476
477 //
478 // Debug files output directory
479 //
480 includes.add("${DEST_DIR_DEBUG}");
481
482 //
483 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir
484 //
485 String[][] sourceFiles = SurfaceAreaQuery.getSourceFiles(fpdModuleId.getArch());
486
487 FileProcess fileProcess = new FileProcess();
488 fileProcess.init(project, includes, document);
489
490 String moduleDir = project.getProperty("MODULE_DIR");
491 //
492 // Parse all Unicode files
493 //
494 for (int i = 0; i < sourceFiles.length; i++) {
495 //
496 // Go through all source files. Add MODULE_DIR to preffix
497 //
498 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);
499 sourceFiles[i][1] = sourceFile.getPath();
500 String filetype = sourceFiles[i][0];
501 if (filetype != null) {
502 fileProcess.parseFile(sourceFiles[i][1], filetype, root, true);
503 } else {
504 fileProcess.parseFile(sourceFiles[i][1], root, true);
505 }
506 }
507
508 //
509 // If exist Unicode file
510 //
511 if (fileProcess.isUnicodeExist()) {
512 Element ele = document.createElement("Build_Unicode_Database");
513 ele.setAttribute("FILEPATH", ".");
514 ele.setAttribute("FILENAME", "${BASE_NAME}");
515 String[] includePaths = includes.toArray(new String[includes.size()]);
516 Element includesEle = document.createElement("EXTRA.INC");
517 for (int i = 0; i < includePaths.length; i++) {
518 Element includeEle = document.createElement("includepath");
519 includeEle.setAttribute("path", includePaths[i]);
520 includesEle.appendChild(includeEle);
521 }
522 ele.appendChild(includesEle);
523 root.appendChild(ele);
524 }
525
526 //
527 // Parse AutoGen.c & AutoGen.h
528 //
529 if ( ! fpdModuleId.getModule().getName().equalsIgnoreCase("Shell")) {
530 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", root, false);
531 }
532
533 //
534 // Parse all source files but Unicode files
535 //
536 for (int i = 0; i < sourceFiles.length; i++) {
537 String filetype = sourceFiles[i][0];
538 if (filetype != null) {
539 fileProcess.parseFile(sourceFiles[i][1], filetype, root, false);
540 } else {
541 fileProcess.parseFile(sourceFiles[i][1], root, false);
542 }
543 }
544
545 //
546 // Initialize SOURCE_FILES for dependcy check use
547 //
548 String str = "";
549 for (int i = 0; i < sourceFiles.length; i++) {
550 str += " " + sourceFiles[i][1];
551 }
552 project.setProperty("SOURCE_FILES", str.replaceAll("(\\\\)", "/"));
553 }
554
555 /**
556 Generate the section elements for BaseName_build.xml. Library module will
557 skip this process.
558
559 @param document current BaseName_build.xml XML document
560 @param root Root element for current
561 **/
562 private void applySectionsElement(Document document, Node root, FfsProcess fp) {
563 if (fpdModuleId.getModule().isLibrary()) {
564 return ;
565 }
566 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {
567 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());
568 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);
569
570 for (int i = 0; i < list.length; i++) {
571 Element ele = document.createElement(list[i]);
572 ele.setAttribute("FILEPATH", ".");
573 ele.setAttribute("FILENAME", "${BASE_NAME}");
574 root.appendChild(ele);
575 }
576 }
577 }
578
579 /**
580 Generate the output elements for BaseName_build.xml. If module is library,
581 call the <em>LIB</em> command, else call the <em>GenFfs</em> command.
582
583 @param document current BaseName_build.xml XML document
584 @param root Root element for current
585 **/
586 private void applyOutputElement(Document document, Node root, FfsProcess fp) {
587 if (fpdModuleId.getModule().isLibrary()) {
588 //
589 // call Lib command
590 //
591 Element cc = document.createElement("Build_Library");
592 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());
593 root.appendChild(cc);
594 }
595 //
596 // if it is a module but library
597 //
598 else {
599 if (fp.getFfsNode() != null) {
600 root.appendChild(fp.getFfsNode());
601 }
602 }
603 }
604
605 }