Fixed grammar in messages.
[mirror_edk2.git] / Tools / 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
21import java.util.List;\r
22import java.util.Map;\r
a29c47e0 23\r
878ddf1f 24import org.apache.xmlbeans.XmlObject;\r
25import org.tianocore.LibraryClassDocument.LibraryClass;\r
26\r
27import org.tianocore.build.global.GlobalData;\r
28import org.tianocore.build.global.SurfaceAreaQuery;\r
a29c47e0 29import org.tianocore.build.id.ModuleIdentification;\r
878ddf1f 30\r
31/**\r
32 This class This class is to reorder library instance sequence according to\r
33 library dependence.\r
34**/\r
35public class AutogenLibOrder {\r
36 ///\r
37 /// The map of library class and its library instance.\r
38 ///\r
a29c47e0 39 private Map<String, ModuleIdentification> libClassMap = new HashMap<String, ModuleIdentification>();\r
878ddf1f 40\r
41 ///\r
a29c47e0 42 /// The map of library instance and its implemet libraryClass.\r
878ddf1f 43 ///\r
a29c47e0 44 private Map<ModuleIdentification, String[]> libInstanceMap = new HashMap<ModuleIdentification, String[]>();\r
878ddf1f 45\r
46 ///\r
47 /// List of library instance. It is String[3] list, String[0] is libraryName,\r
48 /// String[1] is libraryConstructor name, String[2] is libDestructor name.\r
49 ///\r
a29c47e0 50 private List<LibraryInstanceNode> libInstanceList = new ArrayList<LibraryInstanceNode>();\r
878ddf1f 51 \r
52 /**\r
53 Constructor function\r
54 \r
55 This function mainly initialize some member variable.\r
56 \r
57 @param libraryList List of the library instance.\r
58 @throws Exception\r
59 **/\r
a29c47e0 60 AutogenLibOrder(ModuleIdentification[] libraryList, String arch) throws Exception {\r
61 LibraryInstanceNode libInstanceNode;\r
62 String[] libClassDeclList = null;\r
63 String[] libClassConsmList = null;\r
878ddf1f 64 \r
a29c47e0 65 for (int i = 0; i < libraryList.length; i++) {\r
878ddf1f 66 //\r
67 // Add libraryInstance in to libInstanceList.\r
a29c47e0 68 // \r
69 Map<String, XmlObject> libDoc = GlobalData.getDoc(libraryList[i], arch);\r
878ddf1f 70 SurfaceAreaQuery.push(libDoc);\r
a29c47e0 71 libInstanceNode = new LibraryInstanceNode (libraryList[i],SurfaceAreaQuery.getLibConstructorName(), SurfaceAreaQuery.getLibDestructorName());\r
72 libInstanceList.add(libInstanceNode);\r
878ddf1f 73 \r
74 //\r
75 // Add library instance and consumed library class list to\r
76 // libInstanceMap.\r
77 //\r
78 libClassConsmList = SurfaceAreaQuery\r
42b78757 79 .getLibraryClasses(CommonDefinition.AlwaysConsumed, arch);\r
878ddf1f 80 if (libClassConsmList != null) {\r
81 String[] classStr = new String[libClassConsmList.length];\r
82 for (int k = 0; k < libClassConsmList.length; k++) {\r
a29c47e0 83 classStr[k] = libClassConsmList[k];\r
878ddf1f 84 }\r
a29c47e0 85 if (this.libInstanceMap.containsKey(libraryList[i])) {\r
878ddf1f 86 throw new Exception(\r
a29c47e0 87 libraryList[i].getName()\r
391dbbb1 88 + "this library instance already exists, please check the library instance list!");\r
878ddf1f 89 } else {\r
a29c47e0 90 this.libInstanceMap.put(libraryList[i], classStr);\r
878ddf1f 91 }\r
92 }\r
93\r
94 //\r
95 // Add library class and library instance map.\r
96 //\r
97 libClassDeclList = SurfaceAreaQuery\r
42b78757 98 .getLibraryClasses(CommonDefinition.AlwaysProduced, arch);\r
878ddf1f 99 if (libClassDeclList != null) {\r
100 for (int j = 0; j < libClassDeclList.length; j++) {\r
a29c47e0 101 if (this.libClassMap.containsKey(libClassDeclList[j])) {\r
102 System.out.println(libClassDeclList[j]\r
878ddf1f 103 + " class is already implement by "\r
a29c47e0 104 + this.libClassMap.get(libClassDeclList[j]));\r
391dbbb1 105 throw new Exception("Library Class: " + libClassDeclList\r
106 + " already has a library instance!");\r
878ddf1f 107 } else {\r
a29c47e0 108 this.libClassMap.put(libClassDeclList[j], libraryList[i]);\r
878ddf1f 109 }\r
110 }\r
111 }\r
112 SurfaceAreaQuery.pop();\r
113 }\r
114\r
115 //\r
116 // Check is the library instance list meet the require;\r
117 //\r
118 for (int s = 0; s < this.libInstanceList.size(); s++) {\r
119 String[] libClass = this.libInstanceMap.get(this.libInstanceList\r
120 .get(s));\r
121 if (libClass != null) {\r
122 for (int t = 0; t < libClass.length; t++) {\r
123 if (this.libClassMap.get(libClass[t]) == null) {\r
124 //\r
125 // Note: There exist a kind of module which depend on \r
126 // library class with no instance or whose instance will\r
127 // never be linked into the module. \r
128 // For this satuation, the module has the description of \r
129 // library class in MSA file but no description of \r
130 // corresponding library instance in MBD file. There \r
131 // will be a warnig message given here after a standard \r
132 // log way has been decided.\r
133 //\r
134 }\r
135 }\r
136 }\r
137 }\r
138 }\r
139\r
140 /**\r
141 orderLibInstance\r
142 \r
143 This function reorder the library instance according the library class \r
144 dependency.\r
145 \r
146 @return List which content the ordered library instance.\r
147 **/\r
a29c47e0 148 List<ModuleIdentification> orderLibInstance() {\r
149 List<ModuleIdentification> orderList = new ArrayList<ModuleIdentification>();\r
878ddf1f 150 //\r
151 // Stack of node which track the library instance name ant its visiting\r
152 // flag.\r
153 //\r
154 List<Node> stackList = new ArrayList<Node>();\r
155 int stackSize = 0;\r
a29c47e0 156 ModuleIdentification libInstanceId = null;\r
878ddf1f 157 if (libInstanceList.size() < 0) {\r
158 return null;\r
159 }\r
160\r
161 //\r
162 // Reorder the library instance.\r
163 //\r
164 for (int i = 0; i < libInstanceList.size(); i++) {\r
165 //\r
166 // If library instance is already in the order list skip it.\r
167 //\r
a29c47e0 168 if (isInLibInstance(orderList, libInstanceList.get(i).libId)) {\r
878ddf1f 169 continue;\r
170 }\r
171 \r
a29c47e0 172 Node node = new Node(libInstanceList.get(i).libId, false);\r
878ddf1f 173 //\r
174 // Use stack to reorder library instance.\r
175 // Push node to stack.\r
176 //\r
177 stackList.add(node);\r
178 while (stackList.size() > 0) {\r
179 stackSize = stackList.size() - 1;\r
180 //\r
181 // Pop the first node in stack. If the node flag has been visited\r
182 // add this node to orderlist and remove it from stack.\r
183 //\r
184 if (stackList.get(stackSize).isVisit) {\r
185 if (!isInLibInstance(orderList,\r
a29c47e0 186 stackList.get(stackSize).nodeId)) {\r
187 orderList.add(stackList.get(stackSize).nodeId);\r
878ddf1f 188 stackList.remove(stackSize);\r
189 }\r
190 \r
191 } else {\r
192 //\r
193 // Get the node value and set visit flag as true.\r
194 //\r
195 stackList.get(stackList.size() - 1).isVisit = true;\r
196 String[] libClassList = this.libInstanceMap.get(stackList\r
a29c47e0 197 .get(stackSize).nodeId);\r
878ddf1f 198 //\r
199 // Push the node dependence library instance to the stack.\r
200 //\r
201 if (libClassList != null) {\r
202 for (int j = 0; j < libClassList.length; j++) {\r
a29c47e0 203 libInstanceId = this.libClassMap.get(libClassList[j]);\r
204 if (libInstanceId != null\r
205 && !isInLibInstance(orderList, libInstanceId)) {\r
878ddf1f 206 //\r
207 // If and only if the currently library instance\r
208 // is not in stack and it have constructor or \r
209 // destructor function, push this library \r
210 // instacne in stack.\r
211 //\r
212 if (!isInStackList(stackList, this.libClassMap\r
a29c47e0 213 .get(libClassList[j])) && isHaveConsDestructor(libInstanceId)) {\r
878ddf1f 214 stackList.add(new Node(this.libClassMap\r
215 .get(libClassList[j]), false));\r
216 }\r
217 }\r
218 }\r
219 }\r
220 }\r
221 }\r
222 }\r
223 return orderList;\r
224 }\r
225\r
226 /**\r
227 isInLibInstance\r
228 \r
229 This function check does the library instance already in the list.\r
230 \r
231 @param list List of the library instance.\r
232 @param instanceName Name of library instance.\r
233 @return "true" the library instance in list |\r
234 "false" the library instance is not in list.\r
235 **/\r
a29c47e0 236 private boolean isInLibInstance(List<ModuleIdentification> list, ModuleIdentification instanceId) {\r
878ddf1f 237 for (int i = 0; i < list.size(); i++) {\r
a29c47e0 238 \r
239 if (instanceId.equals(list.get(i))) {\r
878ddf1f 240 return true;\r
241 }\r
242 }\r
243 return false;\r
244 }\r
245\r
246 /**\r
247 isInStackList \r
248 \r
249 This function check if the node already in the stack.\r
250 \r
251 @param list Stack.\r
252 @param nodeName Name of node.\r
253 @return "true" if node have in stack |\r
254 "false" if node don't in stack.\r
255 **/ \r
a29c47e0 256 private boolean isInStackList(List<Node> list, ModuleIdentification instanceId) {\r
878ddf1f 257 for (int i = 0; i < list.size(); i++) {\r
a29c47e0 258 if (instanceId.equals(list.get(i).nodeId)) {\r
878ddf1f 259 return true;\r
260 }\r
261 }\r
262 return false;\r
263 }\r
264 \r
265 /**\r
266 isHaveConsDestructor\r
267 \r
268 This function check if the library have constructor or destructor \r
269 function.\r
270 \r
271 @param libName Name of library\r
272 @return "true" if library have constructor or desconstructor |\r
273 "false" if library don't have constructor \r
274 and desconstructor.\r
275 **/\r
a29c47e0 276 private boolean isHaveConsDestructor (ModuleIdentification libNode){\r
878ddf1f 277 for (int i = 0; i < libInstanceList.size(); i++){\r
a29c47e0 278 if (libInstanceList.get(i).libId.equals(libNode)){\r
279 if (libInstanceList.get(i).constructorName != null || libInstanceList.get(i).deconstructorName != null){\r
878ddf1f 280 return true;\r
281 }\r
282 }\r
283 }\r
284 return false;\r
285 }\r
286}\r
287\r
288/**\r
289 Node \r
290 \r
291 This class is used as stack node.\r
292 \r
293 **/\r
294class Node {\r
a29c47e0 295 ModuleIdentification nodeId;\r
878ddf1f 296\r
297 boolean isVisit;\r
298\r
a29c47e0 299 Node(ModuleIdentification nodeId, boolean isVisit) {\r
300 this.nodeId = nodeId;\r
878ddf1f 301 this.isVisit = false;\r
302 }\r
a29c47e0 303} \r
304/**\r
305 LibraryInstance Node \r
306 \r
307 This class is used to store LibrayInstance and it's deconstructor and constructor\r
308**/\r
309 \r
310class LibraryInstanceNode {\r
311 ModuleIdentification libId;\r
312 String deconstructorName;\r
313 String constructorName;\r
314 \r
315 LibraryInstanceNode (ModuleIdentification libId, String deconstructor, String constructor){\r
316 this.libId = libId;\r
317 this.deconstructorName = deconstructor;\r
318 this.constructorName = constructor;\r
319 }\r
320}\r