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