]>
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
20 class OrderedListDict(OrderedDict
, defaultdict
):
21 def __init__(self
, *args
, **kwargs
):
22 super(OrderedListDict
, self
).__init
__(*args
, **kwargs
)
23 self
.default_factory
= list
25 ## Get all packages from platform for specified arch, target and toolchain
27 # @param Platform: DscBuildData instance
28 # @param BuildDatabase: The database saves all data for all metafiles
29 # @param Arch: Current arch
30 # @param Target: Current target
31 # @param Toolchain: Current toolchain
32 # @retval: List of packages which are DecBuildData instances
34 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
36 for ModuleFile
in Platform
.Modules
:
37 Data
= BuildDatabase
[ModuleFile
, Arch
, Target
, Toolchain
]
38 PkgSet
.update(Data
.Packages
)
39 for Lib
in GetLiabraryInstances(Data
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
40 PkgSet
.update(Lib
.Packages
)
43 ## Get all declared PCD from platform for specified arch, target and toolchain
45 # @param Platform: DscBuildData instance
46 # @param BuildDatabase: The database saves all data for all metafiles
47 # @param Arch: Current arch
48 # @param Target: Current target
49 # @param Toolchain: Current toolchain
50 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
51 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
53 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
,additionalPkgs
):
54 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
55 PkgList
= set(PkgList
)
56 PkgList |
= additionalPkgs
61 GuidDict
.update(Guids
)
65 if GlobalData
.MixedPcd
:
66 for PcdItem
in GlobalData
.MixedPcd
:
67 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
70 if (PcdCName
, PcdTokenName
) not in DecPcds
:
71 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
72 return DecPcds
, GuidDict
74 ## Get all dependent libraries for a module
76 # @param Module: InfBuildData instance
77 # @param Platform: DscBuildData instance
78 # @param BuildDatabase: The database saves all data for all metafiles
79 # @param Arch: Current arch
80 # @param Target: Current target
81 # @param Toolchain: Current toolchain
82 # @retval: List of dependent libraries which are InfBuildData instances
84 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
85 if Module
.AutoGenVersion
>= 0x00010005:
86 return GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
88 return _ResolveLibraryReference(Module
, Platform
)
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
:
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 if LibraryClassName
in Platform
.Modules
[str(Module
)].LibraryClasses
:
124 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClassName
]
126 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
127 if LibraryPath
is None or LibraryPath
== "":
128 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
129 if LibraryPath
is None or LibraryPath
== "":
131 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
132 "Instance of library class [%s] is not found" % LibraryClassName
,
134 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
138 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
139 # for those forced library instance (NULL library), add a fake library class
140 if LibraryClassName
.startswith("NULL"):
141 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
142 elif LibraryModule
.LibraryClass
is None \
143 or len(LibraryModule
.LibraryClass
) == 0 \
144 or (ModuleType
!= SUP_MODULE_USER_DEFINED
145 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
146 # only USER_DEFINED can link against any library instance despite of its SupModList
148 EdkLogger
.error("build", OPTION_MISSING
,
149 "Module type [%s] is not supported by library instance [%s]" \
150 % (ModuleType
, LibraryPath
), File
=FileName
,
151 ExtraData
="consumed by [%s]" % str(Module
))
155 LibraryInstance
[LibraryClassName
] = LibraryModule
156 LibraryConsumerList
.append(LibraryModule
)
158 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
160 LibraryModule
= LibraryInstance
[LibraryClassName
]
162 if LibraryModule
is None:
165 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
166 Constructor
.append(LibraryModule
)
168 # don't add current module itself to consumer list
170 if M
in ConsumedByList
[LibraryModule
]:
172 ConsumedByList
[LibraryModule
].append(M
)
174 # Initialize the sorted output list to the empty set
176 SortedLibraryList
= []
178 # Q <- Set of all nodes with no incoming edges
180 LibraryList
= [] #LibraryInstance.values()
182 for LibraryClassName
in LibraryInstance
:
183 M
= LibraryInstance
[LibraryClassName
]
184 LibraryList
.append(M
)
185 if not ConsumedByList
[M
]:
189 # start the DAG algorithm
193 while Q
== [] and EdgeRemoved
:
195 # for each node Item with a Constructor
196 for Item
in LibraryList
:
197 if Item
not in Constructor
:
199 # for each Node without a constructor with an edge e from Item to Node
200 for Node
in ConsumedByList
[Item
]:
201 if Node
in Constructor
:
203 # remove edge e from the graph if Node has no constructor
204 ConsumedByList
[Item
].remove(Node
)
206 if not ConsumedByList
[Item
]:
212 # DAG is done if there's no more incoming edge for all nodes
219 SortedLibraryList
.append(Node
)
221 # for each node Item with an edge e from Node to Item do
222 for Item
in LibraryList
:
223 if Node
not in ConsumedByList
[Item
]:
225 # remove edge e from the graph
226 ConsumedByList
[Item
].remove(Node
)
228 if ConsumedByList
[Item
]:
230 # insert Item into Q, if Item has no other incoming edges
234 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
236 for Item
in LibraryList
:
237 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
239 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
240 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
241 ExtraData
=ErrorMessage
, File
=FileName
)
244 if Item
not in SortedLibraryList
:
245 SortedLibraryList
.append(Item
)
248 # Build the list of constructor and destructir names
249 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
251 SortedLibraryList
.reverse()
252 return SortedLibraryList
254 def _ResolveLibraryReference(Module
, Platform
):
255 LibraryConsumerList
= [Module
]
257 # "CompilerStub" is a must for Edk modules
259 Module
.Libraries
.append("CompilerStub")
261 while len(LibraryConsumerList
) > 0:
262 M
= LibraryConsumerList
.pop()
263 for LibraryName
in M
.Libraries
:
264 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
266 for Key
in Platform
.LibraryClasses
.data
:
267 if LibraryName
.upper() == Key
.upper():
268 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
273 if Library
not in LibraryList
:
274 LibraryList
.append(Library
)
275 LibraryConsumerList
.append(Library
)