]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
EmbeddedPkg/GdbSerialLib: avoid left shift of negative quantity
[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
1f1c6712 14from collections import OrderedDict, defaultdict\r
4afd3d04
LG
15from Common.DataType import SUP_MODULE_USER_DEFINED\r
16from BuildClassObject import LibraryClassObject\r
2a29017e 17import Common.GlobalData as GlobalData\r
ae7b6df8 18from Workspace.BuildClassObject import StructurePcd\r
4afd3d04 19\r
1f1c6712
CJ
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
4afd3d04
LG
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
726c501c 51# @retval: A dictionary contains real GUIDs of TokenSpaceGuid\r
4afd3d04 52#\r
2b8a6c44 53def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain,additionalPkgs):\r
4afd3d04 54 PkgList = GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain)\r
2b8a6c44
LG
55 PkgList = set(PkgList)\r
56 PkgList |= additionalPkgs\r
4afd3d04 57 DecPcds = {}\r
726c501c 58 GuidDict = {}\r
4afd3d04 59 for Pkg in PkgList:\r
726c501c
YZ
60 Guids = Pkg.Guids\r
61 GuidDict.update(Guids)\r
4afd3d04 62 for Pcd in Pkg.Pcds:\r
2a29017e
YZ
63 PcdCName = Pcd[0]\r
64 PcdTokenName = Pcd[1]\r
65 if GlobalData.MixedPcd:\r
9eb87141 66 for PcdItem in GlobalData.MixedPcd:\r
2a29017e
YZ
67 if (PcdCName, PcdTokenName) in GlobalData.MixedPcd[PcdItem]:\r
68 PcdCName = PcdItem[0]\r
69 break\r
9eb87141 70 if (PcdCName, PcdTokenName) not in DecPcds:\r
2a29017e 71 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]\r
726c501c 72 return DecPcds, GuidDict\r
4afd3d04
LG
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
c14b5861 86 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)\r
4afd3d04
LG
87 else:\r
88 return _ResolveLibraryReference(Module, Platform)\r
89\r
c14b5861 90def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):\r
4afd3d04
LG
91 ModuleType = Module.ModuleType\r
92\r
4afd3d04
LG
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
c14b5861 104 for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:\r
4afd3d04 105 if LibraryClass.startswith("NULL"):\r
c14b5861 106 Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]\r
4afd3d04
LG
107\r
108 # EdkII module\r
109 LibraryConsumerList = [Module]\r
110 Constructor = []\r
1f1c6712 111 ConsumedByList = OrderedListDict()\r
a0767bae 112 LibraryInstance = OrderedDict()\r
4afd3d04 113\r
c14b5861
JC
114 if FileName:\r
115 EdkLogger.verbose("")\r
116 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))\r
117\r
4afd3d04
LG
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
c14b5861
JC
123 if LibraryClassName in Platform.Modules[str(Module)].LibraryClasses:\r
124 LibraryPath = Platform.Modules[str(Module)].LibraryClasses[LibraryClassName]\r
4afd3d04
LG
125 else:\r
126 LibraryPath = Platform.LibraryClasses[LibraryClassName, ModuleType]\r
4231a819 127 if LibraryPath is None or LibraryPath == "":\r
4afd3d04 128 LibraryPath = M.LibraryClasses[LibraryClassName]\r
4231a819 129 if LibraryPath is None or LibraryPath == "":\r
c14b5861
JC
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
4afd3d04
LG
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
4231a819 142 elif LibraryModule.LibraryClass is None \\r
4afd3d04 143 or len(LibraryModule.LibraryClass) == 0 \\r
8bb63e37 144 or (ModuleType != SUP_MODULE_USER_DEFINED\r
4afd3d04
LG
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
c14b5861
JC
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
4afd3d04
LG
154\r
155 LibraryInstance[LibraryClassName] = LibraryModule\r
156 LibraryConsumerList.append(LibraryModule)\r
c14b5861
JC
157 if FileName:\r
158 EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))\r
4afd3d04
LG
159 else:\r
160 LibraryModule = LibraryInstance[LibraryClassName]\r
161\r
4231a819 162 if LibraryModule is None:\r
4afd3d04
LG
163 continue\r
164\r
165 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:\r
166 Constructor.append(LibraryModule)\r
167\r
4afd3d04
LG
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
c14b5861 185 if not ConsumedByList[M]:\r
4afd3d04
LG
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
c14b5861 206 if not ConsumedByList[Item]:\r
4afd3d04
LG
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
c14b5861 228 if ConsumedByList[Item]:\r
4afd3d04
LG
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
c14b5861
JC
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
4afd3d04
LG
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
4231a819 265 if Library is None:\r
9eb87141 266 for Key in Platform.LibraryClasses.data:\r
4afd3d04
LG
267 if LibraryName.upper() == Key.upper():\r
268 Library = Platform.LibraryClasses[Key, ':dummy:']\r
269 break\r
4231a819 270 if Library is None:\r
4afd3d04
LG
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