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