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