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