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