removed extra taskdef and taskref definitions that caused some warning messages when...
[mirror_edk2.git] / Tools / Java / 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
91f7d582 18import java.io.IOException;\r
a29c47e0 19import java.io.InputStreamReader;\r
aca6c736 20import java.util.Hashtable;\r
a29c47e0 21import java.util.Iterator;\r
22import java.util.LinkedHashSet;\r
23import java.util.Map;\r
24import java.util.Set;\r
25\r
26import org.apache.tools.ant.BuildException;\r
27import org.apache.tools.ant.Task;\r
91f7d582 28import org.tianocore.build.exception.AutoGenException;\r
29import org.tianocore.build.exception.GenBuildException;\r
30import org.tianocore.build.exception.PcdAutogenException;\r
31import org.tianocore.build.exception.PlatformPcdPreprocessBuildException;\r
19bf6b15 32import org.tianocore.build.fpd.FpdParserForThread;\r
a29c47e0 33import org.tianocore.build.fpd.FpdParserTask;\r
c8df018e 34import org.tianocore.build.global.GenBuildLogger;\r
a29c47e0 35import org.tianocore.build.global.GlobalData;\r
de4bb9f6 36import org.tianocore.build.toolchain.ConfigReader;\r
a29c47e0 37import org.tianocore.build.toolchain.ToolChainInfo;\r
4a6a5026 38import org.tianocore.common.definitions.ToolDefinitions;\r
91f7d582 39import org.tianocore.common.exception.EdkException;\r
c8df018e 40import org.tianocore.common.logger.EdkLog;\r
a29c47e0 41\r
2d16dcec 42/**\r
43 <p>\r
44 <code>FrameworkBuildTask</code> is an Ant task. The main function is finding\r
45 and processing a FPD or MSA file, then building a platform or stand-alone \r
46 module. \r
47 \r
48 <p>\r
49 The task search current directory and find out all MSA and FPD files by file\r
50 extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a\r
51 stand-alone module. The ACTIVE_PLATFORM policy is: \r
52 \r
53 <pre>\r
54 1. More than one MSA files, report error; \r
55 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;\r
56 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;\r
57 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;\r
58 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;\r
59 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;\r
60 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform\r
61 and let user choose one. \r
62 </pre>\r
63 \r
64 <p>\r
65 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].\r
66 And load all system environment variables to Ant properties. \r
67 \r
68 <p>\r
69 The usage for this task is : \r
70 \r
71 <pre>\r
72 &lt;FrameworkBuild type="cleanall" /&gt;\r
73 </pre>\r
74 \r
75 @since GenBuild 1.0\r
76**/\r
a29c47e0 77public class FrameworkBuildTask extends Task{\r
78\r
79 private Set<File> buildFiles = new LinkedHashSet<File>();\r
80 \r
81 private Set<File> fpdFiles = new LinkedHashSet<File>();\r
82 \r
83 private Set<File> msaFiles = new LinkedHashSet<File>();\r
84 \r
aca6c736 85 //\r
86 // This is only for none-multi-thread build to reduce overriding message\r
87 //\r
88 public static Hashtable<String, String> originalProperties = new Hashtable<String, String>();\r
89 \r
4a6a5026 90 String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;\r
de4bb9f6 91 \r
4a6a5026 92 String targetFilename = ToolDefinitions.TARGET_FILE_PATH;\r
93 \r
94 String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;\r
de4bb9f6 95 \r
96 String activePlatform = null;\r
aca6c736 97\r
19bf6b15 98 ///\r
99 /// The flag to present current is multi-thread enabled\r
100 ///\r
101 public static boolean multithread = false;\r
aca6c736 102\r
19bf6b15 103 ///\r
104 /// The concurrent thread number\r
105 ///\r
498e9021 106 public static int MAX_CONCURRENT_THREAD_NUMBER = 2;\r
aca6c736 107\r
a29c47e0 108 ///\r
109 /// there are three type: all (build), clean and cleanall\r
110 ///\r
111 private String type = "all";\r
112 \r
113 public void execute() throws BuildException {\r
892b0e7a 114 //\r
115 // set Logger\r
116 //\r
117 GenBuildLogger logger = new GenBuildLogger(getProject());\r
118 EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));\r
119 EdkLog.setLogger(logger);\r
120 \r
91f7d582 121 try {\r
122 processFrameworkBuild();\r
123 } catch (PcdAutogenException e) {\r
124 //\r
125 // Add more logic process here\r
126 //\r
127 throw new BuildException(e.getMessage());\r
128 } catch (AutoGenException e) {\r
129 //\r
130 // Add more logic process here\r
131 //\r
132 throw new BuildException(e.getMessage());\r
133 } catch (PlatformPcdPreprocessBuildException e) {\r
134 //\r
135 // Add more logic process here\r
136 //\r
137 throw new BuildException(e.getMessage());\r
138 } catch (GenBuildException e) {\r
139 //\r
140 // Add more logic process here\r
141 //\r
142 throw new BuildException(e.getMessage());\r
143 } catch (EdkException e) {\r
144 //\r
145 // Add more logic process here\r
146 //\r
147 throw new BuildException(e.getMessage());\r
148 }\r
149 }\r
150 \r
151 private void processFrameworkBuild() throws EdkException, GenBuildException, AutoGenException, PcdAutogenException, PlatformPcdPreprocessBuildException {\r
a29c47e0 152 //\r
153 // Seach build.xml -> .FPD -> .MSA file\r
154 //\r
155 try {\r
156 //\r
157 // Gen Current Working Directory\r
158 //\r
159 File dummyFile = new File(".");\r
160 File cwd = dummyFile.getCanonicalFile();\r
161 File[] files = cwd.listFiles();\r
162 for (int i = 0; i < files.length; i++) {\r
163 if (files[i].isFile()) {\r
164 if (files[i].getName().equalsIgnoreCase("build.xml")) {\r
165 //\r
166 // First, search build.xml, if found, ANT call it\r
167 //\r
168 buildFiles.add(files[i]);\r
169\r
4a6a5026 170 } else if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
a29c47e0 171 //\r
172 // Second, search FPD file, if found, build it\r
173 //\r
174 fpdFiles.add(files[i]);\r
4a6a5026 175 } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
a29c47e0 176 //\r
177 // Third, search MSA file, if found, build it\r
178 //\r
179 msaFiles.add(files[i]);\r
180 }\r
181 }\r
182 }\r
892b0e7a 183 } catch (IOException ex) {\r
184 BuildException buildException = new BuildException("Scanning current directory error. \n" + ex.getMessage());\r
185 buildException.setStackTrace(ex.getStackTrace());\r
186 throw buildException;\r
a29c47e0 187 }\r
188 \r
a29c47e0 189 //\r
190 // Deal with all environment variable (Add them to properties)\r
191 //\r
192 backupSystemProperties();\r
193 \r
194 //\r
de4bb9f6 195 // Read target.txt file\r
a29c47e0 196 //\r
de4bb9f6 197 readTargetFile();\r
198\r
a29c47e0 199 //\r
200 // Global Data initialization\r
201 //\r
9cf435c2 202 File workspacePath = new File(getProject().getProperty("WORKSPACE"));\r
aca6c736 203 getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));\r
4a6a5026 204 GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);\r
a29c47e0 205 \r
de4bb9f6 206 //\r
207 // If find MSA file and ACTIVE_PLATFORM is set, build the module; \r
208 // else fail build. \r
209 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM. \r
210 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform; \r
211 // If find more than one FPD files, let user select one. \r
212 //\r
213 File buildFile = null;\r
214 if (msaFiles.size() > 1) {\r
4a6a5026 215 throw new BuildException("Having more than one MSA file in a directory is not allowed!");\r
216 } else if (msaFiles.size() == 1 && activePlatform == null) {\r
217 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");\r
218 } else if (msaFiles.size() == 1 && activePlatform != null) {\r
de4bb9f6 219 //\r
220 // Build the single module\r
221 //\r
222 buildFile = msaFiles.toArray(new File[1])[0];\r
4a6a5026 223 } else if (activePlatform != null) {\r
de4bb9f6 224 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
4a6a5026 225 } else if (fpdFiles.size() == 1) {\r
de4bb9f6 226 buildFile = fpdFiles.toArray(new File[1])[0];\r
4a6a5026 227 } else if (fpdFiles.size() > 1) {\r
de4bb9f6 228 buildFile = intercommuniteWithUser();\r
229 }\r
230 //\r
231 // If there is no build files or FPD files or MSA files, stop build\r
232 //\r
233 else {\r
4a6a5026 234 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");\r
de4bb9f6 235 }\r
236\r
a29c47e0 237 //\r
238 // Build every FPD files (PLATFORM build)\r
239 //\r
4a6a5026 240 if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
91f7d582 241 EdkLog.log(this, "Processing the FPD file [" + buildFile.getPath() + "] ..>> ");\r
19bf6b15 242 //\r
243 // Iff for platform build will enable the multi-thread if set in target.txt\r
244 //\r
245 if (multithread && type.equalsIgnoreCase("all")) {\r
91f7d582 246 EdkLog.log(this, "Multi-thread build is enabled. ");\r
19bf6b15 247 FpdParserForThread fpdParserForThread = new FpdParserForThread();\r
248 fpdParserForThread.setType(type);\r
249 fpdParserForThread.setProject(getProject());\r
250 fpdParserForThread.setFpdFile(buildFile);\r
c8df018e 251 fpdParserForThread.perform();\r
19bf6b15 252 return ;\r
253 }\r
254 \r
a29c47e0 255 FpdParserTask fpdParserTask = new FpdParserTask();\r
256 fpdParserTask.setType(type);\r
257 fpdParserTask.setProject(getProject());\r
258 fpdParserTask.setFpdFile(buildFile);\r
c8df018e 259 fpdParserTask.perform();\r
caa44816 260 \r
261 //\r
262 // If cleanall delete the Platform_build.xml\r
263 //\r
264 if (type.compareTo("cleanall") == 0) {\r
265 File platformBuildFile = \r
02c768ee 266 new File(getProject().getProperty("BUILD_DIR") \r
caa44816 267 + File.separatorChar \r
268 + getProject().getProperty("PLATFORM") \r
269 + "_build.xml");\r
270 platformBuildFile.deleteOnExit();\r
271 }\r
a29c47e0 272 }\r
273 \r
274 //\r
275 // Build every MSA files (SINGLE MODULE BUILD)\r
276 //\r
4a6a5026 277 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
d8956f14 278 if (multithread) {\r
279 EdkLog.log(this, EdkLog.EDK_WARNING, "Multi-Thead do not take effect on Stand-Alone (Single) module build. ");\r
280 multithread = false;\r
281 }\r
fa2da5b1 282 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
91f7d582 283 EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r
284 EdkLog.log(this, "Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r
a29c47e0 285 GenBuildTask genBuildTask = new GenBuildTask();\r
de4bb9f6 286 genBuildTask.setSingleModuleBuild(true);\r
a29c47e0 287 genBuildTask.setType(type);\r
aca6c736 288 getProject().setProperty("PLATFORM_FILE", activePlatform);\r
289 if( !multithread) {\r
290 originalProperties.put("PLATFORM_FILE", activePlatform);\r
291 }\r
a29c47e0 292 genBuildTask.setProject(getProject());\r
293 genBuildTask.setMsaFile(buildFile);\r
c8df018e 294 genBuildTask.perform();\r
a29c47e0 295 }\r
296 }\r
297 \r
298 /**\r
299 Transfer system environment variables to ANT properties. If system variable \r
300 already exiests in ANT properties, skip it.\r
301 \r
302 **/\r
303 private void backupSystemProperties() {\r
304 Map<String, String> sysProperties = System.getenv();\r
aca6c736 305 Iterator<String> iter = sysProperties.keySet().iterator();\r
a29c47e0 306 while (iter.hasNext()) {\r
307 String name = iter.next();\r
308 \r
309 //\r
310 // If system environment variable is not in ANT properties, add it\r
311 //\r
312 if (getProject().getProperty(name) == null) {\r
aca6c736 313 getProject().setProperty(name, sysProperties.get(name));\r
a29c47e0 314 }\r
315 }\r
aca6c736 316 \r
317 Hashtable allProperties = getProject().getProperties();\r
318 Iterator piter = allProperties.keySet().iterator();\r
319 while (piter.hasNext()) {\r
320 String name = (String)piter.next();\r
321 originalProperties.put(new String(name), new String((String)allProperties.get(name)));\r
322 }\r
a29c47e0 323 }\r
324\r
325 private File intercommuniteWithUser(){\r
326 File file = null;\r
4a6a5026 327 if (fpdFiles.size() > 1) {\r
328 File[] allFiles = new File[fpdFiles.size()];\r
a29c47e0 329 int index = 0;\r
330 Iterator<File> iter = fpdFiles.iterator();\r
331 while (iter.hasNext()) {\r
332 allFiles[index] = iter.next();\r
333 index++;\r
334 }\r
4a6a5026 335\r
91f7d582 336 EdkLog.log(this, "Finding " + allFiles.length + " FPD files: ");\r
a29c47e0 337 for (int i = 0; i < allFiles.length; i++) {\r
338 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r
339 }\r
340 \r
341 boolean flag = true;\r
91f7d582 342 EdkLog.log(this, "Please select one of the following FPD files to build:[1] ");\r
a29c47e0 343 do{\r
344 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r
345 try {\r
346 String str = br.readLine();\r
347 if (str.trim().length() == 0) {\r
348 file = allFiles[0];\r
349 flag = false;\r
350 continue ;\r
351 }\r
352 int indexSelect = Integer.parseInt(str);\r
353 if (indexSelect <=0 || indexSelect > allFiles.length) {\r
91f7d582 354 EdkLog.log(this, "Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
a29c47e0 355 continue ;\r
356 } else {\r
357 file = allFiles[indexSelect - 1];\r
358 flag = false;\r
359 continue ;\r
360 }\r
361 } catch (Exception e) {\r
91f7d582 362 EdkLog.log(this, "Please enter a valid number:[1] ");\r
a29c47e0 363 flag = true;\r
364 }\r
365 } while (flag);\r
4a6a5026 366 } else if (fpdFiles.size() == 1) {\r
a29c47e0 367 file = fpdFiles.toArray(new File[1])[0];\r
368 }\r
a29c47e0 369 return file;\r
370 }\r
371 \r
372 \r
373 public void setType(String type) {\r
374 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r
375 this.type = type.toLowerCase();\r
4a6a5026 376 } else {\r
a29c47e0 377 this.type = "all";\r
378 }\r
379 }\r
de4bb9f6 380 \r
91f7d582 381 private void readTargetFile() throws EdkException{\r
382 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r
383 \r
384 String[][] targetFileInfo = ConfigReader.parse(targetFile);\r
385 \r
386 //\r
387 // Get ToolChain Info from target.txt\r
388 //\r
389 ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
390 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r
391 if (str == null || str.trim().equals("")) {\r
392 envToolChainInfo.addTargets("*");\r
393 } else {\r
394 envToolChainInfo.addTargets(str);\r
395 }\r
396 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r
397 if (str == null || str.trim().equals("")) {\r
398 envToolChainInfo.addTagnames("*");\r
399 } else {\r
400 envToolChainInfo.addTagnames(str);\r
401 }\r
402 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r
403 if (str == null || str.trim().equals("")) {\r
404 envToolChainInfo.addArchs("*");\r
405 } else {\r
406 envToolChainInfo.addArchs(str);\r
407 }\r
408 GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
409 \r
410 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r
411 if (str != null && str.trim().length() > 0) {\r
412 toolsDefFilename = str;\r
413 }\r
414 \r
415 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r
416 if (str != null && ! str.trim().equals("")) {\r
417 if ( ! str.endsWith(".fpd")) {\r
418 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r
19bf6b15 419 }\r
91f7d582 420 activePlatform = str;\r
421 }\r
422 \r
423 str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);\r
424 if (str != null && str.trim().equalsIgnoreCase("Enable")) {\r
425 multithread = true;\r
426 }\r
2eb7d78d 427\r
91f7d582 428 str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);\r
429 if (str != null ) {\r
430 try {\r
431 int threadNum = Integer.parseInt(str);\r
432 if (threadNum > 0) {\r
433 MAX_CONCURRENT_THREAD_NUMBER = threadNum;\r
19bf6b15 434 }\r
892b0e7a 435 } catch (Exception ex) {\r
19bf6b15 436 }\r
de4bb9f6 437 }\r
de4bb9f6 438 }\r
439 \r
440 private String getValue(String key, String[][] map) {\r
441 for (int i = 0; i < map[0].length; i++){\r
442 if (key.equalsIgnoreCase(map[0][i])) {\r
443 return map[1][i];\r
444 }\r
445 }\r
446 return null;\r
447 }\r
a29c47e0 448}\r