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