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