]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java
Add version check in both PCD database generation and service driver.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / entity / MemoryDatabaseManager.java
CommitLineData
878ddf1f 1/** @file\r
2 MemoryDatabaseManager class.\r
3\r
4 Database hold all PCD information comes from SPD, MSA, FPD file in memory.\r
5 \r
6Copyright (c) 2006, Intel Corporation\r
7All rights reserved. This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11 \r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16package org.tianocore.build.pcd.entity;\r
17\r
18import java.io.BufferedWriter;\r
19import java.io.File;\r
20import java.io.FileWriter;\r
21import java.io.IOException;\r
22import java.util.ArrayList;\r
99d2c3c4 23import java.util.Collections;\r
24import java.util.Comparator;\r
878ddf1f 25import java.util.HashMap;\r
99d2c3c4 26import java.util.Hashtable;\r
878ddf1f 27import java.util.List;\r
28import java.util.Map;\r
29\r
30import org.tianocore.build.autogen.CommonDefinition;\r
31import org.tianocore.build.pcd.action.ActionMessage;\r
32\r
33/** Database hold all PCD information comes from SPD, MSA, FPD file in memory.\r
34**/\r
35public class MemoryDatabaseManager {\r
36 ///\r
37 /// Memory database. The string "cName + SpaceNameGuid" is primary key.\r
38 /// memory database is in global scope, and it will be used for others PCD tools.\r
39 ///\r
40 private static Map<String, Token> memoryDatabase = null;\r
41 ///\r
42 /// The log file name for dumping memory database.\r
43 ///\r
44 private static String logFileName = null;\r
45\r
99d2c3c4 46 public static String PcdPeimHString = "";\r
32648c62 47 public static String PcdPeimCString = "";\r
48 public static String PcdDxeHString = "";\r
49 public static String PcdDxeCString = "";\r
99d2c3c4 50\r
878ddf1f 51 /**\r
52 Constructure function\r
53 **/\r
54 public MemoryDatabaseManager() {\r
55 //\r
56 // Allocate memory for new database in global scope.\r
57 //\r
58 if (memoryDatabase == null) {\r
59 memoryDatabase = new HashMap<String, Token>();\r
60 }\r
61 }\r
62\r
63 /**\r
64 Get the log file name.\r
65 **/\r
66 public String getLogFileName() {\r
67 return logFileName;\r
68 }\r
69\r
70 /**\r
71 Set parameter log file name.\r
72 \r
73 @param fileName log file name parameter.\r
74 **/\r
75 public void setLogFileName(String fileName) {\r
76 logFileName = fileName;\r
77 }\r
78\r
79 /**\r
80 Judege whether token exists in memory database\r
81 \r
82 @param primaryKey the primaryKey for searching token\r
83 \r
84 @retval TRUE - token already exist in database.\r
85 @retval FALSE - token does not exist in database.\r
86 **/\r
87 public boolean isTokenInDatabase(String primaryKey) {\r
88 return (memoryDatabase.get(primaryKey) != null);\r
89 }\r
90\r
91 /**\r
92 Add a pcd token into memory database.\r
93 \r
94 @param primaryKey the primary key for searching token\r
95 @param token token instance\r
96 **/\r
97 public void addTokenToDatabase(String primaryKey, Token token) {\r
98 memoryDatabase.put(primaryKey, token);\r
99 }\r
100\r
101 /**\r
102 Get a token instance from memory database with primary key.\r
103 \r
104 @param primaryKey the primary key for searching token\r
105 \r
106 @return token instance.\r
107 **/\r
108 public Token getTokenByKey(String primaryKey) {\r
109 return memoryDatabase.get(primaryKey);\r
110 }\r
111\r
112 /**\r
113 Get the number of PCD token record in memory database.\r
114 \r
115 @return the number of PCD token record in memory database.\r
116 **/\r
117 public int getDBSize() {\r
118 return memoryDatabase.size();\r
119 }\r
120\r
121 /**\r
122 Get the token record array contained all PCD token in memory database.\r
123 \r
124 @return the token record array contained all PCD token in memory database.\r
125 **/\r
126 public Token[] getRecordArray() {\r
127 Token[] tokenArray = null;\r
128 Object[] dataArray = null;\r
129 Map.Entry entry = null;\r
130 int index = 0;\r
131\r
132 if (memoryDatabase == null) {\r
133 return null;\r
134 }\r
135\r
136 dataArray = memoryDatabase.entrySet().toArray();\r
137 tokenArray = new Token[memoryDatabase.size()];\r
138 for (index = 0; index < memoryDatabase.size(); index ++) {\r
139 entry =(Map.Entry) dataArray [index];\r
140 tokenArray[index] =(Token) entry.getValue();\r
141 }\r
142\r
143 return tokenArray;\r
144 }\r
145\r
99d2c3c4 146\r
147 private ArrayList getDynamicRecordArray() {\r
148 Token[] tokenArray = getRecordArray();\r
149 int index = 0;\r
150 int count = 0;\r
151 ArrayList al = new ArrayList();\r
152\r
153 for (index = 0; index < tokenArray.length; index++) {\r
154 if (tokenArray[index].pcdType == Token.PCD_TYPE.DYNAMIC ||\r
155 tokenArray[index].pcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
156 al.add(tokenArray[index]);\r
157 }\r
158 }\r
159\r
160 return al;\r
161 }\r
162\r
163\r
878ddf1f 164 /**\r
99d2c3c4 165 Get the token record array contained all PCD token referenced by PEI phase.\r
166 The output array is sorted based on descending order of the size of alignment for each feilds.\r
167\r
168 @return the token record array contained all PCD token referenced in PEI phase.\r
169 **/\r
170 public void getTwoPhaseDynamicRecordArray(ArrayList<Token> pei, ArrayList<Token> dxe) {\r
171 int usageInstanceIndex = 0;\r
172 int index = 0;\r
173 ArrayList tokenArrayList = getDynamicRecordArray();\r
174 List<UsageInstance> usageInstanceArray = null;\r
175 UsageInstance usageInstance = null;\r
176\r
32648c62 177 //pei = new ArrayList<Token>();\r
178 //dxe = new ArrayList<Token>();\r
99d2c3c4 179\r
180 for (index = 0; index < tokenArrayList.size(); index++) {\r
181 boolean found = false;\r
182 Token token = (Token) tokenArrayList.get(index);\r
183 if (token.producers != null) {\r
184 usageInstanceArray = token.producers;\r
185 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex++) {\r
186 usageInstance = (UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
187 if (CommonDefinition.isPeiPhaseComponent(usageInstance.componentType)) {\r
188 pei.add(token);\r
189 found = true;\r
190 break;\r
191 }\r
192 }\r
193\r
194 }\r
195 if (!found) {\r
196 if (token.consumers != null) {\r
197 usageInstanceArray = token.consumers;\r
198 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {\r
199 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
200 if (CommonDefinition.isPeiPhaseComponent(usageInstance.componentType)) {\r
201 pei.add(token);\r
32648c62 202 found = true;\r
99d2c3c4 203 break;\r
204 }\r
205 }\r
206 }\r
207 }\r
208\r
32648c62 209 //\r
210 // If no PEI components reference the PCD entry, we insert it to DXE list\r
211 //\r
212 if (!found) {\r
213 dxe.add(token);\r
214 }\r
99d2c3c4 215 }\r
216\r
32648c62 217 return;\r
99d2c3c4 218 }\r
219\r
32648c62 220 /**\r
878ddf1f 221 Get all PCD record for a module according to module's name.\r
222 \r
223 @param moduleName the name of module.\r
224 \r
225 @return all usage instance for this module in memory database.\r
226 **/\r
227 public List<UsageInstance> getUsageInstanceArrayByModuleName(String moduleName) {\r
228 Token[] tokenArray = null;\r
229 int recordIndex = 0; \r
230 int usageInstanceIndex = 0;\r
231 List<UsageInstance> usageInstanceArray = null;\r
232 UsageInstance usageInstance = null;\r
233 List<UsageInstance> returnArray = new ArrayList<UsageInstance>();\r
234\r
235 tokenArray = getRecordArray();\r
236\r
237 //\r
238 // Loop to find all PCD record related to current module\r
239 //\r
240 for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {\r
241 if (tokenArray[recordIndex].producers != null) {\r
242 usageInstanceArray = tokenArray[recordIndex].producers;\r
243 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {\r
244 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
245 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {\r
246 returnArray.add(usageInstance);\r
247 }\r
248 }\r
249 }\r
250\r
251 if (tokenArray[recordIndex].consumers != null) {\r
252 usageInstanceArray = tokenArray[recordIndex].consumers;\r
253 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {\r
254 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);\r
255 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {\r
256 returnArray.add(usageInstance);\r
257 }\r
258 }\r
259 }\r
260 }\r
261\r
262 if (returnArray.size() == 0) {\r
263 ActionMessage.warning(this, "Can *not* find any usage instance for " + moduleName + " !");\r
264 }\r
265\r
266 return returnArray;\r
267 }\r
268\r
269 /**\r
270 Get all modules name who contains PCD information\r
271 \r
272 @return Array for module name\r
273 **/\r
274 public List<String> getAllModuleArray()\r
275 {\r
276 int indexToken = 0;\r
277 int usageIndex = 0;\r
278 int moduleIndex = 0;\r
279 Token[] tokenArray = null;\r
280 List<String> moduleNames = new ArrayList<String>();\r
281 UsageInstance usageInstance = null;\r
282 boolean bFound = false;\r
283\r
284 tokenArray = this.getRecordArray();\r
285 //\r
286 // Find all producer usage instance for retrieving module's name\r
287 //\r
288 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {\r
289 for (usageIndex = 0; usageIndex < tokenArray[indexToken].producers.size(); usageIndex ++) {\r
290 usageInstance = tokenArray[indexToken].producers.get(usageIndex);\r
291 bFound = false;\r
292 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {\r
293 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.moduleName)) {\r
294 bFound = true;\r
295 break;\r
296 }\r
297 }\r
298 if (!bFound) {\r
299 moduleNames.add(usageInstance.moduleName);\r
300 }\r
301 }\r
302 }\r
303\r
304 //\r
305 // Find all consumer usage instance for retrieving module's name\r
306 //\r
307 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {\r
308 for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {\r
309 usageInstance = tokenArray[indexToken].consumers.get(usageIndex);\r
310 bFound = false;\r
311 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {\r
312 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.moduleName)) {\r
313 bFound = true;\r
314 break;\r
315 }\r
316 }\r
317 if (!bFound) {\r
318 moduleNames.add(usageInstance.moduleName);\r
319 }\r
320 }\r
321 }\r
322 return moduleNames;\r
323 }\r
324\r
325 /**\r
326 Dump all PCD record into file for reviewing.\r
327 **/\r
328 public void DumpAllRecords() {\r
329 BufferedWriter bWriter = null;\r
330 Object[] tokenArray = null;\r
331 Map.Entry entry = null;\r
332 Token token = null;\r
333 int index = 0;\r
334 int usageIndex = 0;\r
335 UsageInstance usageInstance = null;\r
336 String inheritString = null;\r
337 String componentTypeName = null;\r
338\r
339 try {\r
340 bWriter = new BufferedWriter(new FileWriter(new File(logFileName)));\r
341 tokenArray = memoryDatabase.entrySet().toArray();\r
342 for (index = 0; index < memoryDatabase.size(); index ++) {\r
343 entry =(Map.Entry) tokenArray [index];\r
344 token =(Token) entry.getValue();\r
345 bWriter.write("****** token [" + Integer.toString(index) + "] ******\r\n");\r
346 bWriter.write(" cName:" + token.cName + "\r\n");\r
347 for (usageIndex = 0; usageIndex < token.producers.size(); usageIndex ++) {\r
348 usageInstance =(UsageInstance)token.producers.get(usageIndex);\r
349 componentTypeName = CommonDefinition.getComponentTypeString(usageInstance.componentType);\r
350\r
351 if (usageInstance.isInherit) {\r
352 inheritString = "Inherit";\r
353 } else {\r
354 inheritString = "";\r
355 }\r
356 bWriter.write(String.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",\r
357 usageIndex,\r
358 componentTypeName,\r
359 usageInstance.moduleName,\r
360 usageInstance.packageName,\r
361 inheritString\r
362 )\r
363 );\r
364 }\r
365 for (usageIndex = 0; usageIndex < token.consumers.size(); usageIndex ++) {\r
366 usageInstance =(UsageInstance)token.consumers.get(usageIndex);\r
367 componentTypeName = CommonDefinition.getComponentTypeString(usageInstance.componentType);\r
368 if (usageInstance.isInherit) {\r
369 inheritString = "Inherit";\r
370 } else {\r
371 inheritString = "";\r
372 }\r
373 bWriter.write(String.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",\r
374 usageIndex,\r
375 componentTypeName,\r
376 usageInstance.moduleName,\r
377 usageInstance.packageName,\r
378 inheritString\r
379 )\r
380 );\r
381 }\r
382 }\r
383 bWriter.close();\r
384 } catch (IOException exp) {\r
385 ActionMessage.warning(this, "Failed to open database log file: " + logFileName);\r
386 }\r
387 }\r
388}\r