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
.global
.PropertyManager
;
29 import org
.tianocore
.build
.toolchain
.ConfigReader
;
30 import org
.tianocore
.build
.toolchain
.ToolChainInfo
;
31 import org
.tianocore
.common
.definitions
.ToolDefinitions
;
35 <code>FrameworkBuildTask</code> is an Ant task. The main function is finding
36 and processing a FPD or MSA file, then building a platform or stand-alone
40 The task search current directory and find out all MSA and FPD files by file
41 extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a
42 stand-alone module. The ACTIVE_PLATFORM policy is:
45 1. More than one MSA files, report error;
46 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;
47 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;
48 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;
49 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;
50 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;
51 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform
52 and let user choose one.
56 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].
57 And load all system environment variables to Ant properties.
60 The usage for this task is :
63 <FrameworkBuild type="cleanall" />
68 public class FrameworkBuildTask
extends Task
{
70 private Set
<File
> buildFiles
= new LinkedHashSet
<File
>();
72 private Set
<File
> fpdFiles
= new LinkedHashSet
<File
>();
74 private Set
<File
> msaFiles
= new LinkedHashSet
<File
>();
76 String toolsDefFilename
= ToolDefinitions
.DEFAULT_TOOLS_DEF_FILE_PATH
;
78 String targetFilename
= ToolDefinitions
.TARGET_FILE_PATH
;
80 String dbFilename
= ToolDefinitions
.FRAMEWORK_DATABASE_FILE_PATH
;
82 String activePlatform
= null;
85 /// there are three type: all (build), clean and cleanall
87 private String type
= "all";
89 public void execute() throws BuildException
{
91 // Seach build.xml -> .FPD -> .MSA file
95 // Gen Current Working Directory
97 File dummyFile
= new File(".");
98 File cwd
= dummyFile
.getCanonicalFile();
99 File
[] files
= cwd
.listFiles();
100 for (int i
= 0; i
< files
.length
; i
++) {
101 if (files
[i
].isFile()) {
102 if (files
[i
].getName().equalsIgnoreCase("build.xml")) {
104 // First, search build.xml, if found, ANT call it
106 buildFiles
.add(files
[i
]);
108 } else if (files
[i
].getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
110 // Second, search FPD file, if found, build it
112 fpdFiles
.add(files
[i
]);
113 } else if (files
[i
].getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
115 // Third, search MSA file, if found, build it
117 msaFiles
.add(files
[i
]);
121 } catch (Exception e
) {
122 throw new BuildException(e
.getMessage());
126 // Deal with all environment variable (Add them to properties)
128 backupSystemProperties();
131 // Read target.txt file
136 // Global Data initialization
138 File workspacePath
= new File(getProject().getProperty("WORKSPACE"));
139 PropertyManager
.setProperty(getProject(), "WORKSPACE_DIR", workspacePath
.getPath().replaceAll("(\\\\)", "/"));
140 GlobalData
.initInfo(dbFilename
, workspacePath
.getPath(), toolsDefFilename
);
143 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
145 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
146 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
147 // If find more than one FPD files, let user select one.
149 File buildFile
= null;
150 if (msaFiles
.size() > 1) {
151 throw new BuildException("Having more than one MSA file in a directory is not allowed!");
152 } else if (msaFiles
.size() == 1 && activePlatform
== null) {
153 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename
+ "]. ");
154 } else if (msaFiles
.size() == 1 && activePlatform
!= null) {
156 // Build the single module
158 buildFile
= msaFiles
.toArray(new File
[1])[0];
159 } else if (activePlatform
!= null) {
160 buildFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
161 } else if (fpdFiles
.size() == 1) {
162 buildFile
= fpdFiles
.toArray(new File
[1])[0];
163 } else if (fpdFiles
.size() > 1) {
164 buildFile
= intercommuniteWithUser();
167 // If there is no build files or FPD files or MSA files, stop build
170 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
174 // Build every FPD files (PLATFORM build)
176 if (buildFile
.getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
177 System
.out
.println("Processing the FPD file [" + buildFile
.getPath() + "] ..>> ");
178 FpdParserTask fpdParserTask
= new FpdParserTask();
179 fpdParserTask
.setType(type
);
180 fpdParserTask
.setProject(getProject());
181 fpdParserTask
.setFpdFile(buildFile
);
182 fpdParserTask
.execute();
185 // If cleanall delete the Platform_build.xml
187 if (type
.compareTo("cleanall") == 0) {
188 File platformBuildFile
=
189 new File(getProject().getProperty("PLATFORM_DIR")
191 + getProject().getProperty("PLATFORM")
193 platformBuildFile
.deleteOnExit();
198 // Build every MSA files (SINGLE MODULE BUILD)
200 else if (buildFile
.getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
201 File tmpFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
202 System
.out
.println("Using the FPD file [" + tmpFile
.getPath() + "] for the active platform. ");
203 System
.out
.println("Processing the MSA file [" + buildFile
.getPath() + "] ..>> ");
204 GenBuildTask genBuildTask
= new GenBuildTask();
205 genBuildTask
.setSingleModuleBuild(true);
206 genBuildTask
.setType(type
);
207 PropertyManager
.setProperty(getProject(), "PLATFORM_FILE", activePlatform
);
208 genBuildTask
.setProject(getProject());
209 genBuildTask
.setMsaFile(buildFile
);
210 genBuildTask
.execute();
215 Transfer system environment variables to ANT properties. If system variable
216 already exiests in ANT properties, skip it.
219 private void backupSystemProperties() {
220 Map
<String
, String
> sysProperties
= System
.getenv();
221 Set
<String
> keys
= sysProperties
.keySet();
222 Iterator
<String
> iter
= keys
.iterator();
223 while (iter
.hasNext()) {
224 String name
= iter
.next();
227 // If system environment variable is not in ANT properties, add it
229 if (getProject().getProperty(name
) == null) {
230 PropertyManager
.setProperty(getProject(), name
, sysProperties
.get(name
));
235 private File
intercommuniteWithUser(){
237 if (fpdFiles
.size() > 1) {
238 File
[] allFiles
= new File
[fpdFiles
.size()];
240 Iterator
<File
> iter
= fpdFiles
.iterator();
241 while (iter
.hasNext()) {
242 allFiles
[index
] = iter
.next();
246 System
.out
.println("Finding " + allFiles
.length
+ " FPD files: ");
247 for (int i
= 0; i
< allFiles
.length
; i
++) {
248 System
.out
.println("[" + (i
+ 1) + "]: " + allFiles
[i
].getName());
252 System
.out
.print("Please select one of the following FPD files to build:[1] ");
254 BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
256 String str
= br
.readLine();
257 if (str
.trim().length() == 0) {
262 int indexSelect
= Integer
.parseInt(str
);
263 if (indexSelect
<=0 || indexSelect
> allFiles
.length
) {
264 System
.out
.print("Please enter a number between [1.." + allFiles
.length
+ "]:[1] ");
267 file
= allFiles
[indexSelect
- 1];
271 } catch (Exception e
) {
272 System
.out
.print("Please enter a valid number:[1] ");
276 } else if (fpdFiles
.size() == 1) {
277 file
= fpdFiles
.toArray(new File
[1])[0];
283 public void setType(String type
) {
284 if (type
.equalsIgnoreCase("clean") || type
.equalsIgnoreCase("cleanall")) {
285 this.type
= type
.toLowerCase();
291 private void readTargetFile(){
293 String targetFile
= getProject().getProperty("WORKSPACE_DIR") + File
.separatorChar
+ targetFilename
;
295 String
[][] targetFileInfo
= ConfigReader
.parse(targetFile
);
298 // Get ToolChain Info from target.txt
300 ToolChainInfo envToolChainInfo
= new ToolChainInfo();
301 String str
= getValue(ToolDefinitions
.TARGET_KEY_TARGET
, targetFileInfo
);
302 if (str
== null || str
.trim().equals("")) {
303 envToolChainInfo
.addTargets("*");
305 envToolChainInfo
.addTargets(str
);
307 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLCHAIN
, targetFileInfo
);
308 if (str
== null || str
.trim().equals("")) {
309 envToolChainInfo
.addTagnames("*");
311 envToolChainInfo
.addTagnames(str
);
313 str
= getValue(ToolDefinitions
.TARGET_KEY_ARCH
, targetFileInfo
);
314 if (str
== null || str
.trim().equals("")) {
315 envToolChainInfo
.addArchs("*");
317 envToolChainInfo
.addArchs(str
);
319 GlobalData
.setToolChainEnvInfo(envToolChainInfo
);
321 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLS_DEF
, targetFileInfo
);
322 if (str
!= null && str
.trim().length() > 0) {
323 toolsDefFilename
= str
;
326 str
= getValue(ToolDefinitions
.TARGET_KEY_ACTIVE_PLATFORM
, targetFileInfo
);
327 if (str
!= null && ! str
.trim().equals("")) {
328 if ( ! str
.endsWith(".fpd")) {
329 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions
.FPD_EXTENSION
+ "\"!");
331 activePlatform
= str
;
334 catch (Exception ex
) {
335 throw new BuildException(ex
.getMessage());
339 private String
getValue(String key
, String
[][] map
) {
340 for (int i
= 0; i
< map
[0].length
; i
++){
341 if (key
.equalsIgnoreCase(map
[0][i
])) {