Introduce a new property INCLUDE_PATHS. This property can used by customized build...
[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
a29c47e0 16import java.util.Map;\r
991d660b 17import java.io.FileOutputStream;\r
18import java.io.OutputStreamWriter;\r
a29c47e0 19\r
20import javax.xml.parsers.DocumentBuilder;\r
21import javax.xml.parsers.DocumentBuilderFactory;\r
22import javax.xml.transform.OutputKeys;\r
23import javax.xml.transform.Result;\r
24import javax.xml.transform.Source;\r
25import javax.xml.transform.Transformer;\r
26import javax.xml.transform.TransformerFactory;\r
27import javax.xml.transform.dom.DOMSource;\r
28import javax.xml.transform.stream.StreamResult;\r
29\r
30import org.apache.tools.ant.BuildException;\r
31import org.apache.tools.ant.Project;\r
32import org.tianocore.build.fpd.FpdParserTask;\r
a29c47e0 33import org.tianocore.build.global.SurfaceAreaQuery;\r
1fa1cb75 34import org.tianocore.build.global.PropertyManager;\r
a29c47e0 35import org.tianocore.build.id.FpdModuleIdentification;\r
36import org.tianocore.build.id.ModuleIdentification;\r
37import org.tianocore.build.id.PackageIdentification;\r
38import org.w3c.dom.Comment;\r
39import org.w3c.dom.Document;\r
40import org.w3c.dom.Element;\r
41import org.w3c.dom.Node;\r
42\r
43public class ModuleBuildFileGenerator {\r
44\r
45 ///\r
46 /// Pass: TARGET, TOOLCHAIN, ARCH\r
47 /// PACKAGE, PACKAGE_GUID, PACKAGE_VERSION\r
48 ///\r
de4bb9f6 49 String[] inheritProperties = {"ARCH", "MODULE_GUID", "MODULE_VERSION", "PLATFORM_FILE", "PACKAGE_GUID", "PACKAGE_VERSION"};\r
a29c47e0 50\r
51 ///\r
52 /// The information at the header of <em>build.xml</em>.\r
53 ///\r
54 private String info = "DO NOT EDIT \n"\r
391dbbb1 55 + "This file is auto-generated by the build utility\n"\r
a29c47e0 56 + "\n"\r
57 + "Abstract:\n"\r
391dbbb1 58 + "Auto-generated ANT build file for build EFI Modules and Platforms\n";\r
a29c47e0 59\r
60 private FpdModuleIdentification fpdModuleId;\r
61 \r
62 private Project project;\r
63 \r
64 private String ffsKeyword;\r
65 \r
bf3a7173 66 private String[] includes;\r
67 \r
68 public ModuleBuildFileGenerator(Project project, String ffsKeyword, FpdModuleIdentification fpdModuleId, String[] includes) {\r
a29c47e0 69 this.project = project;\r
70 this.fpdModuleId = fpdModuleId;\r
71 this.ffsKeyword = ffsKeyword;\r
bf3a7173 72 this.includes = includes;\r
a29c47e0 73 }\r
74 \r
75 /**\r
76 The whole BaseName_build.xml is composed of seven part. \r
77 <ul>\r
78 <li> ANT properties; </li>\r
79 <li> Dependent module (dependent library instances in most case); </li>\r
80 <li> Source files; </li>\r
81 <li> Sections if module is not library; </li>\r
82 <li> Output (different for library module and driver module); </li>\r
83 <li> Clean; </li>\r
84 <li> Clean all. </li>\r
85 </ul>\r
86 \r
87 @throws BuildException\r
88 Error throws during BaseName_build.xml generating. \r
89 **/\r
90 public void genBuildFile(String buildFilename) throws BuildException {\r
91 FfsProcess fp = new FfsProcess();\r
92 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();\r
93 try {\r
94 DocumentBuilder dombuilder = domfac.newDocumentBuilder();\r
95 Document document = dombuilder.newDocument();\r
96 Comment rootComment = document.createComment(info);\r
97 \r
98 //\r
99 // create root element and its attributes\r
100 //\r
101 Element root = document.createElement("project");\r
102 root.setAttribute("name", fpdModuleId.getModule().getName());\r
103 root.setAttribute("default", "all");\r
104 root.setAttribute("basedir", ".");\r
105 \r
106 //\r
107 // element for External ANT tasks\r
108 //\r
109 root.appendChild(document.createComment("Apply external ANT tasks"));\r
110 Element ele = document.createElement("taskdef");\r
111 ele.setAttribute("resource", "frameworktasks.tasks");\r
112 root.appendChild(ele);\r
113 ele = document.createElement("taskdef");\r
114 ele.setAttribute("resource", "cpptasks.tasks");\r
115 root.appendChild(ele);\r
116 ele = document.createElement("typedef");\r
117 ele.setAttribute("resource", "cpptasks.types");\r
118 root.appendChild(ele);\r
119 ele = document.createElement("taskdef");\r
120 ele.setAttribute("resource", "net/sf/antcontrib/antlib.xml");\r
121 root.appendChild(ele);\r
122\r
123 //\r
124 // Generate the default target,\r
125 // which depends on init, sections and output target\r
126 //\r
127 root.appendChild(document.createComment("Default target"));\r
128 ele = document.createElement("target");\r
129 ele.setAttribute("name", "all");\r
130 ele.setAttribute("depends", "libraries, sourcefiles, sections, output");\r
131 root.appendChild(ele);\r
132 \r
133 //\r
134 // compile all source files\r
135 //\r
136 root.appendChild(document.createComment("Compile all dependency Library instances."));\r
137 ele = document.createElement("target");\r
138 ele.setAttribute("name", "libraries");\r
139\r
140 //\r
141 // Parse all sourfiles but files specified in sections\r
142 //\r
143 applyLibraryInstance(document, ele);\r
144 root.appendChild(ele);\r
145\r
146 //\r
147 // compile all source files\r
148 //\r
149 root.appendChild(document.createComment("sourcefiles target"));\r
150 ele = document.createElement("target");\r
151 ele.setAttribute("name", "sourcefiles");\r
152 \r
153 //\r
154 // Parse all sourfiles but files specified in sections\r
155 //\r
156 applyCompileElement(document, ele);\r
157 root.appendChild(ele);\r
158\r
159 //\r
160 // generate the init target\r
161 // main purpose is create all nessary pathes\r
162 // generate the sections target\r
163 //\r
164 root.appendChild(document.createComment("sections target"));\r
165 ele = document.createElement("target");\r
166 ele.setAttribute("name", "sections");\r
167 applySectionsElement(document, ele, fp);\r
168 root.appendChild(ele);\r
169\r
170 //\r
171 // generate the output target\r
172 //\r
173 root.appendChild(document.createComment("output target"));\r
174 ele = document.createElement("target");\r
175 ele.setAttribute("name", "output");\r
176 applyOutputElement(document, ele, fp);\r
177 root.appendChild(ele);\r
178\r
179 \r
180 //\r
181 // generate the clean target\r
182 //\r
183 root.appendChild(document.createComment("clean target"));\r
184 ele = document.createElement("target");\r
185 ele.setAttribute("name", "clean");\r
186 applyCleanElement(document, ele);\r
187 root.appendChild(ele);\r
188 \r
189 //\r
190 // generate the Clean All target\r
191 //\r
192 root.appendChild(document.createComment("Clean All target"));\r
193 ele = document.createElement("target");\r
194 ele.setAttribute("name", "cleanall");\r
195 applyDeepcleanElement(document, ele);\r
196 root.appendChild(ele);\r
197 \r
198 //\r
199 // add the root element to the document\r
200 //\r
201 document.appendChild(rootComment);\r
202 document.appendChild(root);\r
203 //\r
204 // Prepare the DOM document for writing\r
205 //\r
206 Source source = new DOMSource(document);\r
207\r
208 //\r
209 // Prepare the output file\r
210 //\r
211 File file = new File(buildFilename);\r
212\r
213 //\r
214 // generate all directory path\r
215 //\r
216 (new File(file.getParent())).mkdirs();\r
991d660b 217 FileOutputStream outputStream = new FileOutputStream(file);\r
218 Result result = new StreamResult(new OutputStreamWriter(outputStream));\r
a29c47e0 219 \r
220 //\r
221 // Write the DOM document to the file\r
222 //\r
223 Transformer xformer = TransformerFactory.newInstance().newTransformer();\r
224 xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");\r
225 xformer.setOutputProperty(OutputKeys.INDENT, "yes");\r
226 xformer.transform(source, result);\r
227 } catch (Exception ex) {\r
391dbbb1 228 throw new BuildException("Generating the module [" + fpdModuleId.getModule().getName() + "] build.xml file failed!.\n" + ex.getMessage());\r
a29c47e0 229 }\r
230 }\r
231\r
232 /**\r
233 Generate the clean elements for BaseName_build.xml. \r
234 \r
235 @param document current BaseName_build.xml XML document\r
236 @param root Root element for current\r
237 **/\r
238 private void applyCleanElement(Document document, Node root) {\r
a29c47e0 239 //\r
240 // <delete includeemptydirs="true">\r
241 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />\r
242 // </delete>\r
243 //\r
244 Element deleteEle = document.createElement("delete");\r
245 deleteEle.setAttribute("includeemptydirs", "true");\r
246 Element filesetEle = document.createElement("fileset");\r
247 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");\r
248 filesetEle.setAttribute("includes", "**/*");\r
249 filesetEle.setAttribute("excludes", "*.xml");\r
250 deleteEle.appendChild(filesetEle);\r
251 root.appendChild(deleteEle);\r
252 }\r
253\r
254 /**\r
255 Generate the cleanall elements for BaseName_build.xml. \r
256 \r
257 @param document current BaseName_build.xml XML document\r
258 @param root Root element for current\r
259 **/\r
260 private void applyDeepcleanElement(Document document, Node root) {\r
a29c47e0 261 //\r
262 // <delete includeemptydirs="true">\r
263 // <fileset dir="${DEST_DIR_OUTPUT}" includes="" excludes="" />\r
264 // </delete>\r
265 //\r
266 Element deleteEle = document.createElement("delete");\r
267 deleteEle.setAttribute("includeemptydirs", "true");\r
268 Element filesetEle = document.createElement("fileset");\r
269 filesetEle.setAttribute("dir", "${DEST_DIR_OUTPUT}");\r
270 filesetEle.setAttribute("includes", "**/*");\r
271 filesetEle.setAttribute("excludes", "*.xml");\r
272 deleteEle.appendChild(filesetEle);\r
273 root.appendChild(deleteEle);\r
274 \r
275 //\r
276 // <delete includeemptydirs="true">\r
277 // <fileset dir="${DEST_DIR_DEBUG}" includes="" />\r
278 // </delete>\r
279 //\r
280 deleteEle = document.createElement("delete");\r
281 deleteEle.setAttribute("includeemptydirs", "true");\r
282 filesetEle = document.createElement("fileset");\r
283 filesetEle.setAttribute("dir", "${DEST_DIR_DEBUG}");\r
284 filesetEle.setAttribute("includes", "**/*");\r
285 deleteEle.appendChild(filesetEle);\r
286 root.appendChild(deleteEle);\r
287 }\r
288\r
289 /**\r
290 Generate the dependent library instances elements for BaseName_build.xml. \r
291 \r
292 @param document current BaseName_build.xml XML document\r
293 @param root Root element for current\r
294 **/\r
295 private void applyLibraryInstance(Document document, Node root) {\r
296 ModuleIdentification[] libinstances = SurfaceAreaQuery.getLibraryInstance(fpdModuleId.getArch());\r
a29c47e0 297 for (int i = 0; i < libinstances.length; i++) {\r
298 //\r
299 // Put package file path to module identification\r
300 //\r
301 PackageIdentification packageId = libinstances[i].getPackage();\r
302 \r
303 //\r
304 // Generate ANT script to build library instances\r
305 //\r
306 Element ele = document.createElement("GenBuild");\r
307 ele.setAttribute("type", "build");\r
a29c47e0 308 \r
309 //\r
310 // Prepare pass down information\r
311 //\r
312 Map<String, String> passDownMap = new LinkedHashMap<String, String>();\r
313 for (int j = 0; j < inheritProperties.length; j ++){\r
314 passDownMap.put(inheritProperties[j], "${" + inheritProperties[j] + "}");\r
315 }\r
316 \r
317 passDownMap.put("MODULE_GUID", libinstances[i].getGuid());\r
318 passDownMap.put("MODULE_VERSION", libinstances[i].getVersion());\r
319 \r
320 passDownMap.put("PACKAGE_GUID", packageId.getGuid());\r
321 passDownMap.put("PACKAGE_VERSION", packageId.getVersion());\r
322 \r
323 for (int j = 0; j < inheritProperties.length; j ++){\r
324 Element property = document.createElement("property");\r
325 property.setAttribute("name", inheritProperties[j]);\r
326 property.setAttribute("value", passDownMap.get(inheritProperties[j]));\r
327 ele.appendChild(property);\r
328 }\r
329 \r
330 root.appendChild(ele);\r
a29c47e0 331 }\r
a29c47e0 332 }\r
a33f3dd1 333\r
a29c47e0 334 /**\r
335 Generate the build source files elements for BaseName_build.xml. \r
336 \r
337 @param document current BaseName_build.xml XML document\r
338 @param root Root element for current\r
339 **/\r
340 private void applyCompileElement(Document document, Node root) {\r
a29c47e0 341 //\r
342 // sourceFiles[][0] is FileType, [][1] is File name relative to Module_Dir\r
343 //\r
344 String[][] sourceFiles = SurfaceAreaQuery.getSourceFiles(fpdModuleId.getArch());\r
345\r
346 FileProcess fileProcess = new FileProcess();\r
347 fileProcess.init(project, includes, document);\r
88f858a1 348 \r
349 //\r
350 // Initialize some properties by user\r
351 //\r
352 Element initEle = document.createElement("Build_Init");\r
353 root.appendChild(initEle);\r
a29c47e0 354\r
355 String moduleDir = project.getProperty("MODULE_DIR");\r
356 //\r
357 // Parse all Unicode files\r
358 //\r
359 for (int i = 0; i < sourceFiles.length; i++) {\r
360 //\r
361 // Go through all source files. Add MODULE_DIR to preffix\r
362 //\r
363 File sourceFile = new File(moduleDir + File.separatorChar + sourceFiles[i][1]);\r
364 sourceFiles[i][1] = sourceFile.getPath();\r
365 String filetype = sourceFiles[i][0];\r
366 if (filetype != null) {\r
367 fileProcess.parseFile(sourceFiles[i][1], filetype, root, true);\r
368 } else {\r
369 fileProcess.parseFile(sourceFiles[i][1], root, true);\r
370 }\r
371 }\r
372 \r
373 //\r
374 // If exist Unicode file\r
375 //\r
376 if (fileProcess.isUnicodeExist()) {\r
377 Element ele = document.createElement("Build_Unicode_Database");\r
378 ele.setAttribute("FILEPATH", ".");\r
379 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
a29c47e0 380 Element includesEle = document.createElement("EXTRA.INC");\r
bf3a7173 381 for (int i = 0; i < includes.length; i++) {\r
a29c47e0 382 Element includeEle = document.createElement("includepath");\r
bf3a7173 383 includeEle.setAttribute("path", includes[i]);\r
a29c47e0 384 includesEle.appendChild(includeEle);\r
385 }\r
386 ele.appendChild(includesEle);\r
387 root.appendChild(ele);\r
388 }\r
389\r
390 //\r
391 // Parse AutoGen.c & AutoGen.h\r
392 //\r
393 if ( ! fpdModuleId.getModule().getName().equalsIgnoreCase("Shell")) {\r
394 fileProcess.parseFile(project.getProperty("DEST_DIR_DEBUG") + File.separatorChar + "AutoGen.c", root, false);\r
395 }\r
396 \r
397 //\r
398 // Parse all source files but Unicode files\r
399 //\r
400 for (int i = 0; i < sourceFiles.length; i++) {\r
401 String filetype = sourceFiles[i][0];\r
402 if (filetype != null) {\r
403 fileProcess.parseFile(sourceFiles[i][1], filetype, root, false);\r
404 } else {\r
405 fileProcess.parseFile(sourceFiles[i][1], root, false);\r
406 }\r
407 }\r
408 \r
409 //\r
410 // Initialize SOURCE_FILES for dependcy check use\r
411 //\r
412 String str = "";\r
413 for (int i = 0; i < sourceFiles.length; i++) {\r
414 str += " " + sourceFiles[i][1];\r
415 }\r
1fa1cb75 416 PropertyManager.setProperty(project, "SOURCE_FILES", str.replaceAll("(\\\\)", "/"));\r
a29c47e0 417 }\r
418\r
419 /**\r
420 Generate the section elements for BaseName_build.xml. Library module will\r
421 skip this process. \r
422 \r
423 @param document current BaseName_build.xml XML document\r
424 @param root Root element for current\r
425 **/\r
426 private void applySectionsElement(Document document, Node root, FfsProcess fp) {\r
427 if (fpdModuleId.getModule().isLibrary()) {\r
428 return ;\r
429 }\r
430 if (fp.initSections(ffsKeyword, project, fpdModuleId)) {\r
196ad8d7 431 String targetFilename = fpdModuleId.getModule().getGuid() + "-" + "${BASE_NAME}" + FpdParserTask.getSuffix(fpdModuleId.getModule().getModuleType());\r
a29c47e0 432 String[] list = fp.getGenSectionElements(document, "${BASE_NAME}", fpdModuleId.getModule().getGuid(), targetFilename);\r
433\r
434 for (int i = 0; i < list.length; i++) {\r
435 Element ele = document.createElement(list[i]);\r
436 ele.setAttribute("FILEPATH", ".");\r
437 ele.setAttribute("FILENAME", "${BASE_NAME}");\r
438 root.appendChild(ele);\r
439 }\r
440 }\r
441 }\r
442\r
443 /**\r
444 Generate the output elements for BaseName_build.xml. If module is library,\r
445 call the <em>LIB</em> command, else call the <em>GenFfs</em> command. \r
446 \r
447 @param document current BaseName_build.xml XML document\r
448 @param root Root element for current\r
449 **/\r
450 private void applyOutputElement(Document document, Node root, FfsProcess fp) {\r
451 if (fpdModuleId.getModule().isLibrary()) {\r
452 //\r
453 // call Lib command\r
454 //\r
455 Element cc = document.createElement("Build_Library");\r
456 cc.setAttribute("FILENAME", fpdModuleId.getModule().getName());\r
457 root.appendChild(cc);\r
458 }\r
459 //\r
460 // if it is a module but library\r
461 //\r
462 else {\r
463 if (fp.getFfsNode() != null) {\r
464 root.appendChild(fp.getFfsNode());\r
465 }\r
466 }\r
467 }\r
468\r
469}\r