]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/FrameworkBuildTask.java
Fix cleanall can't clean all genereated files. Now .i files generated by VfrCompile...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / FrameworkBuildTask.java
CommitLineData
de4bb9f6 1/** @file FrameworkBuildTask.java\r
2 \r
3 The file is ANT task to find MSA or FPD file and build them. \r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 **/\r
a29c47e0 14package org.tianocore.build;\r
15\r
16import java.io.BufferedReader;\r
17import java.io.File;\r
18import java.io.InputStreamReader;\r
19import java.util.Iterator;\r
20import java.util.LinkedHashSet;\r
21import java.util.Map;\r
22import java.util.Set;\r
23\r
24import org.apache.tools.ant.BuildException;\r
25import org.apache.tools.ant.Task;\r
26import org.tianocore.build.fpd.FpdParserTask;\r
27import org.tianocore.build.global.GlobalData;\r
de4bb9f6 28import org.tianocore.build.toolchain.ConfigReader;\r
a29c47e0 29import org.tianocore.build.toolchain.ToolChainInfo;\r
4a6a5026 30import org.tianocore.common.definitions.ToolDefinitions;\r
a29c47e0 31\r
2d16dcec 32/**\r
33 <p>\r
34 <code>FrameworkBuildTask</code> is an Ant task. The main function is finding\r
35 and processing a FPD or MSA file, then building a platform or stand-alone \r
36 module. \r
37 \r
38 <p>\r
39 The task search current directory and find out all MSA and FPD files by file\r
40 extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a\r
41 stand-alone module. The ACTIVE_PLATFORM policy is: \r
42 \r
43 <pre>\r
44 1. More than one MSA files, report error; \r
45 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;\r
46 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;\r
47 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;\r
48 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;\r
49 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;\r
50 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform\r
51 and let user choose one. \r
52 </pre>\r
53 \r
54 <p>\r
55 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].\r
56 And load all system environment variables to Ant properties. \r
57 \r
58 <p>\r
59 The usage for this task is : \r
60 \r
61 <pre>\r
62 &lt;FrameworkBuild type="cleanall" /&gt;\r
63 </pre>\r
64 \r
65 @since GenBuild 1.0\r
66**/\r
a29c47e0 67public class FrameworkBuildTask extends Task{\r
68\r
69 private Set<File> buildFiles = new LinkedHashSet<File>();\r
70 \r
71 private Set<File> fpdFiles = new LinkedHashSet<File>();\r
72 \r
73 private Set<File> msaFiles = new LinkedHashSet<File>();\r
74 \r
4a6a5026 75 String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;\r
de4bb9f6 76 \r
4a6a5026 77 String targetFilename = ToolDefinitions.TARGET_FILE_PATH;\r
78 \r
79 String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;\r
de4bb9f6 80 \r
81 String activePlatform = null;\r
82 \r
a29c47e0 83 ///\r
84 /// there are three type: all (build), clean and cleanall\r
85 ///\r
86 private String type = "all";\r
87 \r
88 public void execute() throws BuildException {\r
89 //\r
90 // Seach build.xml -> .FPD -> .MSA file\r
91 //\r
92 try {\r
93 //\r
94 // Gen Current Working Directory\r
95 //\r
96 File dummyFile = new File(".");\r
97 File cwd = dummyFile.getCanonicalFile();\r
98 File[] files = cwd.listFiles();\r
99 for (int i = 0; i < files.length; i++) {\r
100 if (files[i].isFile()) {\r
101 if (files[i].getName().equalsIgnoreCase("build.xml")) {\r
102 //\r
103 // First, search build.xml, if found, ANT call it\r
104 //\r
105 buildFiles.add(files[i]);\r
106\r
4a6a5026 107 } else if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
a29c47e0 108 //\r
109 // Second, search FPD file, if found, build it\r
110 //\r
111 fpdFiles.add(files[i]);\r
4a6a5026 112 } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
a29c47e0 113 //\r
114 // Third, search MSA file, if found, build it\r
115 //\r
116 msaFiles.add(files[i]);\r
117 }\r
118 }\r
119 }\r
120 } catch (Exception e) {\r
a29c47e0 121 throw new BuildException(e.getMessage());\r
122 }\r
123 \r
a29c47e0 124 //\r
125 // Deal with all environment variable (Add them to properties)\r
126 //\r
127 backupSystemProperties();\r
128 \r
129 //\r
de4bb9f6 130 // Read target.txt file\r
a29c47e0 131 //\r
de4bb9f6 132 readTargetFile();\r
133\r
a29c47e0 134 //\r
135 // Global Data initialization\r
136 //\r
9cf435c2 137 File workspacePath = new File(getProject().getProperty("WORKSPACE"));\r
138 getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));\r
4a6a5026 139 GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);\r
a29c47e0 140 \r
de4bb9f6 141 //\r
142 // If find MSA file and ACTIVE_PLATFORM is set, build the module; \r
143 // else fail build. \r
144 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM. \r
145 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform; \r
146 // If find more than one FPD files, let user select one. \r
147 //\r
148 File buildFile = null;\r
149 if (msaFiles.size() > 1) {\r
4a6a5026 150 throw new BuildException("Having more than one MSA file in a directory is not allowed!");\r
151 } else if (msaFiles.size() == 1 && activePlatform == null) {\r
152 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");\r
153 } else if (msaFiles.size() == 1 && activePlatform != null) {\r
de4bb9f6 154 //\r
155 // Build the single module\r
156 //\r
157 buildFile = msaFiles.toArray(new File[1])[0];\r
4a6a5026 158 } else if (activePlatform != null) {\r
de4bb9f6 159 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
4a6a5026 160 } else if (fpdFiles.size() == 1) {\r
de4bb9f6 161 buildFile = fpdFiles.toArray(new File[1])[0];\r
4a6a5026 162 } else if (fpdFiles.size() > 1) {\r
de4bb9f6 163 buildFile = intercommuniteWithUser();\r
164 }\r
165 //\r
166 // If there is no build files or FPD files or MSA files, stop build\r
167 //\r
168 else {\r
4a6a5026 169 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");\r
de4bb9f6 170 }\r
171\r
a29c47e0 172 //\r
173 // Build every FPD files (PLATFORM build)\r
174 //\r
4a6a5026 175 if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
176 System.out.println("Processing the FPD file [" + buildFile.getPath() + "] ..>> ");\r
a29c47e0 177 FpdParserTask fpdParserTask = new FpdParserTask();\r
178 fpdParserTask.setType(type);\r
179 fpdParserTask.setProject(getProject());\r
180 fpdParserTask.setFpdFile(buildFile);\r
181 fpdParserTask.execute();\r
caa44816 182 \r
183 //\r
184 // If cleanall delete the Platform_build.xml\r
185 //\r
186 if (type.compareTo("cleanall") == 0) {\r
187 File platformBuildFile = \r
188 new File(getProject().getProperty("PLATFORM_DIR") \r
189 + File.separatorChar \r
190 + getProject().getProperty("PLATFORM") \r
191 + "_build.xml");\r
192 platformBuildFile.deleteOnExit();\r
193 }\r
a29c47e0 194 }\r
195 \r
196 //\r
197 // Build every MSA files (SINGLE MODULE BUILD)\r
198 //\r
4a6a5026 199 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
fa2da5b1 200 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
4a6a5026 201 System.out.println("Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r
202 System.out.println("Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r
a29c47e0 203 GenBuildTask genBuildTask = new GenBuildTask();\r
de4bb9f6 204 genBuildTask.setSingleModuleBuild(true);\r
a29c47e0 205 genBuildTask.setType(type);\r
de4bb9f6 206 getProject().setProperty("PLATFORM_FILE", activePlatform);\r
a29c47e0 207 genBuildTask.setProject(getProject());\r
208 genBuildTask.setMsaFile(buildFile);\r
209 genBuildTask.execute();\r
210 }\r
211 }\r
212 \r
213 /**\r
214 Transfer system environment variables to ANT properties. If system variable \r
215 already exiests in ANT properties, skip it.\r
216 \r
217 **/\r
218 private void backupSystemProperties() {\r
219 Map<String, String> sysProperties = System.getenv();\r
220 Set<String> keys = sysProperties.keySet();\r
221 Iterator<String> iter = keys.iterator();\r
222 while (iter.hasNext()) {\r
223 String name = iter.next();\r
224 \r
225 //\r
226 // If system environment variable is not in ANT properties, add it\r
227 //\r
228 if (getProject().getProperty(name) == null) {\r
229 getProject().setProperty(name, sysProperties.get(name));\r
230 }\r
231 }\r
232 }\r
233\r
234 private File intercommuniteWithUser(){\r
235 File file = null;\r
4a6a5026 236 if (fpdFiles.size() > 1) {\r
237 File[] allFiles = new File[fpdFiles.size()];\r
a29c47e0 238 int index = 0;\r
239 Iterator<File> iter = fpdFiles.iterator();\r
240 while (iter.hasNext()) {\r
241 allFiles[index] = iter.next();\r
242 index++;\r
243 }\r
4a6a5026 244\r
245 System.out.println("Finding " + allFiles.length + " FPD files: ");\r
a29c47e0 246 for (int i = 0; i < allFiles.length; i++) {\r
247 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r
248 }\r
249 \r
250 boolean flag = true;\r
4a6a5026 251 System.out.print("Please select one of the following FPD files to build:[1] ");\r
a29c47e0 252 do{\r
253 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r
254 try {\r
255 String str = br.readLine();\r
256 if (str.trim().length() == 0) {\r
257 file = allFiles[0];\r
258 flag = false;\r
259 continue ;\r
260 }\r
261 int indexSelect = Integer.parseInt(str);\r
262 if (indexSelect <=0 || indexSelect > allFiles.length) {\r
263 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
264 continue ;\r
265 } else {\r
266 file = allFiles[indexSelect - 1];\r
267 flag = false;\r
268 continue ;\r
269 }\r
270 } catch (Exception e) {\r
271 System.out.print("Please enter a valid number:[1] ");\r
272 flag = true;\r
273 }\r
274 } while (flag);\r
4a6a5026 275 } else if (fpdFiles.size() == 1) {\r
a29c47e0 276 file = fpdFiles.toArray(new File[1])[0];\r
277 }\r
a29c47e0 278 return file;\r
279 }\r
280 \r
281 \r
282 public void setType(String type) {\r
283 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r
284 this.type = type.toLowerCase();\r
4a6a5026 285 } else {\r
a29c47e0 286 this.type = "all";\r
287 }\r
288 }\r
de4bb9f6 289 \r
290 private void readTargetFile(){\r
291 try {\r
4a6a5026 292 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r
293 \r
d2059d05 294 String[][] targetFileInfo = ConfigReader.parse(targetFile);\r
de4bb9f6 295 \r
296 //\r
297 // Get ToolChain Info from target.txt\r
298 //\r
299 ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
4a6a5026 300 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r
de4bb9f6 301 if (str == null || str.trim().equals("")) {\r
302 envToolChainInfo.addTargets("*");\r
4a6a5026 303 } else {\r
de4bb9f6 304 envToolChainInfo.addTargets(str);\r
305 }\r
4a6a5026 306 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r
de4bb9f6 307 if (str == null || str.trim().equals("")) {\r
308 envToolChainInfo.addTagnames("*");\r
4a6a5026 309 } else {\r
de4bb9f6 310 envToolChainInfo.addTagnames(str);\r
311 }\r
4a6a5026 312 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r
de4bb9f6 313 if (str == null || str.trim().equals("")) {\r
314 envToolChainInfo.addArchs("*");\r
4a6a5026 315 } else {\r
de4bb9f6 316 envToolChainInfo.addArchs(str);\r
317 }\r
318 GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
319 \r
4a6a5026 320 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r
196ad8d7 321 if (str != null && str.trim().length() > 0) {\r
de4bb9f6 322 toolsDefFilename = str;\r
323 }\r
324 \r
4a6a5026 325 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r
de4bb9f6 326 if (str != null && ! str.trim().equals("")) {\r
327 if ( ! str.endsWith(".fpd")) {\r
4a6a5026 328 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r
de4bb9f6 329 }\r
330 activePlatform = str;\r
331 }\r
332 }\r
333 catch (Exception ex) {\r
334 throw new BuildException(ex.getMessage());\r
335 }\r
336 }\r
337 \r
338 private String getValue(String key, String[][] map) {\r
339 for (int i = 0; i < map[0].length; i++){\r
340 if (key.equalsIgnoreCase(map[0][i])) {\r
341 return map[1][i];\r
342 }\r
343 }\r
344 return null;\r
345 }\r
a29c47e0 346}\r