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