Support prebuild and postbuild for UserExtension for Platform build. If UserExtension...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / FrameworkBuildTask.java
CommitLineData
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 14package org.tianocore.build;\r
15\r
16import java.io.BufferedReader;\r
17import java.io.File;\r
18import java.io.InputStreamReader;\r
19import java.util.Iterator;\r
20import java.util.LinkedHashSet;\r
21import java.util.Map;\r
22import java.util.Set;\r
23\r
24import org.apache.tools.ant.BuildException;\r
25import org.apache.tools.ant.Task;\r
26import org.tianocore.build.fpd.FpdParserTask;\r
27import org.tianocore.build.global.GlobalData;\r
de4bb9f6 28import org.tianocore.build.toolchain.ConfigReader;\r
a29c47e0 29import org.tianocore.build.toolchain.ToolChainInfo;\r
30\r
31public 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
83 e.printStackTrace();\r
84 throw new BuildException(e.getMessage());\r
85 }\r
86 \r
a29c47e0 87 //\r
88 // Deal with all environment variable (Add them to properties)\r
89 //\r
90 backupSystemProperties();\r
91 \r
92 //\r
de4bb9f6 93 // Read target.txt file\r
a29c47e0 94 //\r
de4bb9f6 95 readTargetFile();\r
96\r
a29c47e0 97 //\r
98 // Global Data initialization\r
99 //\r
a29c47e0 100 GlobalData.initInfo("Tools" + File.separatorChar + "Conf" + File.separatorChar + "FrameworkDatabase.db",\r
101 getProject().getProperty("WORKSPACE_DIR"), toolsDefFilename);\r
102 \r
de4bb9f6 103\r
104 \r
105 //\r
106 // If find MSA file and ACTIVE_PLATFORM is set, build the module; \r
107 // else fail build. \r
108 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM. \r
109 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform; \r
110 // If find more than one FPD files, let user select one. \r
111 //\r
112 File buildFile = null;\r
113 if (msaFiles.size() > 1) {\r
114 throw new BuildException("More than one MSA file under current directory. It is not allowd. ");\r
115 }\r
116 else if (msaFiles.size() == 1 && activePlatform == null) {\r
117 throw new BuildException("If try to build a single module, please set ACTIVE_PLATFORM in file [Tool/Conf/target.txt]. ");\r
118 }\r
119 else if (msaFiles.size() == 1 && activePlatform != null) {\r
120 //\r
121 // Build the single module\r
122 //\r
123 buildFile = msaFiles.toArray(new File[1])[0];\r
124 }\r
125 else if (activePlatform != null) {\r
126 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);\r
127 }\r
128 else if (fpdFiles.size() == 1) {\r
129 buildFile = fpdFiles.toArray(new File[1])[0];\r
130 }\r
131 else if (fpdFiles.size() > 1) {\r
132 buildFile = intercommuniteWithUser();\r
133 }\r
134 //\r
135 // If there is no build files or FPD files or MSA files, stop build\r
136 //\r
137 else {\r
138 throw new BuildException("Can't find any FPD files or MSA files in current directory. ");\r
139 }\r
140\r
a29c47e0 141 //\r
142 // Build every FPD files (PLATFORM build)\r
143 //\r
144 if (buildFile.getName().endsWith(".fpd")) {\r
de4bb9f6 145 System.out.println("Start to build FPD file [" + buildFile.getPath() + "] ..>> ");\r
a29c47e0 146 FpdParserTask fpdParserTask = new FpdParserTask();\r
147 fpdParserTask.setType(type);\r
148 fpdParserTask.setProject(getProject());\r
149 fpdParserTask.setFpdFile(buildFile);\r
150 fpdParserTask.execute();\r
151 }\r
152 \r
153 //\r
154 // Build every MSA files (SINGLE MODULE BUILD)\r
155 //\r
156 else if (buildFile.getName().endsWith(".msa")) {\r
de4bb9f6 157 System.out.println("Start to build 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
191 if (fpdFiles.size() + msaFiles.size() > 1) {\r
192 File[] allFiles = new File[fpdFiles.size() + msaFiles.size()];\r
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
199 iter = msaFiles.iterator();\r
200 while (iter.hasNext()) {\r
201 allFiles[index] = iter.next();\r
202 index++;\r
203 }\r
204 System.out.println("Find " + allFiles.length + " FPD and MSA files: ");\r
205 for (int i = 0; i < allFiles.length; i++) {\r
206 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());\r
207 }\r
208 \r
209 boolean flag = true;\r
210 System.out.print("Please select one file to build:[1] ");\r
211 do{\r
212 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r
213 try {\r
214 String str = br.readLine();\r
215 if (str.trim().length() == 0) {\r
216 file = allFiles[0];\r
217 flag = false;\r
218 continue ;\r
219 }\r
220 int indexSelect = Integer.parseInt(str);\r
221 if (indexSelect <=0 || indexSelect > allFiles.length) {\r
222 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");\r
223 continue ;\r
224 } else {\r
225 file = allFiles[indexSelect - 1];\r
226 flag = false;\r
227 continue ;\r
228 }\r
229 } catch (Exception e) {\r
230 System.out.print("Please enter a valid number:[1] ");\r
231 flag = true;\r
232 }\r
233 } while (flag);\r
234 }\r
235 else if (fpdFiles.size() == 1) {\r
236 file = fpdFiles.toArray(new File[1])[0];\r
237 }\r
238 else if (msaFiles.size() == 1) {\r
239 file = msaFiles.toArray(new File[1])[0];\r
240 }\r
241 return file;\r
242 }\r
243 \r
244 \r
245 public void setType(String type) {\r
246 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {\r
247 this.type = type.toLowerCase();\r
248 }\r
249 else {\r
250 this.type = "all";\r
251 }\r
252 }\r
de4bb9f6 253 \r
254 private void readTargetFile(){\r
255 try {\r
256 String[][] targetFileInfo = ConfigReader.parse(getProject().getProperty("WORKSPACE_DIR"), "Tools" + File.separatorChar + "Conf" + File.separatorChar + targetFilename);\r
257 \r
258 //\r
259 // Get ToolChain Info from target.txt\r
260 //\r
261 ToolChainInfo envToolChainInfo = new ToolChainInfo(); \r
262 String str = getValue("TARGET", targetFileInfo);\r
263 if (str == null || str.trim().equals("")) {\r
264 envToolChainInfo.addTargets("*");\r
265 }\r
266 else {\r
267 envToolChainInfo.addTargets(str);\r
268 }\r
c773bec0 269 str = getValue("TOOL_CHAIN_TAG", targetFileInfo);\r
de4bb9f6 270 if (str == null || str.trim().equals("")) {\r
271 envToolChainInfo.addTagnames("*");\r
272 }\r
273 else {\r
274 envToolChainInfo.addTagnames(str);\r
275 }\r
276 str = getValue("TARGET_ARCH", targetFileInfo);\r
277 if (str == null || str.trim().equals("")) {\r
278 envToolChainInfo.addArchs("*");\r
279 }\r
280 else {\r
281 envToolChainInfo.addArchs(str);\r
282 }\r
283 GlobalData.setToolChainEnvInfo(envToolChainInfo);\r
284 \r
c773bec0 285 str = getValue("TOOL_CHAIN_CONF", targetFileInfo);\r
de4bb9f6 286 if (str != null) {\r
287 toolsDefFilename = str;\r
288 }\r
289 \r
290 str = getValue("ACTIVE_PLATFORM", targetFileInfo);\r
291 if (str != null && ! str.trim().equals("")) {\r
292 if ( ! str.endsWith(".fpd")) {\r
293 throw new BuildException("FPD file's file extension must be \".fpd\"");\r
294 }\r
295 activePlatform = str;\r
296 }\r
297 }\r
298 catch (Exception ex) {\r
299 throw new BuildException(ex.getMessage());\r
300 }\r
301 }\r
302 \r
303 private String getValue(String key, String[][] map) {\r
304 for (int i = 0; i < map[0].length; i++){\r
305 if (key.equalsIgnoreCase(map[0][i])) {\r
306 return map[1][i];\r
307 }\r
308 }\r
309 return null;\r
310 }\r
a29c47e0 311}\r