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