]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/fpd/FpdParserForThread.java
de9a28c7d5abf5f84a1e75c2269c8ed26d059553
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / fpd / FpdParserForThread.java
1 /** @file
2 This file is ANT task FpdParserTask.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 package org.tianocore.build.fpd;
14
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.LinkedHashMap;
19 import java.util.LinkedHashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.taskdefs.Ant;
26 import org.apache.xmlbeans.XmlObject;
27
28 import org.tianocore.build.global.GenBuildLogger;
29 import org.tianocore.build.global.GlobalData;
30 import org.tianocore.build.global.OutputManager;
31 import org.tianocore.build.id.FpdModuleIdentification;
32 import org.tianocore.build.id.ModuleIdentification;
33 import org.tianocore.build.FrameworkBuildTask;
34 import org.tianocore.build.GenBuildThread;
35 import org.tianocore.common.exception.EdkException;
36 import org.tianocore.common.logger.EdkLog;
37
38 /**
39
40 @since GenBuild 1.0
41 **/
42 public class FpdParserForThread extends FpdParserTask {
43
44 public static Map<FpdModuleIdentification, GenBuildThread> allThreads = new LinkedHashMap<FpdModuleIdentification, GenBuildThread>();
45
46 List<String> queueList = new ArrayList<String>();
47
48 public final static Object deamonSemaphore = new Object();
49
50 private final static Object countSemaphore = new Object();
51
52 public static int STATUS_DEPENDENCY_NOT_READY = 1;
53
54 public static int STATUS_DEPENDENCY_READY = 2;
55
56 public static int STATUS_START_RUN = 3;
57
58 public static int STATUS_END_RUN = 4;
59
60 private int currentQueueCode = 0;
61
62 public static int currentRunNumber = 0;
63
64 public static int totalNumber = 0;
65
66 public static int remainNumber = 0;
67
68 public static ThreadGroup tg = new ThreadGroup("Framework");
69
70 public static FpdModuleIdentification errorModule = null;
71
72 /**
73 Public construct method. It is necessary for ANT task.
74 **/
75 public FpdParserForThread() {
76 }
77
78 /**
79
80
81 **/
82 public void execute() throws BuildException {
83
84 this.setTaskName(".........");
85 //
86 // Parse FPD file
87 //
88 parseFpdFile();
89
90 //
91 // Prepare BUILD_DIR
92 //
93 isUnified = OutputManager.getInstance().prepareBuildDir(getProject());
94 String buildDir = getProject().getProperty("BUILD_DIR");
95
96 //
97 // For every Target and ToolChain
98 //
99 String[] targetList = GlobalData.getToolChainInfo().getTargets();
100 for (int i = 0; i < targetList.length; i++) {
101 String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
102 for(int j = 0; j < toolchainList.length; j++) {
103 //
104 // Prepare FV_DIR
105 //
106 String ffsCommonDir = buildDir + File.separatorChar
107 + targetList[i] + "_"
108 + toolchainList[j];
109 File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
110 fvDir.mkdirs();
111 getProject().setProperty("FV_DIR", fvDir.getPath().replaceAll("(\\\\)", "/"));
112
113 //
114 // Gen Fv.inf files
115 //
116 genFvInfFiles(ffsCommonDir);
117 }
118 }
119
120 //
121 // Gen build.xml
122 //
123 String platformBuildFile = buildDir + File.separatorChar + platformId.getName() + "_build.xml";
124 PlatformBuildFileGenerator fileGenerator = new PlatformBuildFileGenerator(getProject(), outfiles, fvs, isUnified, saq, platformBuildFile);
125 fileGenerator.genBuildFile();
126
127 //
128 // Prepare Queue
129 //
130 queueList.add("libqueue");
131
132 String[] validFv = saq.getFpdValidImageNames();
133
134 for (int i = 0; i < validFv.length; i++) {
135 queueList.add(validFv[i]);
136 }
137
138 Iterator<String> fvsNameIter = fvs.keySet().iterator();
139
140 while (fvsNameIter.hasNext()) {
141 String fvName = fvsNameIter.next();
142 if (!isContain(validFv, fvName)) {
143 queueList.add(fvName);
144 }
145 }
146
147 //
148 // Ant call ${PLATFORM}_build.xml
149 //
150 Ant ant = new Ant();
151 ant.setProject(getProject());
152 ant.setAntfile(platformBuildFile);
153 ant.setTarget("prebuild");
154 ant.setInheritAll(true);
155 ant.init();
156 ant.execute();
157
158 remainNumber = totalNumber = allThreads.size();
159
160 EdkLog.log(this, EdkLog.EDK_ALWAYS, "Total thread number is " + totalNumber);
161 GenBuildLogger.setCacheEnable(true);
162 //
163 // Waiting for all thread over, or time out
164 //
165 synchronized (deamonSemaphore) {
166
167 while (true) {
168 //
169 // If all modules are already built
170 //
171 if (currentQueueCode >= queueList.size()) {
172 break ;
173 }
174
175 int percentage = (totalNumber - remainNumber) * 100 / totalNumber;
176 EdkLog.log(this, EdkLog.EDK_ALWAYS, percentage + "% finished. Has built " + (totalNumber - remainNumber) + " modules of " + totalNumber + " total. ");
177
178 Set<FpdModuleIdentification> currentQueueModules = fvs.get(queueList.get(currentQueueCode));
179
180 if (currentQueueModules == null) {
181 ++currentQueueCode;
182 continue ;
183 }
184 Iterator<FpdModuleIdentification> currentIter = currentQueueModules.iterator();
185
186 GenBuildThread a = null;
187
188 boolean existNoneReady = false;
189
190 while (currentIter.hasNext()) {
191 GenBuildThread item = allThreads.get(currentIter.next());
192 if (item.getStatus() == STATUS_DEPENDENCY_NOT_READY) {
193 existNoneReady = true;
194 } else if (item.getStatus() == STATUS_DEPENDENCY_READY) {
195 a = item;
196 addCount();
197 a.start();
198 if (currentRunNumber == FrameworkBuildTask.MAX_CONCURRENT_THREAD_NUMBER) {
199 break ;
200 }
201 }
202 }
203
204 if (a != null) {
205 //
206 // Exist ready thread
207 //
208 // EdkLog.log(this, EdkLog.EDK_ALWAYS, "Exist ready thread");
209
210 } else if (existNoneReady && currentRunNumber == 0) {
211 //
212 // No active thread, but still have dependency not read thread
213 //
214 throw new BuildException("Existing some modules can't resolve depedencies. ");
215 } else if (!existNoneReady && currentRunNumber == 0) {
216 //
217 // Current queue build finish, move to next
218 //
219 EdkLog.log(this, EdkLog.EDK_ALWAYS, "Current queue build finish, move to next");
220 ++currentQueueCode;
221 continue ;
222 } else {
223 //
224 // active thread exist, but no ready thread
225 //
226 EdkLog.log(this, EdkLog.EDK_ALWAYS, "Active thread exist, but no ready thread. Current running number is " + currentRunNumber);
227 }
228
229 try {
230 deamonSemaphore.wait();
231
232 //
233 // if find error. Let other threads to finish
234 //
235 if (errorModule != null) {
236 while (currentRunNumber > 0) {
237 deamonSemaphore.wait();
238 }
239
240 GenBuildLogger.setCacheEnable(false);
241
242 GenBuildLogger.flushErrorModuleLog(errorModule);
243
244 EdkLog.flushLogToFile(new File(buildDir + File.separatorChar + "build.log"));
245
246 throw new BuildException(errorModule + " build error. ");
247 }
248 } catch (InterruptedException ex) {
249 BuildException e = new BuildException("Thread wait Error. \n" + ex.getMessage());
250 e.setStackTrace(ex.getStackTrace());
251 throw e;
252 }
253 }
254 }
255
256 GenBuildLogger.setCacheEnable(false);
257 //
258 // call fvs, postbuild
259 //
260 ant = new Ant();
261 ant.setProject(getProject());
262 ant.setAntfile(platformBuildFile);
263 ant.setTarget("fvs");
264 ant.setInheritAll(true);
265 ant.init();
266 ant.execute();
267
268 ant = new Ant();
269 ant.setProject(getProject());
270 ant.setAntfile(platformBuildFile);
271 ant.setTarget("postbuild");
272 ant.setInheritAll(true);
273 ant.init();
274 ant.execute();
275
276 EdkLog.flushLogToFile(new File(buildDir + File.separatorChar + "build.log"));
277 }
278
279
280 /**
281 Parse all modules listed in FPD file.
282 **/
283 void parseModuleSAFiles() throws EdkException{
284
285 Map<FpdModuleIdentification, Map<String, XmlObject>> moduleSAs = saq.getFpdModules();
286
287 //
288 // For every Module lists in FPD file.
289 //
290 Set<FpdModuleIdentification> keys = moduleSAs.keySet();
291 Iterator<FpdModuleIdentification> iter = keys.iterator();
292 while (iter.hasNext()) {
293 FpdModuleIdentification fpdModuleId = iter.next();
294
295 //
296 // Generate GenBuildThread
297 //
298 GenBuildThread genBuildThread = new GenBuildThread(fpdModuleId.getModule(), fpdModuleId.getArch());
299 genBuildThread.setParentModuleId(null);
300 genBuildThread.setProject(getProject());
301
302 Set<FpdModuleIdentification> dependencies = new LinkedHashSet<FpdModuleIdentification>();
303
304 GlobalData.registerFpdModuleSA(fpdModuleId, moduleSAs.get(fpdModuleId));
305
306 //
307 // Add all dependent Library Instance
308 //
309 saq.push(GlobalData.getDoc(fpdModuleId));
310
311 ModuleIdentification[] libinstances = saq.getLibraryInstance(fpdModuleId.getArch());
312 saq.pop();
313
314 for (int i = 0; i < libinstances.length; i++) {
315 FpdModuleIdentification libFpdModuleId = new FpdModuleIdentification(libinstances[i], fpdModuleId.getArch());
316 //
317 // Add to dependencies
318 //
319 dependencies.add(libFpdModuleId);
320
321 //
322 // Create thread for library instances
323 //
324 GenBuildThread liBuildThread = new GenBuildThread(libinstances[i], fpdModuleId.getArch());
325 liBuildThread.setParentModuleId(fpdModuleId.getModule());
326 liBuildThread.setProject(getProject());
327 liBuildThread.setStatus(STATUS_DEPENDENCY_READY);
328 liBuildThread.setHighPriority(true);
329 allThreads.put(libFpdModuleId, liBuildThread);
330
331 updateFvs("libqueue", libFpdModuleId);
332 }
333
334 genBuildThread.setDependencies(dependencies);
335
336 // if (dependencies.size() == 0) {
337 genBuildThread.setStatus(STATUS_DEPENDENCY_READY);
338 // }
339
340 allThreads.put(fpdModuleId, genBuildThread);
341
342 //
343 // Put fpdModuleId to the corresponding FV
344 //
345 saq.push(GlobalData.getDoc(fpdModuleId));
346 String fvBinding = saq.getModuleFvBindingKeyword();
347
348 fpdModuleId.setFvBinding(fvBinding);
349 updateFvs(fvBinding, fpdModuleId);
350
351 //
352 // Prepare for out put file name
353 //
354 ModuleIdentification moduleId = fpdModuleId.getModule();
355
356 String baseName = saq.getModuleOutputFileBasename();
357
358 if (baseName == null) {
359 baseName = moduleId.getName();
360 }
361 outfiles.put(fpdModuleId, fpdModuleId.getArch() + File.separatorChar
362 + moduleId.getGuid() + "-" + baseName
363 + getSuffix(moduleId.getModuleType()));
364
365 //
366 // parse module build options, if any
367 //
368 GlobalData.addModuleToolChainOption(fpdModuleId, parseModuleBuildOptions(false));
369 GlobalData.addModuleToolChainFamilyOption(fpdModuleId, parseModuleBuildOptions(true));
370 saq.pop();
371 }
372 }
373
374 private boolean isContain(String[] list, String item) {
375 for (int i = 0; i < list.length; i++) {
376 if (list[i].equalsIgnoreCase(item)) {
377 return true;
378 }
379 }
380 return false;
381 }
382
383 public synchronized static void addCount() {
384 synchronized (countSemaphore) {
385 ++currentRunNumber;
386 }
387 }
388
389 public synchronized static void subCount() {
390 synchronized (countSemaphore) {
391 --currentRunNumber;
392 --remainNumber;
393 }
394 }
395 }