]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
Using Common Definitions. Remove some unused codes.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / PCDAutoGenAction.java
1 /** @file
2 PCDAutoGenAction class.
3
4 This class is to manage how to generate the PCD information into Autogen.c and
5 Autogen.h.
6
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17 package org.tianocore.build.pcd.action;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.tianocore.build.autogen.CommonDefinition;
25 import org.tianocore.build.global.GlobalData;
26 import org.tianocore.build.id.ModuleIdentification;
27 import org.tianocore.pcd.action.ActionMessage;
28 import org.tianocore.pcd.action.BuildAction;
29 import org.tianocore.pcd.entity.MemoryDatabaseManager;
30 import org.tianocore.pcd.entity.Token;
31 import org.tianocore.pcd.entity.UsageIdentification;
32 import org.tianocore.pcd.entity.UsageInstance;
33 import org.tianocore.pcd.exception.BuildActionException;
34
35 /**
36 This class is to manage how to generate the PCD information into Autogen.c
37 and Autogen.h.
38 **/
39 public class PCDAutoGenAction extends BuildAction {
40 ///
41 /// The reference of DBManager in GlobalData class.
42 ///
43 private MemoryDatabaseManager dbManager;
44
45 ///
46 /// The identification for a UsageInstance.
47 ///
48 private UsageIdentification usageId;
49
50 ///
51 /// Whether current autogen is for building library used by current module.
52 ///
53 private boolean isBuildUsedLibrary;
54
55 ///
56 /// One of PEI_PCD_DRIVER, DXE_PCD_DRIVER, NOT_PCD_DRIVER
57 ///
58 private CommonDefinition.PCD_DRIVER_TYPE pcdDriverType;
59
60 ///
61 /// The generated string for header file.
62 ///
63 private String hAutoGenString;
64
65 ///
66 /// The generated string for C code file.
67 ///
68 private String cAutoGenString;
69
70 ///
71 /// The name array of <PcdCoded> in a module.
72 ///
73 private String[] pcdNameArrayInMsa;
74
75 /**
76 Set parameter moduleId
77
78 @param moduleName the module name parameter.
79 **/
80 public void setUsageId(UsageIdentification usageId) {
81 this.usageId = usageId;
82 }
83
84 /**
85 Set paramter pcdDriverType
86
87 @param pcdDriverType the driver type for PCD
88 **/
89 public void setPcdDriverType(CommonDefinition.PCD_DRIVER_TYPE pcdDriverType) {
90 this.pcdDriverType = pcdDriverType;
91 }
92 /**
93 set isBuildUsedLibrary parameter.
94
95 @param isBuildUsedLibrary
96 **/
97 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {
98 this.isBuildUsedLibrary = isBuildUsedLibrary;
99 }
100
101 /**
102 set pcdNameArrayInMsa parameter.
103
104 @param pcdNameArrayInMsa
105 */
106 public void setPcdNameArrayInMsa(String[] pcdNameArrayInMsa) {
107 this.pcdNameArrayInMsa = pcdNameArrayInMsa;
108 }
109
110 /**
111 Get the output of generated string for header file.
112
113 @return the string of header file for PCD
114 **/
115 public String getHAutoGenString() {
116 return hAutoGenString;
117 }
118
119 /**
120 Get the output of generated string for C Code file.
121
122 @return the string of C code file for PCD
123 **/
124 public String getCAutoGenString() {
125 return cAutoGenString;
126 }
127
128
129 /**
130 Construct function
131
132 This function mainly initialize some member variable.
133
134 @param moduleId the identification for module
135 @param arch the architecture for module
136 @param isBuildUsedLibary Is the current module library.
137 @param pcdNameArrayInMsa the pcd name array got from MSA file.
138 @param pcdDriverType one of PEI_PCD_DRIVER, DXE_PCD_DRIVER,
139 NOT_PCD_DRIVER
140 **/
141 public PCDAutoGenAction(ModuleIdentification moduleId,
142 String arch,
143 boolean isBuildUsedLibrary,
144 String[] pcdNameArrayInMsa,
145 CommonDefinition.PCD_DRIVER_TYPE pcdDriverType) {
146 dbManager = null;
147 hAutoGenString = "";
148 cAutoGenString = "";
149
150 setUsageId(new UsageIdentification(moduleId.getName(),
151 moduleId.getGuid(),
152 moduleId.getPackage().getName(),
153 moduleId.getPackage().getGuid(),
154 arch,
155 moduleId.getVersion(),
156 moduleId.getModuleType()));
157 setIsBuildUsedLibrary(isBuildUsedLibrary);
158 setPcdNameArrayInMsa(pcdNameArrayInMsa);
159 setPcdDriverType(pcdDriverType);
160 }
161
162 /**
163 Override function: check the parameter for action class.
164
165 @throws BuildActionException Bad parameter.
166 **/
167 public void checkParameter() {
168 }
169
170 /**
171 Core execution function for this action class.
172
173 All PCD information of this module comes from memory dabase. The collection
174 work should be done before this action execution.
175 Currently, we should generated all PCD information(maybe all dynamic) as array
176 in Pei emulated driver for simulating PCD runtime database.
177
178 @throws BuildActionException Failed to execute this aciton class.
179 **/
180 public void performAction() {
181 ActionMessage.debug(this,
182 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
183
184 dbManager = GlobalData.getPCDMemoryDBManager();
185
186 if(dbManager.getDBSize() == 0) {
187 return;
188 }
189
190 ActionMessage.debug(this,
191 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens.");
192
193 generateAutogenForModule();
194 }
195
196 /**
197 Generate the autogen string for a common module.
198
199 All PCD information of this module comes from memory dabase. The collection
200 work should be done before this action execution.
201 **/
202 private void generateAutogenForModule()
203 {
204 int index, index2;
205 List<UsageInstance> usageInstanceArray, usageContext;
206 String[] guidStringArray = null;
207 String guidStringCName = null;
208 String guidString = null;
209 String moduleName = usageId.moduleName;
210 UsageInstance usageInstance = null;
211 boolean found = false;
212
213 usageInstanceArray = null;
214 if (!isBuildUsedLibrary) {
215 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);
216 MemoryDatabaseManager.UsageInstanceContext = usageInstanceArray;
217 MemoryDatabaseManager.CurrentModuleName = moduleName;
218 } else if ((pcdNameArrayInMsa != null) && (pcdNameArrayInMsa.length > 0)) {
219 usageContext = MemoryDatabaseManager.UsageInstanceContext;
220 //
221 // For building library package, although all module are library, but PCD entries of
222 // these library should be used to autogen.
223 //
224 if (usageContext == null) {
225 usageInstanceArray = dbManager.getUsageInstanceArrayById(usageId);
226 } else {
227 usageInstanceArray = new ArrayList<UsageInstance>();
228
229 //
230 // Try to find all PCD defined in library's PCD in all <PcdEntry> in module's
231 // <ModuleSA> in FPD file.
232 //
233 for (index = 0; index < pcdNameArrayInMsa.length; index++) {
234 found = false;
235 for (index2 = 0; index2 < usageContext.size(); index2 ++) {
236 if (pcdNameArrayInMsa[index].equalsIgnoreCase(usageContext.get(index2).parentToken.cName)) {
237 usageInstanceArray.add(usageContext.get(index2));
238 found = true;
239 break;
240 }
241 }
242
243 if (!found) {
244 //
245 // All library's PCD should instanted in module's <ModuleSA> who
246 // use this library instance. If not, give errors.
247 //
248 throw new BuildActionException (String.format("Module %s using library instance %s; the PCD %s " +
249 "is required by this library instance, but can not be found " +
250 "in the %s's <ModuleSA> in the FPD file!",
251 MemoryDatabaseManager.CurrentModuleName,
252 moduleName,
253 pcdNameArrayInMsa[index],
254 MemoryDatabaseManager.CurrentModuleName
255 ));
256 }
257 }
258 }
259 }
260
261 if (usageInstanceArray == null) {
262 return;
263 }
264
265 //
266 // Generate all PCD entry for a module.
267 //
268 for(index = 0; index < usageInstanceArray.size(); index ++) {
269 usageInstance = usageInstanceArray.get(index);
270 //
271 // Before generate any PCD information into autogen.h/autogen.c for a module,
272 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
273 // PCD in this module the token, they are all reference to TokenSpaceGuid
274 // array.
275 //
276 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
277 guidStringArray = usageInstance.parentToken.tokenSpaceName.split("-");
278 guidStringCName = "_gPcd_TokenSpaceGuid_" +
279 usageInstance.parentToken.tokenSpaceName.replaceAll("-", "_");
280 guidString = String.format("{ 0x%s, 0x%s, 0x%s, {0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s, 0x%s}}",
281 guidStringArray[0],
282 guidStringArray[1],
283 guidStringArray[2],
284 (guidStringArray[3].substring(0, 2)),
285 (guidStringArray[3].substring(2, 4)),
286 (guidStringArray[4].substring(0, 2)),
287 (guidStringArray[4].substring(2, 4)),
288 (guidStringArray[4].substring(4, 6)),
289 (guidStringArray[4].substring(6, 8)),
290 (guidStringArray[4].substring(8, 10)),
291 (guidStringArray[4].substring(10, 12)));
292
293 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");
294 Matcher matcher = pattern.matcher(cAutoGenString + " ");
295 //
296 // Find whether this guid array variable has been generated into autogen.c
297 // For different DyanmicEx pcd token who use same token space guid, the token space
298 // guid array should be only generated once.
299 //
300 if (!matcher.find()) {
301 hAutoGenString += String.format("extern EFI_GUID %s;\r\n", guidStringCName);
302 if (!isBuildUsedLibrary) {
303 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
304 guidStringCName,
305 guidString);
306 }
307 }
308 }
309
310 usageInstance.generateAutoGen(isBuildUsedLibrary);
311 //
312 // For every PCD entry for this module(usage instance), autogen string would
313 // be appand.
314 //
315 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";
316 cAutoGenString += usageInstance.getCAutogenStr();
317 }
318
319 if (pcdDriverType == CommonDefinition.PCD_DRIVER_TYPE.PEI_PCD_DRIVER) {
320 hAutoGenString += MemoryDatabaseManager.PcdPeimHString;
321 cAutoGenString += MemoryDatabaseManager.PcdPeimCString;
322 } else if (pcdDriverType == CommonDefinition.PCD_DRIVER_TYPE.DXE_PCD_DRIVER) {
323 hAutoGenString += MemoryDatabaseManager.PcdDxeHString;
324 cAutoGenString += MemoryDatabaseManager.PcdDxeCString;
325 }
326 }
327 }