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
;
32 public class FrameworkBuildTask
extends Task
{
34 private Set
<File
> buildFiles
= new LinkedHashSet
<File
>();
36 private Set
<File
> fpdFiles
= new LinkedHashSet
<File
>();
38 private Set
<File
> msaFiles
= new LinkedHashSet
<File
>();
40 String toolsDefFilename
= ToolDefinitions
.DEFAULT_TOOLS_DEF_FILE_PATH
;
42 String targetFilename
= ToolDefinitions
.TARGET_FILE_PATH
;
44 String dbFilename
= ToolDefinitions
.FRAMEWORK_DATABASE_FILE_PATH
;
46 String activePlatform
= null;
49 /// there are three type: all (build), clean and cleanall
51 private String type
= "all";
53 public void execute() throws BuildException
{
55 // Seach build.xml -> .FPD -> .MSA file
59 // Gen Current Working Directory
61 File dummyFile
= new File(".");
62 File cwd
= dummyFile
.getCanonicalFile();
63 File
[] files
= cwd
.listFiles();
64 for (int i
= 0; i
< files
.length
; i
++) {
65 if (files
[i
].isFile()) {
66 if (files
[i
].getName().equalsIgnoreCase("build.xml")) {
68 // First, search build.xml, if found, ANT call it
70 buildFiles
.add(files
[i
]);
72 } else if (files
[i
].getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
74 // Second, search FPD file, if found, build it
76 fpdFiles
.add(files
[i
]);
77 } else if (files
[i
].getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
79 // Third, search MSA file, if found, build it
81 msaFiles
.add(files
[i
]);
85 } catch (Exception e
) {
86 throw new BuildException(e
.getMessage());
90 // Deal with all environment variable (Add them to properties)
92 backupSystemProperties();
95 // Read target.txt file
100 // Global Data initialization
102 File workspacePath
= new File(getProject().getProperty("WORKSPACE"));
103 getProject().setProperty("WORKSPACE_DIR", workspacePath
.getPath().replaceAll("(\\\\)", "/"));
104 GlobalData
.initInfo(dbFilename
, workspacePath
.getPath(), toolsDefFilename
);
109 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
111 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
112 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
113 // If find more than one FPD files, let user select one.
115 File buildFile
= null;
116 if (msaFiles
.size() > 1) {
117 throw new BuildException("Having more than one MSA file in a directory is not allowed!");
118 } else if (msaFiles
.size() == 1 && activePlatform
== null) {
119 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename
+ "]. ");
120 } else if (msaFiles
.size() == 1 && activePlatform
!= null) {
122 // Build the single module
124 buildFile
= msaFiles
.toArray(new File
[1])[0];
125 } else if (activePlatform
!= null) {
126 buildFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
127 } else if (fpdFiles
.size() == 1) {
128 buildFile
= fpdFiles
.toArray(new File
[1])[0];
129 } else if (fpdFiles
.size() > 1) {
130 buildFile
= intercommuniteWithUser();
133 // If there is no build files or FPD files or MSA files, stop build
136 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
140 // Build every FPD files (PLATFORM build)
142 if (buildFile
.getName().endsWith(ToolDefinitions
.FPD_EXTENSION
)) {
143 System
.out
.println("Processing the FPD file [" + buildFile
.getPath() + "] ..>> ");
144 FpdParserTask fpdParserTask
= new FpdParserTask();
145 fpdParserTask
.setType(type
);
146 fpdParserTask
.setProject(getProject());
147 fpdParserTask
.setFpdFile(buildFile
);
148 fpdParserTask
.execute();
152 // Build every MSA files (SINGLE MODULE BUILD)
154 else if (buildFile
.getName().endsWith(ToolDefinitions
.MSA_EXTENSION
)) {
155 File tmpFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
156 System
.out
.println("Using the FPD file [" + tmpFile
.getPath() + "] for the active platform. ");
157 System
.out
.println("Processing the MSA file [" + buildFile
.getPath() + "] ..>> ");
158 GenBuildTask genBuildTask
= new GenBuildTask();
159 genBuildTask
.setSingleModuleBuild(true);
160 genBuildTask
.setType(type
);
161 getProject().setProperty("PLATFORM_FILE", activePlatform
);
162 genBuildTask
.setProject(getProject());
163 genBuildTask
.setMsaFile(buildFile
);
164 genBuildTask
.execute();
169 Transfer system environment variables to ANT properties. If system variable
170 already exiests in ANT properties, skip it.
173 private void backupSystemProperties() {
174 Map
<String
, String
> sysProperties
= System
.getenv();
175 Set
<String
> keys
= sysProperties
.keySet();
176 Iterator
<String
> iter
= keys
.iterator();
177 while (iter
.hasNext()) {
178 String name
= iter
.next();
181 // If system environment variable is not in ANT properties, add it
183 if (getProject().getProperty(name
) == null) {
184 getProject().setProperty(name
, sysProperties
.get(name
));
189 private File
intercommuniteWithUser(){
191 if (fpdFiles
.size() > 1) {
192 File
[] allFiles
= new File
[fpdFiles
.size()];
194 Iterator
<File
> iter
= fpdFiles
.iterator();
195 while (iter
.hasNext()) {
196 allFiles
[index
] = iter
.next();
200 System
.out
.println("Finding " + allFiles
.length
+ " FPD files: ");
201 for (int i
= 0; i
< allFiles
.length
; i
++) {
202 System
.out
.println("[" + (i
+ 1) + "]: " + allFiles
[i
].getName());
206 System
.out
.print("Please select one of the following FPD files to build:[1] ");
208 BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
210 String str
= br
.readLine();
211 if (str
.trim().length() == 0) {
216 int indexSelect
= Integer
.parseInt(str
);
217 if (indexSelect
<=0 || indexSelect
> allFiles
.length
) {
218 System
.out
.print("Please enter a number between [1.." + allFiles
.length
+ "]:[1] ");
221 file
= allFiles
[indexSelect
- 1];
225 } catch (Exception e
) {
226 System
.out
.print("Please enter a valid number:[1] ");
230 } else if (fpdFiles
.size() == 1) {
231 file
= fpdFiles
.toArray(new File
[1])[0];
237 public void setType(String type
) {
238 if (type
.equalsIgnoreCase("clean") || type
.equalsIgnoreCase("cleanall")) {
239 this.type
= type
.toLowerCase();
245 private void readTargetFile(){
247 String targetFile
= getProject().getProperty("WORKSPACE_DIR") + File
.separatorChar
+ targetFilename
;
249 String
[][] targetFileInfo
= ConfigReader
.parse(targetFile
);
252 // Get ToolChain Info from target.txt
254 ToolChainInfo envToolChainInfo
= new ToolChainInfo();
255 String str
= getValue(ToolDefinitions
.TARGET_KEY_TARGET
, targetFileInfo
);
256 if (str
== null || str
.trim().equals("")) {
257 envToolChainInfo
.addTargets("*");
259 envToolChainInfo
.addTargets(str
);
261 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLCHAIN
, targetFileInfo
);
262 if (str
== null || str
.trim().equals("")) {
263 envToolChainInfo
.addTagnames("*");
265 envToolChainInfo
.addTagnames(str
);
267 str
= getValue(ToolDefinitions
.TARGET_KEY_ARCH
, targetFileInfo
);
268 if (str
== null || str
.trim().equals("")) {
269 envToolChainInfo
.addArchs("*");
271 envToolChainInfo
.addArchs(str
);
273 GlobalData
.setToolChainEnvInfo(envToolChainInfo
);
275 str
= getValue(ToolDefinitions
.TARGET_KEY_TOOLS_DEF
, targetFileInfo
);
276 if (str
!= null && str
.trim().length() > 0) {
277 toolsDefFilename
= str
;
280 str
= getValue(ToolDefinitions
.TARGET_KEY_ACTIVE_PLATFORM
, targetFileInfo
);
281 if (str
!= null && ! str
.trim().equals("")) {
282 if ( ! str
.endsWith(".fpd")) {
283 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions
.FPD_EXTENSION
+ "\"!");
285 activePlatform
= str
;
288 catch (Exception ex
) {
289 throw new BuildException(ex
.getMessage());
293 private String
getValue(String key
, String
[][] map
) {
294 for (int i
= 0; i
< map
[0].length
; i
++){
295 if (key
.equalsIgnoreCase(map
[0][i
])) {