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