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