2 # Common routines used by workspace
4 # Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
8 from __future__
import absolute_import
9 from collections
import OrderedDict
, defaultdict
10 from Common
.DataType
import SUP_MODULE_USER_DEFINED
11 from Common
.DataType
import SUP_MODULE_HOST_APPLICATION
12 from .BuildClassObject
import LibraryClassObject
13 import Common
.GlobalData
as GlobalData
14 from Workspace
.BuildClassObject
import StructurePcd
15 from Common
.BuildToolError
import RESOURCE_NOT_AVAILABLE
16 from Common
.BuildToolError
import OPTION_MISSING
17 from Common
.BuildToolError
import BUILD_ERROR
19 class OrderedListDict(OrderedDict
):
20 def __init__(self
, *args
, **kwargs
):
21 super(OrderedListDict
, self
).__init
__(*args
, **kwargs
)
22 self
.default_factory
= list
24 def __missing__(self
, key
):
25 self
[key
] = Value
= self
.default_factory()
28 ## Get all packages from platform for specified arch, target and toolchain
30 # @param Platform: DscBuildData instance
31 # @param BuildDatabase: The database saves all data for all metafiles
32 # @param Arch: Current arch
33 # @param Target: Current target
34 # @param Toolchain: Current toolchain
35 # @retval: List of packages which are DecBuildData instances
37 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
39 for ModuleFile
in Platform
.Modules
:
40 Data
= BuildDatabase
[ModuleFile
, Arch
, Target
, Toolchain
]
41 PkgSet
.update(Data
.Packages
)
42 for Lib
in GetLiabraryInstances(Data
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
43 PkgSet
.update(Lib
.Packages
)
46 ## Get all declared PCD from platform for specified arch, target and toolchain
48 # @param Platform: DscBuildData instance
49 # @param BuildDatabase: The database saves all data for all metafiles
50 # @param Arch: Current arch
51 # @param Target: Current target
52 # @param Toolchain: Current toolchain
53 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
54 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
56 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, additionalPkgs
):
57 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
58 PkgList
= set(PkgList
)
59 PkgList |
= additionalPkgs
64 GuidDict
.update(Guids
)
68 if GlobalData
.MixedPcd
:
69 for PcdItem
in GlobalData
.MixedPcd
:
70 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
73 if (PcdCName
, PcdTokenName
) not in DecPcds
:
74 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
75 return DecPcds
, GuidDict
77 ## Get all dependent libraries for a module
79 # @param Module: InfBuildData instance
80 # @param Platform: DscBuildData instance
81 # @param BuildDatabase: The database saves all data for all metafiles
82 # @param Arch: Current arch
83 # @param Target: Current target
84 # @param Toolchain: Current toolchain
85 # @retval: List of dependent libraries which are InfBuildData instances
87 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
88 return GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
90 def GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, FileName
= '', EdkLogger
= None):
91 if Module
.LibInstances
:
92 return Module
.LibInstances
93 ModuleType
= Module
.ModuleType
95 # add forced library instances (specified under LibraryClasses sections)
97 # If a module has a MODULE_TYPE of USER_DEFINED,
98 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
100 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
and Module
.ModuleType
!= SUP_MODULE_HOST_APPLICATION
:
101 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
102 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
103 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
105 # add forced library instances (specified in module overrides)
106 for LibraryClass
in Platform
.Modules
[str(Module
)].LibraryClasses
:
107 if LibraryClass
.startswith("NULL"):
108 Module
.LibraryClasses
[LibraryClass
] = Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClass
]
111 LibraryConsumerList
= [Module
]
113 ConsumedByList
= OrderedListDict()
114 LibraryInstance
= OrderedDict()
117 EdkLogger
.verbose("")
118 EdkLogger
.verbose("Library instances of module [%s] [%s]:" % (str(Module
), Arch
))
120 while len(LibraryConsumerList
) > 0:
121 M
= LibraryConsumerList
.pop()
122 for LibraryClassName
in M
.LibraryClasses
:
123 if LibraryClassName
not in LibraryInstance
:
124 # override library instance for this module
125 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
.get(LibraryClassName
,Platform
.LibraryClasses
[LibraryClassName
, ModuleType
])
126 if LibraryPath
is None:
127 LibraryPath
= M
.LibraryClasses
.get(LibraryClassName
)
128 if LibraryPath
is None:
130 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
131 "Instance of library class [%s] is not found" % LibraryClassName
,
133 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
137 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
138 # for those forced library instance (NULL library), add a fake library class
139 if LibraryClassName
.startswith("NULL"):
140 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
141 elif LibraryModule
.LibraryClass
is None \
142 or len(LibraryModule
.LibraryClass
) == 0 \
143 or (ModuleType
!= SUP_MODULE_USER_DEFINED
and ModuleType
!= SUP_MODULE_HOST_APPLICATION
144 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
145 # only USER_DEFINED can link against any library instance despite of its SupModList
147 EdkLogger
.error("build", OPTION_MISSING
,
148 "Module type [%s] is not supported by library instance [%s]" \
149 % (ModuleType
, LibraryPath
), File
=FileName
,
150 ExtraData
="consumed by [%s]" % str(Module
))
154 LibraryInstance
[LibraryClassName
] = LibraryModule
155 LibraryConsumerList
.append(LibraryModule
)
157 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
159 LibraryModule
= LibraryInstance
[LibraryClassName
]
161 if LibraryModule
is None:
164 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
165 Constructor
.append(LibraryModule
)
167 # don't add current module itself to consumer list
169 if M
in ConsumedByList
[LibraryModule
]:
171 ConsumedByList
[LibraryModule
].append(M
)
173 # Initialize the sorted output list to the empty set
175 SortedLibraryList
= []
177 # Q <- Set of all nodes with no incoming edges
179 LibraryList
= [] #LibraryInstance.values()
181 for LibraryClassName
in LibraryInstance
:
182 M
= LibraryInstance
[LibraryClassName
]
183 LibraryList
.append(M
)
184 if not ConsumedByList
[M
]:
188 # start the DAG algorithm
192 while Q
== [] and EdgeRemoved
:
194 # for each node Item with a Constructor
195 for Item
in LibraryList
:
196 if Item
not in Constructor
:
198 # for each Node without a constructor with an edge e from Item to Node
199 for Node
in ConsumedByList
[Item
]:
200 if Node
in Constructor
:
202 # remove edge e from the graph if Node has no constructor
203 ConsumedByList
[Item
].remove(Node
)
205 if not ConsumedByList
[Item
]:
211 # DAG is done if there's no more incoming edge for all nodes
218 SortedLibraryList
.append(Node
)
220 # for each node Item with an edge e from Node to Item do
221 for Item
in LibraryList
:
222 if Node
not in ConsumedByList
[Item
]:
224 # remove edge e from the graph
225 ConsumedByList
[Item
].remove(Node
)
227 if ConsumedByList
[Item
]:
229 # insert Item into Q, if Item has no other incoming edges
233 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
235 for Item
in LibraryList
:
236 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
238 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
239 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
240 ExtraData
=ErrorMessage
, File
=FileName
)
243 if Item
not in SortedLibraryList
:
244 SortedLibraryList
.append(Item
)
247 # Build the list of constructor and destructor names
248 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
250 SortedLibraryList
.reverse()
251 Module
.LibInstances
= SortedLibraryList
252 SortedLibraryList
= [lib
.SetReferenceModule(Module
) for lib
in SortedLibraryList
]
253 return SortedLibraryList