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