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
;
31 public class FrameworkBuildTask
extends Task
{
33 private Set
<File
> buildFiles
= new LinkedHashSet
<File
>();
35 private Set
<File
> fpdFiles
= new LinkedHashSet
<File
>();
37 private Set
<File
> msaFiles
= new LinkedHashSet
<File
>();
39 String toolsDefFilename
= "Tools" + File
.separatorChar
+ "Conf" + File
.separatorChar
+ "tools_def.txt";
41 String targetFilename
= "target.txt";
43 String activePlatform
= null;
46 /// there are three type: all (build), clean and cleanall
48 private String type
= "all";
50 public void execute() throws BuildException
{
52 // Seach build.xml -> .FPD -> .MSA file
56 // Gen Current Working Directory
58 File dummyFile
= new File(".");
59 File cwd
= dummyFile
.getCanonicalFile();
60 File
[] files
= cwd
.listFiles();
61 for (int i
= 0; i
< files
.length
; i
++) {
62 if (files
[i
].isFile()) {
63 if (files
[i
].getName().equalsIgnoreCase("build.xml")) {
65 // First, search build.xml, if found, ANT call it
67 buildFiles
.add(files
[i
]);
69 } else if (files
[i
].getName().endsWith(".fpd")) {
71 // Second, search FPD file, if found, build it
73 fpdFiles
.add(files
[i
]);
74 } else if (files
[i
].getName().endsWith(".msa")) {
76 // Third, search MSA file, if found, build it
78 msaFiles
.add(files
[i
]);
82 } catch (Exception e
) {
83 throw new BuildException(e
.getMessage());
87 // Deal with all environment variable (Add them to properties)
89 backupSystemProperties();
92 // Read target.txt file
97 // Global Data initialization
99 File workspacePath
= new File(getProject().getProperty("WORKSPACE"));
100 getProject().setProperty("WORKSPACE_DIR", workspacePath
.getPath().replaceAll("(\\\\)", "/"));
101 GlobalData
.initInfo("Tools" + File
.separatorChar
+ "Conf" + File
.separatorChar
+ "FrameworkDatabase.db",
102 workspacePath
.getPath(), toolsDefFilename
);
107 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
109 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
110 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
111 // If find more than one FPD files, let user select one.
113 File buildFile
= null;
114 if (msaFiles
.size() > 1) {
115 throw new BuildException("Having more than one MSA file in a directory is not allowed!");
116 } else if (msaFiles
.size() == 1 && activePlatform
== null) {
117 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [Tool/Conf/target.txt]. ");
118 } else if (msaFiles
.size() == 1 && activePlatform
!= null) {
120 // Build the single module
122 buildFile
= msaFiles
.toArray(new File
[1])[0];
123 } else if (activePlatform
!= null) {
124 buildFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
125 } else if (fpdFiles
.size() == 1) {
126 buildFile
= fpdFiles
.toArray(new File
[1])[0];
127 } else if (fpdFiles
.size() > 1) {
128 buildFile
= intercommuniteWithUser();
131 // If there is no build files or FPD files or MSA files, stop build
134 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
138 // Build every FPD files (PLATFORM build)
140 if (buildFile
.getName().endsWith(".fpd")) {
141 System
.out
.println("Processing the FPD file [" + buildFile
.getPath() + "] ..>> ");
142 FpdParserTask fpdParserTask
= new FpdParserTask();
143 fpdParserTask
.setType(type
);
144 fpdParserTask
.setProject(getProject());
145 fpdParserTask
.setFpdFile(buildFile
);
146 fpdParserTask
.execute();
150 // Build every MSA files (SINGLE MODULE BUILD)
152 else if (buildFile
.getName().endsWith(".msa")) {
153 File tmpFile
= new File(GlobalData
.getWorkspacePath() + File
.separatorChar
+ activePlatform
);
154 System
.out
.println("Using the FPD file [" + tmpFile
.getPath() + "] for the active platform. ");
155 System
.out
.println("Processing the MSA file [" + buildFile
.getPath() + "] ..>> ");
156 GenBuildTask genBuildTask
= new GenBuildTask();
157 genBuildTask
.setSingleModuleBuild(true);
158 genBuildTask
.setType(type
);
159 getProject().setProperty("PLATFORM_FILE", activePlatform
);
160 genBuildTask
.setProject(getProject());
161 genBuildTask
.setMsaFile(buildFile
);
162 genBuildTask
.execute();
167 Transfer system environment variables to ANT properties. If system variable
168 already exiests in ANT properties, skip it.
171 private void backupSystemProperties() {
172 Map
<String
, String
> sysProperties
= System
.getenv();
173 Set
<String
> keys
= sysProperties
.keySet();
174 Iterator
<String
> iter
= keys
.iterator();
175 while (iter
.hasNext()) {
176 String name
= iter
.next();
179 // If system environment variable is not in ANT properties, add it
181 if (getProject().getProperty(name
) == null) {
182 getProject().setProperty(name
, sysProperties
.get(name
));
187 private File
intercommuniteWithUser(){
189 if (fpdFiles
.size() > 1) {
190 File
[] allFiles
= new File
[fpdFiles
.size()];
192 Iterator
<File
> iter
= fpdFiles
.iterator();
193 while (iter
.hasNext()) {
194 allFiles
[index
] = iter
.next();
198 System
.out
.println("Finding " + allFiles
.length
+ " FPD files: ");
199 for (int i
= 0; i
< allFiles
.length
; i
++) {
200 System
.out
.println("[" + (i
+ 1) + "]: " + allFiles
[i
].getName());
204 System
.out
.print("Please select one of the following FPD files to build:[1] ");
206 BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
208 String str
= br
.readLine();
209 if (str
.trim().length() == 0) {
214 int indexSelect
= Integer
.parseInt(str
);
215 if (indexSelect
<=0 || indexSelect
> allFiles
.length
) {
216 System
.out
.print("Please enter a number between [1.." + allFiles
.length
+ "]:[1] ");
219 file
= allFiles
[indexSelect
- 1];
223 } catch (Exception e
) {
224 System
.out
.print("Please enter a valid number:[1] ");
228 } else if (fpdFiles
.size() == 1) {
229 file
= fpdFiles
.toArray(new File
[1])[0];
235 public void setType(String type
) {
236 if (type
.equalsIgnoreCase("clean") || type
.equalsIgnoreCase("cleanall")) {
237 this.type
= type
.toLowerCase();
243 private void readTargetFile(){
245 String
[][] targetFileInfo
= ConfigReader
.parse(getProject().getProperty("WORKSPACE_DIR"), "Tools" + File
.separatorChar
+ "Conf" + File
.separatorChar
+ targetFilename
);
248 // Get ToolChain Info from target.txt
250 ToolChainInfo envToolChainInfo
= new ToolChainInfo();
251 String str
= getValue("TARGET", targetFileInfo
);
252 if (str
== null || str
.trim().equals("")) {
253 envToolChainInfo
.addTargets("*");
255 envToolChainInfo
.addTargets(str
);
257 str
= getValue("TOOL_CHAIN_TAG", targetFileInfo
);
258 if (str
== null || str
.trim().equals("")) {
259 envToolChainInfo
.addTagnames("*");
261 envToolChainInfo
.addTagnames(str
);
263 str
= getValue("TARGET_ARCH", targetFileInfo
);
264 if (str
== null || str
.trim().equals("")) {
265 envToolChainInfo
.addArchs("*");
267 envToolChainInfo
.addArchs(str
);
269 GlobalData
.setToolChainEnvInfo(envToolChainInfo
);
271 str
= getValue("TOOL_CHAIN_CONF", targetFileInfo
);
272 if (str
!= null && str
.trim().length() > 0) {
273 toolsDefFilename
= str
;
276 str
= getValue("ACTIVE_PLATFORM", targetFileInfo
);
277 if (str
!= null && ! str
.trim().equals("")) {
278 if ( ! str
.endsWith(".fpd")) {
279 throw new BuildException("FPD file's extension must be \".fpd\"!");
281 activePlatform
= str
;
284 catch (Exception ex
) {
285 throw new BuildException(ex
.getMessage());
289 private String
getValue(String key
, String
[][] map
) {
290 for (int i
= 0; i
< map
[0].length
; i
++){
291 if (key
.equalsIgnoreCase(map
[0][i
])) {