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
.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
;
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
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:
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.
55 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].
56 And load all system environment variables to Ant properties.
59 The usage for this task is :
62 <FrameworkBuild type="cleanall" />
67 public class FrameworkBuildTask
extends Task
{
69 private Set
<File
> buildFiles
= new LinkedHashSet
<File
>();
71 private Set
<File
> fpdFiles
= new LinkedHashSet
<File
>();
73 private Set
<File
> msaFiles
= new LinkedHashSet
<File
>();
75 String toolsDefFilename
= ToolDefinitions
.DEFAULT_TOOLS_DEF_FILE_PATH
;
77 String targetFilename
= ToolDefinitions
.TARGET_FILE_PATH
;
79 String dbFilename
= ToolDefinitions
.FRAMEWORK_DATABASE_FILE_PATH
;
81 String activePlatform
= null;
84 /// there are three type: all (build), clean and cleanall
86 private String type
= "all";
88 public void execute() throws BuildException
{
90 // Seach build.xml -> .FPD -> .MSA file
94 // Gen Current Working Directory
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")) {
103 // First, search build.xml, if found, ANT call it
105 buildFiles
.add(files
[i
]);
107 } else if (files
[i
].getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
109 // Second, search FPD file, if found, build it
111 fpdFiles
.add(files
[i
]);
112 } else if (files
[i
].getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
114 // Third, search MSA file, if found, build it
116 msaFiles
.add(files
[i
]);
120 } catch (Exception e
) {
121 throw new BuildException(e
.getMessage());
125 // Deal with all environment variable (Add them to properties)
127 backupSystemProperties();
130 // Read target.txt file
135 // Global Data initialization
137 File workspacePath
= new File(getProject().getProperty("WORKSPACE"));
138 getProject().setProperty("WORKSPACE_DIR", workspacePath
.getPath().replaceAll("(\\\\)", "/"));
139 GlobalData
.initInfo(dbFilename
, workspacePath
.getPath(), toolsDefFilename
);
142 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
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.
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) {
155 // Build the single module
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();
166 // If there is no build files or FPD files or MSA files, stop build
169 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
173 // Build every FPD files (PLATFORM build)
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();
184 // If cleanall delete the Platform_build.xml
186 if (type
.compareTo("cleanall") == 0) {
187 File platformBuildFile
=
188 new File(getProject().getProperty("PLATFORM_DIR")
190 + getProject().getProperty("PLATFORM")
192 platformBuildFile
.deleteOnExit();
197 // Build every MSA files (SINGLE MODULE BUILD)
199 else if (buildFile
.getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
200 File tmpFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
201 System
.out
.println("Using the FPD file [" + tmpFile
.getPath() + "] for the active platform. ");
202 System
.out
.println("Processing the MSA file [" + buildFile
.getPath() + "] ..>> ");
203 GenBuildTask genBuildTask
= new GenBuildTask();
204 genBuildTask
.setSingleModuleBuild(true);
205 genBuildTask
.setType(type
);
206 getProject().setProperty("PLATFORM_FILE", activePlatform
);
207 genBuildTask
.setProject(getProject());
208 genBuildTask
.setMsaFile(buildFile
);
209 genBuildTask
.execute();
214 Transfer system environment variables to ANT properties. If system variable
215 already exiests in ANT properties, skip it.
218 private void backupSystemProperties() {
219 Map
<String
, String
> sysProperties
= System
.getenv();
220 Set
<String
> keys
= sysProperties
.keySet();
221 Iterator
<String
> iter
= keys
.iterator();
222 while (iter
.hasNext()) {
223 String name
= iter
.next();
226 // If system environment variable is not in ANT properties, add it
228 if (getProject().getProperty(name
) == null) {
229 getProject().setProperty(name
, sysProperties
.get(name
));
234 private File
intercommuniteWithUser(){
236 if (fpdFiles
.size() > 1) {
237 File
[] allFiles
= new File
[fpdFiles
.size()];
239 Iterator
<File
> iter
= fpdFiles
.iterator();
240 while (iter
.hasNext()) {
241 allFiles
[index
] = iter
.next();
245 System
.out
.println("Finding " + allFiles
.length
+ " FPD files: ");
246 for (int i
= 0; i
< allFiles
.length
; i
++) {
247 System
.out
.println("[" + (i
+ 1) + "]: " + allFiles
[i
].getName());
251 System
.out
.print("Please select one of the following FPD files to build:[1] ");
253 BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
255 String str
= br
.readLine();
256 if (str
.trim().length() == 0) {
261 int indexSelect
= Integer
.parseInt(str
);
262 if (indexSelect
<=0 || indexSelect
> allFiles
.length
) {
263 System
.out
.print("Please enter a number between [1.." + allFiles
.length
+ "]:[1] ");
266 file
= allFiles
[indexSelect
- 1];
270 } catch (Exception e
) {
271 System
.out
.print("Please enter a valid number:[1] ");
275 } else if (fpdFiles
.size() == 1) {
276 file
= fpdFiles
.toArray(new File
[1])[0];
282 public void setType(String type
) {
283 if (type
.equalsIgnoreCase("clean") || type
.equalsIgnoreCase("cleanall")) {
284 this.type
= type
.toLowerCase();
290 private void readTargetFile(){
292 String targetFile
= getProject().getProperty("WORKSPACE_DIR") + File
.separatorChar
+ targetFilename
;
294 String
[][] targetFileInfo
= ConfigReader
.parse(targetFile
);
297 // Get ToolChain Info from target.txt
299 ToolChainInfo envToolChainInfo
= new ToolChainInfo();
300 String str
= getValue(ToolDefinitions
.TARGET_KEY_TARGET
, targetFileInfo
);
301 if (str
== null || str
.trim().equals("")) {
302 envToolChainInfo
.addTargets("*");
304 envToolChainInfo
.addTargets(str
);
306 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLCHAIN
, targetFileInfo
);
307 if (str
== null || str
.trim().equals("")) {
308 envToolChainInfo
.addTagnames("*");
310 envToolChainInfo
.addTagnames(str
);
312 str
= getValue(ToolDefinitions
.TARGET_KEY_ARCH
, targetFileInfo
);
313 if (str
== null || str
.trim().equals("")) {
314 envToolChainInfo
.addArchs("*");
316 envToolChainInfo
.addArchs(str
);
318 GlobalData
.setToolChainEnvInfo(envToolChainInfo
);
320 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLS_DEF
, targetFileInfo
);
321 if (str
!= null && str
.trim().length() > 0) {
322 toolsDefFilename
= str
;
325 str
= getValue(ToolDefinitions
.TARGET_KEY_ACTIVE_PLATFORM
, targetFileInfo
);
326 if (str
!= null && ! str
.trim().equals("")) {
327 if ( ! str
.endsWith(".fpd")) {
328 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions
.FPD_EXTENSION
+ "\"!");
330 activePlatform
= str
;
333 catch (Exception ex
) {
334 throw new BuildException(ex
.getMessage());
338 private String
getValue(String key
, String
[][] map
) {
339 for (int i
= 0; i
< map
[0].length
; i
++){
340 if (key
.equalsIgnoreCase(map
[0][i
])) {