]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/platform/ui/global/GlobalData.java
1. adjust contents layout of SPD header editor, FPD header editor.
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / platform / ui / 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.frameworkwizard.platform.ui.global;
18
19 import org.apache.xmlbeans.XmlObject;
20 import org.tianocore.DbPathAndFilename;
21 import org.tianocore.FrameworkDatabaseDocument;
22 import org.tianocore.ModuleSurfaceAreaDocument;
23 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
24 import org.tianocore.frameworkwizard.platform.ui.id.FpdModuleIdentification;
25 import org.tianocore.frameworkwizard.platform.ui.id.ModuleIdentification;
26 import org.tianocore.frameworkwizard.platform.ui.id.PackageIdentification;
27 import org.tianocore.frameworkwizard.platform.ui.id.PlatformIdentification;
28
29 import java.io.File;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.logging.Logger;
38
39 /**
40 GlobalData provide initializing, instoring, querying and update global data.
41 It is a bridge to intercommunicate between multiple component, such as AutoGen,
42 PCD and so on.
43
44 <p>Note that all global information are initialized incrementally. All data will
45 parse and record only of necessary during build time. </p>
46
47 @since GenBuild 1.0
48 **/
49 public class GlobalData {
50
51
52 public static Logger log = Logger.getAnonymousLogger();
53 public static KeyComparator comparator = new KeyComparator();
54 ///
55 /// Record current WORKSPACE Directory
56 ///
57 private static String workspaceDir = "";
58
59 ///
60 /// Be used to ensure Global data will be initialized only once.
61 ///
62 // private static boolean globalFlag = false;
63
64 ///
65 /// Framework Database information: package list and platform list
66 ///
67 private static Set<PackageIdentification> packageList = new HashSet<PackageIdentification>();
68
69 private static Set<PlatformIdentification> platformList = new HashSet<PlatformIdentification>();
70
71 ///
72 /// Every detail SPD informations: Module list, Library class definition,
73 /// Package header file, GUID/PPI/Protocol definitions
74 ///
75 private static final Map<PackageIdentification, Spd> spdTable = new HashMap<PackageIdentification, Spd>();
76
77 ///
78 /// Build informations are divided into three parts:
79 /// 1. From MSA 2. From FPD 3. From FPD' ModuleSA
80 ///
81 private static Map<ModuleIdentification, Map<String, XmlObject>> nativeMsa = new HashMap<ModuleIdentification, Map<String, XmlObject>>();
82
83 private static Map<FpdModuleIdentification, Map<String, XmlObject>> fpdModuleSA= new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();
84
85 private static XmlObject fpdBuildOptions;
86
87 private static XmlObject fpdDynamicPcds;
88
89 ///
90 /// Parsed modules list
91 ///
92 private static Map<FpdModuleIdentification, Map<String, XmlObject>> parsedModules = new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();
93
94 ///
95 /// built modules list with ARCH, TARGET, TOOLCHAIN
96 ///
97 private static Set<FpdModuleIdentification> builtModules = new HashSet<FpdModuleIdentification>();
98
99 ///
100 /// PCD memory database stored all PCD information which collected from FPD,MSA and SPD.
101 ///
102 // private static final MemoryDatabaseManager pcdDbManager = new MemoryDatabaseManager();
103
104 ///
105 /// build target + tool chain family/tag name + arch + command types + command options
106 ///
107 private static Map<String, Object> toolChainOptions;
108 private static Map<String, Object> toolChainFamilyOptions;
109 private static Map<String, String> toolChainDefinitions;
110 ///
111 ///
112 ///
113 private static Set<String> targets;
114 ///
115 ///
116 ///
117 private static Set<String> toolChainFamilies;
118 ///
119 ///
120 ///
121 private static Set<String> toolChains;
122 ///
123 /// keep track which toolchain family a toolchain tag belongs to
124 ///
125 private static Map<String, Set<String>> toolChainFamilyMap;
126 private static Map<String, Set<String>> toolChainCommandMap;
127
128 ///
129 /// list of Arch: EBC, ARM, IA32, X64, IPF, PPC
130 ///
131 private static Set<String> archs;
132
133 ///
134 /// list of Command Type: CC, LIB, LINK, ASL, ASM, ASMLINK, PP
135 ///
136 private static Set<String> commandTypes;
137
138 /**
139 Parse framework database (DB) and all SPD files listed in DB to initialize
140 the environment for next build. This method will only be executed only once
141 in the whole build process.
142
143 @param workspaceDatabaseFile the file name of framework database
144 @param workspaceDir current workspace directory path
145 @throws Exception
146 Framework Dababase or SPD or MSA file is not valid
147 **/
148 public synchronized static void initInfo(String workspaceDatabaseFile, String workspaceDir) throws Exception {
149
150 //
151 // Backup workspace directory. It will be used by other method
152 //
153 GlobalData.workspaceDir = workspaceDir.replaceAll("(\\\\)", "/");
154 File dbFile = new File(workspaceDir + File.separatorChar + workspaceDatabaseFile);
155 try {
156 FrameworkDatabaseDocument db = (FrameworkDatabaseDocument) XmlObject.Factory.parse(dbFile);
157 //
158 // validate FrameworkDatabaseFile
159 //
160 // if (! db.validate()) {
161 // throw new Exception("Framework Database file [" + dbFile.getPath() + "] is invalid.");
162 // }
163 //
164 // Get package list
165 //
166 List<DbPathAndFilename> packages = db.getFrameworkDatabase().getPackageList().getFilenameList();
167
168 Iterator iter = packages.iterator();
169 while (iter.hasNext()) {
170 DbPathAndFilename dbPath = (DbPathAndFilename)iter.next();
171 String fileName = dbPath.getStringValue();
172 Spd spd = new Spd(new File(workspaceDir + File.separatorChar + fileName));
173 if (!packageList.contains(spd.getPackageId())) {
174 packageList.add(spd.getPackageId());
175 }
176 spdTable.put(spd.getPackageId(), spd);
177 }
178
179
180 } catch (Exception e) {
181 e.printStackTrace();
182 throw new Exception("Parse workspace Database [" + dbFile.getPath() + "] Error.\n" + e.getMessage());
183 }
184 }
185
186 /**
187 Get the current WORKSPACE Directory.
188
189 @return current workspace directory
190 **/
191 public synchronized static String getWorkspacePath() {
192 return workspaceDir;
193 }
194
195
196 /**
197 Get the MSA file name with absolute path
198 */
199 public synchronized static File getMsaFile(ModuleIdentification moduleId) throws Exception {
200 File msaFile = null;
201 //
202 // TBD. Do only when package is null.
203 //
204 Iterator iter = packageList.iterator();
205 while (iter.hasNext()) {
206 PackageIdentification packageId = (PackageIdentification)iter.next();
207 Spd spd = spdTable.get(packageId);
208 msaFile = spd.getModuleFile(moduleId);
209 if (msaFile != null ) {
210 break ;
211 }
212 }
213 if (msaFile == null){
214 throw new Exception("Can't find Module [" + moduleId.getName() + "] in all packages. ");
215 }
216 else {
217 return msaFile;
218 }
219 }
220
221 public synchronized static PackageIdentification getPackageForModule(ModuleIdentification moduleId) {
222 //
223 // If package already defined in module
224 //
225 if (moduleId.getPackage() != null) {
226 return moduleId.getPackage();
227 }
228
229 PackageIdentification packageId = null;
230 Iterator iter = packageList.iterator();
231 while (iter.hasNext()) {
232 packageId = (PackageIdentification)iter.next();
233
234 Spd spd = spdTable.get(packageId);
235 if (spd.getModuleFile(moduleId) != null ) {
236 moduleId.setPackage(packageId);
237 break ;
238 }
239 }
240 if (packageId == null){
241 return null;
242 }
243 else {
244 return packageId;
245 }
246 }
247
248 /**
249 Difference between build and parse: ToolChain and Target
250 **/
251 public synchronized static boolean isModuleBuilt(FpdModuleIdentification moduleId) {
252 return builtModules.contains(moduleId);
253 }
254
255 public synchronized static void registerBuiltModule(FpdModuleIdentification fpdModuleId) {
256 builtModules.add(fpdModuleId);
257 }
258
259
260 public synchronized static void registerFpdModuleSA(FpdModuleIdentification fpdModuleId, Map<String, XmlObject> doc) throws Exception{
261 Map<String, XmlObject> result = new HashMap<String, XmlObject>();
262 Set keySet = doc.keySet();
263 Iterator iter = keySet.iterator();
264 while (iter.hasNext()){
265 String key = (String)iter.next();
266 XmlObject item = cloneXmlObject(doc.get(key), true);
267 result.put(key, item);
268 }
269 fpdModuleSA.put(fpdModuleId, result);
270 }
271
272 /**
273 Query overrided module surface area information. If current is Package
274 or Platform build, also include the information from FPD file.
275
276 <p>Note that surface area parsing is incremental. That means the method will
277 only parse the MSA and MBD files if necessary. </p>
278
279 @param moduleName the base name of the module
280 @return the overrided module surface area information
281 @throws Exception
282 MSA or MBD is not valid
283 **/
284 public synchronized static Map<String, XmlObject> getDoc(FpdModuleIdentification fpdModuleId) throws Exception {
285 if (parsedModules.containsKey(fpdModuleId)) {
286 return parsedModules.get(fpdModuleId);
287 }
288 Map<String, XmlObject> doc = new HashMap<String, XmlObject>();
289 ModuleIdentification moduleId = fpdModuleId.getModule();
290 //
291 // First part: get the MSA files info
292 //
293 doc = getNativeMsa(moduleId);
294
295 //
296 // Second part: put build options
297 //
298 doc.put("BuildOptions", fpdBuildOptions);
299
300 //
301 // Third part: get Module info from FPD, such as Library instances, PCDs
302 //
303 if (fpdModuleSA.containsKey(fpdModuleId)){
304 //
305 // merge module info in FPD to final Doc
306 // For Library Module, do nothing here
307 //
308 doc.putAll(fpdModuleSA.get(fpdModuleId));
309 }
310 parsedModules.put(fpdModuleId, doc);
311 return doc;
312 }
313
314 public synchronized static Map<String, XmlObject> getDoc(ModuleIdentification moduleId, String arch) throws Exception {
315 FpdModuleIdentification fpdModuleId = new FpdModuleIdentification(moduleId, arch);
316 return getDoc(fpdModuleId);
317 }
318 /**
319 Query the native MSA information with module base name.
320
321 <p>Note that MSA parsing is incremental. That means the method will
322 only to parse the MSA files when never parsed before. </p>
323
324 @param moduleName the base name of the module
325 @return the native MSA information
326 @throws Exception
327 MSA file is not valid
328 **/
329 public synchronized static Map<String, XmlObject> getNativeMsa(ModuleIdentification moduleId) throws Exception {
330 if (nativeMsa.containsKey(moduleId)) {
331 return nativeMsa.get(moduleId);
332 }
333 File msaFile = getMsaFile(moduleId);
334 Map<String, XmlObject> msaMap = getNativeMsa(msaFile);
335 nativeMsa.put(moduleId, msaMap);
336 return msaMap;
337 }
338
339 public synchronized static Map<String, XmlObject> getNativeMsa(File msaFile) throws Exception {
340
341 try {
342 ModuleSurfaceAreaDocument doc = (ModuleSurfaceAreaDocument)XmlObject.Factory.parse(msaFile);
343 //
344 // Validate File if they accord with XML Schema
345 //
346 // if ( ! doc.validate()){
347 // throw new Exception("Module Surface Area file [" + msaFile.getPath() + "] is invalid.");
348 // }
349 //
350 // parse MSA file
351 //
352 ModuleSurfaceArea msa= doc.getModuleSurfaceArea();
353 Map<String, XmlObject> msaMap = new HashMap<String, XmlObject>();
354 msaMap.put("ModuleSurfaceArea", msa);
355 msaMap.put("MsaHeader", cloneXmlObject(msa.getMsaHeader(), true));
356 msaMap.put("LibraryClassDefinitions", cloneXmlObject(msa.getLibraryClassDefinitions(), true));
357 msaMap.put("SourceFiles", cloneXmlObject(msa.getSourceFiles(), true));
358 msaMap.put("PackageDependencies", cloneXmlObject(msa.getPackageDependencies(), true));
359 msaMap.put("Protocols", cloneXmlObject(msa.getProtocols(), true));
360 msaMap.put("PPIs", cloneXmlObject(msa.getPPIs(), true));
361 msaMap.put("Guids", cloneXmlObject(msa.getGuids(), true));
362 msaMap.put("Externs", cloneXmlObject(msa.getExterns(), true));
363 return msaMap;
364 }
365 catch (Exception ex){
366 throw new Exception(ex.getMessage());
367 }
368 }
369
370 public static Map<String, XmlObject> getFpdBuildOptions() {
371 Map<String, XmlObject> map = new HashMap<String, XmlObject>();
372 map.put("BuildOptions", fpdBuildOptions);
373 return map;
374 }
375
376 public static void setFpdBuildOptions(XmlObject fpdBuildOptions) throws Exception{
377 GlobalData.fpdBuildOptions = cloneXmlObject(fpdBuildOptions, true);
378 }
379
380 public static XmlObject getFpdDynamicPcds() {
381 return fpdDynamicPcds;
382 }
383
384 public static void setFpdDynamicPcds(XmlObject fpdDynamicPcds) {
385 GlobalData.fpdDynamicPcds = fpdDynamicPcds;
386 }
387
388 //////////////////////////////////////////////
389 //////////////////////////////////////////////
390
391 public static Set<ModuleIdentification> getModules(PackageIdentification packageId){
392 Spd spd = spdTable.get(packageId);
393 if (spd == null ) {
394 Set<ModuleIdentification> dummy = new HashSet<ModuleIdentification>();
395 return dummy;
396 }
397 else {
398 return spd.getModules();
399 }
400 }
401
402 public synchronized static PlatformIdentification getPlatform(String name) throws Exception {
403 Iterator iter = platformList.iterator();
404 while(iter.hasNext()){
405 PlatformIdentification platformId = (PlatformIdentification)iter.next();
406 if (platformId.getName().equalsIgnoreCase(name)) {
407 GlobalData.log.info("Platform: " + platformId + platformId.getFpdFile());
408 return platformId;
409 }
410 }
411 throw new Exception("Can't find platform [" + name + "] in current workspace. ");
412 }
413
414 public synchronized static File getPackageFile(PackageIdentification packageId) throws Exception {
415 Iterator iter = packageList.iterator();
416 while(iter.hasNext()){
417 PackageIdentification packageItem = (PackageIdentification)iter.next();
418 if (packageItem.equals(packageId)) {
419 packageId.setName(packageItem.getName());
420 return packageItem.getSpdFile();
421 }
422 }
423 throw new Exception("Can't find " + packageId + " in current workspace. ");
424 }
425
426 public synchronized static File getModuleFile(ModuleIdentification moduleId) throws Exception {
427 PackageIdentification packageId = getPackageForModule(moduleId);
428 moduleId.setPackage(packageId);
429 Spd spd = spdTable.get(packageId);
430 return spd.getModuleFile(moduleId);
431 }
432 //
433 // expanded by FrameworkWizard
434 //
435 public synchronized static XmlObject getModuleXmlObject(ModuleIdentification moduleId) throws Exception {
436 PackageIdentification packageId = getPackageForModule(moduleId);
437 moduleId.setPackage(packageId);
438 Spd spd = spdTable.get(packageId);
439 return spd.msaDocMap.get(moduleId);
440 }
441
442 public synchronized static XmlObject getPackageXmlObject(PackageIdentification packageId) {
443 Spd spd = spdTable.get(packageId);
444 if (spd != null){
445 return spd.spdDocMap.get("PackageSurfaceArea");
446 }
447 return null;
448 }
449
450 public synchronized static Set<PackageIdentification> getPackageList(){
451 return packageList;
452 }
453 ///// remove!!
454 private static XmlObject cloneXmlObject(XmlObject object, boolean deep) throws Exception {
455 if ( object == null) {
456 return null;
457 }
458 XmlObject result = null;
459 try {
460 result = XmlObject.Factory.parse(object.getDomNode()
461 .cloneNode(deep));
462 } catch (Exception ex) {
463 throw new Exception(ex.getMessage());
464 }
465 return result;
466 }
467
468 ////// Tool Chain Related, try to refine and put some logic process to ToolChainFactory
469 public static void setBuildToolChainFamilyOptions(Map<String, Object> map) {
470 toolChainFamilyOptions = map;
471 }
472
473 public static Map<String, Object> getToolChainFamilyOptions() {
474 return toolChainFamilyOptions;
475 }
476
477 public static void setBuildToolChainOptions(Map<String, Object> map) {
478 toolChainOptions = map;
479 }
480
481 public static Map<String, Object> getToolChainOptions() {
482 return toolChainOptions;
483 }
484
485 public static void setTargets(Set<String> targetSet) {
486 GlobalData.log.info("TargetSet: " + targetSet);
487 targets = targetSet;
488 }
489
490 public static String[] getTargets() {
491 return (String[])targets.toArray(new String[targets.size()]);
492 }
493
494 public static void setToolChains(Set<String> toolChainSet) {
495 toolChains = toolChainSet;
496 }
497
498 public static String[] getToolChains() {
499 String[] toolChainList = new String[toolChains.size()];
500 return (String[])toolChains.toArray(toolChainList);
501 }
502
503 public static void setToolChainFamilies(Set<String> toolChainFamilySet) {
504 toolChainFamilies = toolChainFamilySet;
505 }
506
507 public static void setToolChainFamiliyMap(Map<String, Set<String>> map) {
508 /*
509 Set<String> keys = map.keySet();
510 Iterator it = keys.iterator();
511 while (it.hasNext()) {
512 String toolchain = (String)it.next();
513 Set<String> familyMap = (Set<String>)map.get(toolchain);
514 Iterator fit = familyMap.iterator();
515 System.out.print(toolchain + ": ");
516 while (fit.hasNext()) {
517 System.out.print((String)fit.next() + " ");
518 }
519 System.out.println("");
520 }
521 */
522 toolChainFamilyMap = map;
523 }
524
525 public static String[] getToolChainFamilies() {
526 String[] toolChainFamilyList = new String[toolChainFamilies.size()];
527 return (String[])toolChainFamilies.toArray(toolChainFamilyList);
528 }
529
530 public static String[] getToolChainFamilies(String toolChain) {
531 Set<String> familySet = (Set<String>)toolChainFamilyMap.get(toolChain);
532 String[] toolChainFamilyList = new String[familySet.size()];
533 return (String[])familySet.toArray(toolChainFamilyList);
534 }
535
536 public static Set<String> getToolChainFamilySet(String toolChain) {
537 return (Set<String>)toolChainFamilyMap.get(toolChain);
538 }
539
540 public static void setArchs(Set<String> archSet) {
541 archs = archSet;
542 }
543
544 public static String[] getArchs() {
545 String[] archList = new String[archs.size()];
546 return (String[])archs.toArray(archList);
547 }
548 /*
549
550 */
551 public static void SetCommandTypes(Set<String> commandTypeSet) {
552 commandTypes = commandTypeSet;
553 }
554 /*
555
556 */
557 public static void SetCommandTypes(Map<String, Set<String>> commandTypeMap) {
558 toolChainCommandMap = commandTypeMap;
559 }
560 /*
561
562 */
563 public static String[] getCommandTypes() {
564 String[] commandList = new String[commandTypes.size()];
565 return (String[])commandTypes.toArray(commandList);
566 }
567 /*
568
569 */
570 public static String[] getCommandTypes(String toolChain) {
571 Set<String> commands = (Set<String>)toolChainCommandMap.get(toolChain);
572 if (commands == null) {
573 return new String[0];
574 }
575
576 String[] commandList = new String[commands.size()];
577 return (String[])commands.toArray(commandList);
578 }
579 /*
580
581 */
582 public static String getCommandSetting(String commandDescString) {
583 return (String)toolChainDefinitions.get(commandDescString);
584 }
585 /*
586
587 */
588 public static void setToolChainDefinitions(Map<String, String> def) {
589 toolChainDefinitions = def;
590 }
591
592 public static Map<String, String> getToolChainDefinitions() {
593 return toolChainDefinitions;
594 }
595
596 }
597
598 final class KeyComparator implements Comparator<String> {
599 public int compare(String x, String y) {
600 return x.compareToIgnoreCase(y);
601 }
602
603 }
604