]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
BaseTools: Correct CCFLAG for PcdValueInit
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / WorkspaceCommon.py
1 ## @file
2 # Common routines used by workspace
3 #
4 # Copyright (c) 2012 - 2018, 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 __future__ import absolute_import
15 from collections import OrderedDict, defaultdict
16 from Common.DataType import SUP_MODULE_USER_DEFINED
17 from .BuildClassObject import LibraryClassObject
18 import Common.GlobalData as GlobalData
19 from Workspace.BuildClassObject import StructurePcd
20 from Common.BuildToolError import RESOURCE_NOT_AVAILABLE
21 from Common.BuildToolError import OPTION_MISSING
22 from Common.BuildToolError import BUILD_ERROR
23
24 class OrderedListDict(OrderedDict):
25 def __init__(self, *args, **kwargs):
26 super(OrderedListDict, self).__init__(*args, **kwargs)
27 self.default_factory = list
28
29 def __missing__(self, key):
30 self[key] = Value = self.default_factory()
31 return Value
32
33 ## Get all packages from platform for specified arch, target and toolchain
34 #
35 # @param Platform: DscBuildData instance
36 # @param BuildDatabase: The database saves all data for all metafiles
37 # @param Arch: Current arch
38 # @param Target: Current target
39 # @param Toolchain: Current toolchain
40 # @retval: List of packages which are DecBuildData instances
41 #
42 def GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain):
43 PkgSet = set()
44 for ModuleFile in Platform.Modules:
45 Data = BuildDatabase[ModuleFile, Arch, Target, Toolchain]
46 PkgSet.update(Data.Packages)
47 for Lib in GetLiabraryInstances(Data, Platform, BuildDatabase, Arch, Target, Toolchain):
48 PkgSet.update(Lib.Packages)
49 return list(PkgSet)
50
51 ## Get all declared PCD from platform for specified arch, target and toolchain
52 #
53 # @param Platform: DscBuildData instance
54 # @param BuildDatabase: The database saves all data for all metafiles
55 # @param Arch: Current arch
56 # @param Target: Current target
57 # @param Toolchain: Current toolchain
58 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
59 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
60 #
61 def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain, additionalPkgs):
62 PkgList = GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain)
63 PkgList = set(PkgList)
64 PkgList |= additionalPkgs
65 DecPcds = {}
66 GuidDict = {}
67 for Pkg in PkgList:
68 Guids = Pkg.Guids
69 GuidDict.update(Guids)
70 for Pcd in Pkg.Pcds:
71 PcdCName = Pcd[0]
72 PcdTokenName = Pcd[1]
73 if GlobalData.MixedPcd:
74 for PcdItem in GlobalData.MixedPcd:
75 if (PcdCName, PcdTokenName) in GlobalData.MixedPcd[PcdItem]:
76 PcdCName = PcdItem[0]
77 break
78 if (PcdCName, PcdTokenName) not in DecPcds:
79 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]
80 return DecPcds, GuidDict
81
82 ## Get all dependent libraries for a module
83 #
84 # @param Module: InfBuildData instance
85 # @param Platform: DscBuildData instance
86 # @param BuildDatabase: The database saves all data for all metafiles
87 # @param Arch: Current arch
88 # @param Target: Current target
89 # @param Toolchain: Current toolchain
90 # @retval: List of dependent libraries which are InfBuildData instances
91 #
92 def GetLiabraryInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain):
93 if Module.AutoGenVersion >= 0x00010005:
94 return GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain)
95 else:
96 return _ResolveLibraryReference(Module, Platform)
97
98 def GetModuleLibInstances(Module, Platform, BuildDatabase, Arch, Target, Toolchain, FileName = '', EdkLogger = None):
99 ModuleType = Module.ModuleType
100
101 # add forced library instances (specified under LibraryClasses sections)
102 #
103 # If a module has a MODULE_TYPE of USER_DEFINED,
104 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
105 #
106 if Module.ModuleType != SUP_MODULE_USER_DEFINED:
107 for LibraryClass in Platform.LibraryClasses.GetKeys():
108 if LibraryClass.startswith("NULL") and Platform.LibraryClasses[LibraryClass, Module.ModuleType]:
109 Module.LibraryClasses[LibraryClass] = Platform.LibraryClasses[LibraryClass, Module.ModuleType]
110
111 # add forced library instances (specified in module overrides)
112 for LibraryClass in Platform.Modules[str(Module)].LibraryClasses:
113 if LibraryClass.startswith("NULL"):
114 Module.LibraryClasses[LibraryClass] = Platform.Modules[str(Module)].LibraryClasses[LibraryClass]
115
116 # EdkII module
117 LibraryConsumerList = [Module]
118 Constructor = []
119 ConsumedByList = OrderedListDict()
120 LibraryInstance = OrderedDict()
121
122 if FileName:
123 EdkLogger.verbose("")
124 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), Arch))
125
126 while len(LibraryConsumerList) > 0:
127 M = LibraryConsumerList.pop()
128 for LibraryClassName in M.LibraryClasses:
129 if LibraryClassName not in LibraryInstance:
130 # override library instance for this module
131 LibraryPath = Platform.Modules[str(Module)].LibraryClasses.get(LibraryClassName,Platform.LibraryClasses[LibraryClassName, ModuleType])
132 if LibraryPath is None:
133 LibraryPath = M.LibraryClasses.get(LibraryClassName)
134 if LibraryPath is None:
135 if FileName:
136 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
137 "Instance of library class [%s] is not found" % LibraryClassName,
138 File=FileName,
139 ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), Arch, str(Module)))
140 else:
141 return []
142
143 LibraryModule = BuildDatabase[LibraryPath, Arch, Target, Toolchain]
144 # for those forced library instance (NULL library), add a fake library class
145 if LibraryClassName.startswith("NULL"):
146 LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))
147 elif LibraryModule.LibraryClass is None \
148 or len(LibraryModule.LibraryClass) == 0 \
149 or (ModuleType != SUP_MODULE_USER_DEFINED
150 and ModuleType not in LibraryModule.LibraryClass[0].SupModList):
151 # only USER_DEFINED can link against any library instance despite of its SupModList
152 if FileName:
153 EdkLogger.error("build", OPTION_MISSING,
154 "Module type [%s] is not supported by library instance [%s]" \
155 % (ModuleType, LibraryPath), File=FileName,
156 ExtraData="consumed by [%s]" % str(Module))
157 else:
158 return []
159
160 LibraryInstance[LibraryClassName] = LibraryModule
161 LibraryConsumerList.append(LibraryModule)
162 if FileName:
163 EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))
164 else:
165 LibraryModule = LibraryInstance[LibraryClassName]
166
167 if LibraryModule is None:
168 continue
169
170 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:
171 Constructor.append(LibraryModule)
172
173 # don't add current module itself to consumer list
174 if M != Module:
175 if M in ConsumedByList[LibraryModule]:
176 continue
177 ConsumedByList[LibraryModule].append(M)
178 #
179 # Initialize the sorted output list to the empty set
180 #
181 SortedLibraryList = []
182 #
183 # Q <- Set of all nodes with no incoming edges
184 #
185 LibraryList = [] #LibraryInstance.values()
186 Q = []
187 for LibraryClassName in LibraryInstance:
188 M = LibraryInstance[LibraryClassName]
189 LibraryList.append(M)
190 if not ConsumedByList[M]:
191 Q.append(M)
192
193 #
194 # start the DAG algorithm
195 #
196 while True:
197 EdgeRemoved = True
198 while Q == [] and EdgeRemoved:
199 EdgeRemoved = False
200 # for each node Item with a Constructor
201 for Item in LibraryList:
202 if Item not in Constructor:
203 continue
204 # for each Node without a constructor with an edge e from Item to Node
205 for Node in ConsumedByList[Item]:
206 if Node in Constructor:
207 continue
208 # remove edge e from the graph if Node has no constructor
209 ConsumedByList[Item].remove(Node)
210 EdgeRemoved = True
211 if not ConsumedByList[Item]:
212 # insert Item into Q
213 Q.insert(0, Item)
214 break
215 if Q != []:
216 break
217 # DAG is done if there's no more incoming edge for all nodes
218 if Q == []:
219 break
220
221 # remove node from Q
222 Node = Q.pop()
223 # output Node
224 SortedLibraryList.append(Node)
225
226 # for each node Item with an edge e from Node to Item do
227 for Item in LibraryList:
228 if Node not in ConsumedByList[Item]:
229 continue
230 # remove edge e from the graph
231 ConsumedByList[Item].remove(Node)
232
233 if ConsumedByList[Item]:
234 continue
235 # insert Item into Q, if Item has no other incoming edges
236 Q.insert(0, Item)
237
238 #
239 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
240 #
241 for Item in LibraryList:
242 if ConsumedByList[Item] and Item in Constructor and len(Constructor) > 1:
243 if FileName:
244 ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join(str(L) for L in ConsumedByList[Item])
245 EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),
246 ExtraData=ErrorMessage, File=FileName)
247 else:
248 return []
249 if Item not in SortedLibraryList:
250 SortedLibraryList.append(Item)
251
252 #
253 # Build the list of constructor and destructir names
254 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
255 #
256 SortedLibraryList.reverse()
257 return SortedLibraryList
258
259 def _ResolveLibraryReference(Module, Platform):
260 LibraryConsumerList = [Module]
261
262 # "CompilerStub" is a must for Edk modules
263 if Module.Libraries:
264 Module.Libraries.append("CompilerStub")
265 LibraryList = []
266 while len(LibraryConsumerList) > 0:
267 M = LibraryConsumerList.pop()
268 for LibraryName in M.Libraries:
269 Library = Platform.LibraryClasses[LibraryName, ':dummy:']
270 if Library is None:
271 for Key in Platform.LibraryClasses.data:
272 if LibraryName.upper() == Key.upper():
273 Library = Platform.LibraryClasses[Key, ':dummy:']
274 break
275 if Library is None:
276 continue
277
278 if Library not in LibraryList:
279 LibraryList.append(Library)
280 LibraryConsumerList.append(Library)
281 return LibraryList