summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/autogen/AutogenLibOrder.java
blob: a131b89cc8c4129e763a2b6b3b62c7b94c5b65e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**@file
 AutogenLibOrder class.

 This class is to reorder library instance sequence according to library
 dependence.

 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php

 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

 **/
package org.tianocore.build.autogen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.HashSet;

import org.apache.xmlbeans.XmlObject;
import org.tianocore.build.exception.AutoGenException;
import org.tianocore.build.global.GlobalData;
import org.tianocore.build.global.SurfaceAreaQuery;
import org.tianocore.build.id.ModuleIdentification;
import org.tianocore.common.exception.EdkException;
import org.tianocore.common.logger.EdkLog;
/**
  This class This class is to reorder library instance sequence according to
  library dependence.
**/
public class AutogenLibOrder {
    ///
    /// The map of library class and its library instance.
    ///
    private Map<String, ModuleIdentification> libClassProducer = new HashMap<String, ModuleIdentification>();

    ///
    /// The map of library instance and its consumed Library Classes.
    ///
    private Map<ModuleIdentification, String[]> libInstanceConsumes = new HashMap<ModuleIdentification, String[]>();

    ///
    /// The map of library instance and its implemeted Library Classes.
    ///
    private Map<ModuleIdentification, String[]> libInstanceProduces = new HashMap<ModuleIdentification, String[]>();

    ///
    /// The map of library instance and its consumers.
    ///
    private Map<ModuleIdentification, HashSet<ModuleIdentification>> libInstanceConsumedBy = new HashMap<ModuleIdentification, HashSet<ModuleIdentification>>();

    ///
    /// List of library instance. It is String[3] list, String[0] is libraryName,
    /// String[1] is libraryConstructor name, String[2] is libDestructor name.
    ///
    private ModuleIdentification[] libInstanceList = null;

    /**
      Constructor function

      This function mainly initialize some member variable.

      @param  libraryList   List of the library instance.
      @throws Exception
    **/
    AutogenLibOrder(ModuleIdentification[] libraryList, String arch) throws EdkException {
        ModuleIdentification libInstance;
        String[]       libClassDeclList = null;
        String[]       libClassConsmList = null;

        libInstanceList = libraryList;
        for (int i = 0; i < libraryList.length; i++) {
            libInstance = libraryList[i];
            //
            // Fetch the constructor & destructor.
            //
            Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstance, arch);
            SurfaceAreaQuery saq = new SurfaceAreaQuery(libDoc);
            libInstance.setConstructor(saq.getLibConstructorName());
            libInstance.setDestructor(saq.getLibDestructorName());

            //
            // Create library class consume database.
            //
            libClassConsmList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, arch, null);
            if (libClassConsmList != null) {
                if (this.libInstanceConsumes.containsKey(libInstance)) {
                    throw new AutoGenException(
                            libraryList[i].getName()
                                    + "-- this library instance already exists, please check the library instance list!");
                } else {
                    this.libInstanceConsumes.put(libInstance, libClassConsmList);
                }
            }

            //
            // Create library class implementer database
            //
            libClassDeclList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, arch, null);
            if (libClassDeclList != null) {
                this.libInstanceProduces.put(libInstance, libClassDeclList);
                for (int j = 0; j < libClassDeclList.length; j++) {
                    if (this.libClassProducer.containsKey(libClassDeclList[j])) {
                        EdkLog.log(EdkLog.EDK_ERROR,libClassDeclList[j]
                                + " class is already implemented by "
                                + this.libClassProducer.get(libClassDeclList[j]));
                        throw new AutoGenException("Library Class: " + libClassDeclList
                                + " already has a library instance!");
                    } else {
                        this.libClassProducer.put(libClassDeclList[j], libInstance);
                    }
                }
            }
        }

        //
        // Create a consumed-by database
        //
        for (Iterator it = libClassProducer.keySet().iterator(); it.hasNext();) {
            String className = (String)it.next();
            libInstance = libClassProducer.get(className);
            libInstanceConsumedBy.put(libInstance, new HashSet<ModuleIdentification>());

            for (int k = 0; k < libraryList.length; ++k) {
                ModuleIdentification consumer = libraryList[k];
                String[] consumedClassList = libInstanceConsumes.get(consumer);

                for (int l = 0; l < consumedClassList.length; ++l) {
                    if (consumedClassList[l].equals(className)) {
                        libInstanceConsumedBy.get(libInstance).add(consumer);
                    }
                }
            }
        }
    }

    /**
      orderLibInstance

      This function reorder the library instance according the library class
      dependency, using DAG anaylysis algothim

      @return     List which content the ordered library instance.
    **/
    List<ModuleIdentification> orderLibInstance() throws EdkException {
        LinkedList<ModuleIdentification> orderList = new LinkedList<ModuleIdentification>();
        LinkedList<ModuleIdentification> noConsumerList = new LinkedList<ModuleIdentification>();

        //
        // First, add the library instance without consumers to the Q
        //
        for (int i = 0; i < libInstanceList.length; ++i) {
            if (libInstanceList[i] == null) {
                continue;
            }
            
            if (libInstanceConsumedBy.get(libInstanceList[i]) == null ||  libInstanceConsumedBy.get(libInstanceList[i]).size() == 0) {
                noConsumerList.add(libInstanceList[i]);
            }
        }

        while (noConsumerList.size() > 0) {
            ModuleIdentification n = noConsumerList.poll();
            orderList.addFirst(n);

            String[] consumedClassList = libInstanceConsumes.get(n);
            for (int i = 0; i < consumedClassList.length; ++i) {
                ModuleIdentification m = libClassProducer.get(consumedClassList[i]);
                if (m == null) {
                    continue;
                }
                HashSet<ModuleIdentification> consumedBy = libInstanceConsumedBy.get(m);
                if (consumedBy == null || consumedBy.size() == 0) {
                  continue;
                }

                consumedBy.remove(n);
                if (consumedBy.size() == 0) {
                    noConsumerList.addLast(m);
                }
            }

            boolean circularlyConsumed = false;
            while (noConsumerList.size() == 0 && !circularlyConsumed) {
                circularlyConsumed = true;
                for (int i = 0; i < libInstanceList.length; ++i) {
                    ModuleIdentification libInstance = libInstanceList[i];
                    if (!libInstance.hasConstructor()) {
                        continue;
                    }

                    HashSet<ModuleIdentification> consumedBy = libInstanceConsumedBy.get(libInstance);
                    if (consumedBy == null || consumedBy.size() == 0) {
                        continue;
                    }

                    ModuleIdentification[] consumedByList = consumedBy.toArray(new ModuleIdentification[consumedBy.size()]);
                    for (int j = 0; j < consumedByList.length; ++j) {
                        ModuleIdentification consumer = consumedByList[j];
                        if (consumer.hasConstructor()) {
                            continue;
                        }

                        //
                        // if there's no constructor in the library instance's consumer,
                        // remove it from the consumer list
                        //
                        consumedBy.remove(consumer);
                        circularlyConsumed = false;
                        if (consumedBy.size() == 0) {
                            noConsumerList.addLast(libInstance);
                            break;
                        }
                    }

                    if (noConsumerList.size() > 0) {
                        break;
                    }
                }

                if (noConsumerList.size() == 0 && !circularlyConsumed) {
                  break;
                }
            }
        }

        //
        // Append the remaining library instance to the end of sorted list
        //
        boolean HasError = false;
        for (int i = 0; i < libInstanceList.length; ++i) {
            HashSet<ModuleIdentification> consumedBy = libInstanceConsumedBy.get(libInstanceList[i]);
            if (consumedBy != null && consumedBy.size() > 0 && libInstanceList[i].hasConstructor()) {
                EdkLog.log(EdkLog.EDK_ERROR, libInstanceList[i].getName()
                           + " with constructor has a circular dependency!");
                ModuleIdentification[] consumedByList = consumedBy.toArray(new ModuleIdentification[consumedBy.size()]);
                for (int j = 0; j < consumedByList.length; ++j) {
                    EdkLog.log(EdkLog.EDK_ERROR, " consumed by " + consumedByList[j].getName());
                }
                HasError = true;
            }

            if (!orderList.contains(libInstanceList[i])) {
                orderList.add(libInstanceList[i]);
            }
        }
        if (HasError) {
            throw new AutoGenException("Circular dependency in library instances is found!");
        }

        return orderList;
    }
}