]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
BaseTools: Remove unused logic for EDKI
[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 __future__ import absolute_import
15 from collections import OrderedDict, defaultdict
16 from Common.DataType import SUP_MODULE_USER_DEFINED
17 from .BuildClassObject import LibraryClassObject
18 import Common.GlobalData as GlobalData
19 from Workspace.BuildClassObject import StructurePcd
20 from Common.BuildToolError import RESOURCE_NOT_AVAILABLE
21 from Common.BuildToolError import OPTION_MISSING
22 from Common.BuildToolError import BUILD_ERROR
23
24 class OrderedListDict(OrderedDict):
25 def __init__(self, *args, **kwargs):
26 super(OrderedListDict, self).__init__(*args, **kwargs)
27 self.default_factory = list
28
29 def __missing__(self, key):
30 self[key] = Value = self.default_factory()
31 return Value
32
33 ## Get all packages from platform for specified arch, target and toolchain
34 #
35 # @param Platform: DscBuildData instance
36 # @param BuildDatabase: The database saves all data for all metafiles
37 # @param Arch: Current arch
38 # @param Target: Current target
39 # @param Toolchain: Current toolchain
40 # @retval: List of packages which are DecBuildData instances
41 #
42 def GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain):
43 PkgSet = set()
44 for ModuleFile in Platform.Modules:
45 Data = BuildDatabase[ModuleFile, Arch, Target, Toolchain]
46 PkgSet.update(Data.Packages)
47 for Lib in GetLiabraryInstances(Data, Platform, BuildDatabase, Arch, Target, Toolchain):
48 PkgSet.update(Lib.Packages)
49 return list(PkgSet)
50
51 ## Get all declared PCD from platform for specified arch, target and toolchain
52 #
53 # @param Platform: DscBuildData instance
54 # @param BuildDatabase: The database saves all data for all metafiles
55 # @param Arch: Current arch
56 # @param Target: Current target
57 # @param Toolchain: Current toolchain
58 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
59 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
60 #
61 def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain, additionalPkgs):
62 PkgList = GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain)
63 PkgList = set(PkgList)
64 PkgList |= additionalPkgs
65 DecPcds = {}
66 GuidDict = {}
67 for Pkg in PkgList:
68 Guids = Pkg.Guids
69 GuidDict.update(Guids)
70 for Pcd in Pkg.Pcds:
71 PcdCName = Pcd[0]
72 PcdTokenName = Pcd[1]
73 if GlobalData.MixedPcd:
74 for PcdItem in GlobalData.MixedPcd:
75 if (PcdCName, PcdTokenName) in GlobalData.MixedPcd[PcdItem]:
76 PcdCName = PcdItem[0]
77 break
78 if (PcdCName, PcdTokenName) not in DecPcds:
79 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]
80 return DecPcds, GuidDict
81
82 ## Get all dependent libraries for a module
83 #
84 # @param Module: InfBuildData instance
85 # @param Platform: DscBuildData instance
86 # @param BuildDatabase: The database saves all data for all metafiles
87 # @param Arch: Current arch
88 # @param Target: Current target
89 # @param Toolchain: Current toolchain
90 # @retval: List of dependent libraries which are InfBuildData instances
91 #
92 def GetLiabraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):
93 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)
94
95 def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):
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 FileName:
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 FileName:
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
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 FileName:
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 [%s]" % str(Module))
154 else:
155 return []
156
157 LibraryInstance[LibraryClassName] = LibraryModule
158 LibraryConsumerList.append(LibraryModule)
159 if FileName:
160 EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))
161 else:
162 LibraryModule = LibraryInstance[LibraryClassName]
163
164 if LibraryModule is None:
165 continue
166
167 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:
168 Constructor.append(LibraryModule)
169
170 # don't add current module itself to consumer list
171 if M != Module:
172 if M in ConsumedByList[LibraryModule]:
173 continue
174 ConsumedByList[LibraryModule].append(M)
175 #
176 # Initialize the sorted output list to the empty set
177 #
178 SortedLibraryList = []
179 #
180 # Q <- Set of all nodes with no incoming edges
181 #
182 LibraryList = [] #LibraryInstance.values()
183 Q = []
184 for LibraryClassName in LibraryInstance:
185 M = LibraryInstance[LibraryClassName]
186 LibraryList.append(M)
187 if not ConsumedByList[M]:
188 Q.append(M)
189
190 #
191 # start the DAG algorithm
192 #
193 while True:
194 EdgeRemoved = True
195 while Q == [] and EdgeRemoved:
196 EdgeRemoved = False
197 # for each node Item with a Constructor
198 for Item in LibraryList:
199 if Item not in Constructor:
200 continue
201 # for each Node without a constructor with an edge e from Item to Node
202 for Node in ConsumedByList[Item]:
203 if Node in Constructor:
204 continue
205 # remove edge e from the graph if Node has no constructor
206 ConsumedByList[Item].remove(Node)
207 EdgeRemoved = True
208 if not ConsumedByList[Item]:
209 # insert Item into Q
210 Q.insert(0, Item)
211 break
212 if Q != []:
213 break
214 # DAG is done if there's no more incoming edge for all nodes
215 if Q == []:
216 break
217
218 # remove node from Q
219 Node = Q.pop()
220 # output Node
221 SortedLibraryList.append(Node)
222
223 # for each node Item with an edge e from Node to Item do
224 for Item in LibraryList:
225 if Node not in ConsumedByList[Item]:
226 continue
227 # remove edge e from the graph
228 ConsumedByList[Item].remove(Node)
229
230 if ConsumedByList[Item]:
231 continue
232 # insert Item into Q, if Item has no other incoming edges
233 Q.insert(0, Item)
234
235 #
236 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
237 #
238 for Item in LibraryList:
239 if ConsumedByList[Item] and Item in Constructor and len(Constructor) > 1:
240 if FileName:
241 ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join(str(L) for L in ConsumedByList[Item])
242 EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),
243 ExtraData=ErrorMessage, File=FileName)
244 else:
245 return []
246 if Item not in SortedLibraryList:
247 SortedLibraryList.append(Item)
248
249 #
250 # Build the list of constructor and destructir names
251 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
252 #
253 SortedLibraryList.reverse()
254 return SortedLibraryList