]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
6564a34ba7241316f893bdb85cb9bada77a06948
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / WorkspaceCommon.py
1 ## @file
2 # Common routines used by workspace
3 #
4 # Copyright (c) 2012 - 2020, 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 import Common.EdkLogger as EdkLogger
19
20 class OrderedListDict(OrderedDict):
21 def __init__(self, *args, **kwargs):
22 super(OrderedListDict, self).__init__(*args, **kwargs)
23 self.default_factory = list
24
25 def __missing__(self, key):
26 self[key] = Value = self.default_factory()
27 return Value
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 if Platform.Packages:
41 PkgSet.update(Platform.Packages)
42 for ModuleFile in Platform.Modules:
43 Data = BuildDatabase[ModuleFile, Arch, Target, Toolchain]
44 PkgSet.update(Data.Packages)
45 for Lib in GetLiabraryInstances(Data, Platform, BuildDatabase, Arch, Target, Toolchain):
46 PkgSet.update(Lib.Packages)
47 return list(PkgSet)
48
49 ## Get all declared PCD from platform for specified arch, target and toolchain
50 #
51 # @param Platform: DscBuildData instance
52 # @param BuildDatabase: The database saves all data for all metafiles
53 # @param Arch: Current arch
54 # @param Target: Current target
55 # @param Toolchain: Current toolchain
56 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
57 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
58 #
59 def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain, additionalPkgs):
60 PkgList = GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain)
61 PkgList = set(PkgList)
62 PkgList |= additionalPkgs
63 DecPcds = {}
64 GuidDict = {}
65 for Pkg in PkgList:
66 Guids = Pkg.Guids
67 GuidDict.update(Guids)
68 for Pcd in Pkg.Pcds:
69 PcdCName = Pcd[0]
70 PcdTokenName = Pcd[1]
71 if GlobalData.MixedPcd:
72 for PcdItem in GlobalData.MixedPcd:
73 if (PcdCName, PcdTokenName) in GlobalData.MixedPcd[PcdItem]:
74 PcdCName = PcdItem[0]
75 break
76 if (PcdCName, PcdTokenName) not in DecPcds:
77 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]
78 if not GlobalData.gPlatformFinalPcds.get(Arch):
79 GlobalData.gPlatformFinalPcds[Arch] = OrderedDict()
80 for Name,Guid in DecPcds:
81 if DecPcds[Name,Guid].Type == 'FeatureFlag' or DecPcds[Name, Guid].Type == 'FixedAtBuild':
82 GlobalData.gPlatformFinalPcds[Arch]['%s.%s'%(Guid, Name)]=DecPcds[Name, Guid].DefaultValue
83 return DecPcds, GuidDict
84
85 ## Get all dependent libraries for a module
86 #
87 # @param Module: InfBuildData instance
88 # @param Platform: DscBuildData instance
89 # @param BuildDatabase: The database saves all data for all metafiles
90 # @param Arch: Current arch
91 # @param Target: Current target
92 # @param Toolchain: Current toolchain
93 # @retval: List of dependent libraries which are InfBuildData instances
94 #
95 def GetLiabraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):
96 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain,Platform.MetaFile,EdkLogger)
97
98 def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):
99 if Module.LibInstances:
100 return Module.LibInstances
101 ModuleType = Module.ModuleType
102
103 # add forced library instances (specified under LibraryClasses sections)
104 #
105 # If a module has a MODULE_TYPE of USER_DEFINED,
106 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
107 #
108 if Module.ModuleType != SUP_MODULE_USER_DEFINED:
109 for LibraryClass in Platform.LibraryClasses.GetKeys():
110 if LibraryClass.startswith("NULL") and Platform.LibraryClasses[LibraryClass, Module.ModuleType]:
111 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]
112
113 # add forced library instances (specified in module overrides)
114 for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:
115 if LibraryClass.startswith("NULL"):
116 Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]
117
118 # EdkII module
119 LibraryConsumerList = [Module]
120 Constructor = []
121 ConsumedByList = OrderedListDict()
122 LibraryInstance = OrderedDict()
123
124 if not Module.LibraryClass:
125 EdkLogger.verbose("")
126 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))
127
128 while len(LibraryConsumerList) > 0:
129 M = LibraryConsumerList.pop()
130 for LibraryClassName in M.LibraryClasses:
131 if LibraryClassName not in LibraryInstance:
132 # override library instance for this module
133 LibraryPath = Platform.Modules[str(Module)].LibraryClasses.get(LibraryClassName,Platform.LibraryClasses[LibraryClassName, ModuleType])
134 if LibraryPath is None:
135 LibraryPath = M.LibraryClasses.get(LibraryClassName)
136 if LibraryPath is None:
137 if not Module.LibraryClass:
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 and ModuleType != SUP_MODULE_HOST_APPLICATION
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 not Module.LibraryClass:
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 not Module.LibraryClass:
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 not Module.LibraryClass:
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 destructor 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 Module.LibInstances = SortedLibraryList
260 SortedLibraryList = [lib.SetReferenceModule(Module) for lib in SortedLibraryList]
261 return SortedLibraryList