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