]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java
Modify PCD tool according to final PCD schema modification.
[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.io.BufferedWriter;
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.Hashtable;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30
31 import org.tianocore.build.pcd.action.ActionMessage;
32
33 /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.
34 **/
35 public class MemoryDatabaseManager {
36 ///
37 /// Memory database. The string "cName + SpaceNameGuid" is primary key.
38 /// memory database is in global scope, and it will be used for others PCD tools.
39 ///
40 private static Map<String, Token> memoryDatabase = null;
41
42 ///
43 /// Before build a module, the used libary will be build firstly, the PCD of these
44 /// libarry is inheritted by the module, so stored module's PCD information as PCD
45 /// context of building libary.
46 ///
47 public static List<UsageInstance> UsageInstanceContext = null;
48 ///
49 ///
50 ///
51 public static String CurrentModuleName = null;
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 int count = 0;
145 ArrayList<Token> al = new ArrayList<Token>();
146
147 for (index = 0; index < tokenArray.length; index++) {
148 if (tokenArray[index].isDynamicPCD) {
149 al.add(tokenArray[index]);
150 }
151 }
152
153 return al;
154 }
155
156
157 /**
158 Get the token record array contained all PCD token referenced by PEI phase.
159 The output array is sorted based on descending order of the size of alignment for each feilds.
160
161 @return the token record array contained all PCD token referenced in PEI phase.
162 **/
163 public void getTwoPhaseDynamicRecordArray(ArrayList<Token> pei, ArrayList<Token> dxe) {
164 int usageInstanceIndex = 0;
165 int index = 0;
166 ArrayList tokenArrayList = getDynamicRecordArray();
167 Object[] usageInstanceArray = null;
168 UsageInstance usageInstance = null;
169
170 //pei = new ArrayList<Token>();
171 //dxe = new ArrayList<Token>();
172
173 for (index = 0; index < tokenArrayList.size(); index++) {
174 boolean found = false;
175 Token token = (Token) tokenArrayList.get(index);
176 if (token.consumers != null) {
177 usageInstanceArray = token.consumers.entrySet().toArray();
178 for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) {
179 usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue());
180 if (usageInstance.isPeiPhaseComponent()) {
181 pei.add(token);
182 found = true;
183 break;
184 }
185 }
186 }
187
188 // If no PEI components reference the PCD entry, we insert it to DXE list
189 //
190 if (!found) {
191 dxe.add(token);
192 }
193 }
194
195 return;
196 }
197
198 /**
199 Get all PCD record for a module according to module's name, module's GUID,
200 package name, package GUID, arch, version information.
201
202 @param moduleName the name of module.
203
204 @return all usage instance for this module in memory database.
205 **/
206 public List<UsageInstance> getUsageInstanceArrayByModuleName(String moduleName,
207 UUID moduleGuid,
208 String packageName,
209 UUID packageGuid,
210 String arch,
211 String version) {
212
213 String primaryKey = UsageInstance.getPrimaryKey(moduleName,
214 moduleGuid,
215 packageName,
216 packageGuid,
217 arch,
218 version);
219
220 return getUsageInstanceArrayByKeyString(primaryKey);
221 }
222
223 /**
224 Get all PCD token for a usage instance according to primary key.
225
226 @param primaryKey the primary key of usage instance.
227
228 @return List<UsageInstance>
229 */
230 public List<UsageInstance> getUsageInstanceArrayByKeyString(String primaryKey) {
231 Token[] tokenArray = null;
232 int recordIndex = 0;
233 UsageInstance usageInstance = null;
234 List<UsageInstance> returnArray = new ArrayList<UsageInstance>();
235
236 tokenArray = getRecordArray();
237
238 //
239 // Loop to find all PCD record related to current module
240 //
241 for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {
242 if (tokenArray[recordIndex].consumers.size() != 0) {
243 usageInstance = tokenArray[recordIndex].consumers.get(primaryKey);
244 if (usageInstance != null) {
245 returnArray.add(usageInstance);
246 }
247 }
248 }
249
250 return returnArray;
251 }
252
253 /**
254 Get all modules name who contains PCD information
255
256 @return Array for module name
257 **/
258 public List<String> getAllModuleArray()
259 {
260 int indexToken = 0;
261 int usageIndex = 0;
262 int moduleIndex = 0;
263 Token[] tokenArray = null;
264 Object[] usageInstanceArray = null;
265 List<String> moduleNames = new ArrayList<String>();
266 UsageInstance usageInstance = null;
267 boolean bFound = false;
268
269 tokenArray = getRecordArray();
270 //
271 // Find all consumer usage instance for retrieving module's name
272 //
273 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {
274 usageInstanceArray = tokenArray[indexToken].consumers.entrySet().toArray();
275 for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {
276 usageInstance = (UsageInstance)((Map.Entry)usageInstanceArray[usageIndex]).getValue();
277 bFound = false;
278 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {
279 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.getPrimaryKey())) {
280 bFound = true;
281 break;
282 }
283 }
284 if (!bFound) {
285 moduleNames.add(usageInstance.getPrimaryKey());
286 }
287 }
288 }
289 return moduleNames;
290 }
291 }