]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/autogen/AutogenLibOrder.java
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
.List
;
24 import org
.apache
.xmlbeans
.XmlObject
;
25 import org
.tianocore
.LibraryClassDocument
.LibraryClass
;
27 import org
.tianocore
.build
.global
.GlobalData
;
28 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
31 This class This class is to reorder library instance sequence according to
34 public class AutogenLibOrder
{
36 /// The map of library class and its library instance.
38 private Map
<String
, String
> libClassMap
= new HashMap
<String
, String
>();
41 /// The map of library instance and its implemet instance.
43 private Map
<String
, String
[]> libInstanceMap
= new HashMap
<String
, String
[]>();
46 /// List of library instance. It is String[3] list, String[0] is libraryName,
47 /// String[1] is libraryConstructor name, String[2] is libDestructor name.
49 private List
<String
[]> libInstanceList
= new ArrayList
<String
[]>();
54 This function mainly initialize some member variable.
56 @param libraryList List of the library instance.
59 AutogenLibOrder(List
<String
> libraryList
) throws Exception
{
60 String
[] libInstance
= new String
[3];
61 LibraryClass
[] libClassDeclList
= null;
62 LibraryClass
[] libClassConsmList
= null;
64 for (int i
= 0; i
< libraryList
.size(); i
++) {
66 // Add libraryInstance in to libInstanceList.
68 libInstance
[0] = libraryList
.get(i
);
69 Map
<String
, XmlObject
> libDoc
= GlobalData
.getDoc(libInstance
[0]);
70 SurfaceAreaQuery
.push(libDoc
);
71 libInstance
[1] = SurfaceAreaQuery
.getLibConstructorName();
72 libInstance
[2] = SurfaceAreaQuery
.getLibDestructorName();
73 libInstanceList
.add(libInstance
.clone());
76 // Add library instance and consumed library class list to
79 libClassConsmList
= SurfaceAreaQuery
80 .getLibraryClassArray(CommonDefinition
.AlwaysConsumed
);
81 if (libClassConsmList
!= null) {
82 String
[] classStr
= new String
[libClassConsmList
.length
];
83 for (int k
= 0; k
< libClassConsmList
.length
; k
++) {
84 classStr
[k
] = libClassConsmList
[k
].getStringValue();
86 if (this.libInstanceMap
.containsKey(libInstance
[0])) {
89 + "this library instance is already exist, please check you library instance list!");
91 this.libInstanceMap
.put(libInstance
[0], classStr
);
96 // Add library class and library instance map.
98 libClassDeclList
= SurfaceAreaQuery
99 .getLibraryClassArray(CommonDefinition
.AlwaysProduced
);
100 if (libClassDeclList
!= null) {
101 for (int j
= 0; j
< libClassDeclList
.length
; j
++) {
102 if (this.libClassMap
.containsKey(libClassDeclList
[j
]
103 .getStringValue())) {
104 System
.out
.println(libClassDeclList
[j
].getStringValue()
105 + " class is already implement by "
106 + this.libClassMap
.get(libClassDeclList
[j
]
108 throw new Exception(libClassDeclList
109 + " is already have library instance!");
111 this.libClassMap
.put(libClassDeclList
[j
]
112 .getStringValue(), libInstance
[0]);
116 SurfaceAreaQuery
.pop();
120 // Check is the library instance list meet the require;
122 for (int s
= 0; s
< this.libInstanceList
.size(); s
++) {
123 String
[] libClass
= this.libInstanceMap
.get(this.libInstanceList
125 if (libClass
!= null) {
126 for (int t
= 0; t
< libClass
.length
; t
++) {
127 if (this.libClassMap
.get(libClass
[t
]) == null) {
129 // Note: There exist a kind of module which depend on
130 // library class with no instance or whose instance will
131 // never be linked into the module.
132 // For this satuation, the module has the description of
133 // library class in MSA file but no description of
134 // corresponding library instance in MBD file. There
135 // will be a warnig message given here after a standard
136 // log way has been decided.
147 This function reorder the library instance according the library class
150 @return List which content the ordered library instance.
152 List
orderLibInstance() {
153 List
<String
> orderList
= new ArrayList
<String
>();
155 // Stack of node which track the library instance name ant its visiting
158 List
<Node
> stackList
= new ArrayList
<Node
>();
160 String libInstance
= null;
161 if (libInstanceList
.size() < 0) {
166 // Reorder the library instance.
168 for (int i
= 0; i
< libInstanceList
.size(); i
++) {
170 // If library instance is already in the order list skip it.
172 if (isInLibInstance(orderList
, libInstanceList
.get(i
)[0])) {
176 Node node
= new Node(libInstanceList
.get(i
)[0], false);
178 // Use stack to reorder library instance.
179 // Push node to stack.
182 while (stackList
.size() > 0) {
183 stackSize
= stackList
.size() - 1;
185 // Pop the first node in stack. If the node flag has been visited
186 // add this node to orderlist and remove it from stack.
188 if (stackList
.get(stackSize
).isVisit
) {
189 if (!isInLibInstance(orderList
,
190 stackList
.get(stackSize
).nodeName
)) {
191 orderList
.add(stackList
.get(stackSize
).nodeName
);
192 stackList
.remove(stackSize
);
197 // Get the node value and set visit flag as true.
199 stackList
.get(stackList
.size() - 1).isVisit
= true;
200 String
[] libClassList
= this.libInstanceMap
.get(stackList
201 .get(stackSize
).nodeName
);
203 // Push the node dependence library instance to the stack.
205 if (libClassList
!= null) {
206 for (int j
= 0; j
< libClassList
.length
; j
++) {
207 libInstance
= this.libClassMap
.get(libClassList
[j
]);
208 if (libInstance
!= null
209 && !isInLibInstance(orderList
, libInstance
)) {
211 // If and only if the currently library instance
212 // is not in stack and it have constructor or
213 // destructor function, push this library
214 // instacne in stack.
216 if (!isInStackList(stackList
, this.libClassMap
217 .get(libClassList
[j
])) && isHaveConsDestructor(libInstance
)) {
218 stackList
.add(new Node(this.libClassMap
219 .get(libClassList
[j
]), false));
233 This function check does the library instance already in the list.
235 @param list List of the library instance.
236 @param instanceName Name of library instance.
237 @return "true" the library instance in list |
238 "false" the library instance is not in list.
240 private boolean isInLibInstance(List list
, String instanceName
) {
241 for (int i
= 0; i
< list
.size(); i
++) {
242 if (instanceName
.equalsIgnoreCase(list
.get(i
).toString())) {
252 This function check if the node already in the stack.
255 @param nodeName Name of node.
256 @return "true" if node have in stack |
257 "false" if node don't in stack.
259 private boolean isInStackList(List
<Node
> list
, String nodeName
) {
260 for (int i
= 0; i
< list
.size(); i
++) {
261 if (nodeName
.equalsIgnoreCase(list
.get(i
).nodeName
)) {
271 This function check if the library have constructor or destructor
274 @param libName Name of library
275 @return "true" if library have constructor or desconstructor |
276 "false" if library don't have constructor
279 private boolean isHaveConsDestructor (String libName
){
280 for (int i
= 0; i
< libInstanceList
.size(); i
++){
281 if (libInstanceList
.get(i
)[0].equalsIgnoreCase(libName
)){
282 if (libInstanceList
.get(i
)[1] != null || libInstanceList
.get(i
)[2] != null){
294 This class is used as stack node.
302 Node(String name
, boolean isVisit
) {
303 this.nodeName
= name
;
304 this.isVisit
= false;