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 ModuleType
= Module
.ModuleType
93 # add forced library instances (specified under LibraryClasses sections)
95 # If a module has a MODULE_TYPE of USER_DEFINED,
96 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
98 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
and Module
.ModuleType
!= SUP_MODULE_HOST_APPLICATION
:
99 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
100 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
101 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
103 # add forced library instances (specified in module overrides)
104 for LibraryClass
in Platform
.Modules
[str(Module
)].LibraryClasses
:
105 if LibraryClass
.startswith("NULL"):
106 Module
.LibraryClasses
[LibraryClass
] = Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClass
]
109 LibraryConsumerList
= [Module
]
111 ConsumedByList
= OrderedListDict()
112 LibraryInstance
= OrderedDict()
115 EdkLogger
.verbose("")
116 EdkLogger
.verbose("Library instances of module [%s] [%s]:" % (str(Module
), Arch
))
118 while len(LibraryConsumerList
) > 0:
119 M
= LibraryConsumerList
.pop()
120 for LibraryClassName
in M
.LibraryClasses
:
121 if LibraryClassName
not in LibraryInstance
:
122 # override library instance for this module
123 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
.get(LibraryClassName
,Platform
.LibraryClasses
[LibraryClassName
, ModuleType
])
124 if LibraryPath
is None:
125 LibraryPath
= M
.LibraryClasses
.get(LibraryClassName
)
126 if LibraryPath
is None:
128 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
129 "Instance of library class [%s] is not found" % LibraryClassName
,
131 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
135 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
136 # for those forced library instance (NULL library), add a fake library class
137 if LibraryClassName
.startswith("NULL"):
138 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
139 elif LibraryModule
.LibraryClass
is None \
140 or len(LibraryModule
.LibraryClass
) == 0 \
141 or (ModuleType
!= SUP_MODULE_USER_DEFINED
and ModuleType
!= SUP_MODULE_HOST_APPLICATION
142 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
143 # only USER_DEFINED can link against any library instance despite of its SupModList
145 EdkLogger
.error("build", OPTION_MISSING
,
146 "Module type [%s] is not supported by library instance [%s]" \
147 % (ModuleType
, LibraryPath
), File
=FileName
,
148 ExtraData
="consumed by [%s]" % str(Module
))
152 LibraryInstance
[LibraryClassName
] = LibraryModule
153 LibraryConsumerList
.append(LibraryModule
)
155 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
157 LibraryModule
= LibraryInstance
[LibraryClassName
]
159 if LibraryModule
is None:
162 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
163 Constructor
.append(LibraryModule
)
165 # don't add current module itself to consumer list
167 if M
in ConsumedByList
[LibraryModule
]:
169 ConsumedByList
[LibraryModule
].append(M
)
171 # Initialize the sorted output list to the empty set
173 SortedLibraryList
= []
175 # Q <- Set of all nodes with no incoming edges
177 LibraryList
= [] #LibraryInstance.values()
179 for LibraryClassName
in LibraryInstance
:
180 M
= LibraryInstance
[LibraryClassName
]
181 LibraryList
.append(M
)
182 if not ConsumedByList
[M
]:
186 # start the DAG algorithm
190 while Q
== [] and EdgeRemoved
:
192 # for each node Item with a Constructor
193 for Item
in LibraryList
:
194 if Item
not in Constructor
:
196 # for each Node without a constructor with an edge e from Item to Node
197 for Node
in ConsumedByList
[Item
]:
198 if Node
in Constructor
:
200 # remove edge e from the graph if Node has no constructor
201 ConsumedByList
[Item
].remove(Node
)
203 if not ConsumedByList
[Item
]:
209 # DAG is done if there's no more incoming edge for all nodes
216 SortedLibraryList
.append(Node
)
218 # for each node Item with an edge e from Node to Item do
219 for Item
in LibraryList
:
220 if Node
not in ConsumedByList
[Item
]:
222 # remove edge e from the graph
223 ConsumedByList
[Item
].remove(Node
)
225 if ConsumedByList
[Item
]:
227 # insert Item into Q, if Item has no other incoming edges
231 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
233 for Item
in LibraryList
:
234 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
236 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
237 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
238 ExtraData
=ErrorMessage
, File
=FileName
)
241 if Item
not in SortedLibraryList
:
242 SortedLibraryList
.append(Item
)
245 # Build the list of constructor and destructor names
246 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
248 SortedLibraryList
.reverse()
249 return SortedLibraryList