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