Support prebuild and postbuild for UserExtension for Platform build. If UserExtension...
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / FrameworkBuildTask.java
1 /** @file FrameworkBuildTask.java
2
3 The file is ANT task to find MSA or FPD file and build them.
4
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
10
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.
13 **/
14 package org.tianocore.build;
15
16 import java.io.BufferedReader;
17 import java.io.File;
18 import java.io.InputStreamReader;
19 import java.util.Iterator;
20 import java.util.LinkedHashSet;
21 import java.util.Map;
22 import java.util.Set;
23
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
31 public class FrameworkBuildTask extends Task{
32
33 private Set<File> buildFiles = new LinkedHashSet<File>();
34
35 private Set<File> fpdFiles = new LinkedHashSet<File>();
36
37 private Set<File> msaFiles = new LinkedHashSet<File>();
38
39 String toolsDefFilename = "Tools" + File.separatorChar + "Conf" + File.separatorChar + "tools_def.txt";
40
41 String targetFilename = "target.txt";
42
43 String activePlatform = null;
44
45 ///
46 /// there are three type: all (build), clean and cleanall
47 ///
48 private String type = "all";
49
50 public void execute() throws BuildException {
51 //
52 // Seach build.xml -> .FPD -> .MSA file
53 //
54 try {
55 //
56 // Gen Current Working Directory
57 //
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")) {
64 //
65 // First, search build.xml, if found, ANT call it
66 //
67 buildFiles.add(files[i]);
68
69 } else if (files[i].getName().endsWith(".fpd")) {
70 //
71 // Second, search FPD file, if found, build it
72 //
73 fpdFiles.add(files[i]);
74 } else if (files[i].getName().endsWith(".msa")) {
75 //
76 // Third, search MSA file, if found, build it
77 //
78 msaFiles.add(files[i]);
79 }
80 }
81 }
82 } catch (Exception e) {
83 e.printStackTrace();
84 throw new BuildException(e.getMessage());
85 }
86
87 //
88 // Deal with all environment variable (Add them to properties)
89 //
90 backupSystemProperties();
91
92 //
93 // Read target.txt file
94 //
95 readTargetFile();
96
97 //
98 // Global Data initialization
99 //
100 GlobalData.initInfo("Tools" + File.separatorChar + "Conf" + File.separatorChar + "FrameworkDatabase.db",
101 getProject().getProperty("WORKSPACE_DIR"), toolsDefFilename);
102
103
104
105 //
106 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
107 // else fail build.
108 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
109 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
110 // If find more than one FPD files, let user select one.
111 //
112 File buildFile = null;
113 if (msaFiles.size() > 1) {
114 throw new BuildException("More than one MSA file under current directory. It is not allowd. ");
115 }
116 else if (msaFiles.size() == 1 && activePlatform == null) {
117 throw new BuildException("If try to build a single module, please set ACTIVE_PLATFORM in file [Tool/Conf/target.txt]. ");
118 }
119 else if (msaFiles.size() == 1 && activePlatform != null) {
120 //
121 // Build the single module
122 //
123 buildFile = msaFiles.toArray(new File[1])[0];
124 }
125 else if (activePlatform != null) {
126 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
127 }
128 else if (fpdFiles.size() == 1) {
129 buildFile = fpdFiles.toArray(new File[1])[0];
130 }
131 else if (fpdFiles.size() > 1) {
132 buildFile = intercommuniteWithUser();
133 }
134 //
135 // If there is no build files or FPD files or MSA files, stop build
136 //
137 else {
138 throw new BuildException("Can't find any FPD files or MSA files in current directory. ");
139 }
140
141 //
142 // Build every FPD files (PLATFORM build)
143 //
144 if (buildFile.getName().endsWith(".fpd")) {
145 System.out.println("Start to build FPD file [" + buildFile.getPath() + "] ..>> ");
146 FpdParserTask fpdParserTask = new FpdParserTask();
147 fpdParserTask.setType(type);
148 fpdParserTask.setProject(getProject());
149 fpdParserTask.setFpdFile(buildFile);
150 fpdParserTask.execute();
151 }
152
153 //
154 // Build every MSA files (SINGLE MODULE BUILD)
155 //
156 else if (buildFile.getName().endsWith(".msa")) {
157 System.out.println("Start to build 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();
165 }
166 }
167
168 /**
169 Transfer system environment variables to ANT properties. If system variable
170 already exiests in ANT properties, skip it.
171
172 **/
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();
179
180 //
181 // If system environment variable is not in ANT properties, add it
182 //
183 if (getProject().getProperty(name) == null) {
184 getProject().setProperty(name, sysProperties.get(name));
185 }
186 }
187 }
188
189 private File intercommuniteWithUser(){
190 File file = null;
191 if (fpdFiles.size() + msaFiles.size() > 1) {
192 File[] allFiles = new File[fpdFiles.size() + msaFiles.size()];
193 int index = 0;
194 Iterator<File> iter = fpdFiles.iterator();
195 while (iter.hasNext()) {
196 allFiles[index] = iter.next();
197 index++;
198 }
199 iter = msaFiles.iterator();
200 while (iter.hasNext()) {
201 allFiles[index] = iter.next();
202 index++;
203 }
204 System.out.println("Find " + allFiles.length + " FPD and MSA files: ");
205 for (int i = 0; i < allFiles.length; i++) {
206 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());
207 }
208
209 boolean flag = true;
210 System.out.print("Please select one file to build:[1] ");
211 do{
212 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
213 try {
214 String str = br.readLine();
215 if (str.trim().length() == 0) {
216 file = allFiles[0];
217 flag = false;
218 continue ;
219 }
220 int indexSelect = Integer.parseInt(str);
221 if (indexSelect <=0 || indexSelect > allFiles.length) {
222 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");
223 continue ;
224 } else {
225 file = allFiles[indexSelect - 1];
226 flag = false;
227 continue ;
228 }
229 } catch (Exception e) {
230 System.out.print("Please enter a valid number:[1] ");
231 flag = true;
232 }
233 } while (flag);
234 }
235 else if (fpdFiles.size() == 1) {
236 file = fpdFiles.toArray(new File[1])[0];
237 }
238 else if (msaFiles.size() == 1) {
239 file = msaFiles.toArray(new File[1])[0];
240 }
241 return file;
242 }
243
244
245 public void setType(String type) {
246 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {
247 this.type = type.toLowerCase();
248 }
249 else {
250 this.type = "all";
251 }
252 }
253
254 private void readTargetFile(){
255 try {
256 String[][] targetFileInfo = ConfigReader.parse(getProject().getProperty("WORKSPACE_DIR"), "Tools" + File.separatorChar + "Conf" + File.separatorChar + targetFilename);
257
258 //
259 // Get ToolChain Info from target.txt
260 //
261 ToolChainInfo envToolChainInfo = new ToolChainInfo();
262 String str = getValue("TARGET", targetFileInfo);
263 if (str == null || str.trim().equals("")) {
264 envToolChainInfo.addTargets("*");
265 }
266 else {
267 envToolChainInfo.addTargets(str);
268 }
269 str = getValue("TOOL_CHAIN_TAG", targetFileInfo);
270 if (str == null || str.trim().equals("")) {
271 envToolChainInfo.addTagnames("*");
272 }
273 else {
274 envToolChainInfo.addTagnames(str);
275 }
276 str = getValue("TARGET_ARCH", targetFileInfo);
277 if (str == null || str.trim().equals("")) {
278 envToolChainInfo.addArchs("*");
279 }
280 else {
281 envToolChainInfo.addArchs(str);
282 }
283 GlobalData.setToolChainEnvInfo(envToolChainInfo);
284
285 str = getValue("TOOL_CHAIN_CONF", targetFileInfo);
286 if (str != null) {
287 toolsDefFilename = str;
288 }
289
290 str = getValue("ACTIVE_PLATFORM", targetFileInfo);
291 if (str != null && ! str.trim().equals("")) {
292 if ( ! str.endsWith(".fpd")) {
293 throw new BuildException("FPD file's file extension must be \".fpd\"");
294 }
295 activePlatform = str;
296 }
297 }
298 catch (Exception ex) {
299 throw new BuildException(ex.getMessage());
300 }
301 }
302
303 private String getValue(String key, String[][] map) {
304 for (int i = 0; i < map[0].length; i++){
305 if (key.equalsIgnoreCase(map[0][i])) {
306 return map[1][i];
307 }
308 }
309 return null;
310 }
311 }