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