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