]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
BaseTools: add missing newlines at end of files
[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
abc0155b 18import Common.EdkLogger as EdkLogger\r
4afd3d04 19\r
abb8e6e9 20class OrderedListDict(OrderedDict):\r
1f1c6712 21 def __init__(self, *args, **kwargs):\r
1ccc4d89 22 super(OrderedListDict, self).__init__(*args, **kwargs)\r
1f1c6712
CJ
23 self.default_factory = list\r
24\r
abb8e6e9
FY
25 def __missing__(self, key):\r
26 self[key] = Value = self.default_factory()\r
27 return Value\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
abc0155b 89 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain,Platform.MetaFile,EdkLogger)\r
4afd3d04 90\r
c14b5861 91def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):\r
e8449e1d
FB
92 if Module.LibInstances:\r
93 return Module.LibInstances\r
4afd3d04
LG
94 ModuleType = Module.ModuleType\r
95\r
4afd3d04
LG
96 # add forced library instances (specified under LibraryClasses sections)\r
97 #\r
98 # If a module has a MODULE_TYPE of USER_DEFINED,\r
99 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.\r
100 #\r
a79841a0 101 if Module.ModuleType != SUP_MODULE_USER_DEFINED and Module.ModuleType != SUP_MODULE_HOST_APPLICATION:\r
4afd3d04
LG
102 for LibraryClass in Platform.LibraryClasses.GetKeys():\r
103 if LibraryClass.startswith("NULL") and Platform.LibraryClasses[LibraryClass, Module.ModuleType]:\r
104 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]\r
105\r
106 # add forced library instances (specified in module overrides)\r
c14b5861 107 for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:\r
4afd3d04 108 if LibraryClass.startswith("NULL"):\r
c14b5861 109 Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]\r
4afd3d04
LG
110\r
111 # EdkII module\r
112 LibraryConsumerList = [Module]\r
113 Constructor = []\r
1f1c6712 114 ConsumedByList = OrderedListDict()\r
a0767bae 115 LibraryInstance = OrderedDict()\r
4afd3d04 116\r
abc0155b 117 if not Module.LibraryClass:\r
c14b5861
JC
118 EdkLogger.verbose("")\r
119 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))\r
120\r
4afd3d04
LG
121 while len(LibraryConsumerList) > 0:\r
122 M = LibraryConsumerList.pop()\r
123 for LibraryClassName in M.LibraryClasses:\r
124 if LibraryClassName not in LibraryInstance:\r
125 # override library instance for this module\r
4e375707
B
126 LibraryPath = Platform.Modules[str(Module)].LibraryClasses.get(LibraryClassName,Platform.LibraryClasses[LibraryClassName, ModuleType])\r
127 if LibraryPath is None:\r
128 LibraryPath = M.LibraryClasses.get(LibraryClassName)\r
129 if LibraryPath is None:\r
abc0155b 130 if not Module.LibraryClass:\r
c14b5861
JC
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
a79841a0 144 or (ModuleType != SUP_MODULE_USER_DEFINED and ModuleType != SUP_MODULE_HOST_APPLICATION\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
abc0155b 147 if not Module.LibraryClass:\r
c14b5861
JC
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
abc0155b 157 if not Module.LibraryClass:\r
c14b5861 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 237 if ConsumedByList[Item] and Item in Constructor and len(Constructor) > 1:\r
abc0155b 238 if not Module.LibraryClass:\r
c14b5861
JC
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
fb0b35e0 248 # Build the list of constructor and destructor names\r
4afd3d04
LG
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
e8449e1d
FB
252 Module.LibInstances = SortedLibraryList\r
253 SortedLibraryList = [lib.SetReferenceModule(Module) for lib in SortedLibraryList]\r
4afd3d04 254 return SortedLibraryList\r