Fixed the issue in the library instance constructor calling sequence
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / autogen / AutogenLibOrder.java
CommitLineData
878ddf1f 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
3dc87a3e 21import java.util.Iterator;\r
22import java.util.LinkedList;\r
878ddf1f 23import java.util.List;\r
24import java.util.Map;\r
3dc87a3e 25import java.util.Stack;\r
26import java.util.HashSet;\r
a29c47e0 27\r
878ddf1f 28import org.apache.xmlbeans.XmlObject;\r
61528a1b 29import org.tianocore.build.exception.AutoGenException;\r
878ddf1f 30import org.tianocore.build.global.GlobalData;\r
31import org.tianocore.build.global.SurfaceAreaQuery;\r
a29c47e0 32import org.tianocore.build.id.ModuleIdentification;\r
61528a1b 33import org.tianocore.common.exception.EdkException;\r
192a42b4 34import org.tianocore.common.logger.EdkLog;\r
878ddf1f 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
a29c47e0 43 private Map<String, ModuleIdentification> libClassMap = new HashMap<String, ModuleIdentification>();\r
878ddf1f 44\r
45 ///\r
a29c47e0 46 /// The map of library instance and its implemet libraryClass.\r
878ddf1f 47 ///\r
a29c47e0 48 private Map<ModuleIdentification, String[]> libInstanceMap = new HashMap<ModuleIdentification, String[]>();\r
878ddf1f 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
a29c47e0 54 private List<LibraryInstanceNode> libInstanceList = new ArrayList<LibraryInstanceNode>();\r
878ddf1f 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
61528a1b 64 AutogenLibOrder(ModuleIdentification[] libraryList, String arch) throws EdkException {\r
a29c47e0 65 LibraryInstanceNode libInstanceNode;\r
66 String[] libClassDeclList = null;\r
67 String[] libClassConsmList = null;\r
878ddf1f 68 \r
a29c47e0 69 for (int i = 0; i < libraryList.length; i++) {\r
878ddf1f 70 //\r
71 // Add libraryInstance in to libInstanceList.\r
a29c47e0 72 // \r
73 Map<String, XmlObject> libDoc = GlobalData.getDoc(libraryList[i], arch);\r
83fba802 74 SurfaceAreaQuery saq = new SurfaceAreaQuery(libDoc);\r
75 libInstanceNode = new LibraryInstanceNode (libraryList[i],saq.getLibConstructorName(), saq.getLibDestructorName());\r
a29c47e0 76 libInstanceList.add(libInstanceNode);\r
878ddf1f 77 \r
78 //\r
79 // Add library instance and consumed library class list to\r
80 // libInstanceMap.\r
81 //\r
83fba802 82 libClassConsmList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, arch);\r
878ddf1f 83 if (libClassConsmList != null) {\r
84 String[] classStr = new String[libClassConsmList.length];\r
85 for (int k = 0; k < libClassConsmList.length; k++) {\r
a29c47e0 86 classStr[k] = libClassConsmList[k];\r
878ddf1f 87 }\r
a29c47e0 88 if (this.libInstanceMap.containsKey(libraryList[i])) {\r
61528a1b 89 throw new AutoGenException(\r
a29c47e0 90 libraryList[i].getName()\r
61528a1b 91 + "-- this library instance already exists, please check the library instance list!");\r
878ddf1f 92 } else {\r
a29c47e0 93 this.libInstanceMap.put(libraryList[i], classStr);\r
878ddf1f 94 }\r
95 }\r
96\r
97 //\r
98 // Add library class and library instance map.\r
99 //\r
83fba802 100 libClassDeclList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, arch);\r
878ddf1f 101 if (libClassDeclList != null) {\r
102 for (int j = 0; j < libClassDeclList.length; j++) {\r
a29c47e0 103 if (this.libClassMap.containsKey(libClassDeclList[j])) {\r
192a42b4 104 EdkLog.log(EdkLog.EDK_ERROR,libClassDeclList[j]\r
878ddf1f 105 + " class is already implement by "\r
a29c47e0 106 + this.libClassMap.get(libClassDeclList[j]));\r
61528a1b 107 throw new AutoGenException("Library Class: " + libClassDeclList\r
391dbbb1 108 + " already has a library instance!");\r
878ddf1f 109 } else {\r
a29c47e0 110 this.libClassMap.put(libClassDeclList[j], libraryList[i]);\r
878ddf1f 111 }\r
112 }\r
113 }\r
878ddf1f 114 }\r
115\r
116 //\r
117 // Check is the library instance list meet the require;\r
118 //\r
cb4d97bd 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
878ddf1f 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
cb4d97bd 135 // }\r
136 // }\r
137 // }\r
138 //}\r
878ddf1f 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
3dc87a3e 149 List<ModuleIdentification> orderLibInstance1() {\r
a29c47e0 150 List<ModuleIdentification> orderList = new ArrayList<ModuleIdentification>();\r
878ddf1f 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
a29c47e0 157 ModuleIdentification libInstanceId = null;\r
878ddf1f 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
a29c47e0 169 if (isInLibInstance(orderList, libInstanceList.get(i).libId)) {\r
878ddf1f 170 continue;\r
171 }\r
172 \r
a29c47e0 173 Node node = new Node(libInstanceList.get(i).libId, false);\r
878ddf1f 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
a29c47e0 187 stackList.get(stackSize).nodeId)) {\r
188 orderList.add(stackList.get(stackSize).nodeId);\r
878ddf1f 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
a29c47e0 198 .get(stackSize).nodeId);\r
878ddf1f 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
a29c47e0 204 libInstanceId = this.libClassMap.get(libClassList[j]);\r
205 if (libInstanceId != null\r
206 && !isInLibInstance(orderList, libInstanceId)) {\r
878ddf1f 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
3dc87a3e 214 .get(libClassList[j])) /* && isHaveConsDestructor(libInstanceId) */) {\r
878ddf1f 215 stackList.add(new Node(this.libClassMap\r
216 .get(libClassList[j]), false));\r
217 }\r
218 }\r
219 }\r
220 }\r
221 }\r
3dc87a3e 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
878ddf1f 226 }\r
227 }\r
228 return orderList;\r
229 }\r
230\r
3dc87a3e 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
878ddf1f 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
a29c47e0 290 private boolean isInLibInstance(List<ModuleIdentification> list, ModuleIdentification instanceId) {\r
878ddf1f 291 for (int i = 0; i < list.size(); i++) {\r
a29c47e0 292 \r
293 if (instanceId.equals(list.get(i))) {\r
878ddf1f 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
a29c47e0 310 private boolean isInStackList(List<Node> list, ModuleIdentification instanceId) {\r
878ddf1f 311 for (int i = 0; i < list.size(); i++) {\r
a29c47e0 312 if (instanceId.equals(list.get(i).nodeId)) {\r
878ddf1f 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
a29c47e0 330 private boolean isHaveConsDestructor (ModuleIdentification libNode){\r
878ddf1f 331 for (int i = 0; i < libInstanceList.size(); i++){\r
a29c47e0 332 if (libInstanceList.get(i).libId.equals(libNode)){\r
333 if (libInstanceList.get(i).constructorName != null || libInstanceList.get(i).deconstructorName != null){\r
878ddf1f 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
a29c47e0 349 ModuleIdentification nodeId;\r
878ddf1f 350\r
351 boolean isVisit;\r
352\r
a29c47e0 353 Node(ModuleIdentification nodeId, boolean isVisit) {\r
354 this.nodeId = nodeId;\r
878ddf1f 355 this.isVisit = false;\r
356 }\r
a29c47e0 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