Change to new XML Schema.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / FrameworkBuildTask.java
1 package org.tianocore.build;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.InputStreamReader;
6 import java.util.Iterator;
7 import java.util.LinkedHashSet;
8 import java.util.Map;
9 import java.util.Set;
10
11 import org.apache.tools.ant.BuildException;
12 import org.apache.tools.ant.Task;
13 import org.tianocore.build.fpd.FpdParserTask;
14 import org.tianocore.build.global.GlobalData;
15 import org.tianocore.build.toolchain.ToolChainInfo;
16
17 public class FrameworkBuildTask extends Task{
18
19 private Set<File> buildFiles = new LinkedHashSet<File>();
20
21 private Set<File> fpdFiles = new LinkedHashSet<File>();
22
23 private Set<File> msaFiles = new LinkedHashSet<File>();
24
25 ///
26 /// there are three type: all (build), clean and cleanall
27 ///
28 private String type = "all";
29
30 public void execute() throws BuildException {
31 //
32 // Seach build.xml -> .FPD -> .MSA file
33 //
34 try {
35 //
36 // Gen Current Working Directory
37 //
38 File dummyFile = new File(".");
39 File cwd = dummyFile.getCanonicalFile();
40 File[] files = cwd.listFiles();
41 for (int i = 0; i < files.length; i++) {
42 if (files[i].isFile()) {
43 if (files[i].getName().equalsIgnoreCase("build.xml")) {
44 //
45 // First, search build.xml, if found, ANT call it
46 //
47 buildFiles.add(files[i]);
48
49 } else if (files[i].getName().endsWith(".fpd")) {
50 //
51 // Second, search FPD file, if found, build it
52 //
53 fpdFiles.add(files[i]);
54 } else if (files[i].getName().endsWith(".msa")) {
55 //
56 // Third, search MSA file, if found, build it
57 //
58 msaFiles.add(files[i]);
59 }
60 }
61 }
62 } catch (Exception e) {
63 e.printStackTrace();
64 throw new BuildException(e.getMessage());
65 }
66
67 //
68 // If there is no build files or FPD files or MSA files, stop build
69 //
70 if (fpdFiles.size() == 0 && msaFiles.size() == 0) {
71 throw new BuildException("Can't find any build.xml file or FPD files or MSA files in current directory. ");
72 }
73
74 File buildFile = intercommuniteWithUser();
75 System.out.println("Start to build file [" + buildFile.getPath() + "] ..>> ");
76
77 //
78 // Deal with all environment variable (Add them to properties)
79 //
80 backupSystemProperties();
81
82 //
83 // Get ToolChain Info from environment
84 //
85 ToolChainInfo envToolChainInfo = new ToolChainInfo();
86 envToolChainInfo.addTargets(getProject().getProperty("TARGET"));
87 envToolChainInfo.addTagnames(getProject().getProperty("TAGNAME"));
88 envToolChainInfo.addArchs(getProject().getProperty("ARCH"));
89 GlobalData.setToolChainEnvInfo(envToolChainInfo);
90
91 //
92 // Global Data initialization
93 //
94 String toolsDefFilename = "tools_def.txt";
95 if (getProject().getProperty("TOOLS_DEF") != null) {
96 toolsDefFilename = getProject().getProperty("TOOLS_DEF");
97 }
98
99 GlobalData.initInfo("Tools" + File.separatorChar + "Conf" + File.separatorChar + "FrameworkDatabase.db",
100 getProject().getProperty("WORKSPACE_DIR"), toolsDefFilename);
101
102 //
103 // Build every FPD files (PLATFORM build)
104 //
105 if (buildFile.getName().endsWith(".fpd")) {
106 FpdParserTask fpdParserTask = new FpdParserTask();
107 fpdParserTask.setType(type);
108 fpdParserTask.setProject(getProject());
109 fpdParserTask.setFpdFile(buildFile);
110 fpdParserTask.execute();
111 }
112
113 //
114 // Build every MSA files (SINGLE MODULE BUILD)
115 //
116 else if (buildFile.getName().endsWith(".msa")) {
117 GenBuildTask genBuildTask = new GenBuildTask();
118 genBuildTask.setType(type);
119 genBuildTask.setProject(getProject());
120 genBuildTask.setMsaFile(buildFile);
121 genBuildTask.execute();
122 }
123 }
124
125 /**
126 Transfer system environment variables to ANT properties. If system variable
127 already exiests in ANT properties, skip it.
128
129 **/
130 private void backupSystemProperties() {
131 Map<String, String> sysProperties = System.getenv();
132 Set<String> keys = sysProperties.keySet();
133 Iterator<String> iter = keys.iterator();
134 while (iter.hasNext()) {
135 String name = iter.next();
136
137 //
138 // If system environment variable is not in ANT properties, add it
139 //
140 if (getProject().getProperty(name) == null) {
141 getProject().setProperty(name, sysProperties.get(name));
142 }
143 }
144 }
145
146 private File intercommuniteWithUser(){
147 File file = null;
148 if (fpdFiles.size() + msaFiles.size() > 1) {
149 File[] allFiles = new File[fpdFiles.size() + msaFiles.size()];
150 int index = 0;
151 Iterator<File> iter = fpdFiles.iterator();
152 while (iter.hasNext()) {
153 allFiles[index] = iter.next();
154 index++;
155 }
156 iter = msaFiles.iterator();
157 while (iter.hasNext()) {
158 allFiles[index] = iter.next();
159 index++;
160 }
161 System.out.println("Find " + allFiles.length + " FPD and MSA files: ");
162 for (int i = 0; i < allFiles.length; i++) {
163 System.out.println("[" + (i + 1) + "]: " + allFiles[i].getName());
164 }
165
166 boolean flag = true;
167 System.out.print("Please select one file to build:[1] ");
168 do{
169 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
170 try {
171 String str = br.readLine();
172 if (str.trim().length() == 0) {
173 file = allFiles[0];
174 flag = false;
175 continue ;
176 }
177 int indexSelect = Integer.parseInt(str);
178 if (indexSelect <=0 || indexSelect > allFiles.length) {
179 System.out.print("Please enter a number between [1.." + allFiles.length + "]:[1] ");
180 continue ;
181 } else {
182 file = allFiles[indexSelect - 1];
183 flag = false;
184 continue ;
185 }
186 } catch (Exception e) {
187 System.out.print("Please enter a valid number:[1] ");
188 flag = true;
189 }
190 } while (flag);
191 }
192 else if (fpdFiles.size() == 1) {
193 file = fpdFiles.toArray(new File[1])[0];
194 }
195 else if (msaFiles.size() == 1) {
196 file = msaFiles.toArray(new File[1])[0];
197 }
198 return file;
199 }
200
201
202 public void setType(String type) {
203 if (type.equalsIgnoreCase("clean") || type.equalsIgnoreCase("cleanall")) {
204 this.type = type.toLowerCase();
205 }
206 else {
207 this.type = "all";
208 }
209 }
210 }