]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
BaseTools: Decouple AutoGen Objects
[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 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 from __future__ import absolute_import
9 from collections import OrderedDict, defaultdict
10 from Common.DataType import SUP_MODULE_USER_DEFINED
11 from Common.DataType import SUP_MODULE_HOST_APPLICATION
12 from .BuildClassObject import LibraryClassObject
13 import Common.GlobalData as GlobalData
14 from Workspace.BuildClassObject import StructurePcd
15 from Common.BuildToolError import RESOURCE_NOT_AVAILABLE
16 from Common.BuildToolError import OPTION_MISSING
17 from Common.BuildToolError import BUILD_ERROR
18
19 class OrderedListDict(OrderedDict):
20 def __init__(self, *args, **kwargs):
21 super(OrderedListDict, self).__init__(*args, **kwargs)
22 self.default_factory = list
23
24 def __missing__(self, key):
25 self[key] = Value = self.default_factory()
26 return Value
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 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)
89
90 def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):
91 if Module.LibInstances:
92 return Module.LibInstances
93 ModuleType = Module.ModuleType
94
95 # add forced library instances (specified under LibraryClasses sections)
96 #
97 # If a module has a MODULE_TYPE of USER_DEFINED,
98 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
99 #
100 if Module.ModuleType != SUP_MODULE_USER_DEFINED and Module.ModuleType != SUP_MODULE_HOST_APPLICATION:
101 for LibraryClass in Platform.LibraryClasses.GetKeys():
102 if LibraryClass.startswith("NULL") and Platform.LibraryClasses[LibraryClass, Module.ModuleType]:
103 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]
104
105 # add forced library instances (specified in module overrides)
106 for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:
107 if LibraryClass.startswith("NULL"):
108 Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]
109
110 # EdkII module
111 LibraryConsumerList = [Module]
112 Constructor = []
113 ConsumedByList = OrderedListDict()
114 LibraryInstance = OrderedDict()
115
116 if FileName:
117 EdkLogger.verbose("")
118 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))
119
120 while len(LibraryConsumerList) > 0:
121 M = LibraryConsumerList.pop()
122 for LibraryClassName in M.LibraryClasses:
123 if LibraryClassName not in LibraryInstance:
124 # override library instance for this module
125 LibraryPath = Platform.Modules[str(Module)].LibraryClasses.get(LibraryClassName,Platform.LibraryClasses[LibraryClassName, ModuleType])
126 if LibraryPath is None:
127 LibraryPath = M.LibraryClasses.get(LibraryClassName)
128 if LibraryPath is None:
129 if FileName:
130 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
131 "Instance of library class [%s] is not found" % LibraryClassName,
132 File=FileName,
133 ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), Arch, str(Module)))
134 else:
135 return []
136
137 LibraryModule = BuildDatabase[LibraryPath, Arch, Target, Toolchain]
138 # for those forced library instance (NULL library), add a fake library class
139 if LibraryClassName.startswith("NULL"):
140 LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))
141 elif LibraryModule.LibraryClass is None \
142 or len(LibraryModule.LibraryClass) == 0 \
143 or (ModuleType != SUP_MODULE_USER_DEFINED and ModuleType != SUP_MODULE_HOST_APPLICATION
144 and ModuleType not in LibraryModule.LibraryClass[0].SupModList):
145 # only USER_DEFINED can link against any library instance despite of its SupModList
146 if FileName:
147 EdkLogger.error("build", OPTION_MISSING,
148 "Module type [%s] is not supported by library instance [%s]" \
149 % (ModuleType, LibraryPath), File=FileName,
150 ExtraData="consumed by [%s]" % str(Module))
151 else:
152 return []
153
154 LibraryInstance[LibraryClassName] = LibraryModule
155 LibraryConsumerList.append(LibraryModule)
156 if FileName:
157 EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))
158 else:
159 LibraryModule = LibraryInstance[LibraryClassName]
160
161 if LibraryModule is None:
162 continue
163
164 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:
165 Constructor.append(LibraryModule)
166
167 # don't add current module itself to consumer list
168 if M != Module:
169 if M in ConsumedByList[LibraryModule]:
170 continue
171 ConsumedByList[LibraryModule].append(M)
172 #
173 # Initialize the sorted output list to the empty set
174 #
175 SortedLibraryList = []
176 #
177 # Q <- Set of all nodes with no incoming edges
178 #
179 LibraryList = [] #LibraryInstance.values()
180 Q = []
181 for LibraryClassName in LibraryInstance:
182 M = LibraryInstance[LibraryClassName]
183 LibraryList.append(M)
184 if not ConsumedByList[M]:
185 Q.append(M)
186
187 #
188 # start the DAG algorithm
189 #
190 while True:
191 EdgeRemoved = True
192 while Q == [] and EdgeRemoved:
193 EdgeRemoved = False
194 # for each node Item with a Constructor
195 for Item in LibraryList:
196 if Item not in Constructor:
197 continue
198 # for each Node without a constructor with an edge e from Item to Node
199 for Node in ConsumedByList[Item]:
200 if Node in Constructor:
201 continue
202 # remove edge e from the graph if Node has no constructor
203 ConsumedByList[Item].remove(Node)
204 EdgeRemoved = True
205 if not ConsumedByList[Item]:
206 # insert Item into Q
207 Q.insert(0, Item)
208 break
209 if Q != []:
210 break
211 # DAG is done if there's no more incoming edge for all nodes
212 if Q == []:
213 break
214
215 # remove node from Q
216 Node = Q.pop()
217 # output Node
218 SortedLibraryList.append(Node)
219
220 # for each node Item with an edge e from Node to Item do
221 for Item in LibraryList:
222 if Node not in ConsumedByList[Item]:
223 continue
224 # remove edge e from the graph
225 ConsumedByList[Item].remove(Node)
226
227 if ConsumedByList[Item]:
228 continue
229 # insert Item into Q, if Item has no other incoming edges
230 Q.insert(0, Item)
231
232 #
233 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
234 #
235 for Item in LibraryList:
236 if ConsumedByList[Item] and Item in Constructor and len(Constructor) > 1:
237 if FileName:
238 ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join(str(L) for L in ConsumedByList[Item])
239 EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),
240 ExtraData=ErrorMessage, File=FileName)
241 else:
242 return []
243 if Item not in SortedLibraryList:
244 SortedLibraryList.append(Item)
245
246 #
247 # Build the list of constructor and destructor names
248 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
249 #
250 SortedLibraryList.reverse()
251 Module.LibInstances = SortedLibraryList
252 SortedLibraryList = [lib.SetReferenceModule(Module) for lib in SortedLibraryList]
253 return SortedLibraryList