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