]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
BaseTools: use combined version of OrderedDict
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / WorkspaceCommon.py
1 ## @file
2 # Common routines used by workspace
3 #
4 # Copyright (c) 2012 - 2017, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 from Common.Misc import sdict
15 from collections import OrderedDict, defaultdict
16 from Common.DataType import SUP_MODULE_USER_DEFINED
17 from BuildClassObject import LibraryClassObject
18 import Common.GlobalData as GlobalData
19 from Workspace.BuildClassObject import StructurePcd
20
21 class OrderedListDict(OrderedDict, defaultdict):
22 def __init__(self, *args, **kwargs):
23 super(OrderedListDict, self).__init__(*args, **kwargs)
24 self.default_factory = list
25
26 ## Get all packages from platform for specified arch, target and toolchain
27 #
28 # @param Platform: DscBuildData instance
29 # @param BuildDatabase: The database saves all data for all metafiles
30 # @param Arch: Current arch
31 # @param Target: Current target
32 # @param Toolchain: Current toolchain
33 # @retval: List of packages which are DecBuildData instances
34 #
35 def GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain):
36 PkgSet = set()
37 for ModuleFile in Platform.Modules:
38 Data = BuildDatabase[ModuleFile, Arch, Target, Toolchain]
39 PkgSet.update(Data.Packages)
40 for Lib in GetLiabraryInstances(Data, Platform, BuildDatabase, Arch, Target, Toolchain):
41 PkgSet.update(Lib.Packages)
42 return list(PkgSet)
43
44 ## Get all declared PCD from platform for specified arch, target and toolchain
45 #
46 # @param Platform: DscBuildData instance
47 # @param BuildDatabase: The database saves all data for all metafiles
48 # @param Arch: Current arch
49 # @param Target: Current target
50 # @param Toolchain: Current toolchain
51 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
52 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
53 #
54 def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain,additionalPkgs):
55 PkgList = GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain)
56 PkgList = set(PkgList)
57 PkgList |= additionalPkgs
58 DecPcds = {}
59 GuidDict = {}
60 for Pkg in PkgList:
61 Guids = Pkg.Guids
62 GuidDict.update(Guids)
63 for Pcd in Pkg.Pcds:
64 PcdCName = Pcd[0]
65 PcdTokenName = Pcd[1]
66 if GlobalData.MixedPcd:
67 for PcdItem in GlobalData.MixedPcd.keys():
68 if (PcdCName, PcdTokenName) in GlobalData.MixedPcd[PcdItem]:
69 PcdCName = PcdItem[0]
70 break
71 if (PcdCName, PcdTokenName) not in DecPcds.keys():
72 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]
73 return DecPcds, GuidDict
74
75 ## Get all dependent libraries for a module
76 #
77 # @param Module: InfBuildData instance
78 # @param Platform: DscBuildData instance
79 # @param BuildDatabase: The database saves all data for all metafiles
80 # @param Arch: Current arch
81 # @param Target: Current target
82 # @param Toolchain: Current toolchain
83 # @retval: List of dependent libraries which are InfBuildData instances
84 #
85 def GetLiabraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):
86 if Module.AutoGenVersion >= 0x00010005:
87 return _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)
88 else:
89 return _ResolveLibraryReference(Module, Platform)
90
91 def _GetModuleLibraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):
92 ModuleType = Module.ModuleType
93
94 # for overriding library instances with module specific setting
95 PlatformModule = Platform.Modules[str(Module)]
96
97 # add forced library instances (specified under LibraryClasses sections)
98 #
99 # If a module has a MODULE_TYPE of USER_DEFINED,
100 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
101 #
102 if Module.ModuleType != SUP_MODULE_USER_DEFINED:
103 for LibraryClass in Platform.LibraryClasses.GetKeys():
104 if LibraryClass.startswith("NULL") and Platform.LibraryClasses[LibraryClass, Module.ModuleType]:
105 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]
106
107 # add forced library instances (specified in module overrides)
108 for LibraryClass in PlatformModule.LibraryClasses:
109 if LibraryClass.startswith("NULL"):
110 Module.LibraryClasses[LibraryClass] = PlatformModule.LibraryClasses[LibraryClass]
111
112 # EdkII module
113 LibraryConsumerList = [Module]
114 Constructor = []
115 ConsumedByList = OrderedListDict()
116 LibraryInstance = sdict()
117
118 while len(LibraryConsumerList) > 0:
119 M = LibraryConsumerList.pop()
120 for LibraryClassName in M.LibraryClasses:
121 if LibraryClassName not in LibraryInstance:
122 # override library instance for this module
123 if LibraryClassName in PlatformModule.LibraryClasses:
124 LibraryPath = PlatformModule.LibraryClasses[LibraryClassName]
125 else:
126 LibraryPath = Platform.LibraryClasses[LibraryClassName, ModuleType]
127 if LibraryPath is None or LibraryPath == "":
128 LibraryPath = M.LibraryClasses[LibraryClassName]
129 if LibraryPath is None or LibraryPath == "":
130 return []
131
132 LibraryModule = BuildDatabase[LibraryPath, Arch, Target, Toolchain]
133 # for those forced library instance (NULL library), add a fake library class
134 if LibraryClassName.startswith("NULL"):
135 LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))
136 elif LibraryModule.LibraryClass is None \
137 or len(LibraryModule.LibraryClass) == 0 \
138 or (ModuleType != 'USER_DEFINED'
139 and ModuleType not in LibraryModule.LibraryClass[0].SupModList):
140 # only USER_DEFINED can link against any library instance despite of its SupModList
141 return []
142
143 LibraryInstance[LibraryClassName] = LibraryModule
144 LibraryConsumerList.append(LibraryModule)
145 else:
146 LibraryModule = LibraryInstance[LibraryClassName]
147
148 if LibraryModule is None:
149 continue
150
151 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:
152 Constructor.append(LibraryModule)
153
154 # don't add current module itself to consumer list
155 if M != Module:
156 if M in ConsumedByList[LibraryModule]:
157 continue
158 ConsumedByList[LibraryModule].append(M)
159 #
160 # Initialize the sorted output list to the empty set
161 #
162 SortedLibraryList = []
163 #
164 # Q <- Set of all nodes with no incoming edges
165 #
166 LibraryList = [] #LibraryInstance.values()
167 Q = []
168 for LibraryClassName in LibraryInstance:
169 M = LibraryInstance[LibraryClassName]
170 LibraryList.append(M)
171 if len(ConsumedByList[M]) == 0:
172 Q.append(M)
173
174 #
175 # start the DAG algorithm
176 #
177 while True:
178 EdgeRemoved = True
179 while Q == [] and EdgeRemoved:
180 EdgeRemoved = False
181 # for each node Item with a Constructor
182 for Item in LibraryList:
183 if Item not in Constructor:
184 continue
185 # for each Node without a constructor with an edge e from Item to Node
186 for Node in ConsumedByList[Item]:
187 if Node in Constructor:
188 continue
189 # remove edge e from the graph if Node has no constructor
190 ConsumedByList[Item].remove(Node)
191 EdgeRemoved = True
192 if len(ConsumedByList[Item]) == 0:
193 # insert Item into Q
194 Q.insert(0, Item)
195 break
196 if Q != []:
197 break
198 # DAG is done if there's no more incoming edge for all nodes
199 if Q == []:
200 break
201
202 # remove node from Q
203 Node = Q.pop()
204 # output Node
205 SortedLibraryList.append(Node)
206
207 # for each node Item with an edge e from Node to Item do
208 for Item in LibraryList:
209 if Node not in ConsumedByList[Item]:
210 continue
211 # remove edge e from the graph
212 ConsumedByList[Item].remove(Node)
213
214 if len(ConsumedByList[Item]) != 0:
215 continue
216 # insert Item into Q, if Item has no other incoming edges
217 Q.insert(0, Item)
218
219 #
220 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
221 #
222 for Item in LibraryList:
223 if len(ConsumedByList[Item]) != 0 and Item in Constructor and len(Constructor) > 1:
224 return []
225 if Item not in SortedLibraryList:
226 SortedLibraryList.append(Item)
227
228 #
229 # Build the list of constructor and destructir names
230 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
231 #
232 SortedLibraryList.reverse()
233 return SortedLibraryList
234
235 def _ResolveLibraryReference(Module, Platform):
236 LibraryConsumerList = [Module]
237
238 # "CompilerStub" is a must for Edk modules
239 if Module.Libraries:
240 Module.Libraries.append("CompilerStub")
241 LibraryList = []
242 while len(LibraryConsumerList) > 0:
243 M = LibraryConsumerList.pop()
244 for LibraryName in M.Libraries:
245 Library = Platform.LibraryClasses[LibraryName, ':dummy:']
246 if Library is None:
247 for Key in Platform.LibraryClasses.data.keys():
248 if LibraryName.upper() == Key.upper():
249 Library = Platform.LibraryClasses[Key, ':dummy:']
250 break
251 if Library is None:
252 continue
253
254 if Library not in LibraryList:
255 LibraryList.append(Library)
256 LibraryConsumerList.append(Library)
257 return LibraryList