]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java
Add folder for common PcdTools classes.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / entity / MemoryDatabaseManager.java
1 /** @file
2 MemoryDatabaseManager class.
3
4 Database hold all PCD information comes from SPD, MSA, FPD file in memory.
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.pcd.entity;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.UUID;
23
24 import org.tianocore.build.id.ModuleIdentification;
25 import org.tianocore.build.pcd.exception.EntityException;
26
27 /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.
28 **/
29 public class MemoryDatabaseManager {
30 ///
31 /// Memory database. The string "cName + SpaceNameGuid" is primary key.
32 /// memory database is in global scope, and it will be used for others PCD tools.
33 ///
34 private static Map<String, Token> memoryDatabase = null;
35
36 ///
37 /// Before build a module, the used libary will be build firstly, the PCD of these
38 /// libarry is inheritted by the module, so stored module's PCD information as PCD
39 /// context of building libary.
40 ///
41 public static List<UsageInstance> UsageInstanceContext = null;
42
43 ///
44 /// Current module name, if now is buiding library, this value indicate this library
45 /// is for building what module.
46 ///
47 public static String CurrentModuleName = null;
48
49 ///
50 /// String for PCD PEIM and DXE autogen file
51 ///
52 public static String PcdPeimHString = "";
53 public static String PcdPeimCString = "";
54 public static String PcdDxeHString = "";
55 public static String PcdDxeCString = "";
56
57 /**
58 Constructure function
59 **/
60 public MemoryDatabaseManager() {
61 //
62 // Allocate memory for new database in global scope.
63 //
64 if (memoryDatabase == null) {
65 memoryDatabase = new HashMap<String, Token>();
66 }
67 }
68
69 /**
70 Judege whether token exists in memory database
71
72 @param primaryKey the primaryKey for searching token
73
74 @retval TRUE - token already exist in database.
75 @retval FALSE - token does not exist in database.
76 **/
77 public boolean isTokenInDatabase(String primaryKey) {
78 return (memoryDatabase.get(primaryKey) != null);
79 }
80
81 /**
82 Add a pcd token into memory database.
83
84 @param primaryKey the primary key for searching token
85 @param token token instance
86 **/
87 public void addTokenToDatabase(String primaryKey, Token token) {
88 memoryDatabase.put(primaryKey, token);
89 }
90
91 /**
92 Get a token instance from memory database with primary key.
93
94 @param primaryKey the primary key for searching token
95
96 @return token instance.
97 **/
98 public Token getTokenByKey(String primaryKey) {
99 return memoryDatabase.get(primaryKey);
100 }
101
102 /**
103 Get the number of PCD token record in memory database.
104
105 @return the number of PCD token record in memory database.
106 **/
107 public int getDBSize() {
108 return memoryDatabase.size();
109 }
110
111 /**
112 Get the token record array contained all PCD token in memory database.
113
114 @return the token record array contained all PCD token in memory database.
115 **/
116 public Token[] getRecordArray() {
117 Token[] tokenArray = null;
118 Object[] dataArray = null;
119 Map.Entry entry = null;
120 int index = 0;
121
122 if (memoryDatabase == null) {
123 return null;
124 }
125
126 dataArray = memoryDatabase.entrySet().toArray();
127 tokenArray = new Token[memoryDatabase.size()];
128 for (index = 0; index < memoryDatabase.size(); index ++) {
129 entry =(Map.Entry) dataArray [index];
130 tokenArray[index] =(Token) entry.getValue();
131 }
132
133 return tokenArray;
134 }
135
136 /**
137 Get record array only contains DYNAMIC or DYNAMIC_EX type PCD.
138
139 @return ArrayList
140 */
141 private ArrayList getDynamicRecordArray() {
142 Token[] tokenArray = getRecordArray();
143 int index = 0;
144 ArrayList<Token> al = new ArrayList<Token>();
145
146 for (index = 0; index < tokenArray.length; index++) {
147 if (tokenArray[index].isDynamicPCD) {
148 al.add(tokenArray[index]);
149 }
150 }
151
152 return al;
153 }
154
155
156 /**
157 Get the token record array contained all PCD token referenced by PEI phase.
158 The output array is sorted based on descending order of the size of alignment for each feilds.
159
160 @return the token record array contained all PCD token referenced in PEI phase.
161 @throws EntityException
162 **/
163 public void getTwoPhaseDynamicRecordArray(ArrayList<Token> pei, ArrayList<Token> dxe)
164 throws EntityException {
165 int usageInstanceIndex = 0;
166 int index = 0;
167 ArrayList tokenArrayList = getDynamicRecordArray();
168 Object[] usageInstanceArray = null;
169 UsageInstance usageInstance = null;
170
171 //pei = new ArrayList<Token>();
172 //dxe = new ArrayList<Token>();
173
174 for (index = 0; index < tokenArrayList.size(); index++) {
175 boolean found = false;
176 Token token = (Token) tokenArrayList.get(index);
177 if (token.consumers != null) {
178 usageInstanceArray = token.consumers.entrySet().toArray();
179 for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) {
180 usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue());
181 if (usageInstance.isPeiPhaseComponent()) {
182 pei.add(token);
183 found = true;
184 break;
185 }
186 }
187 }
188
189 //
190 // If no PEI components reference the PCD entry,
191 // we check if it is referenced in DXE driver.
192 //
193 if (!found) {
194 if (token.consumers != null) {
195 usageInstanceArray = token.consumers.entrySet().toArray();
196 for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) {
197 usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue());
198 if (usageInstance.isDxePhaseComponent()) {
199 dxe.add(token);
200 found = true;
201 break;
202 }
203 }
204 }
205
206 if (!found) {
207 if (token.isDynamicPCD && token.consumers.size() == 0) {
208 dxe.add(token);
209 } else {
210 //
211 // We only support Dynamice(EX) type for PEI and DXE phase.
212 // If it is not referenced in either PEI or DXE, throw exception now.
213 //
214 throw new EntityException("[PCD tool Internal Error] Dynamic(EX) PCD Entries are referenced in module that is not in PEI phase nor in DXE phase.");
215 }
216 }
217 }
218 }
219
220 return;
221 }
222
223 /**
224 Get all PCD record for a module according to module's name, module's GUID,
225 package name, package GUID, arch, version information.
226
227 @param moduleId the id of module.
228 @param arch the architecture
229
230 @return all usage instance for this module in memory database.
231 **/
232 public List<UsageInstance> getUsageInstanceArrayByModuleName(ModuleIdentification moduleId,
233 String arch) {
234
235 String primaryKey = UsageInstance.getPrimaryKey(moduleId, arch);
236
237 return getUsageInstanceArrayByKeyString(primaryKey);
238 }
239
240 /**
241 Get all PCD token for a usage instance according to primary key.
242
243 @param primaryKey the primary key of usage instance.
244
245 @return List<UsageInstance>
246 */
247 public List<UsageInstance> getUsageInstanceArrayByKeyString(String primaryKey) {
248 Token[] tokenArray = null;
249 int recordIndex = 0;
250 UsageInstance usageInstance = null;
251 List<UsageInstance> returnArray = new ArrayList<UsageInstance>();
252
253 tokenArray = getRecordArray();
254
255 //
256 // Loop to find all PCD record related to current module
257 //
258 for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {
259 if (tokenArray[recordIndex].consumers.size() != 0) {
260 usageInstance = tokenArray[recordIndex].consumers.get(primaryKey);
261 if (usageInstance != null) {
262 returnArray.add(usageInstance);
263 }
264 }
265 }
266
267 return returnArray;
268 }
269
270 /**
271 Get all modules name who contains PCD information
272
273 @return Array for module name
274 **/
275 public List<String> getAllModuleArray()
276 {
277 int indexToken = 0;
278 int usageIndex = 0;
279 int moduleIndex = 0;
280 Token[] tokenArray = null;
281 Object[] usageInstanceArray = null;
282 List<String> moduleNames = new ArrayList<String>();
283 UsageInstance usageInstance = null;
284 boolean bFound = false;
285
286 tokenArray = getRecordArray();
287 //
288 // Find all consumer usage instance for retrieving module's name
289 //
290 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {
291 usageInstanceArray = tokenArray[indexToken].consumers.entrySet().toArray();
292 for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {
293 usageInstance = (UsageInstance)((Map.Entry)usageInstanceArray[usageIndex]).getValue();
294 bFound = false;
295 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {
296 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.getPrimaryKey())) {
297 bFound = true;
298 break;
299 }
300 }
301 if (!bFound) {
302 moduleNames.add(usageInstance.getPrimaryKey());
303 }
304 }
305 }
306 return moduleNames;
307 }
308 }