1.modify the usage info
[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
106 public static int MAX_CONCURRENT_THREAD_NUMBER = 1;\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
91f7d582 114 try {\r
115 processFrameworkBuild();\r
116 } catch (PcdAutogenException e) {\r
117 //\r
118 // Add more logic process here\r
119 //\r
120 throw new BuildException(e.getMessage());\r
121 } catch (AutoGenException e) {\r
122 //\r
123 // Add more logic process here\r
124 //\r
125 throw new BuildException(e.getMessage());\r
126 } catch (PlatformPcdPreprocessBuildException e) {\r
127 //\r
128 // Add more logic process here\r
129 //\r
130 throw new BuildException(e.getMessage());\r
131 } catch (GenBuildException e) {\r
132 //\r
133 // Add more logic process here\r
134 //\r
135 throw new BuildException(e.getMessage());\r
136 } catch (EdkException e) {\r
137 //\r
138 // Add more logic process here\r
139 //\r
140 throw new BuildException(e.getMessage());\r
141 }\r
142 }\r
143 \r
144 private void processFrameworkBuild() throws EdkException, GenBuildException, AutoGenException, PcdAutogenException, PlatformPcdPreprocessBuildException {\r
c8df018e 145 //\r
146 // set Logger\r
147 //\r
148 GenBuildLogger logger = new GenBuildLogger(getProject());\r
149 EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));\r
150 EdkLog.setLogger(logger);\r
151\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
91f7d582 183 } catch (IOException e) {\r
a29c47e0 184 throw new BuildException(e.getMessage());\r
185 }\r
186 \r
a29c47e0 187 //\r
188 // Deal with all environment variable (Add them to properties)\r
189 //\r
190 backupSystemProperties();\r
191 \r
192 //\r
de4bb9f6 193 // Read target.txt file\r
a29c47e0 194 //\r
de4bb9f6 195 readTargetFile();\r
196\r
a29c47e0 197 //\r
198 // Global Data initialization\r
199 //\r
9cf435c2 200 File workspacePath = new File(getProject().getProperty("WORKSPACE"));\r
aca6c736 201 getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));\r
4a6a5026 202 GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);\r
a29c47e0 203 \r
de4bb9f6 204 //\r
205 // If find MSA file and ACTIVE_PLATFORM is set, build the module; \r
206 // else fail build. \r
207 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM. \r
208 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform; \r
209 // If find more than one FPD files, let user select one. \r
210 //\r
211 File buildFile = null;\r
212 if (msaFiles.size() > 1) {\r
4a6a5026 213 throw new BuildException("Having more than one MSA file in a directory is not allowed!");\r
214 } else if (msaFiles.size() == 1 && activePlatform == null) {\r
215 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");\r
216 } else if (msaFiles.size() == 1 && activePlatform != null) {\r
de4bb9f6 217 //\r
218 // Build the single module\r
219 //\r
220 buildFile = msaFiles.toArray(new File[1])[0];\r
4a6a5026 221 } else if (activePlatform != null) {\r
de4bb9f6 222 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
4a6a5026 223 } else if (fpdFiles.size() == 1) {\r
de4bb9f6 224 buildFile = fpdFiles.toArray(new File[1])[0];\r
4a6a5026 225 } else if (fpdFiles.size() > 1) {\r
de4bb9f6 226 buildFile = intercommuniteWithUser();\r
227 }\r
228 //\r
229 // If there is no build files or FPD files or MSA files, stop build\r
230 //\r
231 else {\r
4a6a5026 232 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");\r
de4bb9f6 233 }\r
234\r
a29c47e0 235 //\r
236 // Build every FPD files (PLATFORM build)\r
237 //\r
4a6a5026 238 if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r
91f7d582 239 EdkLog.log(this, "Processing the FPD file [" + buildFile.getPath() + "] ..>> ");\r
19bf6b15 240 //\r
241 // Iff for platform build will enable the multi-thread if set in target.txt\r
242 //\r
243 if (multithread && type.equalsIgnoreCase("all")) {\r
91f7d582 244 EdkLog.log(this, "Multi-thread build is enabled. ");\r
19bf6b15 245 FpdParserForThread fpdParserForThread = new FpdParserForThread();\r
246 fpdParserForThread.setType(type);\r
247 fpdParserForThread.setProject(getProject());\r
248 fpdParserForThread.setFpdFile(buildFile);\r
c8df018e 249 fpdParserForThread.perform();\r
19bf6b15 250 return ;\r
251 }\r
252 \r
a29c47e0 253 FpdParserTask fpdParserTask = new FpdParserTask();\r
254 fpdParserTask.setType(type);\r
255 fpdParserTask.setProject(getProject());\r
256 fpdParserTask.setFpdFile(buildFile);\r
c8df018e 257 fpdParserTask.perform();\r
caa44816 258 \r
259 //\r
260 // If cleanall delete the Platform_build.xml\r
261 //\r
262 if (type.compareTo("cleanall") == 0) {\r
263 File platformBuildFile = \r
264 new File(getProject().getProperty("PLATFORM_DIR") \r
265 + File.separatorChar \r
266 + getProject().getProperty("PLATFORM") \r
267 + "_build.xml");\r
268 platformBuildFile.deleteOnExit();\r
269 }\r
a29c47e0 270 }\r
271 \r
272 //\r
273 // Build every MSA files (SINGLE MODULE BUILD)\r
274 //\r
4a6a5026 275 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r
fa2da5b1 276 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
91f7d582 277 EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r
278 EdkLog.log(this, "Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r
a29c47e0 279 GenBuildTask genBuildTask = new GenBuildTask();\r
de4bb9f6 280 genBuildTask.setSingleModuleBuild(true);\r
a29c47e0 281 genBuildTask.setType(type);\r
aca6c736 282 getProject().setProperty("PLATFORM_FILE", activePlatform);\r
283 if( !multithread) {\r
284 originalProperties.put("PLATFORM_FILE", activePlatform);\r
285 }\r
a29c47e0 286 genBuildTask.setProject(getProject());\r
287 genBuildTask.setMsaFile(buildFile);\r
c8df018e 288 genBuildTask.perform();\r
a29c47e0 289 }\r
290 }\r
291 \r
292 /**\r
293 Transfer system environment variables to ANT properties. If system variable \r
294 already exiests in ANT properties, skip it.\r
295 \r
296 **/\r
297 private void backupSystemProperties() {\r
298 Map<String, String> sysProperties = System.getenv();\r
aca6c736 299 Iterator<String> iter = sysProperties.keySet().iterator();\r
a29c47e0 300 while (iter.hasNext()) {\r
301 String name = iter.next();\r
302 \r
303 //\r
304 // If system environment variable is not in ANT properties, add it\r
305 //\r
306 if (getProject().getProperty(name) == null) {\r
aca6c736 307 getProject().setProperty(name, sysProperties.get(name));\r
a29c47e0 308 }\r
309 }\r
aca6c736 310 \r
311 Hashtable allProperties = getProject().getProperties();\r
312 Iterator piter = allProperties.keySet().iterator();\r
313 while (piter.hasNext()) {\r
314 String name = (String)piter.next();\r
315 originalProperties.put(new String(name), new String((String)allProperties.get(name)));\r
316 }\r
a29c47e0 317 }\r
318\r
319 private File intercommuniteWithUser(){\r
320 File file = null;\r
4a6a5026 321 if (fpdFiles.size() > 1) {\r
322 File[] allFiles = new File[fpdFiles.size()];\r
a29c47e0 323 int index = 0;\r
324 Iterator<File> iter = fpdFiles.iterator();\r
325 while (iter.hasNext()) {\r
326 allFiles[index] = iter.next();\r
327 index++;\r
328 }\r
4a6a5026 329\r
91f7d582 330 EdkLog.log(this, "Finding " + allFiles.length + " FPD files: ");\r
a29c47e0 331 for (int i = 0; i < allFiles.length; i++) {\r
332 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r
333 }\r
334 \r
335 boolean flag = true;\r
91f7d582 336 EdkLog.log(this, "Please select one of the following FPD files to build:[1] ");\r
a29c47e0 337 do{\r
338 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r
339 try {\r
340 String str = br.readLine();\r
341 if (str.trim().length() == 0) {\r
342 file = allFiles[0];\r
343 flag = false;\r
344 continue ;\r
345 }\r
346 int indexSelect = Integer.parseInt(str);\r
347 if (indexSelect <=0 || indexSelect > allFiles.length) {\r
91f7d582 348 EdkLog.log(this, "Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
a29c47e0 349 continue ;\r
350 } else {\r
351 file = allFiles[indexSelect - 1];\r
352 flag = false;\r
353 continue ;\r
354 }\r
355 } catch (Exception e) {\r
91f7d582 356 EdkLog.log(this, "Please enter a valid number:[1] ");\r
a29c47e0 357 flag = true;\r
358 }\r
359 } while (flag);\r
4a6a5026 360 } else if (fpdFiles.size() == 1) {\r
a29c47e0 361 file = fpdFiles.toArray(new File[1])[0];\r
362 }\r
a29c47e0 363 return file;\r
364 }\r
365 \r
366 \r
367 public void setType(String type) {\r
368 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r
369 this.type = type.toLowerCase();\r
4a6a5026 370 } else {\r
a29c47e0 371 this.type = "all";\r
372 }\r
373 }\r
de4bb9f6 374 \r
91f7d582 375 private void readTargetFile() throws EdkException{\r
376 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r
377 \r
378 String[][] targetFileInfo = ConfigReader.parse(targetFile);\r
379 \r
380 //\r
381 // Get ToolChain Info from target.txt\r
382 //\r
383 ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
384 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r
385 if (str == null || str.trim().equals("")) {\r
386 envToolChainInfo.addTargets("*");\r
387 } else {\r
388 envToolChainInfo.addTargets(str);\r
389 }\r
390 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r
391 if (str == null || str.trim().equals("")) {\r
392 envToolChainInfo.addTagnames("*");\r
393 } else {\r
394 envToolChainInfo.addTagnames(str);\r
395 }\r
396 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r
397 if (str == null || str.trim().equals("")) {\r
398 envToolChainInfo.addArchs("*");\r
399 } else {\r
400 envToolChainInfo.addArchs(str);\r
401 }\r
402 GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
403 \r
404 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r
405 if (str != null && str.trim().length() > 0) {\r
406 toolsDefFilename = str;\r
407 }\r
408 \r
409 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r
410 if (str != null && ! str.trim().equals("")) {\r
411 if ( ! str.endsWith(".fpd")) {\r
412 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r
19bf6b15 413 }\r
91f7d582 414 activePlatform = str;\r
415 }\r
416 \r
417 str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);\r
418 if (str != null && str.trim().equalsIgnoreCase("Enable")) {\r
419 multithread = true;\r
420 }\r
19bf6b15 421 \r
91f7d582 422 str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);\r
423 if (str != null ) {\r
424 try {\r
425 int threadNum = Integer.parseInt(str);\r
426 if (threadNum > 0) {\r
427 MAX_CONCURRENT_THREAD_NUMBER = threadNum;\r
19bf6b15 428 }\r
91f7d582 429 } catch (Exception enuma) {\r
19bf6b15 430 }\r
de4bb9f6 431 }\r
de4bb9f6 432 }\r
433 \r
434 private String getValue(String key, String[][] map) {\r
435 for (int i = 0; i < map[0].length; i++){\r
436 if (key.equalsIgnoreCase(map[0][i])) {\r
437 return map[1][i];\r
438 }\r
439 }\r
440 return null;\r
441 }\r
a29c47e0 442}\r