]>
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
, defaultdict
):
24 def __init__(self
, *args
, **kwargs
):
25 super(OrderedListDict
, self
).__init
__(*args
, **kwargs
)
26 self
.default_factory
= list
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 if Module
.AutoGenVersion
>= 0x00010005:
89 return GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
91 return _ResolveLibraryReference(Module
, Platform
)
93 def GetModuleLibInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
, FileName
= '', EdkLogger
= None):
94 ModuleType
= Module
.ModuleType
96 # add forced library instances (specified under LibraryClasses sections)
98 # If a module has a MODULE_TYPE of USER_DEFINED,
99 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
101 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
102 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
103 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
104 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
106 # add forced library instances (specified in module overrides)
107 for LibraryClass
in Platform
.Modules
[str(Module
)].LibraryClasses
:
108 if LibraryClass
.startswith("NULL"):
109 Module
.LibraryClasses
[LibraryClass
] = Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClass
]
112 LibraryConsumerList
= [Module
]
114 ConsumedByList
= OrderedListDict()
115 LibraryInstance
= OrderedDict()
118 EdkLogger
.verbose("")
119 EdkLogger
.verbose("Library instances of module [%s] [%s]:" % (str(Module
), Arch
))
121 while len(LibraryConsumerList
) > 0:
122 M
= LibraryConsumerList
.pop()
123 for LibraryClassName
in M
.LibraryClasses
:
124 if LibraryClassName
not in LibraryInstance
:
125 # override library instance for this module
126 if LibraryClassName
in Platform
.Modules
[str(Module
)].LibraryClasses
:
127 LibraryPath
= Platform
.Modules
[str(Module
)].LibraryClasses
[LibraryClassName
]
129 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
130 if LibraryPath
is None or LibraryPath
== "":
131 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
132 if LibraryPath
is None or LibraryPath
== "":
134 EdkLogger
.error("build", RESOURCE_NOT_AVAILABLE
,
135 "Instance of library class [%s] is not found" % LibraryClassName
,
137 ExtraData
="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M
), Arch
, str(Module
)))
141 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
142 # for those forced library instance (NULL library), add a fake library class
143 if LibraryClassName
.startswith("NULL"):
144 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
145 elif LibraryModule
.LibraryClass
is None \
146 or len(LibraryModule
.LibraryClass
) == 0 \
147 or (ModuleType
!= SUP_MODULE_USER_DEFINED
148 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
149 # only USER_DEFINED can link against any library instance despite of its SupModList
151 EdkLogger
.error("build", OPTION_MISSING
,
152 "Module type [%s] is not supported by library instance [%s]" \
153 % (ModuleType
, LibraryPath
), File
=FileName
,
154 ExtraData
="consumed by [%s]" % str(Module
))
158 LibraryInstance
[LibraryClassName
] = LibraryModule
159 LibraryConsumerList
.append(LibraryModule
)
161 EdkLogger
.verbose("\t" + str(LibraryClassName
) + " : " + str(LibraryModule
))
163 LibraryModule
= LibraryInstance
[LibraryClassName
]
165 if LibraryModule
is None:
168 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
169 Constructor
.append(LibraryModule
)
171 # don't add current module itself to consumer list
173 if M
in ConsumedByList
[LibraryModule
]:
175 ConsumedByList
[LibraryModule
].append(M
)
177 # Initialize the sorted output list to the empty set
179 SortedLibraryList
= []
181 # Q <- Set of all nodes with no incoming edges
183 LibraryList
= [] #LibraryInstance.values()
185 for LibraryClassName
in LibraryInstance
:
186 M
= LibraryInstance
[LibraryClassName
]
187 LibraryList
.append(M
)
188 if not ConsumedByList
[M
]:
192 # start the DAG algorithm
196 while Q
== [] and EdgeRemoved
:
198 # for each node Item with a Constructor
199 for Item
in LibraryList
:
200 if Item
not in Constructor
:
202 # for each Node without a constructor with an edge e from Item to Node
203 for Node
in ConsumedByList
[Item
]:
204 if Node
in Constructor
:
206 # remove edge e from the graph if Node has no constructor
207 ConsumedByList
[Item
].remove(Node
)
209 if not ConsumedByList
[Item
]:
215 # DAG is done if there's no more incoming edge for all nodes
222 SortedLibraryList
.append(Node
)
224 # for each node Item with an edge e from Node to Item do
225 for Item
in LibraryList
:
226 if Node
not in ConsumedByList
[Item
]:
228 # remove edge e from the graph
229 ConsumedByList
[Item
].remove(Node
)
231 if ConsumedByList
[Item
]:
233 # insert Item into Q, if Item has no other incoming edges
237 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
239 for Item
in LibraryList
:
240 if ConsumedByList
[Item
] and Item
in Constructor
and len(Constructor
) > 1:
242 ErrorMessage
= "\tconsumed by " + "\n\tconsumed by ".join(str(L
) for L
in ConsumedByList
[Item
])
243 EdkLogger
.error("build", BUILD_ERROR
, 'Library [%s] with constructors has a cycle' % str(Item
),
244 ExtraData
=ErrorMessage
, File
=FileName
)
247 if Item
not in SortedLibraryList
:
248 SortedLibraryList
.append(Item
)
251 # Build the list of constructor and destructir names
252 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
254 SortedLibraryList
.reverse()
255 return SortedLibraryList
257 def _ResolveLibraryReference(Module
, Platform
):
258 LibraryConsumerList
= [Module
]
260 # "CompilerStub" is a must for Edk modules
262 Module
.Libraries
.append("CompilerStub")
264 while len(LibraryConsumerList
) > 0:
265 M
= LibraryConsumerList
.pop()
266 for LibraryName
in M
.Libraries
:
267 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
269 for Key
in Platform
.LibraryClasses
.data
:
270 if LibraryName
.upper() == Key
.upper():
271 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
276 if Library
not in LibraryList
:
277 LibraryList
.append(Library
)
278 LibraryConsumerList
.append(Library
)