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