]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/FrameworkBuildTask.java
Add class header to FrameworkBuildTask. Remove some unused code from top level build...
[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 //
185 // Build every MSA files (SINGLE MODULE BUILD)
186 //
187 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
188 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
189 System.out.println("Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");
190 System.out.println("Processing the MSA file [" + buildFile.getPath() + "] ..>> ");
191 GenBuildTask genBuildTask = new GenBuildTask();
192 genBuildTask.setSingleModuleBuild(true);
193 genBuildTask.setType(type);
194 getProject().setProperty("PLATFORM_FILE", activePlatform);
195 genBuildTask.setProject(getProject());
196 genBuildTask.setMsaFile(buildFile);
197 genBuildTask.execute();
198 }
199 }
200
201 /**
202 Transfer system environment variables to ANT properties. If system variable
203 already exiests in ANT properties, skip it.
204
205 **/
206 private void backupSystemProperties() {
207 Map<String, String> sysProperties = System.getenv();
208 Set<String> keys = sysProperties.keySet();
209 Iterator<String> iter = keys.iterator();
210 while (iter.hasNext()) {
211 String name = iter.next();
212
213 //
214 // If system environment variable is not in ANT properties, add it
215 //
216 if (getProject().getProperty(name) == null) {
217 getProject().setProperty(name, sysProperties.get(name));
218 }
219 }
220 }
221
222 private File intercommuniteWithUser(){
223 File file = null;
224 if (fpdFiles.size() > 1) {
225 File[] allFiles = new File[fpdFiles.size()];
226 int index = 0;
227 Iterator<File> iter = fpdFiles.iterator();
228 while (iter.hasNext()) {
229 allFiles[index] = iter.next();
230 index++;
231 }
232
233 System.out.println("Finding " + allFiles.length + " FPD files: ");
234 for (int i = 0; i < allFiles.length; i++) {
235 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());
236 }
237
238 boolean flag = true;
239 System.out.print("Please select one of the following FPD files to build:[1] ");
240 do{
241 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
242 try {
243 String str = br.readLine();
244 if (str.trim().length() == 0) {
245 file = allFiles[0];
246 flag = false;
247 continue ;
248 }
249 int indexSelect = Integer.parseInt(str);
250 if (indexSelect <=0 || indexSelect > allFiles.length) {
251 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");
252 continue ;
253 } else {
254 file = allFiles[indexSelect - 1];
255 flag = false;
256 continue ;
257 }
258 } catch (Exception e) {
259 System.out.print("Please enter a valid number:[1] ");
260 flag = true;
261 }
262 } while (flag);
263 } else if (fpdFiles.size() == 1) {
264 file = fpdFiles.toArray(new File[1])[0];
265 }
266 return file;
267 }
268
269
270 public void setType(String type) {
271 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {
272 this.type = type.toLowerCase();
273 } else {
274 this.type = "all";
275 }
276 }
277
278 private void readTargetFile(){
279 try {
280 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;
281
282 String[][] targetFileInfo = ConfigReader.parse(targetFile);
283
284 //
285 // Get ToolChain Info from target.txt
286 //
287 ToolChainInfo envToolChainInfo = new ToolChainInfo();
288 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);
289 if (str == null || str.trim().equals("")) {
290 envToolChainInfo.addTargets("*");
291 } else {
292 envToolChainInfo.addTargets(str);
293 }
294 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);
295 if (str == null || str.trim().equals("")) {
296 envToolChainInfo.addTagnames("*");
297 } else {
298 envToolChainInfo.addTagnames(str);
299 }
300 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);
301 if (str == null || str.trim().equals("")) {
302 envToolChainInfo.addArchs("*");
303 } else {
304 envToolChainInfo.addArchs(str);
305 }
306 GlobalData.setToolChainEnvInfo(envToolChainInfo);
307
308 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);
309 if (str != null && str.trim().length() > 0) {
310 toolsDefFilename = str;
311 }
312
313 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);
314 if (str != null && ! str.trim().equals("")) {
315 if ( ! str.endsWith(".fpd")) {
316 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");
317 }
318 activePlatform = str;
319 }
320 }
321 catch (Exception ex) {
322 throw new BuildException(ex.getMessage());
323 }
324 }
325
326 private String getValue(String key, String[][] map) {
327 for (int i = 0; i < map[0].length; i++){
328 if (key.equalsIgnoreCase(map[0][i])) {
329 return map[1][i];
330 }
331 }
332 return null;
333 }
334 }