]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 return DecPcds, GuidDict
79
80 ## Get all dependent libraries for a module
81 #
82 # @param Module: InfBuildData instance
83 # @param Platform: DscBuildData instance
84 # @param BuildDatabase: The database saves all data for all metafiles
85 # @param Arch: Current arch
86 # @param Target: Current target
87 # @param Toolchain: Current toolchain
88 # @retval: List of dependent libraries which are InfBuildData instances
89 #
90 def GetLiabraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):
91 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain,Platform.MetaFile,EdkLogger)
92
93 def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):
94 if Module.LibInstances:
95 return Module.LibInstances
96 ModuleType = Module.ModuleType
97
98 # add forced library instances (specified under LibraryClasses sections)
99 #
100 # If a module has a MODULE_TYPE of USER_DEFINED,
101 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
102 #
103 if Module.ModuleType != SUP_MODULE_USER_DEFINED:
104 for LibraryClass in Platform.LibraryClasses.GetKeys():
105 if LibraryClass.startswith("NULL") and Platform.LibraryClasses[LibraryClass, Module.ModuleType]:
106 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]
107
108 # add forced library instances (specified in module overrides)
109 for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:
110 if LibraryClass.startswith("NULL"):
111 Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]
112
113 # EdkII module
114 LibraryConsumerList = [Module]
115 Constructor = []
116 ConsumedByList = OrderedListDict()
117 LibraryInstance = OrderedDict()
118
119 if not Module.LibraryClass:
120 EdkLogger.verbose("")
121 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))
122
123 while len(LibraryConsumerList) > 0:
124 M = LibraryConsumerList.pop()
125 for LibraryClassName in M.LibraryClasses:
126 if LibraryClassName not in LibraryInstance:
127 # override library instance for this module
128 LibraryPath = Platform.Modules[str(Module)].LibraryClasses.get(LibraryClassName,Platform.LibraryClasses[LibraryClassName, ModuleType])
129 if LibraryPath is None:
130 LibraryPath = M.LibraryClasses.get(LibraryClassName)
131 if LibraryPath is None:
132 if not Module.LibraryClass:
133 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
134 "Instance of library class [%s] is not found" % LibraryClassName,
135 File=FileName,
136 ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), Arch, str(Module)))
137 else:
138 return []
139
140 LibraryModule = BuildDatabase[LibraryPath, Arch, Target, Toolchain]
141 # for those forced library instance (NULL library), add a fake library class
142 if LibraryClassName.startswith("NULL"):
143 LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))
144 elif LibraryModule.LibraryClass is None \
145 or len(LibraryModule.LibraryClass) == 0 \
146 or (ModuleType != SUP_MODULE_USER_DEFINED and ModuleType != SUP_MODULE_HOST_APPLICATION
147 and ModuleType not in LibraryModule.LibraryClass[0].SupModList):
148 # only USER_DEFINED can link against any library instance despite of its SupModList
149 if not Module.LibraryClass:
150 EdkLogger.error("build", OPTION_MISSING,
151 "Module type [%s] is not supported by library instance [%s]" \
152 % (ModuleType, LibraryPath), File=FileName,
153 ExtraData="consumed by library instance [%s] which is consumed by module [%s]" \
154 % (str(M), str(Module))
155 )
156 else:
157 return []
158
159 LibraryInstance[LibraryClassName] = LibraryModule
160 LibraryConsumerList.append(LibraryModule)
161 if not Module.LibraryClass:
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 not Module.LibraryClass:
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 destructor 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 Module.LibInstances = SortedLibraryList
257 SortedLibraryList = [lib.SetReferenceModule(Module) for lib in SortedLibraryList]
258 return SortedLibraryList