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