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