Change behavior if found more than one FPD files from let user choice one to report...
[mirror_edk2.git] / Tools / Java / 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.File;
17 import java.io.IOException;
18 import java.util.Hashtable;
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.exception.AutoGenException;
27 import org.tianocore.build.exception.GenBuildException;
28 import org.tianocore.build.exception.PcdAutogenException;
29 import org.tianocore.build.exception.PlatformPcdPreprocessBuildException;
30 import org.tianocore.build.fpd.FpdParserForThread;
31 import org.tianocore.build.fpd.FpdParserTask;
32 import org.tianocore.build.global.GenBuildLogger;
33 import org.tianocore.build.global.GlobalData;
34 import org.tianocore.build.toolchain.ConfigReader;
35 import org.tianocore.build.toolchain.ToolChainInfo;
36 import org.tianocore.common.definitions.ToolDefinitions;
37 import org.tianocore.common.exception.EdkException;
38 import org.tianocore.common.logger.EdkLog;
39
40 /**
41 <p>
42 <code>FrameworkBuildTask</code> is an Ant task. The main function is finding
43 and processing a FPD or MSA file, then building a platform or stand-alone
44 module.
45
46 <p>
47 The task search current directory and find out all MSA and FPD files by file
48 extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a
49 stand-alone module. The ACTIVE_PLATFORM policy is:
50
51 <pre>
52 1. More than one MSA files, report error;
53 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;
54 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;
55 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;
56 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;
57 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;
58 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, Report Error!
59 </pre>
60
61 <p>
62 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].
63 And load all system environment variables to Ant properties.
64
65 <p>
66 The usage for this task is :
67
68 <pre>
69 &lt;FrameworkBuild type="cleanall" /&gt;
70 </pre>
71
72 @since GenBuild 1.0
73 **/
74 public class FrameworkBuildTask extends Task{
75
76 private Set<File> fpdFiles = new LinkedHashSet<File>();
77
78 private Set<File> msaFiles = new LinkedHashSet<File>();
79
80 //
81 // This is only for none-multi-thread build to reduce overriding message
82 //
83 public static Hashtable<String, String> originalProperties = new Hashtable<String, String>();
84
85 String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;
86
87 String targetFilename = ToolDefinitions.TARGET_FILE_PATH;
88
89 String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;
90
91 String activePlatform = null;
92
93 ///
94 /// The flag to present current is multi-thread enabled
95 ///
96 public static boolean multithread = false;
97
98 ///
99 /// The concurrent thread number
100 ///
101 public static int MAX_CONCURRENT_THREAD_NUMBER = 2;
102
103 ///
104 /// there are three type: all (build), clean and cleanall
105 ///
106 private String type = "all";
107
108 public void execute() throws BuildException {
109 //
110 // set Logger
111 //
112 GenBuildLogger logger = new GenBuildLogger(getProject());
113 EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));
114 EdkLog.setLogger(logger);
115
116 try {
117 processFrameworkBuild();
118 } catch (PcdAutogenException e) {
119 //
120 // Add more logic process here
121 //
122 throw new BuildException(e.getMessage());
123 } catch (AutoGenException e) {
124 //
125 // Add more logic process here
126 //
127 throw new BuildException(e.getMessage());
128 } catch (PlatformPcdPreprocessBuildException e) {
129 //
130 // Add more logic process here
131 //
132 throw new BuildException(e.getMessage());
133 } catch (GenBuildException e) {
134 //
135 // Add more logic process here
136 //
137 throw new BuildException(e.getMessage());
138 } catch (EdkException e) {
139 //
140 // Add more logic process here
141 //
142 throw new BuildException(e.getMessage());
143 }
144 }
145
146 private void processFrameworkBuild() throws EdkException, GenBuildException, AutoGenException, PcdAutogenException, PlatformPcdPreprocessBuildException {
147 try {
148 //
149 // Get current working dir
150 //
151 File dummyFile = new File(".");
152 File cwd = dummyFile.getCanonicalFile();
153 File[] files = cwd.listFiles();
154
155 //
156 // Scan current dir, and find out all .FPD and .MSA files
157 //
158 for (int i = 0; i < files.length; i++) {
159 if (files[i].isFile()) {
160 if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {
161 //
162 // Found FPD file
163 //
164 fpdFiles.add(files[i]);
165 } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
166 //
167 // Found MSA file
168 //
169 msaFiles.add(files[i]);
170 }
171 }
172 }
173 } catch (IOException ex) {
174 BuildException buildException = new BuildException("Scanning current directory error. \n" + ex.getMessage());
175 buildException.setStackTrace(ex.getStackTrace());
176 throw buildException;
177 }
178
179 //
180 // Import all system environment variables to ANT properties
181 //
182 importSystemEnvVariables();
183
184 //
185 // Read target.txt file
186 //
187 readTargetFile();
188
189 //
190 // Global Data initialization
191 //
192 File workspacePath = new File(getProject().getProperty("WORKSPACE"));
193 getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));
194 GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);
195
196 //
197 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
198 // else fail build.
199 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
200 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
201 // If find more than one FPD files, report error.
202 //
203 File buildFile = null;
204 if (msaFiles.size() > 1) {
205 throw new BuildException("Found " + msaFiles.size() + " MSA files in current dir. ");
206 } else if (msaFiles.size() == 1 && activePlatform == null) {
207 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");
208 } else if (msaFiles.size() == 1 && activePlatform != null) {
209 //
210 // Build the single module
211 //
212 buildFile = msaFiles.toArray(new File[1])[0];
213 } else if (activePlatform != null) {
214 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
215 } else if (fpdFiles.size() == 1) {
216 buildFile = fpdFiles.toArray(new File[1])[0];
217 } else if (fpdFiles.size() > 1) {
218 throw new BuildException("Found " + fpdFiles.size() + " FPD files in current dir. ");
219 }
220
221 //
222 // If there is no build files or FPD files or MSA files, stop build
223 //
224 else {
225 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
226 }
227
228 //
229 // Build every FPD files (PLATFORM build)
230 //
231 if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {
232 EdkLog.log(this, "Processing the FPD file [" + buildFile.getPath() + "] ..>> ");
233 //
234 // Iff for platform build will enable the multi-thread if set in target.txt
235 //
236 if (multithread && type.equalsIgnoreCase("all")) {
237 EdkLog.log(this, "Multi-thread build is enabled. ");
238 FpdParserForThread fpdParserForThread = new FpdParserForThread();
239 fpdParserForThread.setType(type);
240 fpdParserForThread.setProject(getProject());
241 fpdParserForThread.setFpdFile(buildFile);
242 fpdParserForThread.perform();
243 return ;
244 }
245
246 FpdParserTask fpdParserTask = new FpdParserTask();
247 fpdParserTask.setType(type);
248 fpdParserTask.setProject(getProject());
249 fpdParserTask.setFpdFile(buildFile);
250 fpdParserTask.perform();
251
252 //
253 // If cleanall delete the Platform_build.xml
254 //
255 if (type.compareTo("cleanall") == 0) {
256 File platformBuildFile =
257 new File(getProject().getProperty("BUILD_DIR")
258 + File.separatorChar
259 + getProject().getProperty("PLATFORM")
260 + "_build.xml");
261 platformBuildFile.deleteOnExit();
262 }
263 }
264
265 //
266 // Build every MSA files (SINGLE MODULE BUILD)
267 //
268 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
269 if (multithread) {
270 EdkLog.log(this, EdkLog.EDK_WARNING, "Multi-Thead do not take effect on Stand-Alone (Single) module build. ");
271 multithread = false;
272 }
273 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
274 EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");
275 EdkLog.log(this, "Processing the MSA file [" + buildFile.getPath() + "] ..>> ");
276 GenBuildTask genBuildTask = new GenBuildTask();
277 genBuildTask.setSingleModuleBuild(true);
278 genBuildTask.setType(type);
279 getProject().setProperty("PLATFORM_FILE", activePlatform);
280 if( !multithread) {
281 originalProperties.put("PLATFORM_FILE", activePlatform);
282 }
283 genBuildTask.setProject(getProject());
284 genBuildTask.setMsaFile(buildFile);
285 genBuildTask.perform();
286 }
287 }
288
289 /**
290 Import system environment variables to ANT properties. If system variable
291 already exiests in ANT properties, skip it.
292
293 **/
294 private void importSystemEnvVariables() {
295 Map<String, String> sysProperties = System.getenv();
296 Iterator<String> iter = sysProperties.keySet().iterator();
297 while (iter.hasNext()) {
298 String name = iter.next();
299
300 //
301 // If system environment variable is not in ANT properties, add it
302 //
303 if (getProject().getProperty(name) == null) {
304 getProject().setProperty(name, sysProperties.get(name));
305 }
306 }
307
308 Hashtable allProperties = getProject().getProperties();
309 Iterator piter = allProperties.keySet().iterator();
310 while (piter.hasNext()) {
311 String name = (String)piter.next();
312 originalProperties.put(new String(name), new String((String)allProperties.get(name)));
313 }
314 }
315
316 public void setType(String type) {
317 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {
318 this.type = type.toLowerCase();
319 } else {
320 this.type = "all";
321 }
322 }
323
324 private void readTargetFile() throws EdkException{
325 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;
326
327 String[][] targetFileInfo = ConfigReader.parse(targetFile);
328
329 //
330 // Get ToolChain Info from target.txt
331 //
332 ToolChainInfo envToolChainInfo = new ToolChainInfo();
333 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);
334 if (str == null || str.trim().equals("")) {
335 envToolChainInfo.addTargets("*");
336 } else {
337 envToolChainInfo.addTargets(str);
338 }
339 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);
340 if (str == null || str.trim().equals("")) {
341 envToolChainInfo.addTagnames("*");
342 } else {
343 envToolChainInfo.addTagnames(str);
344 }
345 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);
346 if (str == null || str.trim().equals("")) {
347 envToolChainInfo.addArchs("*");
348 } else {
349 envToolChainInfo.addArchs(str);
350 }
351 GlobalData.setToolChainEnvInfo(envToolChainInfo);
352
353 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);
354 if (str != null && str.trim().length() > 0) {
355 toolsDefFilename = str;
356 }
357
358 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);
359 if (str != null && ! str.trim().equals("")) {
360 if ( ! str.endsWith(".fpd")) {
361 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");
362 }
363 activePlatform = str;
364 }
365
366 str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);
367 if (str != null && str.trim().equalsIgnoreCase("Enable")) {
368 multithread = true;
369 }
370
371 str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);
372 if (str != null ) {
373 try {
374 int threadNum = Integer.parseInt(str);
375 if (threadNum > 0) {
376 MAX_CONCURRENT_THREAD_NUMBER = threadNum;
377 }
378 } catch (Exception ex) {
379 //
380 // Give a warning message, and keep the default value
381 //
382 EdkLog.log(this, EdkLog.EDK_WARNING, "Incorrent number specified for MAX_CONCURRENT_THREAD_NUMBER in file [" + targetFilename + "]");
383 }
384 }
385 }
386
387 private String getValue(String key, String[][] map) {
388 for (int i = 0; i < map[0].length; i++){
389 if (key.equalsIgnoreCase(map[0][i])) {
390 return map[1][i];
391 }
392 }
393 return null;
394 }
395 }