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