4 This class is to reorder library instance sequence according to library
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
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.
17 package org
.tianocore
.build
.autogen
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.Iterator
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
25 import java
.util
.Stack
;
26 import java
.util
.HashSet
;
28 import org
.apache
.xmlbeans
.XmlObject
;
29 import org
.tianocore
.build
.exception
.AutoGenException
;
30 import org
.tianocore
.build
.global
.GlobalData
;
31 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
32 import org
.tianocore
.build
.id
.ModuleIdentification
;
33 import org
.tianocore
.common
.exception
.EdkException
;
34 import org
.tianocore
.common
.logger
.EdkLog
;
36 This class This class is to reorder library instance sequence according to
39 public class AutogenLibOrder
{
41 /// The map of library class and its library instance.
43 private Map
<String
, ModuleIdentification
> libClassMap
= new HashMap
<String
, ModuleIdentification
>();
46 /// The map of library instance and its implemet libraryClass.
48 private Map
<ModuleIdentification
, String
[]> libInstanceMap
= new HashMap
<ModuleIdentification
, String
[]>();
51 /// List of library instance. It is String[3] list, String[0] is libraryName,
52 /// String[1] is libraryConstructor name, String[2] is libDestructor name.
54 private List
<LibraryInstanceNode
> libInstanceList
= new ArrayList
<LibraryInstanceNode
>();
59 This function mainly initialize some member variable.
61 @param libraryList List of the library instance.
64 AutogenLibOrder(ModuleIdentification
[] libraryList
, String arch
) throws EdkException
{
65 LibraryInstanceNode libInstanceNode
;
66 String
[] libClassDeclList
= null;
67 String
[] libClassConsmList
= null;
69 for (int i
= 0; i
< libraryList
.length
; i
++) {
71 // Add libraryInstance in to libInstanceList.
73 Map
<String
, XmlObject
> libDoc
= GlobalData
.getDoc(libraryList
[i
], arch
);
74 SurfaceAreaQuery saq
= new SurfaceAreaQuery(libDoc
);
75 libInstanceNode
= new LibraryInstanceNode (libraryList
[i
],saq
.getLibConstructorName(), saq
.getLibDestructorName());
76 libInstanceList
.add(libInstanceNode
);
79 // Add library instance and consumed library class list to
82 libClassConsmList
= saq
.getLibraryClasses(CommonDefinition
.ALWAYSCONSUMED
, arch
);
83 if (libClassConsmList
!= null) {
84 String
[] classStr
= new String
[libClassConsmList
.length
];
85 for (int k
= 0; k
< libClassConsmList
.length
; k
++) {
86 classStr
[k
] = libClassConsmList
[k
];
88 if (this.libInstanceMap
.containsKey(libraryList
[i
])) {
89 throw new AutoGenException(
90 libraryList
[i
].getName()
91 + "-- this library instance already exists, please check the library instance list!");
93 this.libInstanceMap
.put(libraryList
[i
], classStr
);
98 // Add library class and library instance map.
100 libClassDeclList
= saq
.getLibraryClasses(CommonDefinition
.ALWAYSPRODUCED
, arch
);
101 if (libClassDeclList
!= null) {
102 for (int j
= 0; j
< libClassDeclList
.length
; j
++) {
103 if (this.libClassMap
.containsKey(libClassDeclList
[j
])) {
104 EdkLog
.log(EdkLog
.EDK_ERROR
,libClassDeclList
[j
]
105 + " class is already implement by "
106 + this.libClassMap
.get(libClassDeclList
[j
]));
107 throw new AutoGenException("Library Class: " + libClassDeclList
108 + " already has a library instance!");
110 this.libClassMap
.put(libClassDeclList
[j
], libraryList
[i
]);
117 // Check is the library instance list meet the require;
119 //for (int s = 0; s < this.libInstanceList.size(); s++) {
120 // String[] libClass = this.libInstanceMap.get(this.libInstanceList
122 // if (libClass != null) {
123 // for (int t = 0; t < libClass.length; t++) {
124 // if (this.libClassMap.get(libClass[t]) == null) {
126 // Note: There exist a kind of module which depend on
127 // library class with no instance or whose instance will
128 // never be linked into the module.
129 // For this satuation, the module has the description of
130 // library class in MSA file but no description of
131 // corresponding library instance in MBD file. There
132 // will be a warnig message given here after a standard
133 // log way has been decided.
144 This function reorder the library instance according the library class
147 @return List which content the ordered library instance.
149 List
<ModuleIdentification
> orderLibInstance1() {
150 List
<ModuleIdentification
> orderList
= new ArrayList
<ModuleIdentification
>();
152 // Stack of node which track the library instance name ant its visiting
155 List
<Node
> stackList
= new ArrayList
<Node
>();
157 ModuleIdentification libInstanceId
= null;
158 if (libInstanceList
.size() < 0) {
163 // Reorder the library instance.
165 for (int i
= 0; i
< libInstanceList
.size(); i
++) {
167 // If library instance is already in the order list skip it.
169 if (isInLibInstance(orderList
, libInstanceList
.get(i
).libId
)) {
173 Node node
= new Node(libInstanceList
.get(i
).libId
, false);
175 // Use stack to reorder library instance.
176 // Push node to stack.
179 while (stackList
.size() > 0) {
180 stackSize
= stackList
.size() - 1;
182 // Pop the first node in stack. If the node flag has been visited
183 // add this node to orderlist and remove it from stack.
185 if (stackList
.get(stackSize
).isVisit
) {
186 if (!isInLibInstance(orderList
,
187 stackList
.get(stackSize
).nodeId
)) {
188 orderList
.add(stackList
.get(stackSize
).nodeId
);
189 stackList
.remove(stackSize
);
194 // Get the node value and set visit flag as true.
196 stackList
.get(stackList
.size() - 1).isVisit
= true;
197 String
[] libClassList
= this.libInstanceMap
.get(stackList
198 .get(stackSize
).nodeId
);
200 // Push the node dependence library instance to the stack.
202 if (libClassList
!= null) {
203 for (int j
= 0; j
< libClassList
.length
; j
++) {
204 libInstanceId
= this.libClassMap
.get(libClassList
[j
]);
205 if (libInstanceId
!= null
206 && !isInLibInstance(orderList
, libInstanceId
)) {
208 // If and only if the currently library instance
209 // is not in stack and it have constructor or
210 // destructor function, push this library
211 // instacne in stack.
213 if (!isInStackList(stackList
, this.libClassMap
214 .get(libClassList
[j
])) /* && isHaveConsDestructor(libInstanceId) */) {
215 stackList
.add(new Node(this.libClassMap
216 .get(libClassList
[j
]), false));
222 System
.out
.println("################################################");
223 for (int ii
= 0; ii
< orderList
.size(); ++ii
) {
224 System
.out
.println(" " + orderList
.get(ii
));
231 List
<ModuleIdentification
> orderLibInstance() {
232 LinkedList
<ModuleIdentification
> orderList
= new LinkedList
<ModuleIdentification
>();
233 for (int i
= 0; i
< libInstanceList
.size(); ++i
) {
234 ModuleIdentification current
= libInstanceList
.get(i
).libId
;
235 int insertPoint
= orderList
.size();
236 for (int j
= 0; j
< orderList
.size(); ++j
) {
237 ModuleIdentification old
= orderList
.get(j
);
238 //System.out.println("### old = " + old);
239 if (consumes(current
, old
)) {
241 } else if (consumes(old
, current
)) {
246 orderList
.add(insertPoint
, current
);
247 // System.out.println("################################################");
248 // for (int ii = 0; ii < orderList.size(); ++ii) {
249 // System.out.println(" " + orderList.get(ii));
256 boolean consumes(ModuleIdentification lib1
, ModuleIdentification lib2
) {
257 //System.out.println("$$$ lib1 = " + lib1);
258 LinkedList
<ModuleIdentification
> stack
= new LinkedList
<ModuleIdentification
>();
261 while (j
< stack
.size()) {
262 ModuleIdentification lib
= stack
.get(j
++);
263 String
[] consumedClasses
= libInstanceMap
.get(lib
);
264 for (int i
= 0; i
< consumedClasses
.length
; ++i
) {
265 ModuleIdentification consumedLib
= libClassMap
.get(consumedClasses
[i
]);
266 //System.out.println("$$$ class = " + consumedClasses[i]);
267 //System.out.println("$$$ insta = " + consumedLib);
268 if (consumedLib
== lib2
) {
269 //System.out.println(lib1 + "\n consumes\n" + lib2 + "\n");
272 if (consumedLib
!= null && !stack
.contains(consumedLib
)) {
273 stack
.offer(consumedLib
);
283 This function check does the library instance already in the list.
285 @param list List of the library instance.
286 @param instanceName Name of library instance.
287 @return "true" the library instance in list |
288 "false" the library instance is not in list.
290 private boolean isInLibInstance(List
<ModuleIdentification
> list
, ModuleIdentification instanceId
) {
291 for (int i
= 0; i
< list
.size(); i
++) {
293 if (instanceId
.equals(list
.get(i
))) {
303 This function check if the node already in the stack.
306 @param nodeName Name of node.
307 @return "true" if node have in stack |
308 "false" if node don't in stack.
310 private boolean isInStackList(List
<Node
> list
, ModuleIdentification instanceId
) {
311 for (int i
= 0; i
< list
.size(); i
++) {
312 if (instanceId
.equals(list
.get(i
).nodeId
)) {
322 This function check if the library have constructor or destructor
325 @param libName Name of library
326 @return "true" if library have constructor or desconstructor |
327 "false" if library don't have constructor
330 private boolean isHaveConsDestructor (ModuleIdentification libNode
){
331 for (int i
= 0; i
< libInstanceList
.size(); i
++){
332 if (libInstanceList
.get(i
).libId
.equals(libNode
)){
333 if (libInstanceList
.get(i
).constructorName
!= null || libInstanceList
.get(i
).deconstructorName
!= null){
345 This class is used as stack node.
349 ModuleIdentification nodeId
;
353 Node(ModuleIdentification nodeId
, boolean isVisit
) {
354 this.nodeId
= nodeId
;
355 this.isVisit
= false;
361 This class is used to store LibrayInstance and it's deconstructor and constructor
364 class LibraryInstanceNode
{
365 ModuleIdentification libId
;
366 String deconstructorName
;
367 String constructorName
;
369 LibraryInstanceNode (ModuleIdentification libId
, String deconstructor
, String constructor
){
371 this.deconstructorName
= deconstructor
;
372 this.constructorName
= constructor
;