+/** @file\r
+ WorkspaceProfile class. \r
+ \r
+ WorkspaceProfile provide initializing, instoring, querying and update global data.\r
+ It is a bridge to intercommunicate between multiple component, such as AutoGen,\r
+ PCD and so on. \r
+ \r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+package org.tianocore.frameworkwizard.platform.ui.global;\r
+\r
+import org.apache.xmlbeans.XmlObject;\r
+import org.tianocore.DbPathAndFilename;\r
+import org.tianocore.FrameworkDatabaseDocument;\r
+import org.tianocore.ModuleSurfaceAreaDocument;\r
+import org.tianocore.PcdCodedDocument;\r
+import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
+import org.tianocore.frameworkwizard.platform.ui.id.FpdModuleIdentification;\r
+import org.tianocore.frameworkwizard.platform.ui.id.ModuleIdentification;\r
+import org.tianocore.frameworkwizard.platform.ui.id.PackageIdentification;\r
+import org.tianocore.frameworkwizard.platform.ui.id.PlatformIdentification;\r
+\r
+import java.io.File;\r
+import java.util.Comparator;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.ListIterator;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.Vector;\r
+import java.util.logging.Logger;\r
+\r
+/**\r
+ WorkspaceProfile provide initializing, instoring, querying and update global data.\r
+ It is a bridge to intercommunicate between multiple component, such as AutoGen,\r
+ PCD and so on. \r
+ \r
+ <p>Note that all global information are initialized incrementally. All data will \r
+ parse and record only of necessary during build time. </p>\r
+ \r
+ @since GenBuild 1.0\r
+**/\r
+public class WorkspaceProfile {\r
+\r
+\r
+ public static Logger log = Logger.getAnonymousLogger();\r
+ public static KeyComparator comparator = new KeyComparator();\r
+ ///\r
+ /// Record current WORKSPACE Directory\r
+ ///\r
+ private static String workspaceDir = "";\r
+ \r
+ ///\r
+ /// Be used to ensure Global data will be initialized only once.\r
+ ///\r
+// private static boolean globalFlag = false;\r
+ \r
+ ///\r
+ /// Framework Database information: package list and platform list\r
+ ///\r
+ private static Set<PackageIdentification> packageList = new HashSet<PackageIdentification>(); \r
+\r
+ private static Set<PlatformIdentification> platformList = new HashSet<PlatformIdentification>();\r
+\r
+ ///\r
+ /// Every detail SPD informations: Module list, Library class definition,\r
+ /// Package header file, GUID/PPI/Protocol definitions\r
+ ///\r
+ private static final Map<PackageIdentification, Spd> spdTable = new HashMap<PackageIdentification, Spd>();\r
+\r
+ ///\r
+ /// Build informations are divided into three parts:\r
+ /// 1. From MSA 2. From FPD 3. From FPD' ModuleSA\r
+ ///\r
+ private static Map<ModuleIdentification, Map<String, XmlObject>> nativeMsa = new HashMap<ModuleIdentification, Map<String, XmlObject>>();\r
+\r
+ private static Map<FpdModuleIdentification, Map<String, XmlObject>> fpdModuleSA= new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();\r
+\r
+ private static XmlObject fpdBuildOptions;\r
+\r
+ private static XmlObject fpdDynamicPcds;\r
+ \r
+ ///\r
+ /// Parsed modules list\r
+ ///\r
+ private static Map<FpdModuleIdentification, Map<String, XmlObject>> parsedModules = new HashMap<FpdModuleIdentification, Map<String, XmlObject>>();\r
+ \r
+ ///\r
+ /// built modules list with ARCH, TARGET, TOOLCHAIN\r
+ ///\r
+ private static Set<FpdModuleIdentification> builtModules = new HashSet<FpdModuleIdentification>();\r
+ \r
+ /**\r
+ Parse framework database (DB) and all SPD files listed in DB to initialize\r
+ the environment for next build. This method will only be executed only once\r
+ in the whole build process. \r
+ \r
+ @param workspaceDatabaseFile the file name of framework database\r
+ @param workspaceDir current workspace directory path\r
+ @throws Exception\r
+ Framework Dababase or SPD or MSA file is not valid\r
+ **/\r
+ public synchronized static void initInfo(String workspaceDatabaseFile, String workspaceDir) throws Exception {\r
+ \r
+ //\r
+ // Backup workspace directory. It will be used by other method\r
+ //\r
+ WorkspaceProfile.workspaceDir = workspaceDir.replaceAll("(\\\\)", "/");\r
+ File dbFile = new File(workspaceDir + File.separatorChar + workspaceDatabaseFile);\r
+ try {\r
+ FrameworkDatabaseDocument db = (FrameworkDatabaseDocument) XmlObject.Factory.parse(dbFile);\r
+ //\r
+ // validate FrameworkDatabaseFile\r
+ //\r
+// if (! db.validate()) {\r
+// throw new Exception("Framework Database file [" + dbFile.getPath() + "] is invalid.");\r
+// }\r
+ //\r
+ // Get package list\r
+ //\r
+ packageList.clear();\r
+ List<DbPathAndFilename> packages = db.getFrameworkDatabase().getPackageList().getFilenameList();\r
+ \r
+ Iterator iter = packages.iterator();\r
+ while (iter.hasNext()) {\r
+ DbPathAndFilename dbPath = (DbPathAndFilename)iter.next();\r
+ String fileName = dbPath.getStringValue();\r
+ Spd spd = new Spd(new File(workspaceDir + File.separatorChar + fileName));\r
+ packageList.add(spd.getPackageId());\r
+ spdTable.put(spd.getPackageId(), spd);\r
+ }\r
+\r
+ \r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ throw new Exception("Parse workspace Database [" + dbFile.getPath() + "] Error.\n" + e.getMessage());\r
+ }\r
+ }\r
+ \r
+ /**\r
+ Get the current WORKSPACE Directory. \r
+ \r
+ @return current workspace directory\r
+ **/\r
+ public synchronized static String getWorkspacePath() {\r
+ return workspaceDir;\r
+ }\r
+\r
+\r
+ /**\r
+ Get the MSA file name with absolute path\r
+ */\r
+ public synchronized static File getMsaFile(ModuleIdentification moduleId) throws Exception {\r
+ File msaFile = null;\r
+ //\r
+ // TBD. Do only when package is null. \r
+ //\r
+ Iterator iter = packageList.iterator();\r
+ while (iter.hasNext()) {\r
+ PackageIdentification packageId = (PackageIdentification)iter.next();\r
+ Spd spd = spdTable.get(packageId);\r
+ msaFile = spd.getModuleFile(moduleId);\r
+ if (msaFile != null ) {\r
+ break ;\r
+ }\r
+ }\r
+ if (msaFile == null){\r
+ throw new Exception("Can't find Module [" + moduleId.getName() + "] in all packages. ");\r
+ }\r
+ else {\r
+ return msaFile;\r
+ }\r
+ }\r
+\r
+ public synchronized static PackageIdentification getPackageForModule(ModuleIdentification moduleId) {\r
+ //\r
+ // If package already defined in module\r
+ //\r
+ if (moduleId.getPackage() != null) {\r
+ return moduleId.getPackage();\r
+ }\r
+ \r
+ PackageIdentification packageId = null;\r
+ Iterator iter = packageList.iterator();\r
+ while (iter.hasNext()) {\r
+ packageId = (PackageIdentification)iter.next();\r
+ \r
+ Spd spd = spdTable.get(packageId);\r
+ if (spd.getModuleFile(moduleId) != null ) {\r
+ moduleId.setPackage(packageId);\r
+ break ;\r
+ }\r
+ }\r
+ if (packageId == null){\r
+ return null;\r
+ }\r
+ else {\r
+ return packageId;\r
+ }\r
+ }\r
+ \r
+ /**\r
+ Difference between build and parse: ToolChain and Target\r
+ **/\r
+ public synchronized static boolean isModuleBuilt(FpdModuleIdentification moduleId) {\r
+ return builtModules.contains(moduleId);\r
+ }\r
+ \r
+ public synchronized static void registerBuiltModule(FpdModuleIdentification fpdModuleId) {\r
+ builtModules.add(fpdModuleId);\r
+ }\r
+\r
+ \r
+ public synchronized static void registerFpdModuleSA(FpdModuleIdentification fpdModuleId, Map<String, XmlObject> doc) throws Exception{\r
+ Map<String, XmlObject> result = new HashMap<String, XmlObject>();\r
+ Set keySet = doc.keySet();\r
+ Iterator iter = keySet.iterator();\r
+ while (iter.hasNext()){\r
+ String key = (String)iter.next();\r
+ XmlObject item = cloneXmlObject(doc.get(key), true);\r
+ result.put(key, item);\r
+ }\r
+ fpdModuleSA.put(fpdModuleId, result);\r
+ }\r
+ \r
+ /**\r
+ Query overrided module surface area information. If current is Package\r
+ or Platform build, also include the information from FPD file. \r
+ \r
+ <p>Note that surface area parsing is incremental. That means the method will \r
+ only parse the MSA and MBD files if necessary. </p>\r
+ \r
+ @param moduleName the base name of the module\r
+ @return the overrided module surface area information\r
+ @throws Exception\r
+ MSA or MBD is not valid\r
+ **/\r
+ public synchronized static Map<String, XmlObject> getDoc(FpdModuleIdentification fpdModuleId) throws Exception {\r
+ if (parsedModules.containsKey(fpdModuleId)) {\r
+ return parsedModules.get(fpdModuleId);\r
+ }\r
+ Map<String, XmlObject> doc = new HashMap<String, XmlObject>();\r
+ ModuleIdentification moduleId = fpdModuleId.getModule();\r
+ //\r
+ // First part: get the MSA files info\r
+ //\r
+ doc = getNativeMsa(moduleId);\r
+ \r
+ //\r
+ // Second part: put build options\r
+ //\r
+ doc.put("BuildOptions", fpdBuildOptions);\r
+ \r
+ //\r
+ // Third part: get Module info from FPD, such as Library instances, PCDs\r
+ //\r
+ if (fpdModuleSA.containsKey(fpdModuleId)){\r
+ //\r
+ // merge module info in FPD to final Doc\r
+ // For Library Module, do nothing here\r
+ //\r
+ doc.putAll(fpdModuleSA.get(fpdModuleId));\r
+ }\r
+ parsedModules.put(fpdModuleId, doc);\r
+ return doc;\r
+ }\r
+\r
+ public synchronized static Map<String, XmlObject> getDoc(ModuleIdentification moduleId, String arch) throws Exception {\r
+ FpdModuleIdentification fpdModuleId = new FpdModuleIdentification(moduleId, arch);\r
+ return getDoc(fpdModuleId);\r
+ }\r
+ /**\r
+ Query the native MSA information with module base name. \r
+ \r
+ <p>Note that MSA parsing is incremental. That means the method will \r
+ only to parse the MSA files when never parsed before. </p>\r
+ \r
+ @param moduleName the base name of the module\r
+ @return the native MSA information\r
+ @throws Exception\r
+ MSA file is not valid\r
+ **/\r
+ public synchronized static Map<String, XmlObject> getNativeMsa(ModuleIdentification moduleId) throws Exception {\r
+ if (nativeMsa.containsKey(moduleId)) {\r
+ return nativeMsa.get(moduleId);\r
+ }\r
+ File msaFile = getMsaFile(moduleId);\r
+ Map<String, XmlObject> msaMap = getNativeMsa(msaFile);\r
+ nativeMsa.put(moduleId, msaMap);\r
+ return msaMap;\r
+ }\r
+ \r
+ public synchronized static Map<String, XmlObject> getNativeMsa(File msaFile) throws Exception {\r
+ \r
+ try {\r
+ ModuleSurfaceAreaDocument doc = (ModuleSurfaceAreaDocument)XmlObject.Factory.parse(msaFile);\r
+ //\r
+ // Validate File if they accord with XML Schema\r
+ //\r
+// if ( ! doc.validate()){\r
+// throw new Exception("Module Surface Area file [" + msaFile.getPath() + "] is invalid.");\r
+// }\r
+ //\r
+ // parse MSA file\r
+ //\r
+ ModuleSurfaceArea msa= doc.getModuleSurfaceArea();\r
+ Map<String, XmlObject> msaMap = new HashMap<String, XmlObject>();\r
+ msaMap.put("ModuleSurfaceArea", msa);\r
+ msaMap.put("MsaHeader", cloneXmlObject(msa.getMsaHeader(), true));\r
+ msaMap.put("LibraryClassDefinitions", cloneXmlObject(msa.getLibraryClassDefinitions(), true));\r
+ msaMap.put("SourceFiles", cloneXmlObject(msa.getSourceFiles(), true));\r
+ msaMap.put("PackageDependencies", cloneXmlObject(msa.getPackageDependencies(), true));\r
+ msaMap.put("Protocols", cloneXmlObject(msa.getProtocols(), true));\r
+ msaMap.put("PPIs", cloneXmlObject(msa.getPPIs(), true));\r
+ msaMap.put("Guids", cloneXmlObject(msa.getGuids(), true));\r
+ msaMap.put("Externs", cloneXmlObject(msa.getExterns(), true));\r
+ return msaMap;\r
+ }\r
+ catch (Exception ex){\r
+ throw new Exception(ex.getMessage());\r
+ }\r
+ }\r
+ \r
+ public static Map<String, XmlObject> getFpdBuildOptions() {\r
+ Map<String, XmlObject> map = new HashMap<String, XmlObject>();\r
+ map.put("BuildOptions", fpdBuildOptions);\r
+ return map;\r
+ }\r
+ \r
+ public static void setFpdBuildOptions(XmlObject fpdBuildOptions) throws Exception{\r
+ WorkspaceProfile.fpdBuildOptions = cloneXmlObject(fpdBuildOptions, true);\r
+ }\r
+\r
+ public static XmlObject getFpdDynamicPcds() {\r
+ return fpdDynamicPcds;\r
+ }\r
+\r
+ public static void setFpdDynamicPcds(XmlObject fpdDynamicPcds) {\r
+ WorkspaceProfile.fpdDynamicPcds = fpdDynamicPcds;\r
+ }\r
+\r
+ //////////////////////////////////////////////\r
+ //////////////////////////////////////////////\r
+ \r
+ public static Set<ModuleIdentification> getModules(PackageIdentification packageId){\r
+ Spd spd = spdTable.get(packageId);\r
+ if (spd == null ) {\r
+ Set<ModuleIdentification> dummy = new HashSet<ModuleIdentification>();\r
+ return dummy;\r
+ }\r
+ else {\r
+ return spd.getModules();\r
+ }\r
+ }\r
+\r
+ public synchronized static PlatformIdentification getPlatform(String name) throws Exception {\r
+ Iterator iter = platformList.iterator();\r
+ while(iter.hasNext()){\r
+ PlatformIdentification platformId = (PlatformIdentification)iter.next();\r
+ if (platformId.getName().equalsIgnoreCase(name)) {\r
+ WorkspaceProfile.log.info("Platform: " + platformId + platformId.getFpdFile());\r
+ return platformId;\r
+ }\r
+ }\r
+ throw new Exception("Can't find platform [" + name + "] in current workspace. ");\r
+ }\r
+ \r
+ public synchronized static File getPackageFile(PackageIdentification packageId) throws Exception {\r
+ Iterator iter = packageList.iterator();\r
+ while(iter.hasNext()){\r
+ PackageIdentification packageItem = (PackageIdentification)iter.next();\r
+ if (packageItem.equals(packageId)) {\r
+ packageId.setName(packageItem.getName());\r
+ return packageItem.getSpdFile();\r
+ }\r
+ }\r
+ throw new Exception("Can't find " + packageId + " in current workspace. ");\r
+ }\r
+ \r
+ public synchronized static File getModuleFile(ModuleIdentification moduleId) throws Exception {\r
+ PackageIdentification packageId = getPackageForModule(moduleId);\r
+ moduleId.setPackage(packageId);\r
+ Spd spd = spdTable.get(packageId);\r
+ return spd.getModuleFile(moduleId);\r
+ }\r
+ //\r
+ // expanded by FrameworkWizard\r
+ //\r
+ public synchronized static XmlObject getModuleXmlObject(ModuleIdentification moduleId) throws Exception {\r
+ PackageIdentification packageId = getPackageForModule(moduleId);\r
+ moduleId.setPackage(packageId);\r
+ Spd spd = spdTable.get(packageId);\r
+ return spd.msaDocMap.get(moduleId);\r
+ }\r
+ \r
+ public synchronized static XmlObject getPackageXmlObject(PackageIdentification packageId) {\r
+ Spd spd = spdTable.get(packageId);\r
+ if (spd != null){\r
+ return spd.spdDocMap.get("PackageSurfaceArea");\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ public synchronized static Set<PackageIdentification> getPackageList(){\r
+ return packageList;\r
+ }\r
+ ///// remove!!\r
+ private static XmlObject cloneXmlObject(XmlObject object, boolean deep) throws Exception {\r
+ if ( object == null) {\r
+ return null;\r
+ }\r
+ XmlObject result = null;\r
+ try {\r
+ result = XmlObject.Factory.parse(object.getDomNode()\r
+ .cloneNode(deep));\r
+ } catch (Exception ex) {\r
+ throw new Exception(ex.getMessage());\r
+ }\r
+ return result;\r
+ }\r
+\r
+ public static ModuleIdentification getModuleId(String key){\r
+ //\r
+ // Get ModuleGuid, ModuleVersion, PackageGuid, PackageVersion, Arch into string array.\r
+ //\r
+ String[] keyPart = key.split(" ");\r
+ Set<PackageIdentification> spi = WorkspaceProfile.getPackageList();\r
+ Iterator ispi = spi.iterator();\r
+ \r
+ while(ispi.hasNext()) {\r
+ PackageIdentification pi = (PackageIdentification)ispi.next();\r
+ if ( !pi.getGuid().equalsIgnoreCase(keyPart[2])){ \r
+\r
+ continue;\r
+ }\r
+ if (keyPart[3] != null && keyPart[3].length() > 0 && !keyPart[3].equals("null")){\r
+ if(!pi.getVersion().equals(keyPart[3])){\r
+ continue;\r
+ }\r
+ }\r
+ Set<ModuleIdentification> smi = WorkspaceProfile.getModules(pi);\r
+ Iterator ismi = smi.iterator();\r
+ while(ismi.hasNext()) {\r
+ ModuleIdentification mi = (ModuleIdentification)ismi.next();\r
+ if (mi.getGuid().equalsIgnoreCase(keyPart[0])){\r
+ if (keyPart[1] != null && keyPart[1].length() > 0 && !keyPart[1].equals("null")){\r
+ if(!mi.getVersion().equals(keyPart[1])){\r
+ continue;\r
+ }\r
+ }\r
+\r
+ return mi;\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ public static Vector<String> getModuleSupArchs(ModuleIdentification mi) throws Exception {\r
+ Vector<String> vArchs = null;\r
+ ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)getModuleXmlObject(mi);\r
+ if (msa.getModuleDefinitions() == null || msa.getModuleDefinitions().getSupportedArchitectures() == null) {\r
+ return vArchs;\r
+ }\r
+ ListIterator li = msa.getModuleDefinitions().getSupportedArchitectures().listIterator();\r
+ while (li.hasNext()) {\r
+ if (vArchs == null) {\r
+ vArchs = new Vector<String>();\r
+ }\r
+ vArchs.add((String)li.next());\r
+ }\r
+ \r
+ return vArchs;\r
+ }\r
+ \r
+ public static boolean pcdInMsa (String cName, String tsGuid, ModuleIdentification mi) throws Exception {\r
+ ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)getModuleXmlObject(mi);\r
+ if (msa.getPcdCoded() == null || msa.getPcdCoded().getPcdEntryList() == null) {\r
+ return false;\r
+ }\r
+ ListIterator li = msa.getPcdCoded().getPcdEntryList().listIterator();\r
+ while (li.hasNext()) {\r
+ PcdCodedDocument.PcdCoded.PcdEntry msaPcd = (PcdCodedDocument.PcdCoded.PcdEntry)li.next();\r
+ if (msaPcd.getCName().equals(cName) && msaPcd.getTokenSpaceGuidCName().equals(tsGuid)) {\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+}\r
+\r
+final class KeyComparator implements Comparator<String> {\r
+ public int compare(String x, String y) {\r
+ return x.compareToIgnoreCase(y);\r
+ }\r
+ \r
+}\r
+\r