]>
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 collections
import OrderedDict
, defaultdict
15 from Common
.DataType
import SUP_MODULE_USER_DEFINED
16 from .BuildClassObject
import LibraryClassObject
17 import Common
.GlobalData
as GlobalData
18 from Workspace
.BuildClassObject
import StructurePcd
19 from Common
.BuildToolError
import RESOURCE_NOT_AVAILABLE
20 from Common
.BuildToolError
import OPTION_MISSING
21 from Common
.BuildToolError
import BUILD_ERROR
23 class OrderedListDict(OrderedDict
):
24 def __init__(self
, *args
, **kwargs
):
25 super().__init
__(*args
, **kwargs
)
26 self
.default_factory
= list
28 def __missing__(self
, key
):
29 self
[key
] = Value
= self
.default_factory()
32 ## Get all packages from platform for specified arch, target and toolchain
34 # @param Platform: DscBuildData instance
35 # @param BuildDatabase: The database saves all data for all metafiles
36 # @param Arch: Current arch
37 # @param Target: Current target
38 # @param Toolchain: Current toolchain
39 # @retval: List of packages which are DecBuildData instances
41 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
43 for ModuleFile
in Platform
.Modules
:
44 Data
= BuildDatabase
[ModuleFile
, Arch
, Target
, Toolchain
]
45 PkgSet
.update(Data
.Packages
)
46 for Lib
in GetLiabraryInstances(Data
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
47 PkgSet
.update(Lib
.Packages
)
50 ## Get all declared PCD from platform for specified arch, target and toolchain
52 # @param Platform: DscBuildData instance
53 # @param BuildDatabase: The database saves all data for all metafiles
54 # @param Arch: Current arch
55 # @param Target: Current target
56 # @param Toolchain: Current toolchain
57 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
58 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
60 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, additionalPkgs
):
61 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
62 PkgList
= set(PkgList
)
63 PkgList |
= additionalPkgs
68 GuidDict
.update(Guids
)
72 if GlobalData
.MixedPcd
:
73 for PcdItem
in GlobalData
.MixedPcd
:
74 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
77 if (PcdCName
, PcdTokenName
) not in DecPcds
:
78 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
79 return DecPcds
, GuidDict
81 ## Get all dependent libraries for a module
83 # @param Module: InfBuildData instance
84 # @param Platform: DscBuildData instance
85 # @param BuildDatabase: The database saves all data for all metafiles
86 # @param Arch: Current arch
87 # @param Target: Current target
88 # @param Toolchain: Current toolchain
89 # @retval: List of dependent libraries which are InfBuildData instances
91 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
92 if Module
.AutoGenVersion
>= 0x00010005:
93 return GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
95 return _ResolveLibraryReference(Module
, Platform
)
97 def GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, FileName
= '', EdkLogger
= None):
98 ModuleType
= Module
.ModuleType
100 # add forced library instances (specified under LibraryClasses sections)
102 # If a module has a MODULE_TYPE of USER_DEFINED,
103 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
105 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
106 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
107 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
108 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
110 # add forced library instances (specified in module overrides)
111 for LibraryClass
in Platform
.Modules
[str(Module
)].LibraryClasses
:
112 if LibraryClass
.startswith("NULL"):
113 Module
.LibraryClasses
[LibraryClass
] = Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClass
]
116 LibraryConsumerList
= [Module
]
118 ConsumedByList
= OrderedListDict()
119 LibraryInstance
= OrderedDict()
122 EdkLogger
.verbose("")
123 EdkLogger
.verbose("Library instances of module [%s] [%s]:" % (str(Module
), Arch
))
125 while len(LibraryConsumerList
) > 0:
126 M
= LibraryConsumerList
.pop()
127 for LibraryClassName
in M
.LibraryClasses
:
128 if LibraryClassName
not in LibraryInstance
:
129 # override library instance for this module
130 if LibraryClassName
in Platform
.Modules
[str(Module
)].LibraryClasses
:
131 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClassName
]
133 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
134 if LibraryPath
is None or LibraryPath
== "":
135 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
136 if LibraryPath
is None or LibraryPath
== "":
138 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
139 "Instance of library class [%s] is not found" % LibraryClassName
,
141 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
145 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
146 # for those forced library instance (NULL library), add a fake library class
147 if LibraryClassName
.startswith("NULL"):
148 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
149 elif LibraryModule
.LibraryClass
is None \
150 or len(LibraryModule
.LibraryClass
) == 0 \
151 or (ModuleType
!= SUP_MODULE_USER_DEFINED
152 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
153 # only USER_DEFINED can link against any library instance despite of its SupModList
155 EdkLogger
.error("build", OPTION_MISSING
,
156 "Module type [%s] is not supported by library instance [%s]" \
157 % (ModuleType
, LibraryPath
), File
=FileName
,
158 ExtraData
="consumed by [%s]" % str(Module
))
162 LibraryInstance
[LibraryClassName
] = LibraryModule
163 LibraryConsumerList
.append(LibraryModule
)
165 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
167 LibraryModule
= LibraryInstance
[LibraryClassName
]
169 if LibraryModule
is None:
172 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
173 Constructor
.append(LibraryModule
)
175 # don't add current module itself to consumer list
177 if M
in ConsumedByList
[LibraryModule
]:
179 ConsumedByList
[LibraryModule
].append(M
)
181 # Initialize the sorted output list to the empty set
183 SortedLibraryList
= []
185 # Q <- Set of all nodes with no incoming edges
187 LibraryList
= [] #LibraryInstance.values()
189 for LibraryClassName
in LibraryInstance
:
190 M
= LibraryInstance
[LibraryClassName
]
191 LibraryList
.append(M
)
192 if not ConsumedByList
[M
]:
196 # start the DAG algorithm
200 while Q
== [] and EdgeRemoved
:
202 # for each node Item with a Constructor
203 for Item
in LibraryList
:
204 if Item
not in Constructor
:
206 # for each Node without a constructor with an edge e from Item to Node
207 for Node
in ConsumedByList
[Item
]:
208 if Node
in Constructor
:
210 # remove edge e from the graph if Node has no constructor
211 ConsumedByList
[Item
].remove(Node
)
213 if not ConsumedByList
[Item
]:
219 # DAG is done if there's no more incoming edge for all nodes
226 SortedLibraryList
.append(Node
)
228 # for each node Item with an edge e from Node to Item do
229 for Item
in LibraryList
:
230 if Node
not in ConsumedByList
[Item
]:
232 # remove edge e from the graph
233 ConsumedByList
[Item
].remove(Node
)
235 if ConsumedByList
[Item
]:
237 # insert Item into Q, if Item has no other incoming edges
241 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
243 for Item
in LibraryList
:
244 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
246 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
247 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
248 ExtraData
=ErrorMessage
, File
=FileName
)
251 if Item
not in SortedLibraryList
:
252 SortedLibraryList
.append(Item
)
255 # Build the list of constructor and destructir names
256 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
258 SortedLibraryList
.reverse()
259 return SortedLibraryList
261 def _ResolveLibraryReference(Module
, Platform
):
262 LibraryConsumerList
= [Module
]
264 # "CompilerStub" is a must for Edk modules
266 Module
.Libraries
.append("CompilerStub")
268 while len(LibraryConsumerList
) > 0:
269 M
= LibraryConsumerList
.pop()
270 for LibraryName
in M
.Libraries
:
271 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
273 for Key
in Platform
.LibraryClasses
.data
:
274 if LibraryName
.upper() == Key
.upper():
275 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
280 if Library
not in LibraryList
:
281 LibraryList
.append(Library
)
282 LibraryConsumerList
.append(Library
)