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