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
> libClassProducer
= new HashMap
<String
, ModuleIdentification
>();
46 /// The map of library instance and its consumed Library Classes.
48 private Map
<ModuleIdentification
, String
[]> libInstanceConsumes
= new HashMap
<ModuleIdentification
, String
[]>();
51 /// The map of library instance and its implemeted Library Classes.
53 private Map
<ModuleIdentification
, String
[]> libInstanceProduces
= new HashMap
<ModuleIdentification
, String
[]>();
56 /// The map of library instance and its consumers.
58 private Map
<ModuleIdentification
, HashSet
<ModuleIdentification
>> libInstanceConsumedBy
= new HashMap
<ModuleIdentification
, HashSet
<ModuleIdentification
>>();
61 /// List of library instance. It is String[3] list, String[0] is libraryName,
62 /// String[1] is libraryConstructor name, String[2] is libDestructor name.
64 private ModuleIdentification
[] libInstanceList
= null;
69 This function mainly initialize some member variable.
71 @param libraryList List of the library instance.
74 AutogenLibOrder(ModuleIdentification
[] libraryList
, String arch
) throws EdkException
{
75 ModuleIdentification libInstance
;
76 String
[] libClassDeclList
= null;
77 String
[] libClassConsmList
= null;
79 libInstanceList
= libraryList
;
80 for (int i
= 0; i
< libraryList
.length
; i
++) {
81 libInstance
= libraryList
[i
];
83 // Fetch the constructor & destructor.
85 Map
<String
, XmlObject
> libDoc
= GlobalData
.getDoc(libInstance
, arch
);
86 SurfaceAreaQuery saq
= new SurfaceAreaQuery(libDoc
);
87 libInstance
.setConstructor(saq
.getLibConstructorName());
88 libInstance
.setDestructor(saq
.getLibDestructorName());
91 // Create library class consume database.
93 libClassConsmList
= saq
.getLibraryClasses(CommonDefinition
.ALWAYSCONSUMED
, arch
, null);
94 if (libClassConsmList
.length
> 0) {
95 if (this.libInstanceConsumes
.containsKey(libInstance
)) {
96 throw new AutoGenException(
97 libraryList
[i
].getName()
98 + "-- this library instance already exists, please check the library instance list!");
100 this.libInstanceConsumes
.put(libInstance
, libClassConsmList
);
105 // Create library class implementer database
107 libClassDeclList
= saq
.getLibraryClasses(CommonDefinition
.ALWAYSPRODUCED
, arch
, null);
108 if (libClassDeclList
.length
> 0) {
109 this.libInstanceProduces
.put(libInstance
, libClassDeclList
);
110 for (int j
= 0; j
< libClassDeclList
.length
; j
++) {
111 if (this.libClassProducer
.containsKey(libClassDeclList
[j
])) {
112 EdkLog
.log(EdkLog
.EDK_ERROR
,libClassDeclList
[j
]
113 + " class is already implemented by "
114 + this.libClassProducer
.get(libClassDeclList
[j
]));
115 throw new AutoGenException("Library Class: " + libClassDeclList
116 + " already has a library instance!");
118 this.libClassProducer
.put(libClassDeclList
[j
], libInstance
);
125 // Create a consumed-by database
127 for (Iterator it
= libClassProducer
.keySet().iterator(); it
.hasNext();) {
128 String className
= (String
)it
.next();
129 libInstance
= libClassProducer
.get(className
);
130 libInstanceConsumedBy
.put(libInstance
, new HashSet
<ModuleIdentification
>());
132 for (int k
= 0; k
< libraryList
.length
; ++k
) {
133 ModuleIdentification consumer
= libraryList
[k
];
134 String
[] consumedClassList
= libInstanceConsumes
.get(consumer
);
135 if (consumedClassList
== null) {
139 for (int l
= 0; l
< consumedClassList
.length
; ++l
) {
140 if (consumedClassList
[l
].equals(className
)) {
141 libInstanceConsumedBy
.get(libInstance
).add(consumer
);
151 This function reorder the library instance according the library class
152 dependency, using DAG anaylysis algothim
154 @return List which content the ordered library instance.
156 List
<ModuleIdentification
> orderLibInstance() throws EdkException
{
157 LinkedList
<ModuleIdentification
> orderList
= new LinkedList
<ModuleIdentification
>();
158 LinkedList
<ModuleIdentification
> noConsumerList
= new LinkedList
<ModuleIdentification
>();
161 // First, add the library instance without consumers to the Q
163 for (int i
= 0; i
< libInstanceList
.length
; ++i
) {
164 if (libInstanceConsumedBy
.get(libInstanceList
[i
]).size() == 0) {
165 noConsumerList
.add(libInstanceList
[i
]);
169 while (noConsumerList
.size() > 0) {
170 ModuleIdentification n
= noConsumerList
.poll();
171 orderList
.addFirst(n
);
173 String
[] consumedClassList
= libInstanceConsumes
.get(n
);
174 if (consumedClassList
== null) {
177 for (int i
= 0; i
< consumedClassList
.length
; ++i
) {
178 ModuleIdentification m
= libClassProducer
.get(consumedClassList
[i
]);
182 HashSet
<ModuleIdentification
> consumedBy
= libInstanceConsumedBy
.get(m
);
183 if (consumedBy
.size() == 0) {
187 consumedBy
.remove(n
);
188 if (consumedBy
.size() == 0) {
189 noConsumerList
.addLast(m
);
193 boolean circularlyConsumed
= false;
194 while (noConsumerList
.size() == 0 && !circularlyConsumed
) {
195 circularlyConsumed
= true;
196 for (int i
= 0; i
< libInstanceList
.length
; ++i
) {
197 ModuleIdentification libInstance
= libInstanceList
[i
];
198 if (!libInstance
.hasConstructor()) {
202 HashSet
<ModuleIdentification
> consumedBy
= libInstanceConsumedBy
.get(libInstance
);
203 if (consumedBy
.size() == 0) {
207 ModuleIdentification
[] consumedByList
= consumedBy
.toArray(new ModuleIdentification
[consumedBy
.size()]);
208 for (int j
= 0; j
< consumedByList
.length
; ++j
) {
209 ModuleIdentification consumer
= consumedByList
[j
];
210 if (consumer
.hasConstructor()) {
215 // if there's no constructor in the library instance's consumer,
216 // remove it from the consumer list
218 consumedBy
.remove(consumer
);
219 circularlyConsumed
= false;
220 if (consumedBy
.size() == 0) {
221 noConsumerList
.addLast(libInstance
);
226 if (noConsumerList
.size() > 0) {
231 if (noConsumerList
.size() == 0 && !circularlyConsumed
) {
238 // Append the remaining library instance to the end of sorted list
240 boolean HasError
= false;
241 for (int i
= 0; i
< libInstanceList
.length
; ++i
) {
242 HashSet
<ModuleIdentification
> consumedBy
= libInstanceConsumedBy
.get(libInstanceList
[i
]);
243 if (consumedBy
.size() > 0 && libInstanceList
[i
].hasConstructor()) {
244 EdkLog
.log(EdkLog
.EDK_ERROR
, libInstanceList
[i
].getName()
245 + " with constructor has a circular dependency!");
246 ModuleIdentification
[] consumedByList
= consumedBy
.toArray(new ModuleIdentification
[consumedBy
.size()]);
247 for (int j
= 0; j
< consumedByList
.length
; ++j
) {
248 EdkLog
.log(EdkLog
.EDK_ERROR
, " consumed by " + consumedByList
[j
].getName());
253 if (!orderList
.contains(libInstanceList
[i
])) {
254 orderList
.add(libInstanceList
[i
]);
258 throw new AutoGenException("Circular dependency in library instances is found!");