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