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