]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/FrameworkBuildTask.java
Fixed EDKT102;
[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 GlobalData.initInfo("Tools" + File.separatorChar + "Conf" + File.separatorChar + "FrameworkDatabase.db",
100 getProject().getProperty("WORKSPACE_DIR"), toolsDefFilename);
101
102
103
104 //
105 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
106 // else fail build.
107 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
108 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
109 // If find more than one FPD files, let user select one.
110 //
111 File buildFile = null;
112 if (msaFiles.size() > 1) {
113 throw new BuildException("More than one MSA file under current directory. It is not allowd. ");
114 }
115 else if (msaFiles.size() == 1 && activePlatform == null) {
116 throw new BuildException("If try to build a single module, please set ACTIVE_PLATFORM in file [Tool/Conf/target.txt]. ");
117 }
118 else if (msaFiles.size() == 1 && activePlatform != null) {
119 //
120 // Build the single module
121 //
122 buildFile = msaFiles.toArray(new File[1])[0];
123 }
124 else if (activePlatform != null) {
125 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
126 }
127 else if (fpdFiles.size() == 1) {
128 buildFile = fpdFiles.toArray(new File[1])[0];
129 }
130 else if (fpdFiles.size() > 1) {
131 buildFile = intercommuniteWithUser();
132 }
133 //
134 // If there is no build files or FPD files or MSA files, stop build
135 //
136 else {
137 throw new BuildException("Can't find any FPD files or MSA files in current directory. ");
138 }
139
140 //
141 // Build every FPD files (PLATFORM build)
142 //
143 if (buildFile.getName().endsWith(".fpd")) {
144 System.out.println("Start to build FPD file [" + buildFile.getPath() + "] ..>> ");
145 FpdParserTask fpdParserTask = new FpdParserTask();
146 fpdParserTask.setType(type);
147 fpdParserTask.setProject(getProject());
148 fpdParserTask.setFpdFile(buildFile);
149 fpdParserTask.execute();
150 }
151
152 //
153 // Build every MSA files (SINGLE MODULE BUILD)
154 //
155 else if (buildFile.getName().endsWith(".msa")) {
156 System.out.println("Start to build MSA file [" + buildFile.getPath() + "] ..>> ");
157 GenBuildTask genBuildTask = new GenBuildTask();
158 genBuildTask.setSingleModuleBuild(true);
159 genBuildTask.setType(type);
160 getProject().setProperty("PLATFORM_FILE", activePlatform);
161 genBuildTask.setProject(getProject());
162 genBuildTask.setMsaFile(buildFile);
163 genBuildTask.execute();
164 }
165 }
166
167 /**
168 Transfer system environment variables to ANT properties. If system variable
169 already exiests in ANT properties, skip it.
170
171 **/
172 private void backupSystemProperties() {
173 Map<String, String> sysProperties = System.getenv();
174 Set<String> keys = sysProperties.keySet();
175 Iterator<String> iter = keys.iterator();
176 while (iter.hasNext()) {
177 String name = iter.next();
178
179 //
180 // If system environment variable is not in ANT properties, add it
181 //
182 if (getProject().getProperty(name) == null) {
183 getProject().setProperty(name, sysProperties.get(name));
184 }
185 }
186 }
187
188 private File intercommuniteWithUser(){
189 File file = null;
190 if (fpdFiles.size() + msaFiles.size() > 1) {
191 File[] allFiles = new File[fpdFiles.size() + msaFiles.size()];
192 int index = 0;
193 Iterator<File> iter = fpdFiles.iterator();
194 while (iter.hasNext()) {
195 allFiles[index] = iter.next();
196 index++;
197 }
198 iter = msaFiles.iterator();
199 while (iter.hasNext()) {
200 allFiles[index] = iter.next();
201 index++;
202 }
203 System.out.println("Find " + allFiles.length + " FPD and MSA files: ");
204 for (int i = 0; i < allFiles.length; i++) {
205 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());
206 }
207
208 boolean flag = true;
209 System.out.print("Please select one file to build:[1] ");
210 do{
211 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
212 try {
213 String str = br.readLine();
214 if (str.trim().length() == 0) {
215 file = allFiles[0];
216 flag = false;
217 continue ;
218 }
219 int indexSelect = Integer.parseInt(str);
220 if (indexSelect <=0 || indexSelect > allFiles.length) {
221 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");
222 continue ;
223 } else {
224 file = allFiles[indexSelect - 1];
225 flag = false;
226 continue ;
227 }
228 } catch (Exception e) {
229 System.out.print("Please enter a valid number:[1] ");
230 flag = true;
231 }
232 } while (flag);
233 }
234 else if (fpdFiles.size() == 1) {
235 file = fpdFiles.toArray(new File[1])[0];
236 }
237 else if (msaFiles.size() == 1) {
238 file = msaFiles.toArray(new File[1])[0];
239 }
240 return file;
241 }
242
243
244 public void setType(String type) {
245 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {
246 this.type = type.toLowerCase();
247 }
248 else {
249 this.type = "all";
250 }
251 }
252
253 private void readTargetFile(){
254 try {
255 String[][] targetFileInfo = ConfigReader.parse(getProject().getProperty("WORKSPACE_DIR"), "Tools" + File.separatorChar + "Conf" + File.separatorChar + targetFilename);
256
257 //
258 // Get ToolChain Info from target.txt
259 //
260 ToolChainInfo envToolChainInfo = new ToolChainInfo();
261 String str = getValue("TARGET", targetFileInfo);
262 if (str == null || str.trim().equals("")) {
263 envToolChainInfo.addTargets("*");
264 }
265 else {
266 envToolChainInfo.addTargets(str);
267 }
268 str = getValue("TOOL_CHAIN_TAG", targetFileInfo);
269 if (str == null || str.trim().equals("")) {
270 envToolChainInfo.addTagnames("*");
271 }
272 else {
273 envToolChainInfo.addTagnames(str);
274 }
275 str = getValue("TARGET_ARCH", targetFileInfo);
276 if (str == null || str.trim().equals("")) {
277 envToolChainInfo.addArchs("*");
278 }
279 else {
280 envToolChainInfo.addArchs(str);
281 }
282 GlobalData.setToolChainEnvInfo(envToolChainInfo);
283
284 str = getValue("TOOL_CHAIN_CONF", targetFileInfo);
285 if (str != null && str.trim().length() > 0) {
286 toolsDefFilename = str;
287 }
288
289 str = getValue("ACTIVE_PLATFORM", targetFileInfo);
290 if (str != null && ! str.trim().equals("")) {
291 if ( ! str.endsWith(".fpd")) {
292 throw new BuildException("FPD file's file extension must be \".fpd\"");
293 }
294 activePlatform = str;
295 }
296 }
297 catch (Exception ex) {
298 throw new BuildException(ex.getMessage());
299 }
300 }
301
302 private String getValue(String key, String[][] map) {
303 for (int i = 0; i < map[0].length; i++){
304 if (key.equalsIgnoreCase(map[0][i])) {
305 return map[1][i];
306 }
307 }
308 return null;
309 }
310 }