]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Java/Source/GenBuild/org/tianocore/build/autogen/AutogenLibOrder.java
Fixed the issue in the library instance constructor calling sequence
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / autogen / AutogenLibOrder.java
... / ...
CommitLineData
1/**@file\r
2 AutogenLibOrder class.\r
3\r
4 This class is to reorder library instance sequence according to library \r
5 dependence.\r
6 \r
7 Copyright (c) 2006, Intel Corporation\r
8 All rights reserved. This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12 \r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16 **/\r
17package org.tianocore.build.autogen;\r
18\r
19import java.util.ArrayList;\r
20import java.util.HashMap;\r
21import java.util.Iterator;\r
22import java.util.LinkedList;\r
23import java.util.List;\r
24import java.util.Map;\r
25import java.util.Stack;\r
26import java.util.HashSet;\r
27\r
28import org.apache.xmlbeans.XmlObject;\r
29import org.tianocore.build.exception.AutoGenException;\r
30import org.tianocore.build.global.GlobalData;\r
31import org.tianocore.build.global.SurfaceAreaQuery;\r
32import org.tianocore.build.id.ModuleIdentification;\r
33import org.tianocore.common.exception.EdkException;\r
34import org.tianocore.common.logger.EdkLog;\r
35/**\r
36 This class This class is to reorder library instance sequence according to\r
37 library dependence.\r
38**/\r
39public class AutogenLibOrder {\r
40 ///\r
41 /// The map of library class and its library instance.\r
42 ///\r
43 private Map<String, ModuleIdentification> libClassMap = new HashMap<String, ModuleIdentification>();\r
44\r
45 ///\r
46 /// The map of library instance and its implemet libraryClass.\r
47 ///\r
48 private Map<ModuleIdentification, String[]> libInstanceMap = new HashMap<ModuleIdentification, String[]>();\r
49\r
50 ///\r
51 /// List of library instance. It is String[3] list, String[0] is libraryName,\r
52 /// String[1] is libraryConstructor name, String[2] is libDestructor name.\r
53 ///\r
54 private List<LibraryInstanceNode> libInstanceList = new ArrayList<LibraryInstanceNode>();\r
55 \r
56 /**\r
57 Constructor function\r
58 \r
59 This function mainly initialize some member variable.\r
60 \r
61 @param libraryList List of the library instance.\r
62 @throws Exception\r
63 **/\r
64 AutogenLibOrder(ModuleIdentification[] libraryList, String arch) throws EdkException {\r
65 LibraryInstanceNode libInstanceNode;\r
66 String[] libClassDeclList = null;\r
67 String[] libClassConsmList = null;\r
68 \r
69 for (int i = 0; i < libraryList.length; i++) {\r
70 //\r
71 // Add libraryInstance in to libInstanceList.\r
72 // \r
73 Map<String, XmlObject> libDoc = GlobalData.getDoc(libraryList[i], arch);\r
74 SurfaceAreaQuery saq = new SurfaceAreaQuery(libDoc);\r
75 libInstanceNode = new LibraryInstanceNode (libraryList[i],saq.getLibConstructorName(), saq.getLibDestructorName());\r
76 libInstanceList.add(libInstanceNode);\r
77 \r
78 //\r
79 // Add library instance and consumed library class list to\r
80 // libInstanceMap.\r
81 //\r
82 libClassConsmList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, arch);\r
83 if (libClassConsmList != null) {\r
84 String[] classStr = new String[libClassConsmList.length];\r
85 for (int k = 0; k < libClassConsmList.length; k++) {\r
86 classStr[k] = libClassConsmList[k];\r
87 }\r
88 if (this.libInstanceMap.containsKey(libraryList[i])) {\r
89 throw new AutoGenException(\r
90 libraryList[i].getName()\r
91 + "-- this library instance already exists, please check the library instance list!");\r
92 } else {\r
93 this.libInstanceMap.put(libraryList[i], classStr);\r
94 }\r
95 }\r
96\r
97 //\r
98 // Add library class and library instance map.\r
99 //\r
100 libClassDeclList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, arch);\r
101 if (libClassDeclList != null) {\r
102 for (int j = 0; j < libClassDeclList.length; j++) {\r
103 if (this.libClassMap.containsKey(libClassDeclList[j])) {\r
104 EdkLog.log(EdkLog.EDK_ERROR,libClassDeclList[j]\r
105 + " class is already implement by "\r
106 + this.libClassMap.get(libClassDeclList[j]));\r
107 throw new AutoGenException("Library Class: " + libClassDeclList\r
108 + " already has a library instance!");\r
109 } else {\r
110 this.libClassMap.put(libClassDeclList[j], libraryList[i]);\r
111 }\r
112 }\r
113 }\r
114 }\r
115\r
116 //\r
117 // Check is the library instance list meet the require;\r
118 //\r
119 //for (int s = 0; s < this.libInstanceList.size(); s++) {\r
120 // String[] libClass = this.libInstanceMap.get(this.libInstanceList\r
121 // .get(s));\r
122 // if (libClass != null) {\r
123 // for (int t = 0; t < libClass.length; t++) {\r
124 // if (this.libClassMap.get(libClass[t]) == null) {\r
125 //\r
126 // Note: There exist a kind of module which depend on \r
127 // library class with no instance or whose instance will\r
128 // never be linked into the module. \r
129 // For this satuation, the module has the description of \r
130 // library class in MSA file but no description of \r
131 // corresponding library instance in MBD file. There \r
132 // will be a warnig message given here after a standard \r
133 // log way has been decided.\r
134 //\r
135 // }\r
136 // }\r
137 // }\r
138 //}\r
139 }\r
140\r
141 /**\r
142 orderLibInstance\r
143 \r
144 This function reorder the library instance according the library class \r
145 dependency.\r
146 \r
147 @return List which content the ordered library instance.\r
148 **/\r
149 List<ModuleIdentification> orderLibInstance1() {\r
150 List<ModuleIdentification> orderList = new ArrayList<ModuleIdentification>();\r
151 //\r
152 // Stack of node which track the library instance name ant its visiting\r
153 // flag.\r
154 //\r
155 List<Node> stackList = new ArrayList<Node>();\r
156 int stackSize = 0;\r
157 ModuleIdentification libInstanceId = null;\r
158 if (libInstanceList.size() < 0) {\r
159 return null;\r
160 }\r
161\r
162 //\r
163 // Reorder the library instance.\r
164 //\r
165 for (int i = 0; i < libInstanceList.size(); i++) {\r
166 //\r
167 // If library instance is already in the order list skip it.\r
168 //\r
169 if (isInLibInstance(orderList, libInstanceList.get(i).libId)) {\r
170 continue;\r
171 }\r
172 \r
173 Node node = new Node(libInstanceList.get(i).libId, false);\r
174 //\r
175 // Use stack to reorder library instance.\r
176 // Push node to stack.\r
177 //\r
178 stackList.add(node);\r
179 while (stackList.size() > 0) {\r
180 stackSize = stackList.size() - 1;\r
181 //\r
182 // Pop the first node in stack. If the node flag has been visited\r
183 // add this node to orderlist and remove it from stack.\r
184 //\r
185 if (stackList.get(stackSize).isVisit) {\r
186 if (!isInLibInstance(orderList,\r
187 stackList.get(stackSize).nodeId)) {\r
188 orderList.add(stackList.get(stackSize).nodeId);\r
189 stackList.remove(stackSize);\r
190 }\r
191 \r
192 } else {\r
193 //\r
194 // Get the node value and set visit flag as true.\r
195 //\r
196 stackList.get(stackList.size() - 1).isVisit = true;\r
197 String[] libClassList = this.libInstanceMap.get(stackList\r
198 .get(stackSize).nodeId);\r
199 //\r
200 // Push the node dependence library instance to the stack.\r
201 //\r
202 if (libClassList != null) {\r
203 for (int j = 0; j < libClassList.length; j++) {\r
204 libInstanceId = this.libClassMap.get(libClassList[j]);\r
205 if (libInstanceId != null\r
206 && !isInLibInstance(orderList, libInstanceId)) {\r
207 //\r
208 // If and only if the currently library instance\r
209 // is not in stack and it have constructor or \r
210 // destructor function, push this library \r
211 // instacne in stack.\r
212 //\r
213 if (!isInStackList(stackList, this.libClassMap\r
214 .get(libClassList[j])) /* && isHaveConsDestructor(libInstanceId) */) {\r
215 stackList.add(new Node(this.libClassMap\r
216 .get(libClassList[j]), false));\r
217 }\r
218 }\r
219 }\r
220 }\r
221 }\r
222 System.out.println("################################################");\r
223 for (int ii = 0; ii < orderList.size(); ++ii) {\r
224 System.out.println(" " + orderList.get(ii));\r
225 }\r
226 }\r
227 }\r
228 return orderList;\r
229 }\r
230\r
231 List<ModuleIdentification> orderLibInstance() {\r
232 LinkedList<ModuleIdentification> orderList = new LinkedList<ModuleIdentification>();\r
233 for (int i = 0; i < libInstanceList.size(); ++i) {\r
234 ModuleIdentification current = libInstanceList.get(i).libId;\r
235 int insertPoint = orderList.size();\r
236 for (int j = 0; j < orderList.size(); ++j) {\r
237 ModuleIdentification old = orderList.get(j);\r
238 //System.out.println("### old = " + old);\r
239 if (consumes(current, old)) {\r
240 insertPoint = j + 1;\r
241 } else if (consumes(old, current)) {\r
242 insertPoint = j;\r
243 break;\r
244 }\r
245 }\r
246 orderList.add(insertPoint, current);\r
247// System.out.println("################################################");\r
248// for (int ii = 0; ii < orderList.size(); ++ii) {\r
249// System.out.println(" " + orderList.get(ii));\r
250// }\r
251 }\r
252\r
253 return orderList;\r
254 }\r
255\r
256 boolean consumes(ModuleIdentification lib1, ModuleIdentification lib2) {\r
257 //System.out.println("$$$ lib1 = " + lib1);\r
258 LinkedList<ModuleIdentification> stack = new LinkedList<ModuleIdentification>();\r
259 stack.add(lib1);\r
260 int j = 0;\r
261 while (j < stack.size()) {\r
262 ModuleIdentification lib = stack.get(j++);\r
263 String[] consumedClasses = libInstanceMap.get(lib);\r
264 for (int i = 0; i < consumedClasses.length; ++i) {\r
265 ModuleIdentification consumedLib = libClassMap.get(consumedClasses[i]);\r
266 //System.out.println("$$$ class = " + consumedClasses[i]);\r
267 //System.out.println("$$$ insta = " + consumedLib);\r
268 if (consumedLib == lib2) {\r
269 //System.out.println(lib1 + "\n consumes\n" + lib2 + "\n");\r
270 return true;\r
271 }\r
272 if (consumedLib != null && !stack.contains(consumedLib)) {\r
273 stack.offer(consumedLib);\r
274 }\r
275 }\r
276 }\r
277 return false;\r
278 }\r
279\r
280 /**\r
281 isInLibInstance\r
282 \r
283 This function check does the library instance already in the list.\r
284 \r
285 @param list List of the library instance.\r
286 @param instanceName Name of library instance.\r
287 @return "true" the library instance in list |\r
288 "false" the library instance is not in list.\r
289 **/\r
290 private boolean isInLibInstance(List<ModuleIdentification> list, ModuleIdentification instanceId) {\r
291 for (int i = 0; i < list.size(); i++) {\r
292 \r
293 if (instanceId.equals(list.get(i))) {\r
294 return true;\r
295 }\r
296 }\r
297 return false;\r
298 }\r
299\r
300 /**\r
301 isInStackList \r
302 \r
303 This function check if the node already in the stack.\r
304 \r
305 @param list Stack.\r
306 @param nodeName Name of node.\r
307 @return "true" if node have in stack |\r
308 "false" if node don't in stack.\r
309 **/ \r
310 private boolean isInStackList(List<Node> list, ModuleIdentification instanceId) {\r
311 for (int i = 0; i < list.size(); i++) {\r
312 if (instanceId.equals(list.get(i).nodeId)) {\r
313 return true;\r
314 }\r
315 }\r
316 return false;\r
317 }\r
318 \r
319 /**\r
320 isHaveConsDestructor\r
321 \r
322 This function check if the library have constructor or destructor \r
323 function.\r
324 \r
325 @param libName Name of library\r
326 @return "true" if library have constructor or desconstructor |\r
327 "false" if library don't have constructor \r
328 and desconstructor.\r
329 **/\r
330 private boolean isHaveConsDestructor (ModuleIdentification libNode){\r
331 for (int i = 0; i < libInstanceList.size(); i++){\r
332 if (libInstanceList.get(i).libId.equals(libNode)){\r
333 if (libInstanceList.get(i).constructorName != null || libInstanceList.get(i).deconstructorName != null){\r
334 return true;\r
335 }\r
336 }\r
337 }\r
338 return false;\r
339 }\r
340}\r
341\r
342/**\r
343 Node \r
344 \r
345 This class is used as stack node.\r
346 \r
347 **/\r
348class Node {\r
349 ModuleIdentification nodeId;\r
350\r
351 boolean isVisit;\r
352\r
353 Node(ModuleIdentification nodeId, boolean isVisit) {\r
354 this.nodeId = nodeId;\r
355 this.isVisit = false;\r
356 }\r
357} \r
358/**\r
359 LibraryInstance Node \r
360 \r
361 This class is used to store LibrayInstance and it's deconstructor and constructor\r
362**/\r
363 \r
364class LibraryInstanceNode {\r
365 ModuleIdentification libId;\r
366 String deconstructorName;\r
367 String constructorName;\r
368 \r
369 LibraryInstanceNode (ModuleIdentification libId, String deconstructor, String constructor){\r
370 this.libId = libId;\r
371 this.deconstructorName = deconstructor;\r
372 this.constructorName = constructor;\r
373 }\r
374}\r