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