]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/platform/ui/global/GlobalData.java
change table column positions in tables of FrameworkModules and horizontal scrollbar...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / platform / ui / global / GlobalData.java
CommitLineData
a13899c5 1/** @file\r
2 GlobalData class. \r
3 \r
4 GlobalData provide initializing, instoring, querying and update global data.\r
5 It is a bridge to intercommunicate between multiple component, such as AutoGen,\r
6 PCD and so on. \r
7 \r
8Copyright (c) 2006, Intel Corporation\r
9All rights reserved. This program and the accompanying materials\r
10are licensed and made available under the terms and conditions of the BSD License\r
11which accompanies this distribution. The full text of the license may be found at\r
12http://opensource.org/licenses/bsd-license.php\r
13\r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16**/\r
17package org.tianocore.frameworkwizard.platform.ui.global;\r
18\r
19import org.apache.xmlbeans.XmlObject;\r
20import org.tianocore.DbPathAndFilename;\r
21import org.tianocore.FrameworkDatabaseDocument;\r
22import org.tianocore.ModuleSurfaceAreaDocument;\r
1dac04ab 23import org.tianocore.PcdCodedDocument;\r
a13899c5 24import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
25import org.tianocore.frameworkwizard.platform.ui.id.FpdModuleIdentification;\r
26import org.tianocore.frameworkwizard.platform.ui.id.ModuleIdentification;\r
27import org.tianocore.frameworkwizard.platform.ui.id.PackageIdentification;\r
28import org.tianocore.frameworkwizard.platform.ui.id.PlatformIdentification;\r
29\r
30import java.io.File;\r
31import java.util.Comparator;\r
32import java.util.HashMap;\r
33import java.util.HashSet;\r
34import java.util.Iterator;\r
a13899c5 35import java.util.List;\r
bf5abaff 36import java.util.ListIterator;\r
a13899c5 37import java.util.Map;\r
38import java.util.Set;\r
bf5abaff 39import java.util.Vector;\r
a13899c5 40import java.util.logging.Logger;\r
a13899c5 41\r
42/**\r
43 GlobalData provide initializing, instoring, querying and update global data.\r
44 It is a bridge to intercommunicate between multiple component, such as AutoGen,\r
45 PCD and so on. \r
46 \r
47 <p>Note that all global information are initialized incrementally. All data will \r
48 parse and record only of necessary during build time. </p>\r
49 \r
50 @since GenBuild 1.0\r
51**/\r
52public class GlobalData {\r
53\r
54\r
55 public static Logger log = Logger.getAnonymousLogger();\r
56 public static KeyComparator comparator = new KeyComparator();\r
57 ///\r
58 /// Record current WORKSPACE Directory\r
59 ///\r
60 private static String workspaceDir = "";\r
61 \r
62 ///\r
63 /// Be used to ensure Global data will be initialized only once.\r
64 ///\r
410e0e9f 65// private static boolean globalFlag = false;\r
a13899c5 66 \r
67 ///\r
68 /// Framework Database information: package list and platform list\r
69 ///\r
70 private static Set<PackageIdentification> packageList = new HashSet<PackageIdentification>(); \r
71\r
72 private static Set<PlatformIdentification> platformList = new HashSet<PlatformIdentification>();\r
73\r
74 ///\r
75 /// Every detail SPD informations: Module list, Library class definition,\r
76 /// Package header file, GUID/PPI/Protocol definitions\r
77 ///\r
78 private static final Map<PackageIdentification, Spd> spdTable = new HashMap<PackageIdentification, Spd>();\r
79\r
80 ///\r
81 /// Build informations are divided into three parts:\r
82 /// 1. From MSA 2. From FPD 3. From FPD' ModuleSA\r
83 ///\r
84 private static Map<ModuleIdentification, Map<String, XmlObject>> nativeMsa = new HashMap<ModuleIdentification, Map<String, XmlObject>>();\r
85\r
86 private static Map<FpdModuleIdentification, Map<String, XmlObject>> fpdModuleSA= new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();\r
87\r
88 private static XmlObject fpdBuildOptions;\r
89\r
90 private static XmlObject fpdDynamicPcds;\r
91 \r
92 ///\r
93 /// Parsed modules list\r
94 ///\r
95 private static Map<FpdModuleIdentification, Map<String, XmlObject>> parsedModules = new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();\r
96 \r
97 ///\r
98 /// built modules list with ARCH, TARGET, TOOLCHAIN\r
99 ///\r
100 private static Set<FpdModuleIdentification> builtModules = new HashSet<FpdModuleIdentification>();\r
101 \r
a13899c5 102 /**\r
103 Parse framework database (DB) and all SPD files listed in DB to initialize\r
104 the environment for next build. This method will only be executed only once\r
105 in the whole build process. \r
106 \r
107 @param workspaceDatabaseFile the file name of framework database\r
108 @param workspaceDir current workspace directory path\r
109 @throws Exception\r
110 Framework Dababase or SPD or MSA file is not valid\r
111 **/\r
112 public synchronized static void initInfo(String workspaceDatabaseFile, String workspaceDir) throws Exception {\r
a13899c5 113 \r
114 //\r
115 // Backup workspace directory. It will be used by other method\r
116 //\r
117 GlobalData.workspaceDir = workspaceDir.replaceAll("(\\\\)", "/");\r
118 File dbFile = new File(workspaceDir + File.separatorChar + workspaceDatabaseFile);\r
119 try {\r
120 FrameworkDatabaseDocument db = (FrameworkDatabaseDocument) XmlObject.Factory.parse(dbFile);\r
121 //\r
122 // validate FrameworkDatabaseFile\r
123 //\r
124// if (! db.validate()) {\r
125// throw new Exception("Framework Database file [" + dbFile.getPath() + "] is invalid.");\r
126// }\r
127 //\r
128 // Get package list\r
129 //\r
add40ab2 130 packageList.clear();\r
a13899c5 131 List<DbPathAndFilename> packages = db.getFrameworkDatabase().getPackageList().getFilenameList();\r
132 \r
133 Iterator iter = packages.iterator();\r
134 while (iter.hasNext()) {\r
135 DbPathAndFilename dbPath = (DbPathAndFilename)iter.next();\r
136 String fileName = dbPath.getStringValue();\r
137 Spd spd = new Spd(new File(workspaceDir + File.separatorChar + fileName));\r
add40ab2 138 packageList.add(spd.getPackageId());\r
a13899c5 139 spdTable.put(spd.getPackageId(), spd);\r
140 }\r
141\r
142 \r
143 } catch (Exception e) {\r
144 e.printStackTrace();\r
145 throw new Exception("Parse workspace Database [" + dbFile.getPath() + "] Error.\n" + e.getMessage());\r
146 }\r
147 }\r
148 \r
149 /**\r
150 Get the current WORKSPACE Directory. \r
151 \r
152 @return current workspace directory\r
153 **/\r
154 public synchronized static String getWorkspacePath() {\r
155 return workspaceDir;\r
156 }\r
157\r
158\r
159 /**\r
160 Get the MSA file name with absolute path\r
161 */\r
162 public synchronized static File getMsaFile(ModuleIdentification moduleId) throws Exception {\r
163 File msaFile = null;\r
164 //\r
165 // TBD. Do only when package is null. \r
166 //\r
167 Iterator iter = packageList.iterator();\r
168 while (iter.hasNext()) {\r
169 PackageIdentification packageId = (PackageIdentification)iter.next();\r
170 Spd spd = spdTable.get(packageId);\r
171 msaFile = spd.getModuleFile(moduleId);\r
172 if (msaFile != null ) {\r
173 break ;\r
174 }\r
175 }\r
176 if (msaFile == null){\r
177 throw new Exception("Can't find Module [" + moduleId.getName() + "] in all packages. ");\r
178 }\r
179 else {\r
180 return msaFile;\r
181 }\r
182 }\r
183\r
184 public synchronized static PackageIdentification getPackageForModule(ModuleIdentification moduleId) {\r
185 //\r
186 // If package already defined in module\r
187 //\r
188 if (moduleId.getPackage() != null) {\r
189 return moduleId.getPackage();\r
190 }\r
191 \r
192 PackageIdentification packageId = null;\r
193 Iterator iter = packageList.iterator();\r
194 while (iter.hasNext()) {\r
195 packageId = (PackageIdentification)iter.next();\r
196 \r
197 Spd spd = spdTable.get(packageId);\r
198 if (spd.getModuleFile(moduleId) != null ) {\r
199 moduleId.setPackage(packageId);\r
200 break ;\r
201 }\r
202 }\r
203 if (packageId == null){\r
204 return null;\r
205 }\r
206 else {\r
207 return packageId;\r
208 }\r
209 }\r
210 \r
211 /**\r
212 Difference between build and parse: ToolChain and Target\r
213 **/\r
214 public synchronized static boolean isModuleBuilt(FpdModuleIdentification moduleId) {\r
215 return builtModules.contains(moduleId);\r
216 }\r
217 \r
218 public synchronized static void registerBuiltModule(FpdModuleIdentification fpdModuleId) {\r
219 builtModules.add(fpdModuleId);\r
220 }\r
221\r
222 \r
223 public synchronized static void registerFpdModuleSA(FpdModuleIdentification fpdModuleId, Map<String, XmlObject> doc) throws Exception{\r
224 Map<String, XmlObject> result = new HashMap<String, XmlObject>();\r
225 Set keySet = doc.keySet();\r
226 Iterator iter = keySet.iterator();\r
227 while (iter.hasNext()){\r
228 String key = (String)iter.next();\r
229 XmlObject item = cloneXmlObject(doc.get(key), true);\r
230 result.put(key, item);\r
231 }\r
232 fpdModuleSA.put(fpdModuleId, result);\r
233 }\r
234 \r
235 /**\r
236 Query overrided module surface area information. If current is Package\r
237 or Platform build, also include the information from FPD file. \r
238 \r
239 <p>Note that surface area parsing is incremental. That means the method will \r
240 only parse the MSA and MBD files if necessary. </p>\r
241 \r
242 @param moduleName the base name of the module\r
243 @return the overrided module surface area information\r
244 @throws Exception\r
245 MSA or MBD is not valid\r
246 **/\r
247 public synchronized static Map<String, XmlObject> getDoc(FpdModuleIdentification fpdModuleId) throws Exception {\r
248 if (parsedModules.containsKey(fpdModuleId)) {\r
249 return parsedModules.get(fpdModuleId);\r
250 }\r
251 Map<String, XmlObject> doc = new HashMap<String, XmlObject>();\r
252 ModuleIdentification moduleId = fpdModuleId.getModule();\r
253 //\r
254 // First part: get the MSA files info\r
255 //\r
256 doc = getNativeMsa(moduleId);\r
257 \r
258 //\r
259 // Second part: put build options\r
260 //\r
261 doc.put("BuildOptions", fpdBuildOptions);\r
262 \r
263 //\r
264 // Third part: get Module info from FPD, such as Library instances, PCDs\r
265 //\r
266 if (fpdModuleSA.containsKey(fpdModuleId)){\r
267 //\r
268 // merge module info in FPD to final Doc\r
269 // For Library Module, do nothing here\r
270 //\r
271 doc.putAll(fpdModuleSA.get(fpdModuleId));\r
272 }\r
273 parsedModules.put(fpdModuleId, doc);\r
274 return doc;\r
275 }\r
276\r
277 public synchronized static Map<String, XmlObject> getDoc(ModuleIdentification moduleId, String arch) throws Exception {\r
278 FpdModuleIdentification fpdModuleId = new FpdModuleIdentification(moduleId, arch);\r
279 return getDoc(fpdModuleId);\r
280 }\r
281 /**\r
282 Query the native MSA information with module base name. \r
283 \r
284 <p>Note that MSA parsing is incremental. That means the method will \r
285 only to parse the MSA files when never parsed before. </p>\r
286 \r
287 @param moduleName the base name of the module\r
288 @return the native MSA information\r
289 @throws Exception\r
290 MSA file is not valid\r
291 **/\r
292 public synchronized static Map<String, XmlObject> getNativeMsa(ModuleIdentification moduleId) throws Exception {\r
293 if (nativeMsa.containsKey(moduleId)) {\r
294 return nativeMsa.get(moduleId);\r
295 }\r
296 File msaFile = getMsaFile(moduleId);\r
297 Map<String, XmlObject> msaMap = getNativeMsa(msaFile);\r
298 nativeMsa.put(moduleId, msaMap);\r
299 return msaMap;\r
300 }\r
301 \r
302 public synchronized static Map<String, XmlObject> getNativeMsa(File msaFile) throws Exception {\r
410e0e9f 303 \r
a13899c5 304 try {\r
305 ModuleSurfaceAreaDocument doc = (ModuleSurfaceAreaDocument)XmlObject.Factory.parse(msaFile);\r
306 //\r
307 // Validate File if they accord with XML Schema\r
308 //\r
309// if ( ! doc.validate()){\r
310// throw new Exception("Module Surface Area file [" + msaFile.getPath() + "] is invalid.");\r
311// }\r
312 //\r
313 // parse MSA file\r
314 //\r
315 ModuleSurfaceArea msa= doc.getModuleSurfaceArea();\r
316 Map<String, XmlObject> msaMap = new HashMap<String, XmlObject>();\r
317 msaMap.put("ModuleSurfaceArea", msa);\r
318 msaMap.put("MsaHeader", cloneXmlObject(msa.getMsaHeader(), true));\r
319 msaMap.put("LibraryClassDefinitions", cloneXmlObject(msa.getLibraryClassDefinitions(), true));\r
320 msaMap.put("SourceFiles", cloneXmlObject(msa.getSourceFiles(), true));\r
321 msaMap.put("PackageDependencies", cloneXmlObject(msa.getPackageDependencies(), true));\r
322 msaMap.put("Protocols", cloneXmlObject(msa.getProtocols(), true));\r
323 msaMap.put("PPIs", cloneXmlObject(msa.getPPIs(), true));\r
324 msaMap.put("Guids", cloneXmlObject(msa.getGuids(), true));\r
325 msaMap.put("Externs", cloneXmlObject(msa.getExterns(), true));\r
326 return msaMap;\r
327 }\r
328 catch (Exception ex){\r
329 throw new Exception(ex.getMessage());\r
330 }\r
331 }\r
332 \r
333 public static Map<String, XmlObject> getFpdBuildOptions() {\r
334 Map<String, XmlObject> map = new HashMap<String, XmlObject>();\r
335 map.put("BuildOptions", fpdBuildOptions);\r
336 return map;\r
337 }\r
338 \r
339 public static void setFpdBuildOptions(XmlObject fpdBuildOptions) throws Exception{\r
340 GlobalData.fpdBuildOptions = cloneXmlObject(fpdBuildOptions, true);\r
341 }\r
342\r
343 public static XmlObject getFpdDynamicPcds() {\r
344 return fpdDynamicPcds;\r
345 }\r
346\r
347 public static void setFpdDynamicPcds(XmlObject fpdDynamicPcds) {\r
348 GlobalData.fpdDynamicPcds = fpdDynamicPcds;\r
349 }\r
350\r
351 //////////////////////////////////////////////\r
352 //////////////////////////////////////////////\r
353 \r
354 public static Set<ModuleIdentification> getModules(PackageIdentification packageId){\r
355 Spd spd = spdTable.get(packageId);\r
356 if (spd == null ) {\r
357 Set<ModuleIdentification> dummy = new HashSet<ModuleIdentification>();\r
358 return dummy;\r
359 }\r
360 else {\r
361 return spd.getModules();\r
362 }\r
363 }\r
364\r
a13899c5 365 public synchronized static PlatformIdentification getPlatform(String name) throws Exception {\r
366 Iterator iter = platformList.iterator();\r
367 while(iter.hasNext()){\r
368 PlatformIdentification platformId = (PlatformIdentification)iter.next();\r
369 if (platformId.getName().equalsIgnoreCase(name)) {\r
370 GlobalData.log.info("Platform: " + platformId + platformId.getFpdFile());\r
371 return platformId;\r
372 }\r
373 }\r
374 throw new Exception("Can't find platform [" + name + "] in current workspace. ");\r
375 }\r
376 \r
377 public synchronized static File getPackageFile(PackageIdentification packageId) throws Exception {\r
378 Iterator iter = packageList.iterator();\r
379 while(iter.hasNext()){\r
380 PackageIdentification packageItem = (PackageIdentification)iter.next();\r
381 if (packageItem.equals(packageId)) {\r
382 packageId.setName(packageItem.getName());\r
383 return packageItem.getSpdFile();\r
384 }\r
385 }\r
386 throw new Exception("Can't find " + packageId + " in current workspace. ");\r
387 }\r
388 \r
389 public synchronized static File getModuleFile(ModuleIdentification moduleId) throws Exception {\r
390 PackageIdentification packageId = getPackageForModule(moduleId);\r
391 moduleId.setPackage(packageId);\r
392 Spd spd = spdTable.get(packageId);\r
393 return spd.getModuleFile(moduleId);\r
394 }\r
395 //\r
396 // expanded by FrameworkWizard\r
397 //\r
398 public synchronized static XmlObject getModuleXmlObject(ModuleIdentification moduleId) throws Exception {\r
399 PackageIdentification packageId = getPackageForModule(moduleId);\r
400 moduleId.setPackage(packageId);\r
401 Spd spd = spdTable.get(packageId);\r
402 return spd.msaDocMap.get(moduleId);\r
403 }\r
404 \r
405 public synchronized static XmlObject getPackageXmlObject(PackageIdentification packageId) {\r
406 Spd spd = spdTable.get(packageId);\r
407 if (spd != null){\r
408 return spd.spdDocMap.get("PackageSurfaceArea");\r
409 }\r
410 return null;\r
411 }\r
412 \r
413 public synchronized static Set<PackageIdentification> getPackageList(){\r
414 return packageList;\r
415 }\r
416 ///// remove!!\r
417 private static XmlObject cloneXmlObject(XmlObject object, boolean deep) throws Exception {\r
418 if ( object == null) {\r
419 return null;\r
420 }\r
421 XmlObject result = null;\r
422 try {\r
423 result = XmlObject.Factory.parse(object.getDomNode()\r
424 .cloneNode(deep));\r
425 } catch (Exception ex) {\r
426 throw new Exception(ex.getMessage());\r
427 }\r
428 return result;\r
429 }\r
430\r
34c614e1 431 public static ModuleIdentification getModuleId(String key){\r
432 //\r
433 // Get ModuleGuid, ModuleVersion, PackageGuid, PackageVersion, Arch into string array.\r
434 //\r
435 String[] keyPart = key.split(" ");\r
436 Set<PackageIdentification> spi = GlobalData.getPackageList();\r
437 Iterator ispi = spi.iterator();\r
438 \r
439 while(ispi.hasNext()) {\r
440 PackageIdentification pi = (PackageIdentification)ispi.next();\r
441 if ( !pi.getGuid().equalsIgnoreCase(keyPart[2])){ \r
442\r
443 continue;\r
444 }\r
445 if (keyPart[3] != null && keyPart[3].length() > 0 && !keyPart[3].equals("null")){\r
446 if(!pi.getVersion().equals(keyPart[3])){\r
447 continue;\r
448 }\r
449 }\r
450 Set<ModuleIdentification> smi = GlobalData.getModules(pi);\r
451 Iterator ismi = smi.iterator();\r
452 while(ismi.hasNext()) {\r
453 ModuleIdentification mi = (ModuleIdentification)ismi.next();\r
454 if (mi.getGuid().equalsIgnoreCase(keyPart[0])){\r
455 if (keyPart[1] != null && keyPart[1].length() > 0 && !keyPart[1].equals("null")){\r
456 if(!mi.getVersion().equals(keyPart[1])){\r
457 continue;\r
458 }\r
459 }\r
460\r
461 return mi;\r
462 }\r
463 }\r
464 }\r
465 return null;\r
466 }\r
bf5abaff 467 \r
1dac04ab 468 public static Vector<String> getModuleSupArchs(ModuleIdentification mi) throws Exception {\r
bf5abaff 469 Vector<String> vArchs = null;\r
470 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)getModuleXmlObject(mi);\r
471 if (msa.getModuleDefinitions() == null || msa.getModuleDefinitions().getSupportedArchitectures() == null) {\r
472 return vArchs;\r
a13899c5 473 }\r
bf5abaff 474 ListIterator li = msa.getModuleDefinitions().getSupportedArchitectures().listIterator();\r
475 while (li.hasNext()) {\r
476 if (vArchs == null) {\r
477 vArchs = new Vector<String>();\r
478 }\r
479 vArchs.add((String)li.next());\r
a13899c5 480 }\r
bf5abaff 481 \r
482 return vArchs;\r
a13899c5 483 }\r
bf5abaff 484 \r
1dac04ab 485 public static boolean pcdInMsa (String cName, String tsGuid, ModuleIdentification mi) throws Exception {\r
486 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)getModuleXmlObject(mi);\r
487 if (msa.getPcdCoded() == null || msa.getPcdCoded().getPcdEntryList() == null) {\r
488 return false;\r
489 }\r
490 ListIterator li = msa.getPcdCoded().getPcdEntryList().listIterator();\r
491 while (li.hasNext()) {\r
492 PcdCodedDocument.PcdCoded.PcdEntry msaPcd = (PcdCodedDocument.PcdCoded.PcdEntry)li.next();\r
493 if (msaPcd.getCName().equals(cName) && msaPcd.getTokenSpaceGuidCName().equals(tsGuid)) {\r
494 return true;\r
495 }\r
496 }\r
497 return false;\r
498 }\r
499 \r
a13899c5 500}\r
501\r
502final class KeyComparator implements Comparator<String> {\r
503 public int compare(String x, String y) {\r
504 return x.compareToIgnoreCase(y);\r
505 }\r
506 \r
507}\r
508\r