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