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