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