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