8772385e4b47ae7ed5ed255f6602aea007b94b95
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / global / Spd.java
1 /** @file
2 Spd class.
3
4 This class is to generate a global table for the content of spd file.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16 package org.tianocore.build.global;
17
18 import java.io.File;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.xmlbeans.XmlObject;
26 import org.tianocore.build.id.ModuleIdentification;
27 import org.tianocore.build.id.PackageIdentification;
28
29 /**
30
31 This class is to generate a global table for the content of spd file.
32
33 **/
34 public class Spd {
35 ///
36 ///
37 ///
38 Map<ModuleIdentification, File> msaInfo = new HashMap<ModuleIdentification, File>();
39
40 ///
41 /// Map of module info.
42 /// Key : moduletype
43 /// Value: moduletype related include file
44 ///
45 Map<String, String> packageHeaderInfo = new HashMap<String, String>();
46
47 ///
48 /// Map of PPI info.
49 /// Key : PPI name
50 /// value: String[] a. PPI C_NAME; b. PPI GUID;
51 ///
52 Map<String, String[]> ppiInfo = new HashMap<String, String[]>();
53
54 ///
55 /// Map of Protocol info.
56 /// Key : Protocol name
57 /// value: String[] a. Protocol C_NAME; b. Protocol GUID;
58 ///
59 Map<String, String[]> protocolInfo = new HashMap<String, String[]>();
60
61 ///
62 /// Map of Guid info.
63 /// Key : Guid name
64 /// value: String[] a. Guid C_NAME; b. Guid's GUID;
65 ///
66 Map<String, String[]> guidInfo = new HashMap<String, String[]>();
67
68 ///
69 /// Map of Guid info
70 /// Key: GuidCName
71 /// value: String Guid's GUID
72 ///
73 Map<String, String> guidCnameInfo = new HashMap<String, String>();
74
75 /// Map of library class and its exposed header file.
76 /// Key : library class name
77 /// value : library class corresponding header file
78 ///
79 Map<String, String[]> libClassHeaderList = new HashMap<String, String[]>();
80
81 ///
82 /// Package path.
83 ///
84 PackageIdentification packageId;
85
86 /**
87 Constructor function
88
89 This function mainly initialize some member variables.
90 **/
91 Spd(File packageFile) throws BuildException {
92 //
93 // If specified package file not exists
94 //
95 if ( ! packageFile.exists()) {
96 throw new BuildException("Package file [" + packageFile.getPath() + "] does not exist!");
97 }
98 try {
99 XmlObject spdDoc = XmlObject.Factory.parse(packageFile);
100 //
101 // Verify SPD file, if is invalid, throw Exception
102 //
103 if (! spdDoc.validate()) {
104 throw new BuildException("Package Surface Area file [" + packageFile.getPath() + "] format is invalid!");
105 }
106 // We can change Map to XmlObject
107 Map<String, XmlObject> spdDocMap = new HashMap<String, XmlObject>();
108 spdDocMap.put("PackageSurfaceArea", spdDoc);
109 SurfaceAreaQuery.setDoc(spdDocMap);
110 //
111 //
112 //
113 packageId = SurfaceAreaQuery.getSpdHeader();
114 packageId.setSpdFile(packageFile);
115
116 //
117 // initialize Msa Files
118 // MSA file is absolute file path
119 //
120 String[] msaFilenames = SurfaceAreaQuery.getSpdMsaFile();
121 for (int i = 0; i < msaFilenames.length; i++){
122 File msaFile = new File(packageId.getPackageDir() + File.separatorChar + msaFilenames[i]);
123 Map<String, XmlObject> msaDoc = GlobalData.getNativeMsa( msaFile );
124 SurfaceAreaQuery.push(msaDoc);
125 ModuleIdentification moduleId = SurfaceAreaQuery.getMsaHeader();
126 SurfaceAreaQuery.pop();
127 moduleId.setPackage(packageId);
128 moduleId.setMsaFile(msaFile);
129 if (msaInfo.containsKey(moduleId)) {
130 throw new BuildException("Found two modules with the same GUID and Version in package " + packageId + ".\nThey are module [" + msaInfo.get(moduleId) + "] and MSA file [" + msaFile + "]!");
131 }
132 msaInfo.put(moduleId, msaFile);
133 }
134
135 //
136 // initialize Package header files
137 //
138 Map<String, String> packageHeaders = SurfaceAreaQuery.getSpdPackageHeaderFiles();
139 Set keys = packageHeaders.keySet();
140 Iterator iter = keys.iterator();
141 while (iter.hasNext()){
142 String moduleType = (String)iter.next();
143 String header = packageId.getPackageRelativeDir() + File.separatorChar + packageHeaders.get(moduleType);
144
145 //
146 // Change path seperator to system-dependent path separator
147 //
148 File file = new File (header);
149 header = file.getPath();
150 packageHeaderInfo.put(moduleType, header);
151 }
152
153 //
154 // initialize Guid Info
155 //
156 guidInfo.putAll(SurfaceAreaQuery.getSpdGuid());
157
158 //
159 // For Pcd get TokenSpaceGuid
160 //
161 Set<String> key = guidInfo.keySet();
162 Iterator item = key.iterator();
163 String [] nameValue = new String[2];
164 while(item.hasNext()){
165 nameValue = guidInfo.get(item.next());
166 guidCnameInfo.put(nameValue[0], nameValue[1]);
167 }
168
169 //
170 // initialize PPI info
171 //
172 ppiInfo.putAll(SurfaceAreaQuery.getSpdPpi());
173
174 //
175 // initialize Protocol info
176 //
177 protocolInfo.putAll(SurfaceAreaQuery.getSpdProtocol());
178
179 //
180 // initialize library class declaration
181 //
182 Map<String, String[]> libraryClassHeaders = SurfaceAreaQuery.getSpdLibraryClasses();
183 keys = libraryClassHeaders.keySet();
184 iter = keys.iterator();
185 while (iter.hasNext()){
186 String libraryClassName = (String)iter.next();
187 String[] headerFiles = libraryClassHeaders.get(libraryClassName);
188 for (int i = 0; i < headerFiles.length; i++){
189 headerFiles[i] = packageId.getPackageRelativeDir() + File.separatorChar + headerFiles[i];
190
191 //
192 // Change path separator to system system-dependent path separator.
193 //
194 File file = new File (headerFiles[i]);
195 headerFiles[i] = file.getPath();
196 }
197 libClassHeaderList.put(libraryClassName, headerFiles);
198 }
199 }
200 catch (Exception e) {
201 e.setStackTrace(e.getStackTrace());
202 throw new BuildException("Parse of the package description file [" + packageId.getSpdFile() + "] failed!\n"
203 + e.getMessage());
204 }
205 }
206
207 public PackageIdentification getPackageId() {
208 return packageId;
209 }
210
211 public File getModuleFile(ModuleIdentification moduleId) {
212 return msaInfo.get(moduleId);
213 }
214
215 public Set<ModuleIdentification> getModules(){
216 return msaInfo.keySet();
217 }
218
219 /**
220 return two value {CName, Guid}. If not found, return null.
221 **/
222 public String[] getPpi(String ppiName) {
223 return ppiInfo.get(ppiName);
224 }
225
226 /**
227 return two value {CName, Guid}. If not found, return null.
228 **/
229 public String[] getProtocol(String protocolName) {
230 return protocolInfo.get(protocolName);
231 }
232
233 /**
234 return two value {CName, Guid}. If not found, return null.
235 **/
236 public String[] getGuid(String guidName) {
237 return guidInfo.get(guidName);
238 }
239
240 /**
241 * return Guid Value.
242 */
243 public String getGuidFromCname(String cName){
244 return guidCnameInfo.get(cName);
245 }
246
247 /**
248 getLibClassInclude
249
250 This function is to get the library exposed header file name according
251 library class name.
252
253 @param libName Name of library class
254 @return Name of header file
255 **/
256 String[] getLibClassIncluder(String libName) {
257 return libClassHeaderList.get(libName);
258 }
259
260 /**
261 getModuleTypeIncluder
262
263 This function is to get the header file name from module info map
264 according to module type.
265
266 @param moduleType Module type.
267 @return Name of header file.
268 **/
269 String getPackageIncluder(String moduleType) {
270 return packageHeaderInfo.get(moduleType);
271 }
272
273
274 }