]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/FrameworkBuildTask.java
Fixed an Autogen issue which will cause build break when encountering some kind of...
[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(EdkLog.EDK_DEBUG);
114 EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));
115 EdkLog.setLogger(logger);
116
117 try {
118 processFrameworkBuild();
119 }catch (BuildException e) {
120 //
121 // Add more logic process here
122 //
123 BuildException buildException = new BuildException(e.getMessage());
124 buildException.setStackTrace(e.getStackTrace());
125 throw buildException;
126 } catch (PcdAutogenException e) {
127 //
128 // Add more logic process here
129 //
130 BuildException buildException = new BuildException(e.getMessage());
131 buildException.setStackTrace(e.getStackTrace());
132 throw buildException;
133 } catch (AutoGenException e) {
134 //
135 // Add more logic process here
136 //
137 BuildException buildException = new BuildException(e.getMessage());
138 buildException.setStackTrace(e.getStackTrace());
139 throw buildException;
140 } catch (PlatformPcdPreprocessBuildException e) {
141 //
142 // Add more logic process here
143 //
144 BuildException buildException = new BuildException(e.getMessage());
145 buildException.setStackTrace(e.getStackTrace());
146 throw buildException;
147 } catch (GenBuildException e) {
148 //
149 // Add more logic process here
150 //
151 BuildException buildException = new BuildException(e.getMessage());
152 buildException.setStackTrace(e.getStackTrace());
153 throw buildException;
154 } catch (EdkException e) {
155 //
156 // Add more logic process here
157 //
158 BuildException buildException = new BuildException(e.getMessage());
159 buildException.setStackTrace(e.getStackTrace());
160 throw buildException;
161 }
162 }
163
164 private void processFrameworkBuild() throws EdkException, GenBuildException, AutoGenException, PcdAutogenException, PlatformPcdPreprocessBuildException {
165 try {
166 //
167 // Get current working dir
168 //
169 File dummyFile = new File(".");
170 File cwd = dummyFile.getCanonicalFile();
171 File[] files = cwd.listFiles();
172
173 //
174 // Scan current dir, and find out all .FPD and .MSA files
175 //
176 for (int i = 0; i < files.length; i++) {
177 if (files[i].isFile()) {
178 if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {
179 //
180 // Found FPD file
181 //
182 fpdFiles.add(files[i]);
183 } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
184 //
185 // Found MSA file
186 //
187 msaFiles.add(files[i]);
188 }
189 }
190 }
191 } catch (IOException ex) {
192 BuildException buildException = new BuildException("Scanning current directory error. \n" + ex.getMessage());
193 buildException.setStackTrace(ex.getStackTrace());
194 throw buildException;
195 }
196
197 //
198 // Import all system environment variables to ANT properties
199 //
200 importSystemEnvVariables();
201
202 //
203 // Read target.txt file
204 //
205 readTargetFile();
206
207 //
208 // Global Data initialization
209 //
210 File workspacePath = new File(getProject().getProperty("WORKSPACE"));
211 getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));
212 GlobalData.initInfo(getProject(), dbFilename, workspacePath.getPath(), toolsDefFilename);
213
214 //
215 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
216 // else fail build.
217 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
218 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
219 // If find more than one FPD files, report error.
220 //
221 File buildFile = null;
222 if (msaFiles.size() > 0) {
223 if (activePlatform == null) {
224 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");
225 }
226 //
227 // Build the single module
228 //
229 buildFile = msaFiles.toArray(new File[1])[0];
230 } else if (activePlatform != null) {
231 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
232 } else if (fpdFiles.size() == 1) {
233 buildFile = fpdFiles.toArray(new File[1])[0];
234 } else if (fpdFiles.size() > 1) {
235 throw new BuildException("Found " + fpdFiles.size() + " FPD files in current dir. ");
236 }
237
238 //
239 // If there is no build files or FPD files or MSA files, stop build
240 //
241 else {
242 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
243 }
244
245 //
246 // Build every FPD files (PLATFORM build)
247 //
248 if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {
249 EdkLog.log(this, "Processing the FPD file [" + buildFile.getPath() + "] ..>> ");
250 //
251 // Iff for platform build will enable the multi-thread if set in target.txt
252 //
253 if (multithread && type.equalsIgnoreCase("all")) {
254 EdkLog.log(this, "Multi-thread build is enabled. ");
255 FpdParserForThread fpdParserForThread = new FpdParserForThread();
256 fpdParserForThread.setType(type);
257 fpdParserForThread.setProject(getProject());
258 fpdParserForThread.setFpdFile(buildFile);
259 fpdParserForThread.perform();
260 return ;
261 }
262
263 FpdParserTask fpdParserTask = new FpdParserTask();
264 fpdParserTask.setType(type);
265 fpdParserTask.setProject(getProject());
266 fpdParserTask.setFpdFile(buildFile);
267 fpdParserTask.perform();
268
269 //
270 // If cleanall delete the Platform_build.xml
271 //
272 if (type.compareTo("cleanall") == 0) {
273 File platformBuildFile =
274 new File(getProject().getProperty("BUILD_DIR")
275 + File.separatorChar
276 + getProject().getProperty("PLATFORM")
277 + "_build.xml");
278 platformBuildFile.deleteOnExit();
279 }
280 }
281
282 //
283 // Build every MSA files (SINGLE MODULE BUILD)
284 //
285 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
286 if (multithread) {
287 EdkLog.log(this, EdkLog.EDK_WARNING, "Multi-Thead do not take effect on Stand-Alone (Single) module build. ");
288 multithread = false;
289 }
290 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
291 EdkLog.log(this, "Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");
292
293 File[] moduleFiles = msaFiles.toArray(new File[msaFiles.size()]);
294 for (int i = 0; i < moduleFiles.length; ++i) {
295 EdkLog.log(this, "Processing the MSA file [" + moduleFiles[i].getPath() + "] ..>> ");
296 GenBuildTask genBuildTask = new GenBuildTask();
297 genBuildTask.setSingleModuleBuild(true);
298 genBuildTask.setType(type);
299 getProject().setProperty("PLATFORM_FILE", activePlatform);
300 if( !multithread) {
301 originalProperties.put("PLATFORM_FILE", activePlatform);
302 }
303 genBuildTask.setProject(getProject());
304 genBuildTask.setMsaFile(moduleFiles[i]);
305 genBuildTask.perform();
306 }
307 }
308 }
309
310 /**
311 Import system environment variables to ANT properties. If system variable
312 already exiests in ANT properties, skip it.
313
314 **/
315 private void importSystemEnvVariables() {
316 Map<String, String> sysProperties = System.getenv();
317 Iterator<String> iter = sysProperties.keySet().iterator();
318 while (iter.hasNext()) {
319 String name = iter.next();
320
321 //
322 // If system environment variable is not in ANT properties, add it
323 //
324 if (getProject().getProperty(name) == null) {
325 getProject().setProperty(name, sysProperties.get(name));
326 }
327 }
328
329 Hashtable allProperties = getProject().getProperties();
330 Iterator piter = allProperties.keySet().iterator();
331 while (piter.hasNext()) {
332 String name = (String)piter.next();
333 originalProperties.put(new String(name), new String((String)allProperties.get(name)));
334 }
335 }
336
337 public void setType(String type) {
338 this.type = type.toLowerCase();
339 }
340
341 private void readTargetFile() throws EdkException{
342 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;
343
344 String[][] targetFileInfo = ConfigReader.parse(getProject(), targetFile);
345
346 //
347 // Get ToolChain Info from target.txt
348 //
349 ToolChainInfo envToolChainInfo = new ToolChainInfo();
350 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);
351 if (str == null || str.trim().equals("")) {
352 envToolChainInfo.addTargets("*");
353 } else {
354 envToolChainInfo.addTargets(str);
355 }
356 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);
357 if (str == null || str.trim().equals("")) {
358 envToolChainInfo.addTagnames("*");
359 } else {
360 envToolChainInfo.addTagnames(str);
361 }
362 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);
363 if (str == null || str.trim().equals("")) {
364 envToolChainInfo.addArchs("*");
365 } else {
366 envToolChainInfo.addArchs(str);
367 }
368 GlobalData.setToolChainEnvInfo(envToolChainInfo);
369
370 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);
371 if (str != null && str.trim().length() > 0) {
372 toolsDefFilename = str;
373 }
374
375 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);
376 if (str != null && ! str.trim().equals("")) {
377 if ( ! str.endsWith(".fpd")) {
378 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");
379 }
380 activePlatform = str;
381 }
382
383 str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);
384 if (str != null && str.trim().equalsIgnoreCase("Enable")) {
385 multithread = true;
386 }
387
388 str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);
389 //
390 // Need to check the # of threads iff multithread is enabled.
391 //
392 if ((multithread) && (str != null )) {
393 try {
394 int threadNum = Integer.parseInt(str);
395 if (threadNum > 0) {
396 MAX_CONCURRENT_THREAD_NUMBER = threadNum;
397 }
398 } catch (Exception ex) {
399 //
400 // Give a warning message, and keep the default value
401 //
402 EdkLog.log(this, EdkLog.EDK_WARNING, "Incorrent number specified for MAX_CONCURRENT_THREAD_NUMBER in file [" + targetFilename + "]");
403 }
404 }
405 }
406
407 private String getValue(String key, String[][] map) {
408 for (int i = 0; i < map[0].length; i++){
409 if (key.equalsIgnoreCase(map[0][i])) {
410 return map[1][i];
411 }
412 }
413 return null;
414 }
415 }