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