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