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