]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java
1) remove some dead code from WinNtBusDriver.c
[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.io.File;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.UUID;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.tianocore.build.global.GlobalData;
27 import org.tianocore.build.pcd.entity.MemoryDatabaseManager;
28 import org.tianocore.build.pcd.entity.Token;
29 import org.tianocore.build.pcd.entity.UsageInstance;
30 import org.tianocore.build.pcd.exception.BuildActionException;
31 import org.tianocore.build.pcd.exception.EntityException;
32
33 /** This class is to manage how to generate the PCD information into Autogen.c and
34 Autogen.h.
35 **/
36 public class PCDAutoGenAction extends BuildAction {
37 ///
38 /// The reference of DBManager in GlobalData class.
39 ///
40 private MemoryDatabaseManager dbManager;
41 ///
42 /// The name of module which is analysised currently.
43 ///
44 private String moduleName;
45 ///
46 /// The Guid of module which is analyzed currently.
47 ///
48 private UUID moduleGuid;
49 ///
50 /// The name of package whose module is analysized currently.
51 ///
52 private String packageName;
53 ///
54 /// The Guid of package whose module is analyszed curretnly.
55 ///
56 private UUID packageGuid;
57 ///
58 /// The arch of current module
59 ///
60 private String arch;
61 ///
62 /// The version of current module
63 ///
64 private String version;
65 ///
66 /// Whether current autogen is for building library used by current module.
67 ///
68 private boolean isBuildUsedLibrary;
69 ///
70 /// The generated string for header file.
71 ///
72 private String hAutoGenString;
73 ///
74 /// The generated string for C code file.
75 ///
76 private String cAutoGenString;
77 ///
78 /// The name array of <PcdCoded> in a module.
79 ///
80 private String[] pcdNameArray;
81 /**
82 Set parameter ModuleName
83
84 @param moduleName the module name parameter.
85 **/
86 public void setModuleName(String moduleName) {
87 this.moduleName = moduleName;
88 }
89
90 /**
91 set the moduleGuid parameter.
92
93 @param moduleGuid
94 **/
95 public void setModuleGuid(UUID moduleGuid) {
96 this.moduleGuid = moduleGuid;
97 }
98
99 /**
100 set packageName parameter.
101
102 @param packageName
103 **/
104 public void setPackageName(String packageName) {
105 this.packageName = packageName;
106 }
107
108 /**
109 set packageGuid parameter.
110
111 @param packageGuid
112 **/
113 public void setPackageGuid(UUID packageGuid) {
114 this.packageGuid = packageGuid;
115 }
116
117 /**
118 set Arch parameter.
119
120 @param arch
121 **/
122 public void setArch(String arch) {
123 this.arch = arch;
124 }
125
126 /**
127 set version parameter
128
129 @param version
130 */
131 public void setVersion(String version) {
132 this.version = version;
133 }
134
135 /**
136 set isBuildUsedLibrary parameter.
137
138 @param isBuildUsedLibrary
139 */
140 public void setIsBuildUsedLibrary(boolean isBuildUsedLibrary) {
141 this.isBuildUsedLibrary = isBuildUsedLibrary;
142 }
143 /**
144 set pcdNameArray parameter.
145
146 @param pcdNameArray
147 */
148 public void setPcdNameArray(String[] pcdNameArray) {
149 this.pcdNameArray = pcdNameArray;
150 }
151
152 /**
153 Get the output of generated string for header file.
154
155 @return the string of header file for PCD
156 **/
157 public String OutputH() {
158 return hAutoGenString;
159 }
160
161 /**
162 Get the output of generated string for C Code file.
163
164 @return the string of C code file for PCD
165 **/
166 public String OutputC() {
167 return cAutoGenString;
168 }
169
170 /**
171 Construct function
172
173 This function mainly initialize some member variable.
174
175 @param moduleName Parameter of this action class.
176 @param isEmulatedPCDDriver Parameter of this action class.
177 **/
178 public PCDAutoGenAction(String moduleName,
179 UUID moduleGuid,
180 String packageName,
181 UUID packageGuid,
182 String arch,
183 String version,
184 boolean isBuildUsedLibrary,
185 String[] pcdNameArray) {
186 dbManager = null;
187 hAutoGenString = "";
188 cAutoGenString = "";
189
190 setModuleName(moduleName);
191 setModuleGuid(moduleGuid);
192 setPackageName(packageName);
193 setPackageGuid(packageGuid);
194 setPcdNameArray(pcdNameArray);
195 setArch(arch);
196 setVersion(version);
197 setIsBuildUsedLibrary(isBuildUsedLibrary);
198 }
199
200 /**
201 check the parameter for action class.
202
203 @throws BuildActionException Bad parameter.
204 **/
205 void checkParameter() throws BuildActionException {
206
207 }
208
209 /**
210 Core execution function for this action class.
211
212 All PCD information of this module comes from memory dabase. The collection
213 work should be done before this action execution.
214 Currently, we should generated all PCD information(maybe all dynamic) as array
215 in Pei emulated driver for simulating PCD runtime database.
216
217 @throws BuildActionException Failed to execute this aciton class.
218 **/
219 void performAction() throws BuildActionException {
220 ActionMessage.debug(this,
221 "Starting PCDAutoGenAction to generate autogen.h and autogen.c!...");
222 //
223 // Check the PCD memory database manager is valid.
224 //
225 if(GlobalData.getPCDMemoryDBManager() == null) {
226 throw new BuildActionException("Memory database has not been initlizated!");
227 }
228
229 dbManager = GlobalData.getPCDMemoryDBManager();
230
231 if(dbManager.getDBSize() == 0) {
232 return;
233 }
234
235 ActionMessage.debug(this,
236 "PCD memory database contains " + dbManager.getDBSize() + " PCD tokens");
237
238
239
240 generateAutogenForModule();
241 }
242
243 /**
244 Generate the autogen string for a common module.
245
246 All PCD information of this module comes from memory dabase. The collection
247 work should be done before this action execution.
248 **/
249 private void generateAutogenForModule()
250 {
251 int index, index2;
252 List<UsageInstance> usageInstanceArray, usageContext;
253 String[] guidStringArray = null;
254 String guidStringCName = null;
255 String guidString = null;
256 UsageInstance usageInstance = null;
257
258 if (!isBuildUsedLibrary) {
259 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,
260 moduleGuid,
261 packageName,
262 packageGuid,
263 arch,
264 version);
265 dbManager.UsageInstanceContext = usageInstanceArray;
266 dbManager.CurrentModuleName = moduleName;
267 } else {
268 usageContext = dbManager.UsageInstanceContext;
269 //
270 // For building MDE package, although all module are library, but PCD entries of
271 // these library should be used to autogen.
272 //
273 if (usageContext == null) {
274 usageInstanceArray = dbManager.getUsageInstanceArrayByModuleName(moduleName,
275 moduleGuid,
276 packageName,
277 packageGuid,
278 arch,
279 version);
280 } else {
281 usageInstanceArray = new ArrayList<UsageInstance>();
282 //
283 // Remove PCD entries which are not belong to this library.
284 //
285 for (index = 0; index < usageContext.size(); index++) {
286 if ((pcdNameArray == null) || (pcdNameArray.length == 0)){
287 break;
288 }
289
290 for (index2 = 0; index2 < pcdNameArray.length; index2 ++) {
291 if (pcdNameArray[index2].equalsIgnoreCase(usageContext.get(index).parentToken.cName)) {
292 usageInstanceArray.add(usageContext.get(index));
293 break;
294 }
295 }
296 }
297 }
298 }
299
300 if(usageInstanceArray.size() != 0) {
301 //
302 // Add "#include 'PcdLib.h'" for Header file
303 //
304 hAutoGenString = "#include <MdePkg/Include/Library/PcdLib.h>\r\n";
305 }
306
307 //
308 // Generate all PCD entry for a module.
309 //
310 for(index = 0; index < usageInstanceArray.size(); index ++) {
311 ActionMessage.debug(this,
312 "Module " + moduleName + "'s PCD [" + Integer.toHexString(index) +
313 "]: " + usageInstanceArray.get(index).parentToken.cName);
314 try {
315 usageInstance = usageInstanceArray.get(index);
316 //
317 // Before generate any PCD information into autogen.h/autogen.c for a module,
318 // generate TokenSpaceGuid array variable firstly. For every dynamicEx type
319 // PCD in this module the token, they are all reference to TokenSpaceGuid
320 // array.
321 //
322 if (usageInstanceArray.get(index).modulePcdType == Token.PCD_TYPE.DYNAMIC_EX) {
323 guidStringArray = usageInstance.parentToken.tokenSpaceName.toString().split("-");
324 guidStringCName = "_gPcd_TokenSpaceGuid_" +
325 usageInstance.parentToken.tokenSpaceName.toString().replaceAll("-", "_");
326 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}}",
327 guidStringArray[0],
328 guidStringArray[1],
329 guidStringArray[2],
330 (guidStringArray[3].substring(0, 2)),
331 (guidStringArray[3].substring(2, 4)),
332 (guidStringArray[4].substring(0, 2)),
333 (guidStringArray[4].substring(2, 4)),
334 (guidStringArray[4].substring(4, 6)),
335 (guidStringArray[4].substring(6, 8)),
336 (guidStringArray[4].substring(8, 10)),
337 (guidStringArray[4].substring(10, 12)));
338
339 Pattern pattern = Pattern.compile("(" + guidStringCName + ")+?");
340 Matcher matcher = pattern.matcher(cAutoGenString + " ");
341 //
342 // Find whether this guid array variable has been generated into autogen.c
343 // For different DyanmicEx pcd token who use same token space guid, the token space
344 // guid array should be only generated once.
345 //
346 if (!matcher.find()) {
347 hAutoGenString += String.format("extern EFI_GUID %s;\r\n",
348 guidStringCName);
349 if (!isBuildUsedLibrary) {
350 cAutoGenString += String.format("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID %s = %s;\r\n",
351 guidStringCName,
352 guidString);
353 }
354 }
355 }
356
357 usageInstance.generateAutoGen(isBuildUsedLibrary);
358 //
359 // For every PCD entry for this module(usage instance), autogen string would
360 // be appand.
361 //
362 hAutoGenString += usageInstance.getHAutogenStr() + "\r\n";
363 cAutoGenString += usageInstance.getCAutogenStr();
364
365 } catch(EntityException exp) {
366 throw new BuildActionException("[PCD Autogen Error]: " + exp.getMessage());
367 }
368 }
369
370 //
371 // Work around code, In furture following code should be modified that get
372 // these information from Uplevel Autogen tools.
373 //
374 if (moduleName.equalsIgnoreCase("PcdPeim")) {
375 hAutoGenString += dbManager.PcdPeimHString;
376 cAutoGenString += dbManager.PcdPeimCString;
377 } else if (moduleName.equalsIgnoreCase("PcdDxe")) {
378 hAutoGenString += dbManager.PcdDxeHString;
379 cAutoGenString += dbManager.PcdDxeCString;
380 }
381
382 ActionMessage.debug(this,
383 "Module " + moduleName + "'s PCD header file:\r\n" + hAutoGenString + "\r\n"
384 );
385 ActionMessage.debug(this,
386 "Module " + moduleName + "'s PCD C file:\r\n" + cAutoGenString + "\r\n"
387 );
388 }
389
390 /**
391 Test case function
392
393 @param argv paramter from command line
394 **/
395 public static void main(String argv[]) {
396
397 String WorkSpace = "X:/edk2";
398 String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd";
399 String[] nameArray = null;
400
401 //
402 // At first, CollectPCDAction should be invoked to collect
403 // all PCD information from SPD, MSA, FPD.
404 //
405 CollectPCDAction collectionAction = new CollectPCDAction();
406 GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",
407 WorkSpace);
408
409 try {
410 collectionAction.perform(WorkSpace,
411 logFilePath,
412 ActionMessage.MAX_MESSAGE_LEVEL);
413 } catch(Exception e) {
414 e.printStackTrace();
415 }
416
417 //
418 // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c
419 //
420 PCDAutoGenAction autogenAction = new PCDAutoGenAction("PcdPeim",
421 null,
422 null,
423 null,
424 "IA32",
425 null,
426 true,
427 nameArray);
428 autogenAction.execute();
429
430 System.out.println(autogenAction.OutputH());
431 System.out.println("WQWQWQWQWQ");
432 System.out.println(autogenAction.OutputC());
433 }
434 }