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