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