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