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