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