]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/global/GlobalData.java
Added support for macro/property in tools_def.txt. Now you can define a property...
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / global / GlobalData.java
1 /** @file
2 GlobalData class.
3
4 GlobalData provide initializing, instoring, querying and update global data.
5 It is a bridge to intercommunicate between multiple component, such as AutoGen,
6 PCD and so on.
7
8 Copyright (c) 2006, Intel Corporation
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 **/
17 package org.tianocore.build.global;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.tools.ant.Project;
32 import org.apache.xmlbeans.XmlException;
33 import org.apache.xmlbeans.XmlObject;
34 import org.apache.xmlbeans.XmlOptions;
35 import org.tianocore.DbPathAndFilename;
36 import org.tianocore.FrameworkDatabaseDocument;
37 import org.tianocore.ModuleSurfaceAreaDocument;
38 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
39 import org.tianocore.build.id.FpdModuleIdentification;
40 import org.tianocore.build.id.ModuleIdentification;
41 import org.tianocore.build.id.PackageIdentification;
42 import org.tianocore.build.id.PlatformIdentification;
43 import org.tianocore.build.toolchain.ToolChainConfig;
44 import org.tianocore.build.toolchain.ToolChainElement;
45 import org.tianocore.build.toolchain.ToolChainInfo;
46 import org.tianocore.build.toolchain.ToolChainKey;
47 import org.tianocore.build.toolchain.ToolChainMap;
48 import org.tianocore.common.definitions.ToolDefinitions;
49 import org.tianocore.common.exception.EdkException;
50 import org.tianocore.common.logger.EdkLog;
51 import org.tianocore.pcd.entity.MemoryDatabaseManager;
52
53 /**
54 GlobalData provide initializing, instoring, querying and update global data.
55 It is a bridge to intercommunicate between multiple component, such as AutoGen,
56 PCD and so on.
57
58 <p>Note that all global information are initialized incrementally. All data will
59 parse and record only of necessary during build time. </p>
60
61 @since GenBuild 1.0
62 **/
63 public class GlobalData {
64 ///
65 /// Record current WORKSPACE Directory
66 ///
67 private static String workspaceDir = "";
68
69 ///
70 /// Be used to ensure Global data will be initialized only once.
71 ///
72 private static boolean globalFlag = false;
73
74 ///
75 /// Framework Database information: package list and platform list
76 ///
77 private static Set<PackageIdentification> packageList = new HashSet<PackageIdentification>();
78
79 private static Set<PlatformIdentification> platformList = new HashSet<PlatformIdentification>();
80
81 ///
82 /// Every detail SPD informations: Module list, Library class definition,
83 /// Package header file, GUID/PPI/Protocol definitions
84 ///
85 private static final Map<PackageIdentification, Spd> spdTable = new HashMap<PackageIdentification, Spd>();
86
87 ///
88 /// Build informations are divided into three parts:
89 /// 1. From MSA 2. From FPD 3. From FPD' ModuleSA
90 ///
91 private static Map<ModuleIdentification, Map<String, XmlObject>> nativeMsa = new HashMap<ModuleIdentification, Map<String, XmlObject>>();
92
93 private static Map<FpdModuleIdentification, Map<String, XmlObject>> fpdModuleSA= new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();
94
95 private static Map<String, XmlObject> fpdBuildOptionsMap = new HashMap<String, XmlObject>();
96
97 private static XmlObject fpdBuildOptions;
98
99 private static XmlObject fpdDynamicPcds;
100
101 ///
102 /// Parsed modules list
103 ///
104 private static Map<FpdModuleIdentification, Map<String, XmlObject>> parsedModules = new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();
105
106 ///
107 /// built modules list with ARCH, TARGET, TOOLCHAIN
108 ///
109 private static Set<FpdModuleIdentification> builtModules = new HashSet<FpdModuleIdentification>();
110
111 ///
112 /// PCD memory database stored all PCD information which collected from FPD,MSA and SPD.
113 ///
114 private static final MemoryDatabaseManager pcdDbManager = new MemoryDatabaseManager();
115
116 ///
117 /// build target + tool chain family/tag name + arch + command types + command options
118 ///
119 ///
120 /// Tool Chain Data
121 /// toolsDef - build tool program information
122 /// fpdBuildOption - all modules's build options for tool tag or tool chain families
123 /// moduleSaBuildOption - build options for a specific module
124 ///
125 private static ToolChainConfig toolsDef;
126
127 private static ToolChainInfo toolChainInfo;
128 private static ToolChainInfo toolChainEnvInfo;
129 private static ToolChainInfo toolChainPlatformInfo;
130
131 private static ToolChainMap platformToolChainOption;
132 private static ToolChainMap platformToolChainFamilyOption;
133
134 private static Map<FpdModuleIdentification, ToolChainMap> moduleToolChainOption = new HashMap<FpdModuleIdentification, ToolChainMap>();
135 private static Map<FpdModuleIdentification, ToolChainMap> moduleToolChainFamilyOption = new HashMap<FpdModuleIdentification, ToolChainMap>();
136
137 private static Map<ModuleIdentification, ToolChainMap> msaBuildOption = new HashMap<ModuleIdentification, ToolChainMap>();
138 private static Map<ModuleIdentification, ToolChainMap> msaFamilyBuildOption = new HashMap<ModuleIdentification, ToolChainMap>();
139
140 /**
141 Parse framework database (DB) and all SPD files listed in DB to initialize
142 the environment for next build. This method will only be executed only once
143 in the whole build process.
144
145 @param workspaceDatabaseFile the file name of framework database
146 @param workspaceDir current workspace directory path
147 @throws BuildException
148 Framework Dababase or SPD or MSA file is not valid
149 **/
150 public synchronized static void initInfo(Project prj, String workspaceDatabaseFile, String workspaceDir, String toolsDefFilename ) throws EdkException {
151 //
152 // ensure this method will be revoked only once
153 //
154 if (globalFlag) {
155 return;
156 }
157 globalFlag = true;
158
159 //
160 // Backup workspace directory. It will be used by other method
161 //
162 GlobalData.workspaceDir = workspaceDir.replaceAll("(\\\\)", "/");
163
164 //
165 // Parse tools definition file
166 //
167 //
168 // If ToolChain has been set up before, do nothing.
169 // CONF dir + tools definition file name
170 //
171 File toolsDefFile = new File(workspaceDir + File.separatorChar + toolsDefFilename);
172 EdkLog.log("Init", EdkLog.EDK_ALWAYS, "Using tool definition file [" + toolsDefFile.getPath() + "].");
173 toolsDef = new ToolChainConfig(prj, toolsDefFile);
174
175 //
176 // Parse Framework Database
177 //
178 File dbFile = new File(workspaceDir + File.separatorChar + workspaceDatabaseFile);
179 FrameworkDatabaseDocument db = null;
180 try {
181 db = (FrameworkDatabaseDocument)parseXmlFile(dbFile);
182 //
183 // Get package list
184 //
185 if (db.getFrameworkDatabase().getPackageList() != null ) {
186 List<DbPathAndFilename> packages = db.getFrameworkDatabase().getPackageList().getFilenameList();
187 Iterator<DbPathAndFilename> iter = packages.iterator();
188 while (iter.hasNext()) {
189 String fileName = iter.next().getStringValue().trim();
190 Spd spd = new Spd(new File(workspaceDir + File.separatorChar + fileName));
191 packageList.add(spd.getPackageId());
192 //
193 // Report warning if existing two packages with same GUID and Version
194 //
195 if (spdTable.containsKey(spd.getPackageId())) {
196 //
197 // BUGBUG
198 //
199 EdkLog.log("Init", EdkLog.EDK_WARNING, "Warning: Existing two packages with same GUID and Version. They are ... " + spd.getPackageId().getSpdFile().getPath());
200 }
201 spdTable.put(spd.getPackageId(), spd);
202 }
203 }
204 } catch(IOException ex) {
205 EdkException edkException = new EdkException("Parse of WORKSPACE Database file [" + dbFile.getPath() + "] failed!\n" + ex.getMessage());
206 edkException.setStackTrace(ex.getStackTrace());
207 throw edkException;
208 } catch(XmlException ex) {
209 EdkException edkException = new EdkException("Parse of WORKSPACE Database file [" + dbFile.getPath() + "] failed!\n" + ex.getMessage());
210 edkException.setStackTrace(ex.getStackTrace());
211 throw edkException;
212 }
213
214 File fpdFile = null;
215 try {
216 //
217 // Get platform list
218 //
219 if (db.getFrameworkDatabase().getPlatformList() != null) {
220 List<DbPathAndFilename> platforms = db.getFrameworkDatabase().getPlatformList().getFilenameList();
221 Iterator<DbPathAndFilename> iter = platforms.iterator();
222 while (iter.hasNext()) {
223 String fileName = iter.next().getStringValue().trim();
224 fpdFile = new File(workspaceDir + File.separatorChar + fileName);
225 if ( !fpdFile.exists() ) {
226 throw new EdkException("Platform file [" + fpdFile.getPath() + "] not exists. ");
227 }
228 XmlObject fpdDoc = parseXmlFile(fpdFile);
229 //
230 // We can change Map to XmlObject
231 //
232 Map<String, XmlObject> fpdDocMap = new HashMap<String, XmlObject>();
233 fpdDocMap.put("PlatformSurfaceArea", fpdDoc);
234 SurfaceAreaQuery saq = new SurfaceAreaQuery(fpdDocMap);
235 PlatformIdentification platformId = saq.getFpdHeader();
236 platformId.setFpdFile(fpdFile);
237 //
238 // Report warning if existing two platfrom with same GUID and Version
239 //
240 if (platformList.contains(platformId)) {
241 //
242 // BUGBUG
243 //
244 EdkLog.log("Init", EdkLog.EDK_WARNING, "Warning: Existing two platforms with same GUID and Version. They are ... " + fpdFile.getPath());
245 }
246 platformList.add(platformId);
247 }
248 }
249 } catch(IOException ex) {
250 EdkException edkException = new EdkException("Parse of platform definition file [" + fpdFile.getPath() + "] failed!\n" + ex.getMessage());
251 edkException.setStackTrace(ex.getStackTrace());
252 throw edkException;
253 } catch(XmlException ex) {
254 EdkException edkException = new EdkException("Parse of platform definition file [" + fpdFile.getPath() + "] failed!\n" + ex.getMessage());
255 edkException.setStackTrace(ex.getStackTrace());
256 throw edkException;
257 }
258 }
259
260 /**
261 Get the current WORKSPACE Directory.
262
263 @return current workspace directory
264 **/
265 public synchronized static String getWorkspacePath() {
266 return workspaceDir;
267 }
268
269
270 /**
271 Get the MSA file name with absolute path
272 */
273 public synchronized static File getMsaFile(ModuleIdentification moduleId) throws EdkException {
274 File msaFile = null;
275 //
276 // TBD. Do only when package is null.
277 //
278 Iterator iter = packageList.iterator();
279 while (iter.hasNext()) {
280 PackageIdentification packageId = (PackageIdentification)iter.next();
281 Spd spd = spdTable.get(packageId);
282 msaFile = spd.getModuleFile(moduleId);
283 if (msaFile != null ) {
284 break ;
285 }
286 }
287 if (msaFile == null){
288 throw new EdkException("Can't find Module [" + moduleId.getName() + "] in any SPD package!");
289 } else {
290 return msaFile;
291 }
292 }
293
294 public synchronized static PackageIdentification getPackageForModule(ModuleIdentification moduleId) throws EdkException {
295 //
296 // If package already defined in module
297 //
298 if (moduleId.getPackage() != null) {
299 return moduleId.getPackage();
300 }
301
302 PackageIdentification packageId = null;
303 Iterator iter = packageList.iterator();
304 while (iter.hasNext()) {
305 packageId = (PackageIdentification)iter.next();
306 moduleId.setPackage(packageId);
307 Spd spd = spdTable.get(packageId);
308 File tempMsaFile = null;
309 if ((tempMsaFile = spd.getModuleFile(moduleId)) != null ) {
310 if (tempMsaFile.getParent().equalsIgnoreCase(moduleId.getMsaFile().getParent())) {
311 break ;
312 }
313 tempMsaFile = null;
314 }
315 }
316 if (packageId == null){
317 throw new EdkException("Can't find Module [" + moduleId.getName() + "] in any SPD package!");
318 } else {
319 return packageId;
320 }
321 }
322
323 /**
324 Difference between build and parse: ToolChain and Target
325 **/
326 public synchronized static boolean isModuleBuilt(FpdModuleIdentification moduleId) {
327 return builtModules.contains(moduleId);
328 }
329
330 public synchronized static void registerBuiltModule(FpdModuleIdentification fpdModuleId) {
331 builtModules.add(fpdModuleId);
332 }
333
334
335 public synchronized static void registerFpdModuleSA(FpdModuleIdentification fpdModuleId, Map<String, XmlObject> doc) throws EdkException{
336 Map<String, XmlObject> result = new HashMap<String, XmlObject>();
337 Set keySet = doc.keySet();
338 Iterator iter = keySet.iterator();
339 while (iter.hasNext()){
340 String key = (String)iter.next();
341 XmlObject item = cloneXmlObject(doc.get(key), true);
342 result.put(key, item);
343 }
344 fpdModuleSA.put(fpdModuleId, result);
345 }
346
347 public synchronized static boolean hasFpdModuleSA(FpdModuleIdentification fpdModuleId) {
348 return fpdModuleSA.containsKey(fpdModuleId);
349 }
350
351 /**
352 Query module surface area information.
353
354 <p>Note that surface area parsing is incremental. That means the method will
355 only parse the MSA files if necessary. </p>
356
357 @param fpdModuleId Module ID with arch
358 @return ModuleSA info and MSA info for fpdModuleId
359 @throws BuildException Can't find MSA
360 **/
361 public synchronized static Map<String, XmlObject> getDoc(FpdModuleIdentification fpdModuleId) throws EdkException{
362 if (parsedModules.containsKey(fpdModuleId)) {
363 return parsedModules.get(fpdModuleId);
364 }
365 Map<String, XmlObject> doc = new HashMap<String, XmlObject>();
366 ModuleIdentification moduleId = fpdModuleId.getModule();
367 //
368 // First part: get the MSA files info
369 //
370 doc.putAll(getNativeMsa(moduleId));
371
372 //
373 // Second part: put build options
374 //
375 doc.put("BuildOptions", fpdBuildOptions);
376
377 //
378 // Third part: get Module info from FPD, such as Library instances, PCDs
379 //
380 if (fpdModuleSA.containsKey(fpdModuleId)){
381 //
382 // merge module info in FPD to final Doc
383 // For Library Module, do nothing here
384 //
385 doc.putAll(fpdModuleSA.get(fpdModuleId));
386 }
387 parsedModules.put(fpdModuleId, doc);
388 return doc;
389 }
390
391 public synchronized static Map<String, XmlObject> getDoc(ModuleIdentification moduleId, String arch) throws EdkException{
392 FpdModuleIdentification fpdModuleId = new FpdModuleIdentification(moduleId, arch);
393 return getDoc(fpdModuleId);
394 }
395
396 /**
397 Query the native MSA information with module base name.
398
399 <p>Note that MSA parsing is incremental. That means the method will
400 only to parse the MSA files when never parsed before. </p>
401
402 @param moduleName the base name of the module
403 @return the native MSA information
404 @throws BuildException
405 MSA file is not valid
406 **/
407 public synchronized static Map<String, XmlObject> getNativeMsa(ModuleIdentification moduleId) throws EdkException {
408 if (nativeMsa.containsKey(moduleId)) {
409 return nativeMsa.get(moduleId);
410 }
411 File msaFile = getMsaFile(moduleId);
412 Map<String, XmlObject> msaMap = getNativeMsa(msaFile);
413 nativeMsa.put(moduleId, msaMap);
414 return msaMap;
415 }
416
417 public synchronized static Map<String, XmlObject> getNativeMsa(File msaFile) throws EdkException {
418 if (!msaFile.exists()) {
419 throw new EdkException("Module Surface Area file [" + msaFile.getPath() + "] can't be found!");
420 }
421 try {
422 ModuleSurfaceAreaDocument doc = (ModuleSurfaceAreaDocument)parseXmlFile(msaFile);
423 //
424 // parse MSA file
425 //
426 ModuleSurfaceArea msa= doc.getModuleSurfaceArea();
427 Map<String, XmlObject> msaMap = new HashMap<String, XmlObject>();
428 msaMap.put("MsaHeader", cloneXmlObject(msa.getMsaHeader(), true));
429 msaMap.put("ModuleDefinitions", cloneXmlObject(msa.getModuleDefinitions(), true));
430 msaMap.put("LibraryClassDefinitions", cloneXmlObject(msa.getLibraryClassDefinitions(), true));
431 msaMap.put("SourceFiles", cloneXmlObject(msa.getSourceFiles(), true));
432 msaMap.put("PackageDependencies", cloneXmlObject(msa.getPackageDependencies(), true));
433 msaMap.put("Protocols", cloneXmlObject(msa.getProtocols(), true));
434 msaMap.put("PPIs", cloneXmlObject(msa.getPPIs(), true));
435 msaMap.put("Guids", cloneXmlObject(msa.getGuids(), true));
436 msaMap.put("Externs", cloneXmlObject(msa.getExterns(), true));
437 msaMap.put("PcdCoded", cloneXmlObject(msa.getPcdCoded(), true));
438 msaMap.put("ModuleBuildOptions", cloneXmlObject(msa.getModuleBuildOptions(), true));
439 return msaMap;
440 } catch(IOException ex) {
441 EdkException edkException = new EdkException("Parse of MSA file [" + msaFile.getPath() + "] failed!\n" + ex.getMessage());
442 edkException.setStackTrace(ex.getStackTrace());
443 throw edkException;
444 } catch(XmlException ex) {
445 EdkException edkException = new EdkException("Parse of MSA file [" + msaFile.getPath() + "] failed!\n" + ex.getMessage());
446 edkException.setStackTrace(ex.getStackTrace());
447 throw edkException;
448 }
449 }
450
451 public static Map<String, XmlObject> getFpdBuildOptionsMap() {
452 return fpdBuildOptionsMap;
453 }
454
455 public static void setFpdBuildOptions(XmlObject fpdBuildOptions) throws EdkException {
456 GlobalData.fpdBuildOptions = cloneXmlObject(fpdBuildOptions, true);
457 fpdBuildOptionsMap.put("BuildOptions", GlobalData.fpdBuildOptions);
458 }
459
460 public static XmlObject getFpdDynamicPcds() {
461 return fpdDynamicPcds;
462 }
463
464 public static void setFpdDynamicPcds(XmlObject fpdDynamicPcds) {
465 GlobalData.fpdDynamicPcds = fpdDynamicPcds;
466 }
467
468 public static Set<ModuleIdentification> getModules(PackageIdentification packageId){
469 Spd spd = spdTable.get(packageId);
470 if (spd == null ) {
471 Set<ModuleIdentification> dummy = new HashSet<ModuleIdentification>();
472 return dummy;
473 } else {
474 return spd.getModules();
475 }
476 }
477
478 /**
479 * The header file path is relative to workspace dir
480 */
481 public static String[] getLibraryClassHeaderFiles(
482 PackageIdentification[] packages, String name) throws EdkException{
483 if (packages == null) {
484 // throw Exception or not????
485 return new String[0];
486 }
487 String[] result = null;
488 for (int i = 0; i < packages.length; i++) {
489 Spd spd = spdTable.get(packages[i]);
490 //
491 // If find one package defined the library class
492 //
493 if ((result = spd.getLibClassIncluder(name)) != null) {
494 return result;
495 }
496 }
497 //
498 // If can't find library class declaration in every package
499 //
500 throw new EdkException("Can not find library class [" + name
501 + "] declaration in any SPD package!");
502 }
503
504 /**
505 * The header file path is relative to workspace dir
506 */
507 public static String getPackageHeaderFiles(PackageIdentification packages,
508 String moduleType) {
509 if (packages == null) {
510 return new String("");
511 }
512 Spd spd = spdTable.get(packages);
513 //
514 // If can't find package header file, skip it
515 //
516 String temp = null;
517 if (spd != null) {
518 if ((temp = spd.getPackageIncluder(moduleType)) != null) {
519 return temp;
520 } else {
521 temp = "";
522 return temp;
523 }
524 } else {
525 return null;
526 }
527 }
528
529 /**
530 * return two values: {cName, GuidValue}
531 */
532 public static String[] getGuid(List<PackageIdentification> packages, String name) {
533 if (packages == null) {
534 // throw Exception or not????
535 return new String[0];
536 }
537 String[] result = null;
538 Iterator item = packages.iterator();
539 while (item.hasNext()){
540 Spd spd = spdTable.get(item.next());
541 //
542 // If find one package defined the GUID
543 //
544 if ((result = spd.getGuid(name)) != null) {
545 return result;
546 }
547 }
548
549 return null;
550 }
551
552 /**
553 * return two values: {cName, GuidValue}
554 */
555 public static String[] getPpiGuid(List<PackageIdentification> packages,
556 String name) {
557 if (packages == null) {
558 return new String[0];
559 }
560 String[] result = null;
561 Iterator item = packages.iterator();
562 while (item.hasNext()){
563 Spd spd = spdTable.get(item.next());
564 //
565 // If find one package defined the Ppi GUID
566 //
567 if ((result = spd.getPpi(name)) != null) {
568 return result;
569 }
570 }
571 return null;
572 }
573
574 /**
575 * return two values: {cName, GuidValue}
576 */
577 public static String[] getProtocolGuid(List<PackageIdentification> packages,
578 String name) {
579 if (packages == null) {
580 return new String[0];
581 }
582 String[] result = null;
583 Iterator item = packages.iterator();
584 while (item.hasNext()){
585 Spd spd = spdTable.get(item.next());
586 //
587 // If find one package defined the protocol GUID
588 //
589 if ((result = spd.getProtocol(name))!= null){
590 return result;
591 }
592 }
593 return null;
594
595 }
596
597 public synchronized static PlatformIdentification getPlatformByName(String name) throws EdkException {
598 Iterator iter = platformList.iterator();
599 while(iter.hasNext()){
600 PlatformIdentification platformId = (PlatformIdentification)iter.next();
601 if (platformId.getName().equalsIgnoreCase(name)) {
602 return platformId;
603 }
604 }
605 throw new EdkException("Can't find platform [" + name + "] in the current WORKSPACE database!");
606 }
607
608 public synchronized static PlatformIdentification getPlatform(String filename) throws EdkException {
609 File file = new File(workspaceDir + File.separatorChar + filename);
610 Iterator iter = platformList.iterator();
611 while(iter.hasNext()){
612 PlatformIdentification platformId = (PlatformIdentification)iter.next();
613 if (platformId.getFpdFile().getPath().equalsIgnoreCase(file.getPath())) {
614 return platformId;
615 }
616 }
617 throw new EdkException("Can't find platform file [" + filename + "] in the current WORKSPACE database!");
618 }
619
620 public synchronized static PackageIdentification refreshPackageIdentification(PackageIdentification packageId) throws EdkException {
621 Iterator iter = packageList.iterator();
622 while(iter.hasNext()){
623 PackageIdentification packageItem = (PackageIdentification)iter.next();
624 if (packageItem.equals(packageId)) {
625 packageId.setName(packageItem.getName());
626 packageId.setSpdFile(packageItem.getSpdFile());
627 return packageId;
628 }
629 }
630 throw new EdkException("Can't find package GUID value " + packageId.toGuidString() + " in the current workspace!");
631 }
632
633 public synchronized static ModuleIdentification refreshModuleIdentification(ModuleIdentification moduleId) throws EdkException {
634 PackageIdentification packageId = getPackageForModule(moduleId);
635 moduleId.setPackage(packageId);
636 Spd spd = spdTable.get(packageId);
637 if (spd == null) {
638 throw new EdkException("Can't find package GUID value " + packageId.toGuidString() + " in the current workspace!");
639 }
640 Set<ModuleIdentification> modules = spd.getModules();
641 Iterator<ModuleIdentification> iter = modules.iterator();
642 while (iter.hasNext()) {
643 ModuleIdentification item = iter.next();
644 if (item.equals(moduleId)) {
645 moduleId.setName(item.getName());
646 moduleId.setModuleType(item.getModuleType());
647 moduleId.setMsaFile(item.getMsaFile());
648 return moduleId;
649 }
650 }
651 throw new EdkException("Can't find module GUID value " + moduleId.toGuidString() + " in " + packageId + " under the current workspace!");
652 }
653
654 public synchronized static Set<PackageIdentification> getPackageList(){
655 return packageList;
656 }
657
658 /**
659 BUGBUG: It is a walk around method. If do not clone, can't query info with
660 XPath correctly.
661
662 @param object XmlObject
663 @param deep flag for deep clone
664 @return XmlObject after clone
665 @throws BuildException parse original XmlObject error.
666 **/
667 private static XmlObject cloneXmlObject(XmlObject object, boolean deep) throws EdkException {
668 if ( object == null) {
669 return null;
670 }
671 XmlObject result = null;
672 try {
673 result = XmlObject.Factory.parse(object.getDomNode()
674 .cloneNode(deep));
675 } catch (XmlException ex) {
676 EdkException edkException = new EdkException(ex.getMessage());
677 edkException.setStackTrace(ex.getStackTrace());
678 throw edkException;
679 }
680 return result;
681 }
682
683 ///
684 /// Tool Chain Related, try to refine and put some logic process to ToolChainFactory
685 ///
686 public synchronized static ToolChainInfo getToolChainInfo() {
687 if (toolChainInfo == null) {
688 toolChainInfo = toolsDef.getConfigInfo().intersection(toolChainEnvInfo);
689 if (toolChainPlatformInfo != null) {
690 toolChainInfo = toolChainInfo.intersection(toolChainPlatformInfo);
691 }
692 toolChainInfo.addCommands(toolsDef.getConfigInfo().getCommands());
693 toolChainInfo.normalize();
694
695 EdkLog.log("Init", EdkLog.EDK_ALWAYS, "Current build tool chain information summary: ");
696 EdkLog.log("Init", EdkLog.EDK_ALWAYS, toolChainInfo + "");
697 }
698 return toolChainInfo;
699 }
700
701 public static void setPlatformToolChainFamilyOption(ToolChainMap map) {
702 platformToolChainFamilyOption = map;
703 }
704
705 public static void setPlatformToolChainOption(ToolChainMap map) {
706 platformToolChainOption = map;
707 }
708
709 public static void addModuleToolChainOption(FpdModuleIdentification fpdModuleId,
710 ToolChainMap toolChainOption) {
711 moduleToolChainOption.put(fpdModuleId, toolChainOption);
712 }
713
714 public static void addModuleToolChainFamilyOption(FpdModuleIdentification fpdModuleId,
715 ToolChainMap toolChainOption) {
716 moduleToolChainFamilyOption.put(fpdModuleId, toolChainOption);
717 }
718
719 public static void addMsaBuildOption(ModuleIdentification moduleId,
720 ToolChainMap toolChainOption) {
721 msaBuildOption.put(moduleId, toolChainOption);
722 }
723
724 public static void addMsaFamilyBuildOption(ModuleIdentification moduleId,
725 ToolChainMap toolChainOption) {
726 msaFamilyBuildOption.put(moduleId, toolChainOption);
727 }
728
729 public static boolean isCommandSet(String target, String toolchain, String arch) throws EdkException {
730 String[] commands = getToolChainInfo().getCommands();
731
732 for (int i = 0; i < commands.length; ++i) {
733 String cmdName = toolsDef.getConfig().get(new String[] {target, toolchain, arch, commands[i], ToolDefinitions.TOOLS_DEF_ATTRIBUTE_NAME});
734 if (cmdName != null && cmdName.length() != 0) {
735 return true;
736 }
737 }
738
739 return false;
740 }
741
742 /**
743 Except FLAGS, all attribute are from TOOLS_DEF file.
744
745 For FLAGS, information from four places, they are:
746 <pre>
747 1. tools_def.txt
748 2. MSA &lt;BuildOptions&gt;/&lt;Options&gt;
749 3. FPD &lt;BuildOptions&gt;/&lt;Options&gt;
750 4. FPD &lt;FrameworkModules&gt;/&lt;ModuleSaBuildOptions&gt;/&lt;Options&gt;
751 </pre>
752
753 @param commandDescription Key: TARGET, TAGNAME, ARCH, COMMANDTYPE, ATTRIBUTE
754 @param fpdModuleId Module Identification with Arch
755 @return The corresponding String
756 @throws EdkException If build option definition error
757 **/
758 public synchronized static String getCommandSetting(String[] commandDescription, FpdModuleIdentification fpdModuleId) throws EdkException {
759 ToolChainKey toolChainKey = new ToolChainKey(commandDescription);
760 ToolChainMap toolChainConfig = toolsDef.getConfig();
761 String setting = null;
762
763 //
764 // Default in tools_def.txt
765 //
766 setting = toolChainConfig.get(toolChainKey);
767 if (setting == null) {
768 setting = "";
769 }
770 if (!commandDescription[ToolChainElement.ATTRIBUTE.value].equals(ToolDefinitions.TOOLS_DEF_ATTRIBUTE_FLAGS)) {
771 return setting;
772 }
773
774 Set<String> flagSet = new LinkedHashSet<String>();
775 flagSet.add(setting);
776
777 //
778 // Tool's option can be in .fpd and/or .msa file
779 //
780 String optionString;
781 ToolChainMap option = null;
782 ToolChainKey toolChainFamilyKey = new ToolChainKey(commandDescription);
783
784 toolChainFamilyKey.setKey(ToolDefinitions.TOOLS_DEF_ATTRIBUTE_FAMILY, ToolChainElement.ATTRIBUTE.value);
785 String family = toolChainConfig.get(toolChainFamilyKey);
786 toolChainFamilyKey.setKey(family, ToolChainElement.TOOLCHAIN.value);
787 toolChainFamilyKey.setKey(ToolDefinitions.TOOLS_DEF_ATTRIBUTE_FLAGS, ToolChainElement.ATTRIBUTE.value);
788
789 //
790 // MSA's tool chain family option
791 //
792 option = msaFamilyBuildOption.get(fpdModuleId.getModule());
793 if (option != null && (optionString = option.get(toolChainFamilyKey)) != null) {
794 flagSet.add(optionString);
795 }
796
797 //
798 // MSA's tool chain option
799 //
800 option = msaBuildOption.get(fpdModuleId.getModule());
801 if (option != null && (optionString = option.get(toolChainKey)) != null) {
802 flagSet.add(optionString);
803 }
804
805 //
806 // Platform's tool chain family option
807 //
808 optionString = platformToolChainFamilyOption.get(toolChainFamilyKey);
809 if (optionString != null) {
810 flagSet.add(optionString);
811 }
812
813 //
814 // Platform's tool chain tag option
815 //
816 optionString = platformToolChainOption.get(toolChainKey);
817 if (optionString != null) {
818 flagSet.add(optionString);
819 }
820
821 //
822 // Module's tool chain family option
823 //
824 option = moduleToolChainFamilyOption.get(fpdModuleId);
825 if (option != null && (optionString = option.get(toolChainFamilyKey)) != null) {
826 flagSet.add(optionString);
827 }
828
829 //
830 // Module's tool chain tag option
831 //
832 option = moduleToolChainOption.get(fpdModuleId);
833 if (option != null && (optionString = option.get(toolChainKey)) != null) {
834 flagSet.add(optionString);
835 }
836
837 setting = "";
838 for(Iterator<String> iter = flagSet.iterator(); iter.hasNext();) {
839 setting += iter.next() +" ";
840 }
841 return setting;
842 }
843
844 public static void setToolChainEnvInfo(ToolChainInfo envInfo) {
845 toolChainEnvInfo = envInfo;
846 }
847 public static void setToolChainPlatformInfo(ToolChainInfo platformInfo) {
848 toolChainPlatformInfo = platformInfo;
849 }
850
851 //
852 // for PCD
853 //
854 public synchronized static MemoryDatabaseManager getPCDMemoryDBManager() {
855 return pcdDbManager;
856 }
857
858 //
859 // For PCD get tokenSpaceGUid
860 //
861 public synchronized static String getGuidInfoFromCname(String cName){
862 String cNameGuid = null;
863 String guid = null;
864 Set set = spdTable.keySet();
865 Iterator iter = set.iterator();
866
867 if (iter == null) {
868 return null;
869 }
870
871 while (iter.hasNext()){
872 Spd spd = (Spd) spdTable.get(iter.next());
873 guid = spd.getGuidFromCname(cName);
874 if (guid != null){
875 cNameGuid = guid;
876 break;
877 }
878 }
879 return cNameGuid;
880 }
881
882 //
883 // For PCD
884 //
885 public synchronized static Map<FpdModuleIdentification, XmlObject>
886 getFpdModuleSaXmlObject(String xmlObjectName) {
887 Set<FpdModuleIdentification> fpdModuleSASet = fpdModuleSA.keySet();
888 Iterator item = fpdModuleSASet.iterator();
889
890
891 Map<FpdModuleIdentification, XmlObject> SAPcdBuildDef = new HashMap<FpdModuleIdentification, XmlObject>();
892 Map<String, XmlObject> SANode = new HashMap<String, XmlObject>();
893 FpdModuleIdentification moduleId;
894 while (item.hasNext()) {
895
896 moduleId = (FpdModuleIdentification) item.next();
897 SANode = fpdModuleSA.get(moduleId);
898 try{
899 if (SANode.get(xmlObjectName)!= null){
900 SAPcdBuildDef.put(moduleId,
901 (XmlObject) SANode.get(xmlObjectName));
902
903 }
904 } catch (Exception e){
905 EdkLog.log(EdkLog.EDK_INFO, e.getMessage());
906 }
907 }
908 return SAPcdBuildDef;
909 }
910
911 public synchronized static Map<FpdModuleIdentification,XmlObject> getFpdPcdBuildDefinitions() {
912 Map<FpdModuleIdentification,XmlObject> pcdBuildDef = getFpdModuleSaXmlObject ("PcdBuildDefinition");
913
914 return pcdBuildDef;
915 }
916
917 public static XmlObject parseXmlFile(File xmlFile) throws IOException, XmlException {
918 Collection errors = new ArrayList();
919 XmlOptions opt = new XmlOptions();
920
921 opt.setLoadLineNumbers();
922 opt.setLoadMessageDigest();
923 opt.setErrorListener(errors);
924
925 XmlObject doc = XmlObject.Factory.parse(xmlFile, opt);
926 //
927 // Validate File if they accord with XML Schema
928 //
929 if (!doc.validate(opt)){
930 StringBuilder errorMessage = new StringBuilder(1024);
931 for (Iterator it = errors.iterator(); it.hasNext(); ) {
932 errorMessage.append(it.next());
933 errorMessage.append("\n");
934 }
935 throw new XmlException(errorMessage.toString());
936 }
937
938 return doc;
939 }
940 }
941