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