]>
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 | |
1f1c6712 | 14 | from collections import OrderedDict, defaultdict\r |
4afd3d04 LG |
15 | from Common.DataType import SUP_MODULE_USER_DEFINED\r |
16 | from BuildClassObject import LibraryClassObject\r | |
2a29017e | 17 | import Common.GlobalData as GlobalData\r |
ae7b6df8 | 18 | from Workspace.BuildClassObject import StructurePcd\r |
87a46244 MH |
19 | from Common.BuildToolError import RESOURCE_NOT_AVAILABLE\r |
20 | from Common.BuildToolError import OPTION_MISSING\r | |
21 | from Common.BuildToolError import BUILD_ERROR\r | |
4afd3d04 | 22 | \r |
1f1c6712 CJ |
23 | class OrderedListDict(OrderedDict, defaultdict):\r |
24 | def __init__(self, *args, **kwargs):\r | |
25 | super(OrderedListDict, self).__init__(*args, **kwargs)\r | |
26 | self.default_factory = list\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 | |
88 | if Module.AutoGenVersion >= 0x00010005:\r | |
c14b5861 | 89 | return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)\r |
4afd3d04 LG |
90 | else:\r |
91 | return _ResolveLibraryReference(Module, Platform)\r | |
92 | \r | |
c14b5861 | 93 | def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):\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 | |
101 | if Module.ModuleType != SUP_MODULE_USER_DEFINED:\r | |
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 |
c14b5861 JC |
117 | if FileName:\r |
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 | |
c14b5861 JC |
126 | if LibraryClassName in Platform.Modules[str(Module)].LibraryClasses:\r |
127 | LibraryPath = Platform.Modules[str(Module)].LibraryClasses[LibraryClassName]\r | |
4afd3d04 LG |
128 | else:\r |
129 | LibraryPath = Platform.LibraryClasses[LibraryClassName, ModuleType]\r | |
4231a819 | 130 | if LibraryPath is None or LibraryPath == "":\r |
4afd3d04 | 131 | LibraryPath = M.LibraryClasses[LibraryClassName]\r |
4231a819 | 132 | if LibraryPath is None or LibraryPath == "":\r |
c14b5861 JC |
133 | if FileName:\r |
134 | EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,\r | |
135 | "Instance of library class [%s] is not found" % LibraryClassName,\r | |
136 | File=FileName,\r | |
137 | ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), Arch, str(Module)))\r | |
138 | else:\r | |
139 | return []\r | |
4afd3d04 LG |
140 | \r |
141 | LibraryModule = BuildDatabase[LibraryPath, Arch, Target, Toolchain]\r | |
142 | # for those forced library instance (NULL library), add a fake library class\r | |
143 | if LibraryClassName.startswith("NULL"):\r | |
144 | LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))\r | |
4231a819 | 145 | elif LibraryModule.LibraryClass is None \\r |
4afd3d04 | 146 | or len(LibraryModule.LibraryClass) == 0 \\r |
8bb63e37 | 147 | or (ModuleType != SUP_MODULE_USER_DEFINED\r |
4afd3d04 LG |
148 | and ModuleType not in LibraryModule.LibraryClass[0].SupModList):\r |
149 | # only USER_DEFINED can link against any library instance despite of its SupModList\r | |
c14b5861 JC |
150 | if FileName:\r |
151 | EdkLogger.error("build", OPTION_MISSING,\r | |
152 | "Module type [%s] is not supported by library instance [%s]" \\r | |
153 | % (ModuleType, LibraryPath), File=FileName,\r | |
154 | ExtraData="consumed by [%s]" % str(Module))\r | |
155 | else:\r | |
156 | return []\r | |
4afd3d04 LG |
157 | \r |
158 | LibraryInstance[LibraryClassName] = LibraryModule\r | |
159 | LibraryConsumerList.append(LibraryModule)\r | |
c14b5861 JC |
160 | if FileName:\r |
161 | EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))\r | |
4afd3d04 LG |
162 | else:\r |
163 | LibraryModule = LibraryInstance[LibraryClassName]\r | |
164 | \r | |
4231a819 | 165 | if LibraryModule is None:\r |
4afd3d04 LG |
166 | continue\r |
167 | \r | |
168 | if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:\r | |
169 | Constructor.append(LibraryModule)\r | |
170 | \r | |
4afd3d04 LG |
171 | # don't add current module itself to consumer list\r |
172 | if M != Module:\r | |
173 | if M in ConsumedByList[LibraryModule]:\r | |
174 | continue\r | |
175 | ConsumedByList[LibraryModule].append(M)\r | |
176 | #\r | |
177 | # Initialize the sorted output list to the empty set\r | |
178 | #\r | |
179 | SortedLibraryList = []\r | |
180 | #\r | |
181 | # Q <- Set of all nodes with no incoming edges\r | |
182 | #\r | |
183 | LibraryList = [] #LibraryInstance.values()\r | |
184 | Q = []\r | |
185 | for LibraryClassName in LibraryInstance:\r | |
186 | M = LibraryInstance[LibraryClassName]\r | |
187 | LibraryList.append(M)\r | |
c14b5861 | 188 | if not ConsumedByList[M]:\r |
4afd3d04 LG |
189 | Q.append(M)\r |
190 | \r | |
191 | #\r | |
192 | # start the DAG algorithm\r | |
193 | #\r | |
194 | while True:\r | |
195 | EdgeRemoved = True\r | |
196 | while Q == [] and EdgeRemoved:\r | |
197 | EdgeRemoved = False\r | |
198 | # for each node Item with a Constructor\r | |
199 | for Item in LibraryList:\r | |
200 | if Item not in Constructor:\r | |
201 | continue\r | |
202 | # for each Node without a constructor with an edge e from Item to Node\r | |
203 | for Node in ConsumedByList[Item]:\r | |
204 | if Node in Constructor:\r | |
205 | continue\r | |
206 | # remove edge e from the graph if Node has no constructor\r | |
207 | ConsumedByList[Item].remove(Node)\r | |
208 | EdgeRemoved = True\r | |
c14b5861 | 209 | if not ConsumedByList[Item]:\r |
4afd3d04 LG |
210 | # insert Item into Q\r |
211 | Q.insert(0, Item)\r | |
212 | break\r | |
213 | if Q != []:\r | |
214 | break\r | |
215 | # DAG is done if there's no more incoming edge for all nodes\r | |
216 | if Q == []:\r | |
217 | break\r | |
218 | \r | |
219 | # remove node from Q\r | |
220 | Node = Q.pop()\r | |
221 | # output Node\r | |
222 | SortedLibraryList.append(Node)\r | |
223 | \r | |
224 | # for each node Item with an edge e from Node to Item do\r | |
225 | for Item in LibraryList:\r | |
226 | if Node not in ConsumedByList[Item]:\r | |
227 | continue\r | |
228 | # remove edge e from the graph\r | |
229 | ConsumedByList[Item].remove(Node)\r | |
230 | \r | |
c14b5861 | 231 | if ConsumedByList[Item]:\r |
4afd3d04 LG |
232 | continue\r |
233 | # insert Item into Q, if Item has no other incoming edges\r | |
234 | Q.insert(0, Item)\r | |
235 | \r | |
236 | #\r | |
237 | # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle\r | |
238 | #\r | |
239 | for Item in LibraryList:\r | |
c14b5861 JC |
240 | if ConsumedByList[Item] and Item in Constructor and len(Constructor) > 1:\r |
241 | if FileName:\r | |
242 | ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join(str(L) for L in ConsumedByList[Item])\r | |
243 | EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),\r | |
244 | ExtraData=ErrorMessage, File=FileName)\r | |
245 | else:\r | |
246 | return []\r | |
4afd3d04 LG |
247 | if Item not in SortedLibraryList:\r |
248 | SortedLibraryList.append(Item)\r | |
249 | \r | |
250 | #\r | |
251 | # Build the list of constructor and destructir names\r | |
252 | # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order\r | |
253 | #\r | |
254 | SortedLibraryList.reverse()\r | |
255 | return SortedLibraryList\r | |
256 | \r | |
257 | def _ResolveLibraryReference(Module, Platform):\r | |
258 | LibraryConsumerList = [Module]\r | |
259 | \r | |
260 | # "CompilerStub" is a must for Edk modules\r | |
261 | if Module.Libraries:\r | |
262 | Module.Libraries.append("CompilerStub")\r | |
263 | LibraryList = []\r | |
264 | while len(LibraryConsumerList) > 0:\r | |
265 | M = LibraryConsumerList.pop()\r | |
266 | for LibraryName in M.Libraries:\r | |
267 | Library = Platform.LibraryClasses[LibraryName, ':dummy:']\r | |
4231a819 | 268 | if Library is None:\r |
9eb87141 | 269 | for Key in Platform.LibraryClasses.data:\r |
4afd3d04 LG |
270 | if LibraryName.upper() == Key.upper():\r |
271 | Library = Platform.LibraryClasses[Key, ':dummy:']\r | |
272 | break\r | |
4231a819 | 273 | if Library is None:\r |
4afd3d04 LG |
274 | continue\r |
275 | \r | |
276 | if Library not in LibraryList:\r | |
277 | LibraryList.append(Library)\r | |
278 | LibraryConsumerList.append(Library)\r | |
279 | return LibraryList\r |