]>
Commit | Line | Data |
---|---|---|
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 | |
14 | package org.tianocore.build;\r | |
15 | \r | |
16 | import java.io.BufferedReader;\r | |
17 | import java.io.File;\r | |
18 | import java.io.IOException;\r | |
19 | import java.io.InputStreamReader;\r | |
20 | import java.util.Hashtable;\r | |
21 | import java.util.Iterator;\r | |
22 | import java.util.LinkedHashSet;\r | |
23 | import java.util.Map;\r | |
24 | import java.util.Set;\r | |
25 | \r | |
26 | import org.apache.tools.ant.BuildException;\r | |
27 | import org.apache.tools.ant.Task;\r | |
28 | import org.tianocore.build.exception.AutoGenException;\r | |
29 | import org.tianocore.build.exception.GenBuildException;\r | |
30 | import org.tianocore.build.exception.PcdAutogenException;\r | |
31 | import org.tianocore.build.exception.PlatformPcdPreprocessBuildException;\r | |
32 | import org.tianocore.build.fpd.FpdParserForThread;\r | |
33 | import org.tianocore.build.fpd.FpdParserTask;\r | |
34 | import org.tianocore.build.global.GenBuildLogger;\r | |
35 | import org.tianocore.build.global.GlobalData;\r | |
36 | import org.tianocore.build.toolchain.ConfigReader;\r | |
37 | import org.tianocore.build.toolchain.ToolChainInfo;\r | |
38 | import org.tianocore.common.definitions.ToolDefinitions;\r | |
39 | import org.tianocore.common.exception.EdkException;\r | |
40 | import org.tianocore.common.logger.EdkLog;\r | |
41 | \r | |
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 | <FrameworkBuild type="cleanall" />\r | |
73 | </pre>\r | |
74 | \r | |
75 | @since GenBuild 1.0\r | |
76 | **/\r | |
77 | public 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 | |
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 | |
90 | String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;\r | |
91 | \r | |
92 | String targetFilename = ToolDefinitions.TARGET_FILE_PATH;\r | |
93 | \r | |
94 | String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;\r | |
95 | \r | |
96 | String activePlatform = null;\r | |
97 | \r | |
98 | ///\r | |
99 | /// The flag to present current is multi-thread enabled\r | |
100 | ///\r | |
101 | public static boolean multithread = false;\r | |
102 | \r | |
103 | ///\r | |
104 | /// The concurrent thread number\r | |
105 | ///\r | |
106 | public static int MAX_CONCURRENT_THREAD_NUMBER = 2;\r | |
107 | \r | |
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 | |
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 | |
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 | |
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 | |
170 | } else if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r | |
171 | //\r | |
172 | // Second, search FPD file, if found, build it\r | |
173 | //\r | |
174 | fpdFiles.add(files[i]);\r | |
175 | } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r | |
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 | |
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 | |
187 | }\r | |
188 | \r | |
189 | //\r | |
190 | // Deal with all environment variable (Add them to properties)\r | |
191 | //\r | |
192 | backupSystemProperties();\r | |
193 | \r | |
194 | //\r | |
195 | // Read target.txt file\r | |
196 | //\r | |
197 | readTargetFile();\r | |
198 | \r | |
199 | //\r | |
200 | // Global Data initialization\r | |
201 | //\r | |
202 | File workspacePath = new File(getProject().getProperty("WORKSPACE"));\r | |
203 | getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));\r | |
204 | GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);\r | |
205 | \r | |
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 | |
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 | |
219 | //\r | |
220 | // Build the single module\r | |
221 | //\r | |
222 | buildFile = msaFiles.toArray(new File[1])[0];\r | |
223 | } else if (activePlatform != null) {\r | |
224 | buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r | |
225 | } else if (fpdFiles.size() == 1) {\r | |
226 | buildFile = fpdFiles.toArray(new File[1])[0];\r | |
227 | } else if (fpdFiles.size() > 1) {\r | |
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 | |
234 | throw new BuildException("Can't find any FPD or MSA files in the current directory. ");\r | |
235 | }\r | |
236 | \r | |
237 | //\r | |
238 | // Build every FPD files (PLATFORM build)\r | |
239 | //\r | |
240 | if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {\r | |
241 | EdkLog.log(this, "Processing the FPD file [" + buildFile.getPath() + "] ..>> ");\r | |
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 | |
246 | EdkLog.log(this, "Multi-thread build is enabled. ");\r | |
247 | FpdParserForThread fpdParserForThread = new FpdParserForThread();\r | |
248 | fpdParserForThread.setType(type);\r | |
249 | fpdParserForThread.setProject(getProject());\r | |
250 | fpdParserForThread.setFpdFile(buildFile);\r | |
251 | fpdParserForThread.perform();\r | |
252 | return ;\r | |
253 | }\r | |
254 | \r | |
255 | FpdParserTask fpdParserTask = new FpdParserTask();\r | |
256 | fpdParserTask.setType(type);\r | |
257 | fpdParserTask.setProject(getProject());\r | |
258 | fpdParserTask.setFpdFile(buildFile);\r | |
259 | fpdParserTask.perform();\r | |
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 | |
266 | new File(getProject().getProperty("BUILD_DIR") \r | |
267 | + File.separatorChar \r | |
268 | + getProject().getProperty("PLATFORM") \r | |
269 | + "_build.xml");\r | |
270 | platformBuildFile.deleteOnExit();\r | |
271 | }\r | |
272 | }\r | |
273 | \r | |
274 | //\r | |
275 | // Build every MSA files (SINGLE MODULE BUILD)\r | |
276 | //\r | |
277 | else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {\r | |
278 | if (multithread) {\r | |
279 | EdkLog.log(this, EdkLog.EDK_WARNING, "Multi-Thead do not take effect on Stand-Alone (Single) module build. ");\r | |
280 | multithread = false;\r | |
281 | }\r | |
282 | File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r | |
283 | EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");\r | |
284 | EdkLog.log(this, "Processing the MSA file [" + buildFile.getPath() + "] ..>> ");\r | |
285 | GenBuildTask genBuildTask = new GenBuildTask();\r | |
286 | genBuildTask.setSingleModuleBuild(true);\r | |
287 | genBuildTask.setType(type);\r | |
288 | getProject().setProperty("PLATFORM_FILE", activePlatform);\r | |
289 | if( !multithread) {\r | |
290 | originalProperties.put("PLATFORM_FILE", activePlatform);\r | |
291 | }\r | |
292 | genBuildTask.setProject(getProject());\r | |
293 | genBuildTask.setMsaFile(buildFile);\r | |
294 | genBuildTask.perform();\r | |
295 | }\r | |
296 | }\r | |
297 | \r | |
298 | /**\r | |
299 | Transfer system environment variables to ANT properties. If system variable \r | |
300 | already exiests in ANT properties, skip it.\r | |
301 | \r | |
302 | **/\r | |
303 | private void backupSystemProperties() {\r | |
304 | Map<String, String> sysProperties = System.getenv();\r | |
305 | Iterator<String> iter = sysProperties.keySet().iterator();\r | |
306 | while (iter.hasNext()) {\r | |
307 | String name = iter.next();\r | |
308 | \r | |
309 | //\r | |
310 | // If system environment variable is not in ANT properties, add it\r | |
311 | //\r | |
312 | if (getProject().getProperty(name) == null) {\r | |
313 | getProject().setProperty(name, sysProperties.get(name));\r | |
314 | }\r | |
315 | }\r | |
316 | \r | |
317 | Hashtable allProperties = getProject().getProperties();\r | |
318 | Iterator piter = allProperties.keySet().iterator();\r | |
319 | while (piter.hasNext()) {\r | |
320 | String name = (String)piter.next();\r | |
321 | originalProperties.put(new String(name), new String((String)allProperties.get(name)));\r | |
322 | }\r | |
323 | }\r | |
324 | \r | |
325 | private File intercommuniteWithUser(){\r | |
326 | File file = null;\r | |
327 | if (fpdFiles.size() > 1) {\r | |
328 | File[] allFiles = new File[fpdFiles.size()];\r | |
329 | int index = 0;\r | |
330 | Iterator<File> iter = fpdFiles.iterator();\r | |
331 | while (iter.hasNext()) {\r | |
332 | allFiles[index] = iter.next();\r | |
333 | index++;\r | |
334 | }\r | |
335 | \r | |
336 | EdkLog.log(this, "Finding " + allFiles.length + " FPD files: ");\r | |
337 | for (int i = 0; i < allFiles.length; i++) {\r | |
338 | System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r | |
339 | }\r | |
340 | \r | |
341 | boolean flag = true;\r | |
342 | EdkLog.log(this, "Please select one of the following FPD files to build:[1] ");\r | |
343 | do{\r | |
344 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r | |
345 | try {\r | |
346 | String str = br.readLine();\r | |
347 | if (str.trim().length() == 0) {\r | |
348 | file = allFiles[0];\r | |
349 | flag = false;\r | |
350 | continue ;\r | |
351 | }\r | |
352 | int indexSelect = Integer.parseInt(str);\r | |
353 | if (indexSelect <=0 || indexSelect > allFiles.length) {\r | |
354 | EdkLog.log(this, "Please enter a number between [1.." + allFiles.length + "]:[1] ");\r | |
355 | continue ;\r | |
356 | } else {\r | |
357 | file = allFiles[indexSelect - 1];\r | |
358 | flag = false;\r | |
359 | continue ;\r | |
360 | }\r | |
361 | } catch (Exception e) {\r | |
362 | EdkLog.log(this, "Please enter a valid number:[1] ");\r | |
363 | flag = true;\r | |
364 | }\r | |
365 | } while (flag);\r | |
366 | } else if (fpdFiles.size() == 1) {\r | |
367 | file = fpdFiles.toArray(new File[1])[0];\r | |
368 | }\r | |
369 | return file;\r | |
370 | }\r | |
371 | \r | |
372 | \r | |
373 | public void setType(String type) {\r | |
374 | if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r | |
375 | this.type = type.toLowerCase();\r | |
376 | } else {\r | |
377 | this.type = "all";\r | |
378 | }\r | |
379 | }\r | |
380 | \r | |
381 | private void readTargetFile() throws EdkException{\r | |
382 | String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;\r | |
383 | \r | |
384 | String[][] targetFileInfo = ConfigReader.parse(targetFile);\r | |
385 | \r | |
386 | //\r | |
387 | // Get ToolChain Info from target.txt\r | |
388 | //\r | |
389 | ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r | |
390 | String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);\r | |
391 | if (str == null || str.trim().equals("")) {\r | |
392 | envToolChainInfo.addTargets("*");\r | |
393 | } else {\r | |
394 | envToolChainInfo.addTargets(str);\r | |
395 | }\r | |
396 | str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);\r | |
397 | if (str == null || str.trim().equals("")) {\r | |
398 | envToolChainInfo.addTagnames("*");\r | |
399 | } else {\r | |
400 | envToolChainInfo.addTagnames(str);\r | |
401 | }\r | |
402 | str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);\r | |
403 | if (str == null || str.trim().equals("")) {\r | |
404 | envToolChainInfo.addArchs("*");\r | |
405 | } else {\r | |
406 | envToolChainInfo.addArchs(str);\r | |
407 | }\r | |
408 | GlobalData.setToolChainEnvInfo(envToolChainInfo);\r | |
409 | \r | |
410 | str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);\r | |
411 | if (str != null && str.trim().length() > 0) {\r | |
412 | toolsDefFilename = str;\r | |
413 | }\r | |
414 | \r | |
415 | str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);\r | |
416 | if (str != null && ! str.trim().equals("")) {\r | |
417 | if ( ! str.endsWith(".fpd")) {\r | |
418 | throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");\r | |
419 | }\r | |
420 | activePlatform = str;\r | |
421 | }\r | |
422 | \r | |
423 | str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);\r | |
424 | if (str != null && str.trim().equalsIgnoreCase("Enable")) {\r | |
425 | multithread = true;\r | |
426 | }\r | |
427 | \r | |
428 | str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);\r | |
429 | if (str != null ) {\r | |
430 | try {\r | |
431 | int threadNum = Integer.parseInt(str);\r | |
432 | if (threadNum > 0) {\r | |
433 | MAX_CONCURRENT_THREAD_NUMBER = threadNum;\r | |
434 | }\r | |
435 | } catch (Exception ex) {\r | |
436 | }\r | |
437 | }\r | |
438 | }\r | |
439 | \r | |
440 | private String getValue(String key, String[][] map) {\r | |
441 | for (int i = 0; i < map[0].length; i++){\r | |
442 | if (key.equalsIgnoreCase(map[0][i])) {\r | |
443 | return map[1][i];\r | |
444 | }\r | |
445 | }\r | |
446 | return null;\r | |
447 | }\r | |
448 | }\r |