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();
185 // Build every MSA files (SINGLE MODULE BUILD)
187 else if (buildFile
.getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
188 File tmpFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
189 System
.out
.println("Using the FPD file [" + tmpFile
.getPath() + "] for the active platform. ");
190 System
.out
.println("Processing the MSA file [" + buildFile
.getPath() + "] ..>> ");
191 GenBuildTask genBuildTask
= new GenBuildTask();
192 genBuildTask
.setSingleModuleBuild(true);
193 genBuildTask
.setType(type
);
194 getProject().setProperty("PLATFORM_FILE", activePlatform
);
195 genBuildTask
.setProject(getProject());
196 genBuildTask
.setMsaFile(buildFile
);
197 genBuildTask
.execute();
202 Transfer system environment variables to ANT properties. If system variable
203 already exiests in ANT properties, skip it.
206 private void backupSystemProperties() {
207 Map
<String
, String
> sysProperties
= System
.getenv();
208 Set
<String
> keys
= sysProperties
.keySet();
209 Iterator
<String
> iter
= keys
.iterator();
210 while (iter
.hasNext()) {
211 String name
= iter
.next();
214 // If system environment variable is not in ANT properties, add it
216 if (getProject().getProperty(name
) == null) {
217 getProject().setProperty(name
, sysProperties
.get(name
));
222 private File
intercommuniteWithUser(){
224 if (fpdFiles
.size() > 1) {
225 File
[] allFiles
= new File
[fpdFiles
.size()];
227 Iterator
<File
> iter
= fpdFiles
.iterator();
228 while (iter
.hasNext()) {
229 allFiles
[index
] = iter
.next();
233 System
.out
.println("Finding " + allFiles
.length
+ " FPD files: ");
234 for (int i
= 0; i
< allFiles
.length
; i
++) {
235 System
.out
.println("[" + (i
+ 1) + "]: " + allFiles
[i
].getName());
239 System
.out
.print("Please select one of the following FPD files to build:[1] ");
241 BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
243 String str
= br
.readLine();
244 if (str
.trim().length() == 0) {
249 int indexSelect
= Integer
.parseInt(str
);
250 if (indexSelect
<=0 || indexSelect
> allFiles
.length
) {
251 System
.out
.print("Please enter a number between [1.." + allFiles
.length
+ "]:[1] ");
254 file
= allFiles
[indexSelect
- 1];
258 } catch (Exception e
) {
259 System
.out
.print("Please enter a valid number:[1] ");
263 } else if (fpdFiles
.size() == 1) {
264 file
= fpdFiles
.toArray(new File
[1])[0];
270 public void setType(String type
) {
271 if (type
.equalsIgnoreCase("clean") || type
.equalsIgnoreCase("cleanall")) {
272 this.type
= type
.toLowerCase();
278 private void readTargetFile(){
280 String targetFile
= getProject().getProperty("WORKSPACE_DIR") + File
.separatorChar
+ targetFilename
;
282 String
[][] targetFileInfo
= ConfigReader
.parse(targetFile
);
285 // Get ToolChain Info from target.txt
287 ToolChainInfo envToolChainInfo
= new ToolChainInfo();
288 String str
= getValue(ToolDefinitions
.TARGET_KEY_TARGET
, targetFileInfo
);
289 if (str
== null || str
.trim().equals("")) {
290 envToolChainInfo
.addTargets("*");
292 envToolChainInfo
.addTargets(str
);
294 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLCHAIN
, targetFileInfo
);
295 if (str
== null || str
.trim().equals("")) {
296 envToolChainInfo
.addTagnames("*");
298 envToolChainInfo
.addTagnames(str
);
300 str
= getValue(ToolDefinitions
.TARGET_KEY_ARCH
, targetFileInfo
);
301 if (str
== null || str
.trim().equals("")) {
302 envToolChainInfo
.addArchs("*");
304 envToolChainInfo
.addArchs(str
);
306 GlobalData
.setToolChainEnvInfo(envToolChainInfo
);
308 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLS_DEF
, targetFileInfo
);
309 if (str
!= null && str
.trim().length() > 0) {
310 toolsDefFilename
= str
;
313 str
= getValue(ToolDefinitions
.TARGET_KEY_ACTIVE_PLATFORM
, targetFileInfo
);
314 if (str
!= null && ! str
.trim().equals("")) {
315 if ( ! str
.endsWith(".fpd")) {
316 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions
.FPD_EXTENSION
+ "\"!");
318 activePlatform
= str
;
321 catch (Exception ex
) {
322 throw new BuildException(ex
.getMessage());
326 private String
getValue(String key
, String
[][] map
) {
327 for (int i
= 0; i
< map
[0].length
; i
++){
328 if (key
.equalsIgnoreCase(map
[0][i
])) {