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