]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
2 # Common routines used by workspace
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
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.
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
24 class OrderedListDict(OrderedDict
):
25 def __init__(self
, *args
, **kwargs
):
26 super(OrderedListDict
, self
).__init
__(*args
, **kwargs
)
27 self
.default_factory
= list
29 def __missing__(self
, key
):
30 self
[key
] = Value
= self
.default_factory()
33 ## Get all packages from platform for specified arch, target and toolchain
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
42 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
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
)
51 ## Get all declared PCD from platform for specified arch, target and toolchain
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
61 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, additionalPkgs
):
62 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
63 PkgList
= set(PkgList
)
64 PkgList |
= additionalPkgs
69 GuidDict
.update(Guids
)
73 if GlobalData
.MixedPcd
:
74 for PcdItem
in GlobalData
.MixedPcd
:
75 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
78 if (PcdCName
, PcdTokenName
) not in DecPcds
:
79 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
80 return DecPcds
, GuidDict
82 ## Get all dependent libraries for a module
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
92 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
93 if Module
.AutoGenVersion
>= 0x00010005:
94 return GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
96 return _ResolveLibraryReference(Module
, Platform
)
98 def GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, FileName
= '', EdkLogger
= None):
99 ModuleType
= Module
.ModuleType
101 # add forced library instances (specified under LibraryClasses sections)
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.
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
]
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
]
117 LibraryConsumerList
= [Module
]
119 ConsumedByList
= OrderedListDict()
120 LibraryInstance
= OrderedDict()
123 EdkLogger
.verbose("")
124 EdkLogger
.verbose("Library instances of module [%s] [%s]:" % (str(Module
), Arch
))
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 if LibraryClassName
in Platform
.Modules
[str(Module
)].LibraryClasses
:
132 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClassName
]
134 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
135 if LibraryPath
is None or LibraryPath
== "":
136 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
137 if LibraryPath
is None or LibraryPath
== "":
139 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
140 "Instance of library class [%s] is not found" % LibraryClassName
,
142 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
146 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
147 # for those forced library instance (NULL library), add a fake library class
148 if LibraryClassName
.startswith("NULL"):
149 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
150 elif LibraryModule
.LibraryClass
is None \
151 or len(LibraryModule
.LibraryClass
) == 0 \
152 or (ModuleType
!= SUP_MODULE_USER_DEFINED
153 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
154 # only USER_DEFINED can link against any library instance despite of its SupModList
156 EdkLogger
.error("build", OPTION_MISSING
,
157 "Module type [%s] is not supported by library instance [%s]" \
158 % (ModuleType
, LibraryPath
), File
=FileName
,
159 ExtraData
="consumed by [%s]" % str(Module
))
163 LibraryInstance
[LibraryClassName
] = LibraryModule
164 LibraryConsumerList
.append(LibraryModule
)
166 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
168 LibraryModule
= LibraryInstance
[LibraryClassName
]
170 if LibraryModule
is None:
173 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
174 Constructor
.append(LibraryModule
)
176 # don't add current module itself to consumer list
178 if M
in ConsumedByList
[LibraryModule
]:
180 ConsumedByList
[LibraryModule
].append(M
)
182 # Initialize the sorted output list to the empty set
184 SortedLibraryList
= []
186 # Q <- Set of all nodes with no incoming edges
188 LibraryList
= [] #LibraryInstance.values()
190 for LibraryClassName
in LibraryInstance
:
191 M
= LibraryInstance
[LibraryClassName
]
192 LibraryList
.append(M
)
193 if not ConsumedByList
[M
]:
197 # start the DAG algorithm
201 while Q
== [] and EdgeRemoved
:
203 # for each node Item with a Constructor
204 for Item
in LibraryList
:
205 if Item
not in Constructor
:
207 # for each Node without a constructor with an edge e from Item to Node
208 for Node
in ConsumedByList
[Item
]:
209 if Node
in Constructor
:
211 # remove edge e from the graph if Node has no constructor
212 ConsumedByList
[Item
].remove(Node
)
214 if not ConsumedByList
[Item
]:
220 # DAG is done if there's no more incoming edge for all nodes
227 SortedLibraryList
.append(Node
)
229 # for each node Item with an edge e from Node to Item do
230 for Item
in LibraryList
:
231 if Node
not in ConsumedByList
[Item
]:
233 # remove edge e from the graph
234 ConsumedByList
[Item
].remove(Node
)
236 if ConsumedByList
[Item
]:
238 # insert Item into Q, if Item has no other incoming edges
242 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
244 for Item
in LibraryList
:
245 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
247 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
248 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
249 ExtraData
=ErrorMessage
, File
=FileName
)
252 if Item
not in SortedLibraryList
:
253 SortedLibraryList
.append(Item
)
256 # Build the list of constructor and destructir names
257 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
259 SortedLibraryList
.reverse()
260 return SortedLibraryList
262 def _ResolveLibraryReference(Module
, Platform
):
263 LibraryConsumerList
= [Module
]
265 # "CompilerStub" is a must for Edk modules
267 Module
.Libraries
.append("CompilerStub")
269 while len(LibraryConsumerList
) > 0:
270 M
= LibraryConsumerList
.pop()
271 for LibraryName
in M
.Libraries
:
272 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
274 for Key
in Platform
.LibraryClasses
.data
:
275 if LibraryName
.upper() == Key
.upper():
276 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
281 if Library
not in LibraryList
:
282 LibraryList
.append(Library
)
283 LibraryConsumerList
.append(Library
)