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