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