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