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