]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/autogen/AutogenLibOrder.java
50668b096bc88be1589d5a1e1ad152093d947a88
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
;
23 import org
.apache
.xmlbeans
.XmlObject
;
24 import org
.tianocore
.LibraryClassDocument
.LibraryClass
;
26 import org
.tianocore
.build
.global
.GlobalData
;
27 import org
.tianocore
.build
.global
.SurfaceAreaQuery
;
30 This class This class is to reorder library instance sequence according to
33 public class AutogenLibOrder
{
35 /// The map of library class and its library instance.
37 private Map
<String
, String
> libClassMap
= new HashMap
<String
, String
>();
40 /// The map of library instance and its implemet instance.
42 private Map
<String
, String
[]> libInstanceMap
= new HashMap
<String
, String
[]>();
45 /// List of library instance. It is String[3] list, String[0] is libraryName,
46 /// String[1] is libraryConstructor name, String[2] is libDestructor name.
48 private List
<String
[]> libInstanceList
= new ArrayList
<String
[]>();
53 This function mainly initialize some member variable.
55 @param libraryList List of the library instance.
58 AutogenLibOrder(List
<String
> libraryList
) throws Exception
{
59 String
[] libInstance
= new String
[3];
60 LibraryClass
[] libClassDeclList
= null;
61 LibraryClass
[] libClassConsmList
= null;
63 for (int i
= 0; i
< libraryList
.size(); i
++) {
65 // Add libraryInstance in to libInstanceList.
67 libInstance
[0] = libraryList
.get(i
);
68 Map
<String
, XmlObject
> libDoc
= GlobalData
.getDoc(libInstance
[0]);
69 SurfaceAreaQuery
.push(libDoc
);
70 libInstance
[1] = SurfaceAreaQuery
.getLibConstructorName();
71 libInstance
[2] = SurfaceAreaQuery
.getLibDestructorName();
72 libInstanceList
.add(libInstance
.clone());
75 // Add library instance and consumed library class list to
78 libClassConsmList
= SurfaceAreaQuery
79 .getLibraryClassArray(CommonDefinition
.AlwaysConsumed
);
80 if (libClassConsmList
!= null) {
81 String
[] classStr
= new String
[libClassConsmList
.length
];
82 for (int k
= 0; k
< libClassConsmList
.length
; k
++) {
83 //classStr[k] = libClassConsmList[k].getStringValue();
84 classStr
[k
] = getStringValue((XmlObject
)libClassConsmList
[k
]);
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 String libClassName
= getStringValue((XmlObject
)libClassDeclList
[j
]);
105 if (this.libClassMap
.containsKey(libClassName
)) {
106 System
.out
.println(libClassName
107 + " class is already implement by "
108 + this.libClassMap
.get(libClassName
));
109 throw new Exception(libClassDeclList
110 + " is already have library instance!");
112 this.libClassMap
.put(libClassName
, 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){
290 private String
getStringValue(XmlObject xmlDoc
) {
291 return xmlDoc
.getDomNode().getFirstChild().getNodeValue();
298 This class is used as stack node.
306 Node(String name
, boolean isVisit
) {
307 this.nodeName
= name
;
308 this.isVisit
= false;