Auto close multi-thread flag when stand-alone module build. Also change some logs.
[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
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
d3945b9d 278 multithread = false; \r
fa2da5b1 279 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
91f7d582 280 EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r
281 EdkLog.log(this, "Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r
a29c47e0 282 GenBuildTask genBuildTask = new GenBuildTask();\r
de4bb9f6 283 genBuildTask.setSingleModuleBuild(true);\r
a29c47e0 284 genBuildTask.setType(type);\r
aca6c736 285 getProject().setProperty("PLATFORM_FILE", activePlatform);\r
286 if( !multithread) {\r
287 originalProperties.put("PLATFORM_FILE", activePlatform);\r
288 }\r
a29c47e0 289 genBuildTask.setProject(getProject());\r
290 genBuildTask.setMsaFile(buildFile);\r
c8df018e 291 genBuildTask.perform();\r
a29c47e0 292 }\r
293 }\r
294 \r
295 /**\r
296 Transfer system environment variables to ANT properties. If system variable \r
297 already exiests in ANT properties, skip it.\r
298 \r
299 **/\r
300 private void backupSystemProperties() {\r
301 Map<String, String> sysProperties = System.getenv();\r
aca6c736 302 Iterator<String> iter = sysProperties.keySet().iterator();\r
a29c47e0 303 while (iter.hasNext()) {\r
304 String name = iter.next();\r
305 \r
306 //\r
307 // If system environment variable is not in ANT properties, add it\r
308 //\r
309 if (getProject().getProperty(name) == null) {\r
aca6c736 310 getProject().setProperty(name, sysProperties.get(name));\r
a29c47e0 311 }\r
312 }\r
aca6c736 313 \r
314 Hashtable allProperties = getProject().getProperties();\r
315 Iterator piter = allProperties.keySet().iterator();\r
316 while (piter.hasNext()) {\r
317 String name = (String)piter.next();\r
318 originalProperties.put(new String(name), new String((String)allProperties.get(name)));\r
319 }\r
a29c47e0 320 }\r
321\r
322 private File intercommuniteWithUser(){\r
323 File file = null;\r
4a6a5026 324 if (fpdFiles.size() > 1) {\r
325 File[] allFiles = new File[fpdFiles.size()];\r
a29c47e0 326 int index = 0;\r
327 Iterator<File> iter = fpdFiles.iterator();\r
328 while (iter.hasNext()) {\r
329 allFiles[index] = iter.next();\r
330 index++;\r
331 }\r
4a6a5026 332\r
91f7d582 333 EdkLog.log(this, "Finding " + allFiles.length + " FPD files: ");\r
a29c47e0 334 for (int i = 0; i < allFiles.length; i++) {\r
335 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r
336 }\r
337 \r
338 boolean flag = true;\r
91f7d582 339 EdkLog.log(this, "Please select one of the following FPD files to build:[1] ");\r
a29c47e0 340 do{\r
341 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r
342 try {\r
343 String str = br.readLine();\r
344 if (str.trim().length() == 0) {\r
345 file = allFiles[0];\r
346 flag = false;\r
347 continue ;\r
348 }\r
349 int indexSelect = Integer.parseInt(str);\r
350 if (indexSelect <=0 || indexSelect > allFiles.length) {\r
91f7d582 351 EdkLog.log(this, "Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
a29c47e0 352 continue ;\r
353 } else {\r
354 file = allFiles[indexSelect - 1];\r
355 flag = false;\r
356 continue ;\r
357 }\r
358 } catch (Exception e) {\r
91f7d582 359 EdkLog.log(this, "Please enter a valid number:[1] ");\r
a29c47e0 360 flag = true;\r
361 }\r
362 } while (flag);\r
4a6a5026 363 } else if (fpdFiles.size() == 1) {\r
a29c47e0 364 file = fpdFiles.toArray(new File[1])[0];\r
365 }\r
a29c47e0 366 return file;\r
367 }\r
368 \r
369 \r
370 public void setType(String type) {\r
371 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r
372 this.type = type.toLowerCase();\r
4a6a5026 373 } else {\r
a29c47e0 374 this.type = "all";\r
375 }\r
376 }\r
de4bb9f6 377 \r
91f7d582 378 private void readTargetFile() throws EdkException{\r
379 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r
380 \r
381 String[][] targetFileInfo = ConfigReader.parse(targetFile);\r
382 \r
383 //\r
384 // Get ToolChain Info from target.txt\r
385 //\r
386 ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
387 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r
388 if (str == null || str.trim().equals("")) {\r
389 envToolChainInfo.addTargets("*");\r
390 } else {\r
391 envToolChainInfo.addTargets(str);\r
392 }\r
393 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r
394 if (str == null || str.trim().equals("")) {\r
395 envToolChainInfo.addTagnames("*");\r
396 } else {\r
397 envToolChainInfo.addTagnames(str);\r
398 }\r
399 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r
400 if (str == null || str.trim().equals("")) {\r
401 envToolChainInfo.addArchs("*");\r
402 } else {\r
403 envToolChainInfo.addArchs(str);\r
404 }\r
405 GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
406 \r
407 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r
408 if (str != null && str.trim().length() > 0) {\r
409 toolsDefFilename = str;\r
410 }\r
411 \r
412 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r
413 if (str != null && ! str.trim().equals("")) {\r
414 if ( ! str.endsWith(".fpd")) {\r
415 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r
19bf6b15 416 }\r
91f7d582 417 activePlatform = str;\r
418 }\r
419 \r
420 str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);\r
421 if (str != null && str.trim().equalsIgnoreCase("Enable")) {\r
422 multithread = true;\r
423 }\r
2eb7d78d 424\r
91f7d582 425 str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);\r
426 if (str != null ) {\r
427 try {\r
428 int threadNum = Integer.parseInt(str);\r
429 if (threadNum > 0) {\r
430 MAX_CONCURRENT_THREAD_NUMBER = threadNum;\r
19bf6b15 431 }\r
892b0e7a 432 } catch (Exception ex) {\r
19bf6b15 433 }\r
de4bb9f6 434 }\r
de4bb9f6 435 }\r
436 \r
437 private String getValue(String key, String[][] map) {\r
438 for (int i = 0; i < map[0].length; i++){\r
439 if (key.equalsIgnoreCase(map[0][i])) {\r
440 return map[1][i];\r
441 }\r
442 }\r
443 return null;\r
444 }\r
a29c47e0 445}\r