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