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