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