]>
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 _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
88 return _ResolveLibraryReference(Module
, Platform
)
90 def _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
91 ModuleType
= Module
.ModuleType
93 # for overriding library instances with module specific setting
94 PlatformModule
= Platform
.Modules
[str(Module
)]
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 PlatformModule
.LibraryClasses
:
108 if LibraryClass
.startswith("NULL"):
109 Module
.LibraryClasses
[LibraryClass
] = PlatformModule
.LibraryClasses
[LibraryClass
]
112 LibraryConsumerList
= [Module
]
114 ConsumedByList
= OrderedListDict()
115 LibraryInstance
= OrderedDict()
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 if LibraryClassName
in PlatformModule
.LibraryClasses
:
123 LibraryPath
= PlatformModule
.LibraryClasses
[LibraryClassName
]
125 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
126 if LibraryPath
is None or LibraryPath
== "":
127 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
128 if LibraryPath
is None or LibraryPath
== "":
131 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
132 # for those forced library instance (NULL library), add a fake library class
133 if LibraryClassName
.startswith("NULL"):
134 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
135 elif LibraryModule
.LibraryClass
is None \
136 or len(LibraryModule
.LibraryClass
) == 0 \
137 or (ModuleType
!= SUP_MODULE_USER_DEFINED
138 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
139 # only USER_DEFINED can link against any library instance despite of its SupModList
142 LibraryInstance
[LibraryClassName
] = LibraryModule
143 LibraryConsumerList
.append(LibraryModule
)
145 LibraryModule
= LibraryInstance
[LibraryClassName
]
147 if LibraryModule
is None:
150 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
151 Constructor
.append(LibraryModule
)
153 # don't add current module itself to consumer list
155 if M
in ConsumedByList
[LibraryModule
]:
157 ConsumedByList
[LibraryModule
].append(M
)
159 # Initialize the sorted output list to the empty set
161 SortedLibraryList
= []
163 # Q <- Set of all nodes with no incoming edges
165 LibraryList
= [] #LibraryInstance.values()
167 for LibraryClassName
in LibraryInstance
:
168 M
= LibraryInstance
[LibraryClassName
]
169 LibraryList
.append(M
)
170 if len(ConsumedByList
[M
]) == 0:
174 # start the DAG algorithm
178 while Q
== [] and EdgeRemoved
:
180 # for each node Item with a Constructor
181 for Item
in LibraryList
:
182 if Item
not in Constructor
:
184 # for each Node without a constructor with an edge e from Item to Node
185 for Node
in ConsumedByList
[Item
]:
186 if Node
in Constructor
:
188 # remove edge e from the graph if Node has no constructor
189 ConsumedByList
[Item
].remove(Node
)
191 if len(ConsumedByList
[Item
]) == 0:
197 # DAG is done if there's no more incoming edge for all nodes
204 SortedLibraryList
.append(Node
)
206 # for each node Item with an edge e from Node to Item do
207 for Item
in LibraryList
:
208 if Node
not in ConsumedByList
[Item
]:
210 # remove edge e from the graph
211 ConsumedByList
[Item
].remove(Node
)
213 if len(ConsumedByList
[Item
]) != 0:
215 # insert Item into Q, if Item has no other incoming edges
219 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
221 for Item
in LibraryList
:
222 if len(ConsumedByList
[Item
]) != 0 and Item
in Constructor
and len(Constructor
) > 1:
224 if Item
not in SortedLibraryList
:
225 SortedLibraryList
.append(Item
)
228 # Build the list of constructor and destructir names
229 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
231 SortedLibraryList
.reverse()
232 return SortedLibraryList
234 def _ResolveLibraryReference(Module
, Platform
):
235 LibraryConsumerList
= [Module
]
237 # "CompilerStub" is a must for Edk modules
239 Module
.Libraries
.append("CompilerStub")
241 while len(LibraryConsumerList
) > 0:
242 M
= LibraryConsumerList
.pop()
243 for LibraryName
in M
.Libraries
:
244 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
246 for Key
in Platform
.LibraryClasses
.data
:
247 if LibraryName
.upper() == Key
.upper():
248 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
253 if Library
not in LibraryList
:
254 LibraryList
.append(Library
)
255 LibraryConsumerList
.append(Library
)