]>
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 # 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 .BuildClassObject
import LibraryClassObject
12 import Common
.GlobalData
as GlobalData
13 from Workspace
.BuildClassObject
import StructurePcd
14 from Common
.BuildToolError
import RESOURCE_NOT_AVAILABLE
15 from Common
.BuildToolError
import OPTION_MISSING
16 from Common
.BuildToolError
import BUILD_ERROR
18 class OrderedListDict(OrderedDict
):
19 def __init__(self
, *args
, **kwargs
):
20 super(OrderedListDict
, self
).__init
__(*args
, **kwargs
)
21 self
.default_factory
= list
23 def __missing__(self
, key
):
24 self
[key
] = Value
= self
.default_factory()
27 ## Get all packages from platform for specified arch, target and toolchain
29 # @param Platform: DscBuildData instance
30 # @param BuildDatabase: The database saves all data for all metafiles
31 # @param Arch: Current arch
32 # @param Target: Current target
33 # @param Toolchain: Current toolchain
34 # @retval: List of packages which are DecBuildData instances
36 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
38 for ModuleFile
in Platform
.Modules
:
39 Data
= BuildDatabase
[ModuleFile
, Arch
, Target
, Toolchain
]
40 PkgSet
.update(Data
.Packages
)
41 for Lib
in GetLiabraryInstances(Data
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
42 PkgSet
.update(Lib
.Packages
)
45 ## Get all declared PCD from platform for specified arch, target and toolchain
47 # @param Platform: DscBuildData instance
48 # @param BuildDatabase: The database saves all data for all metafiles
49 # @param Arch: Current arch
50 # @param Target: Current target
51 # @param Toolchain: Current toolchain
52 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
53 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
55 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, additionalPkgs
):
56 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
57 PkgList
= set(PkgList
)
58 PkgList |
= additionalPkgs
63 GuidDict
.update(Guids
)
67 if GlobalData
.MixedPcd
:
68 for PcdItem
in GlobalData
.MixedPcd
:
69 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
72 if (PcdCName
, PcdTokenName
) not in DecPcds
:
73 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
74 return DecPcds
, GuidDict
76 ## Get all dependent libraries for a module
78 # @param Module: InfBuildData instance
79 # @param Platform: DscBuildData instance
80 # @param BuildDatabase: The database saves all data for all metafiles
81 # @param Arch: Current arch
82 # @param Target: Current target
83 # @param Toolchain: Current toolchain
84 # @retval: List of dependent libraries which are InfBuildData instances
86 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
87 return GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
89 def GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, FileName
= '', EdkLogger
= None):
90 ModuleType
= Module
.ModuleType
92 # add forced library instances (specified under LibraryClasses sections)
94 # If a module has a MODULE_TYPE of USER_DEFINED,
95 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
97 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
98 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
99 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
100 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
102 # add forced library instances (specified in module overrides)
103 for LibraryClass
in Platform
.Modules
[str(Module
)].LibraryClasses
:
104 if LibraryClass
.startswith("NULL"):
105 Module
.LibraryClasses
[LibraryClass
] = Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClass
]
108 LibraryConsumerList
= [Module
]
110 ConsumedByList
= OrderedListDict()
111 LibraryInstance
= OrderedDict()
114 EdkLogger
.verbose("")
115 EdkLogger
.verbose("Library instances of module [%s] [%s]:" % (str(Module
), Arch
))
117 while len(LibraryConsumerList
) > 0:
118 M
= LibraryConsumerList
.pop()
119 for LibraryClassName
in M
.LibraryClasses
:
120 if LibraryClassName
not in LibraryInstance
:
121 # override library instance for this module
122 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
.get(LibraryClassName
,Platform
.LibraryClasses
[LibraryClassName
, ModuleType
])
123 if LibraryPath
is None:
124 LibraryPath
= M
.LibraryClasses
.get(LibraryClassName
)
125 if LibraryPath
is None:
127 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
128 "Instance of library class [%s] is not found" % LibraryClassName
,
130 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
134 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
135 # for those forced library instance (NULL library), add a fake library class
136 if LibraryClassName
.startswith("NULL"):
137 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
138 elif LibraryModule
.LibraryClass
is None \
139 or len(LibraryModule
.LibraryClass
) == 0 \
140 or (ModuleType
!= SUP_MODULE_USER_DEFINED
141 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
142 # only USER_DEFINED can link against any library instance despite of its SupModList
144 EdkLogger
.error("build", OPTION_MISSING
,
145 "Module type [%s] is not supported by library instance [%s]" \
146 % (ModuleType
, LibraryPath
), File
=FileName
,
147 ExtraData
="consumed by [%s]" % str(Module
))
151 LibraryInstance
[LibraryClassName
] = LibraryModule
152 LibraryConsumerList
.append(LibraryModule
)
154 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
156 LibraryModule
= LibraryInstance
[LibraryClassName
]
158 if LibraryModule
is None:
161 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
162 Constructor
.append(LibraryModule
)
164 # don't add current module itself to consumer list
166 if M
in ConsumedByList
[LibraryModule
]:
168 ConsumedByList
[LibraryModule
].append(M
)
170 # Initialize the sorted output list to the empty set
172 SortedLibraryList
= []
174 # Q <- Set of all nodes with no incoming edges
176 LibraryList
= [] #LibraryInstance.values()
178 for LibraryClassName
in LibraryInstance
:
179 M
= LibraryInstance
[LibraryClassName
]
180 LibraryList
.append(M
)
181 if not ConsumedByList
[M
]:
185 # start the DAG algorithm
189 while Q
== [] and EdgeRemoved
:
191 # for each node Item with a Constructor
192 for Item
in LibraryList
:
193 if Item
not in Constructor
:
195 # for each Node without a constructor with an edge e from Item to Node
196 for Node
in ConsumedByList
[Item
]:
197 if Node
in Constructor
:
199 # remove edge e from the graph if Node has no constructor
200 ConsumedByList
[Item
].remove(Node
)
202 if not ConsumedByList
[Item
]:
208 # DAG is done if there's no more incoming edge for all nodes
215 SortedLibraryList
.append(Node
)
217 # for each node Item with an edge e from Node to Item do
218 for Item
in LibraryList
:
219 if Node
not in ConsumedByList
[Item
]:
221 # remove edge e from the graph
222 ConsumedByList
[Item
].remove(Node
)
224 if ConsumedByList
[Item
]:
226 # insert Item into Q, if Item has no other incoming edges
230 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
232 for Item
in LibraryList
:
233 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
235 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
236 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
237 ExtraData
=ErrorMessage
, File
=FileName
)
240 if Item
not in SortedLibraryList
:
241 SortedLibraryList
.append(Item
)
244 # Build the list of constructor and destructor names
245 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
247 SortedLibraryList
.reverse()
248 return SortedLibraryList