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