]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/FrameworkBuildTask.java
modify r8onlylib generate
[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.Hashtable;
20 import java.util.Iterator;
21 import java.util.LinkedHashSet;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.tianocore.build.fpd.FpdParserForThread;
28 import org.tianocore.build.fpd.FpdParserTask;
29 import org.tianocore.build.global.GenBuildLogger;
30 import org.tianocore.build.global.GlobalData;
31 import org.tianocore.build.toolchain.ConfigReader;
32 import org.tianocore.build.toolchain.ToolChainInfo;
33 import org.tianocore.common.definitions.ToolDefinitions;
34 import org.tianocore.common.logger.EdkLog;
35
36 /**
37 <p>
38 <code>FrameworkBuildTask</code> is an Ant task. The main function is finding
39 and processing a FPD or MSA file, then building a platform or stand-alone
40 module.
41
42 <p>
43 The task search current directory and find out all MSA and FPD files by file
44 extension. Base on ACTIVE_PLATFORM policy, decide to build a platform or a
45 stand-alone module. The ACTIVE_PLATFORM policy is:
46
47 <pre>
48 1. More than one MSA files, report error;
49 2. Only one MSA file, but ACTIVE_PLATFORM is not specified, report error;
50 3. Only one MSA file, and ACTIVE_PLATFORM is also specified, build this module;
51 4. No MSA file, and ACTIVE_PLATFORM is specified, build the active platform;
52 5. No MSA file, no ACTIVE_PLATFORM, and no FPD file, report error;
53 6. No MSA file, no ACTIVE_PLATFORM, and only one FPD file, build the platform;
54 7. No MSA file, no ACTIVE_PLATFORM, and more than one FPD files, list all platform
55 and let user choose one.
56 </pre>
57
58 <p>
59 Framework build task also parse target file [${WORKSPACE_DIR}/Tools/Conf/target.txt].
60 And load all system environment variables to Ant properties.
61
62 <p>
63 The usage for this task is :
64
65 <pre>
66 &lt;FrameworkBuild type="cleanall" /&gt;
67 </pre>
68
69 @since GenBuild 1.0
70 **/
71 public class FrameworkBuildTask extends Task{
72
73 private Set<File> buildFiles = new LinkedHashSet<File>();
74
75 private Set<File> fpdFiles = new LinkedHashSet<File>();
76
77 private Set<File> msaFiles = new LinkedHashSet<File>();
78
79 //
80 // This is only for none-multi-thread build to reduce overriding message
81 //
82 public static Hashtable<String, String> originalProperties = new Hashtable<String, String>();
83
84 String toolsDefFilename = ToolDefinitions.DEFAULT_TOOLS_DEF_FILE_PATH;
85
86 String targetFilename = ToolDefinitions.TARGET_FILE_PATH;
87
88 String dbFilename = ToolDefinitions.FRAMEWORK_DATABASE_FILE_PATH;
89
90 String activePlatform = null;
91
92 ///
93 /// The flag to present current is multi-thread enabled
94 ///
95 public static boolean multithread = false;
96
97 ///
98 /// The concurrent thread number
99 ///
100 public static int MAX_CONCURRENT_THREAD_NUMBER = 1;
101
102 ///
103 /// there are three type: all (build), clean and cleanall
104 ///
105 private String type = "all";
106
107 public void execute() throws BuildException {
108 //
109 // set Logger
110 //
111 GenBuildLogger logger = new GenBuildLogger(getProject());
112 EdkLog.setLogLevel(getProject().getProperty("env.LOGLEVEL"));
113 EdkLog.setLogger(logger);
114
115 //
116 // Seach build.xml -> .FPD -> .MSA file
117 //
118 try {
119 //
120 // Gen Current Working Directory
121 //
122 File dummyFile = new File(".");
123 File cwd = dummyFile.getCanonicalFile();
124 File[] files = cwd.listFiles();
125 for (int i = 0; i < files.length; i++) {
126 if (files[i].isFile()) {
127 if (files[i].getName().equalsIgnoreCase("build.xml")) {
128 //
129 // First, search build.xml, if found, ANT call it
130 //
131 buildFiles.add(files[i]);
132
133 } else if (files[i].getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {
134 //
135 // Second, search FPD file, if found, build it
136 //
137 fpdFiles.add(files[i]);
138 } else if (files[i].getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
139 //
140 // Third, search MSA file, if found, build it
141 //
142 msaFiles.add(files[i]);
143 }
144 }
145 }
146 } catch (Exception e) {
147 throw new BuildException(e.getMessage());
148 }
149
150 //
151 // Deal with all environment variable (Add them to properties)
152 //
153 backupSystemProperties();
154
155 //
156 // Read target.txt file
157 //
158 readTargetFile();
159
160 //
161 // Global Data initialization
162 //
163 File workspacePath = new File(getProject().getProperty("WORKSPACE"));
164 getProject().setProperty("WORKSPACE_DIR", workspacePath.getPath().replaceAll("(\\\\)", "/"));
165 GlobalData.initInfo(dbFilename, workspacePath.getPath(), toolsDefFilename);
166
167 //
168 // If find MSA file and ACTIVE_PLATFORM is set, build the module;
169 // else fail build.
170 // If without MSA file, and ACTIVE_PLATFORM is set, build the ACTIVE_PLATFORM.
171 // If ACTIVE_PLATFORM is not set, and only find one FPD file, build the platform;
172 // If find more than one FPD files, let user select one.
173 //
174 File buildFile = null;
175 if (msaFiles.size() > 1) {
176 throw new BuildException("Having more than one MSA file in a directory is not allowed!");
177 } else if (msaFiles.size() == 1 && activePlatform == null) {
178 throw new BuildException("If trying to build a single module, please set ACTIVE_PLATFORM in file [" + targetFilename + "]. ");
179 } else if (msaFiles.size() == 1 && activePlatform != null) {
180 //
181 // Build the single module
182 //
183 buildFile = msaFiles.toArray(new File[1])[0];
184 } else if (activePlatform != null) {
185 buildFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
186 } else if (fpdFiles.size() == 1) {
187 buildFile = fpdFiles.toArray(new File[1])[0];
188 } else if (fpdFiles.size() > 1) {
189 buildFile = intercommuniteWithUser();
190 }
191 //
192 // If there is no build files or FPD files or MSA files, stop build
193 //
194 else {
195 throw new BuildException("Can't find any FPD or MSA files in the current directory. ");
196 }
197
198 //
199 // Build every FPD files (PLATFORM build)
200 //
201 if (buildFile.getName().endsWith(ToolDefinitions.FPD_EXTENSION)) {
202 System.out.println("Processing the FPD file [" + buildFile.getPath() + "] ..>> ");
203 //
204 // Iff for platform build will enable the multi-thread if set in target.txt
205 //
206 if (multithread && type.equalsIgnoreCase("all")) {
207 System.out.println("Multi-thread build is enabled. ");
208 FpdParserForThread fpdParserForThread = new FpdParserForThread();
209 fpdParserForThread.setType(type);
210 fpdParserForThread.setProject(getProject());
211 fpdParserForThread.setFpdFile(buildFile);
212 fpdParserForThread.perform();
213 return ;
214 }
215
216 FpdParserTask fpdParserTask = new FpdParserTask();
217 fpdParserTask.setType(type);
218 fpdParserTask.setProject(getProject());
219 fpdParserTask.setFpdFile(buildFile);
220 fpdParserTask.perform();
221
222 //
223 // If cleanall delete the Platform_build.xml
224 //
225 if (type.compareTo("cleanall") == 0) {
226 File platformBuildFile =
227 new File(getProject().getProperty("PLATFORM_DIR")
228 + File.separatorChar
229 + getProject().getProperty("PLATFORM")
230 + "_build.xml");
231 platformBuildFile.deleteOnExit();
232 }
233 }
234
235 //
236 // Build every MSA files (SINGLE MODULE BUILD)
237 //
238 else if (buildFile.getName().endsWith(ToolDefinitions.MSA_EXTENSION)) {
239 File tmpFile = new File(GlobalData.getWorkspacePath() + File.separatorChar + activePlatform);
240 System.out.println("Using the FPD file [" + tmpFile.getPath() + "] for the active platform. ");
241 System.out.println("Processing the MSA file [" + buildFile.getPath() + "] ..>> ");
242 GenBuildTask genBuildTask = new GenBuildTask();
243 genBuildTask.setSingleModuleBuild(true);
244 genBuildTask.setType(type);
245 getProject().setProperty("PLATFORM_FILE", activePlatform);
246 if( !multithread) {
247 originalProperties.put("PLATFORM_FILE", activePlatform);
248 }
249 genBuildTask.setProject(getProject());
250 genBuildTask.setMsaFile(buildFile);
251 genBuildTask.perform();
252 }
253 }
254
255 /**
256 Transfer system environment variables to ANT properties. If system variable
257 already exiests in ANT properties, skip it.
258
259 **/
260 private void backupSystemProperties() {
261 Map<String, String> sysProperties = System.getenv();
262 Iterator<String> iter = sysProperties.keySet().iterator();
263 while (iter.hasNext()) {
264 String name = iter.next();
265
266 //
267 // If system environment variable is not in ANT properties, add it
268 //
269 if (getProject().getProperty(name) == null) {
270 getProject().setProperty(name, sysProperties.get(name));
271 }
272 }
273
274 Hashtable allProperties = getProject().getProperties();
275 Iterator piter = allProperties.keySet().iterator();
276 while (piter.hasNext()) {
277 String name = (String)piter.next();
278 originalProperties.put(new String(name), new String((String)allProperties.get(name)));
279 }
280 }
281
282 private File intercommuniteWithUser(){
283 File file = null;
284 if (fpdFiles.size() > 1) {
285 File[] allFiles = new File[fpdFiles.size()];
286 int index = 0;
287 Iterator<File> iter = fpdFiles.iterator();
288 while (iter.hasNext()) {
289 allFiles[index] = iter.next();
290 index++;
291 }
292
293 System.out.println("Finding " + allFiles.length + " FPD files: ");
294 for (int i = 0; i < allFiles.length; i++) {
295 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());
296 }
297
298 boolean flag = true;
299 System.out.print("Please select one of the following FPD files to build:[1] ");
300 do{
301 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
302 try {
303 String str = br.readLine();
304 if (str.trim().length() == 0) {
305 file = allFiles[0];
306 flag = false;
307 continue ;
308 }
309 int indexSelect = Integer.parseInt(str);
310 if (indexSelect <=0 || indexSelect > allFiles.length) {
311 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");
312 continue ;
313 } else {
314 file = allFiles[indexSelect - 1];
315 flag = false;
316 continue ;
317 }
318 } catch (Exception e) {
319 System.out.print("Please enter a valid number:[1] ");
320 flag = true;
321 }
322 } while (flag);
323 } else if (fpdFiles.size() == 1) {
324 file = fpdFiles.toArray(new File[1])[0];
325 }
326 return file;
327 }
328
329
330 public void setType(String type) {
331 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {
332 this.type = type.toLowerCase();
333 } else {
334 this.type = "all";
335 }
336 }
337
338 private void readTargetFile(){
339 try {
340 String targetFile = getProject().getProperty("WORKSPACE_DIR") + File.separatorChar + targetFilename;
341
342 String[][] targetFileInfo = ConfigReader.parse(targetFile);
343
344 //
345 // Get ToolChain Info from target.txt
346 //
347 ToolChainInfo envToolChainInfo = new ToolChainInfo();
348 String str = getValue(ToolDefinitions.TARGET_KEY_TARGET, targetFileInfo);
349 if (str == null || str.trim().equals("")) {
350 envToolChainInfo.addTargets("*");
351 } else {
352 envToolChainInfo.addTargets(str);
353 }
354 str = getValue(ToolDefinitions.TARGET_KEY_TOOLCHAIN, targetFileInfo);
355 if (str == null || str.trim().equals("")) {
356 envToolChainInfo.addTagnames("*");
357 } else {
358 envToolChainInfo.addTagnames(str);
359 }
360 str = getValue(ToolDefinitions.TARGET_KEY_ARCH, targetFileInfo);
361 if (str == null || str.trim().equals("")) {
362 envToolChainInfo.addArchs("*");
363 } else {
364 envToolChainInfo.addArchs(str);
365 }
366 GlobalData.setToolChainEnvInfo(envToolChainInfo);
367
368 str = getValue(ToolDefinitions.TARGET_KEY_TOOLS_DEF, targetFileInfo);
369 if (str != null && str.trim().length() > 0) {
370 toolsDefFilename = str;
371 }
372
373 str = getValue(ToolDefinitions.TARGET_KEY_ACTIVE_PLATFORM, targetFileInfo);
374 if (str != null && ! str.trim().equals("")) {
375 if ( ! str.endsWith(".fpd")) {
376 throw new BuildException("FPD file's extension must be \"" + ToolDefinitions.FPD_EXTENSION + "\"!");
377 }
378 activePlatform = str;
379 }
380
381 str = getValue(ToolDefinitions.TARGET_KEY_MULTIPLE_THREAD, targetFileInfo);
382 if (str != null && str.trim().equalsIgnoreCase("Enable")) {
383 multithread = true;
384 }
385
386 str = getValue(ToolDefinitions.TARGET_KEY_MAX_CONCURRENT_THREAD_NUMBER, targetFileInfo);
387 if (str != null ) {
388 try {
389 int threadNum = Integer.parseInt(str);
390 if (threadNum > 0) {
391 MAX_CONCURRENT_THREAD_NUMBER = threadNum;
392 }
393 } catch (Exception enuma) {
394
395 }
396 }
397 }
398 catch (Exception ex) {
399 throw new BuildException(ex.getMessage());
400 }
401 }
402
403 private String getValue(String key, String[][] map) {
404 for (int i = 0; i < map[0].length; i++){
405 if (key.equalsIgnoreCase(map[0][i])) {
406 return map[1][i];
407 }
408 }
409 return null;
410 }
411 }