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