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