1 /** @file FrameworkBuildTask.java
3 The file is ANT task to find MSA or FPD file and build them.
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
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.
14 package org
.tianocore
.build
;
16 import java
.io
.BufferedReader
;
18 import java
.io
.InputStreamReader
;
19 import java
.util
.Iterator
;
20 import java
.util
.LinkedHashSet
;
24 import org
.apache
.tools
.ant
.BuildException
;
25 import org
.apache
.tools
.ant
.Task
;
26 import org
.tianocore
.build
.fpd
.FpdParserForThread
;
27 import org
.tianocore
.build
.fpd
.FpdParserTask
;
28 import org
.tianocore
.build
.global
.GlobalData
;
29 import org
.tianocore
.build
.global
.PropertyManager
;
30 import org
.tianocore
.build
.toolchain
.ConfigReader
;
31 import org
.tianocore
.build
.toolchain
.ToolChainInfo
;
32 import org
.tianocore
.common
.definitions
.ToolDefinitions
;
36 <code>FrameworkBuildTask</code> is an Ant task. The main function is finding
37 and processing a FPD or MSA file, then building a platform or stand-alone
41 The task search current directory and find out all MSA and FPD files by file
42 extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a
43 stand-alone module. The ACTIVE_PLATFORM policy is:
46 1. More than one MSA files, report error;
47 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;
48 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;
49 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;
50 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;
51 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;
52 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform
53 and let user choose one.
57 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].
58 And load all system environment variables to Ant properties.
61 The usage for this task is :
64 <FrameworkBuild type="cleanall" />
69 public class FrameworkBuildTask
extends Task
{
71 private Set
<File
> buildFiles
= new LinkedHashSet
<File
>();
73 private Set
<File
> fpdFiles
= new LinkedHashSet
<File
>();
75 private Set
<File
> msaFiles
= new LinkedHashSet
<File
>();
77 String toolsDefFilename
= ToolDefinitions
.DEFAULT_TOOLS_DEF_FILE_PATH
;
79 String targetFilename
= ToolDefinitions
.TARGET_FILE_PATH
;
81 String dbFilename
= ToolDefinitions
.FRAMEWORK_DATABASE_FILE_PATH
;
83 String activePlatform
= null;
86 /// The flag to present current is multi-thread enabled
88 public static boolean multithread
= false;
91 /// The concurrent thread number
93 public static int MAX_CONCURRENT_THREAD_NUMBER
= 1;
96 /// there are three type: all (build), clean and cleanall
98 private String type
= "all";
100 public void execute() throws BuildException
{
102 // Seach build.xml -> .FPD -> .MSA file
106 // Gen Current Working Directory
108 File dummyFile
= new File(".");
109 File cwd
= dummyFile
.getCanonicalFile();
110 File
[] files
= cwd
.listFiles();
111 for (int i
= 0; i
< files
.length
; i
++) {
112 if (files
[i
].isFile()) {
113 if (files
[i
].getName().equalsIgnoreCase("build.xml")) {
115 // First, search build.xml, if found, ANT call it
117 buildFiles
.add(files
[i
]);
119 } else if (files
[i
].getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
121 // Second, search FPD file, if found, build it
123 fpdFiles
.add(files
[i
]);
124 } else if (files
[i
].getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
126 // Third, search MSA file, if found, build it
128 msaFiles
.add(files
[i
]);
132 } catch (Exception e
) {
133 throw new BuildException(e
.getMessage());
137 // Deal with all environment variable (Add them to properties)
139 backupSystemProperties();
142 // Read target.txt file
147 // Global Data initialization
149 File workspacePath
= new File(getProject().getProperty("WORKSPACE"));
150 PropertyManager
.setProperty(getProject(), "WORKSPACE_DIR", workspacePath
.getPath().replaceAll("(\\\\)", "/"));
151 GlobalData
.initInfo(dbFilename
, workspacePath
.getPath(), toolsDefFilename
);
154 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
156 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
157 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
158 // If find more than one FPD files, let user select one.
160 File buildFile
= null;
161 if (msaFiles
.size() > 1) {
162 throw new BuildException("Having more than one MSA file in a directory is not allowed!");
163 } else if (msaFiles
.size() == 1 && activePlatform
== null) {
164 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename
+ "]. ");
165 } else if (msaFiles
.size() == 1 && activePlatform
!= null) {
167 // Build the single module
169 buildFile
= msaFiles
.toArray(new File
[1])[0];
170 } else if (activePlatform
!= null) {
171 buildFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
172 } else if (fpdFiles
.size() == 1) {
173 buildFile
= fpdFiles
.toArray(new File
[1])[0];
174 } else if (fpdFiles
.size() > 1) {
175 buildFile
= intercommuniteWithUser();
178 // If there is no build files or FPD files or MSA files, stop build
181 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
185 // Build every FPD files (PLATFORM build)
187 if (buildFile
.getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
188 System
.out
.println("Processing the FPD file [" + buildFile
.getPath() + "] ..>> ");
190 // Iff for platform build will enable the multi-thread if set in target.txt
192 if (multithread
&& type
.equalsIgnoreCase("all")) {
193 System
.out
.println("Multi-thread build is enabled. ");
194 FpdParserForThread fpdParserForThread
= new FpdParserForThread();
195 fpdParserForThread
.setType(type
);
196 fpdParserForThread
.setProject(getProject());
197 fpdParserForThread
.setFpdFile(buildFile
);
198 fpdParserForThread
.execute();
202 FpdParserTask fpdParserTask
= new FpdParserTask();
203 fpdParserTask
.setType(type
);
204 fpdParserTask
.setProject(getProject());
205 fpdParserTask
.setFpdFile(buildFile
);
206 fpdParserTask
.execute();
209 // If cleanall delete the Platform_build.xml
211 if (type
.compareTo("cleanall") == 0) {
212 File platformBuildFile
=
213 new File(getProject().getProperty("PLATFORM_DIR")
215 + getProject().getProperty("PLATFORM")
217 platformBuildFile
.deleteOnExit();
222 // Build every MSA files (SINGLE MODULE BUILD)
224 else if (buildFile
.getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
225 File tmpFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
226 System
.out
.println("Using the FPD file [" + tmpFile
.getPath() + "] for the active platform. ");
227 System
.out
.println("Processing the MSA file [" + buildFile
.getPath() + "] ..>> ");
228 GenBuildTask genBuildTask
= new GenBuildTask();
229 genBuildTask
.setSingleModuleBuild(true);
230 genBuildTask
.setType(type
);
231 PropertyManager
.setProperty(getProject(), "PLATFORM_FILE", activePlatform
);
232 genBuildTask
.setProject(getProject());
233 genBuildTask
.setMsaFile(buildFile
);
234 genBuildTask
.execute();
239 Transfer system environment variables to ANT properties. If system variable
240 already exiests in ANT properties, skip it.
243 private void backupSystemProperties() {
244 Map
<String
, String
> sysProperties
= System
.getenv();
245 Set
<String
> keys
= sysProperties
.keySet();
246 Iterator
<String
> iter
= keys
.iterator();
247 while (iter
.hasNext()) {
248 String name
= iter
.next();
251 // If system environment variable is not in ANT properties, add it
253 if (getProject().getProperty(name
) == null) {
254 PropertyManager
.setProperty(getProject(), name
, sysProperties
.get(name
));
259 private File
intercommuniteWithUser(){
261 if (fpdFiles
.size() > 1) {
262 File
[] allFiles
= new File
[fpdFiles
.size()];
264 Iterator
<File
> iter
= fpdFiles
.iterator();
265 while (iter
.hasNext()) {
266 allFiles
[index
] = iter
.next();
270 System
.out
.println("Finding " + allFiles
.length
+ " FPD files: ");
271 for (int i
= 0; i
< allFiles
.length
; i
++) {
272 System
.out
.println("[" + (i
+ 1) + "]: " + allFiles
[i
].getName());
276 System
.out
.print("Please select one of the following FPD files to build:[1] ");
278 BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
280 String str
= br
.readLine();
281 if (str
.trim().length() == 0) {
286 int indexSelect
= Integer
.parseInt(str
);
287 if (indexSelect
<=0 || indexSelect
> allFiles
.length
) {
288 System
.out
.print("Please enter a number between [1.." + allFiles
.length
+ "]:[1] ");
291 file
= allFiles
[indexSelect
- 1];
295 } catch (Exception e
) {
296 System
.out
.print("Please enter a valid number:[1] ");
300 } else if (fpdFiles
.size() == 1) {
301 file
= fpdFiles
.toArray(new File
[1])[0];
307 public void setType(String type
) {
308 if (type
.equalsIgnoreCase("clean") || type
.equalsIgnoreCase("cleanall")) {
309 this.type
= type
.toLowerCase();
315 private void readTargetFile(){
317 String targetFile
= getProject().getProperty("WORKSPACE_DIR") + File
.separatorChar
+ targetFilename
;
319 String
[][] targetFileInfo
= ConfigReader
.parse(targetFile
);
322 // Get ToolChain Info from target.txt
324 ToolChainInfo envToolChainInfo
= new ToolChainInfo();
325 String str
= getValue(ToolDefinitions
.TARGET_KEY_TARGET
, targetFileInfo
);
326 if (str
== null || str
.trim().equals("")) {
327 envToolChainInfo
.addTargets("*");
329 envToolChainInfo
.addTargets(str
);
331 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLCHAIN
, targetFileInfo
);
332 if (str
== null || str
.trim().equals("")) {
333 envToolChainInfo
.addTagnames("*");
335 envToolChainInfo
.addTagnames(str
);
337 str
= getValue(ToolDefinitions
.TARGET_KEY_ARCH
, targetFileInfo
);
338 if (str
== null || str
.trim().equals("")) {
339 envToolChainInfo
.addArchs("*");
341 envToolChainInfo
.addArchs(str
);
343 GlobalData
.setToolChainEnvInfo(envToolChainInfo
);
345 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLS_DEF
, targetFileInfo
);
346 if (str
!= null && str
.trim().length() > 0) {
347 toolsDefFilename
= str
;
350 str
= getValue(ToolDefinitions
.TARGET_KEY_ACTIVE_PLATFORM
, targetFileInfo
);
351 if (str
!= null && ! str
.trim().equals("")) {
352 if ( ! str
.endsWith(".fpd")) {
353 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions
.FPD_EXTENSION
+ "\"!");
355 activePlatform
= str
;
358 str
= getValue("MULTIPLE_THREAD", targetFileInfo
);
359 if (str
!= null && str
.trim().equalsIgnoreCase("Enable")) {
363 str
= getValue("MAX_CONCURRENT_THREAD_NUMBER", targetFileInfo
);
366 int threadNum
= Integer
.parseInt(str
);
368 MAX_CONCURRENT_THREAD_NUMBER
= threadNum
;
370 } catch (Exception enuma
) {
375 catch (Exception ex
) {
376 throw new BuildException(ex
.getMessage());
380 private String
getValue(String key
, String
[][] map
) {
381 for (int i
= 0; i
< map
[0].length
; i
++){
382 if (key
.equalsIgnoreCase(map
[0][i
])) {