]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
2 # Common routines used by workspace
4 # Copyright (c) 2012 - 2017, 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 Common
.Misc
import sdict
15 from collections
import OrderedDict
, defaultdict
16 from Common
.DataType
import SUP_MODULE_USER_DEFINED
17 from BuildClassObject
import LibraryClassObject
18 import Common
.GlobalData
as GlobalData
19 from Workspace
.BuildClassObject
import StructurePcd
21 class OrderedListDict(OrderedDict
, defaultdict
):
22 def __init__(self
, *args
, **kwargs
):
23 super(OrderedListDict
, self
).__init
__(*args
, **kwargs
)
24 self
.default_factory
= list
26 ## Get all packages from platform for specified arch, target and toolchain
28 # @param Platform: DscBuildData instance
29 # @param BuildDatabase: The database saves all data for all metafiles
30 # @param Arch: Current arch
31 # @param Target: Current target
32 # @param Toolchain: Current toolchain
33 # @retval: List of packages which are DecBuildData instances
35 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
37 for ModuleFile
in Platform
.Modules
:
38 Data
= BuildDatabase
[ModuleFile
, Arch
, Target
, Toolchain
]
39 PkgSet
.update(Data
.Packages
)
40 for Lib
in GetLiabraryInstances(Data
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
41 PkgSet
.update(Lib
.Packages
)
44 ## Get all declared PCD from platform for specified arch, target and toolchain
46 # @param Platform: DscBuildData instance
47 # @param BuildDatabase: The database saves all data for all metafiles
48 # @param Arch: Current arch
49 # @param Target: Current target
50 # @param Toolchain: Current toolchain
51 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
52 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
54 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
,additionalPkgs
):
55 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
56 PkgList
= set(PkgList
)
57 PkgList |
= additionalPkgs
62 GuidDict
.update(Guids
)
66 if GlobalData
.MixedPcd
:
67 for PcdItem
in GlobalData
.MixedPcd
.keys():
68 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
71 if (PcdCName
, PcdTokenName
) not in DecPcds
.keys():
72 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
73 return DecPcds
, GuidDict
75 ## Get all dependent libraries for a module
77 # @param Module: InfBuildData instance
78 # @param Platform: DscBuildData instance
79 # @param BuildDatabase: The database saves all data for all metafiles
80 # @param Arch: Current arch
81 # @param Target: Current target
82 # @param Toolchain: Current toolchain
83 # @retval: List of dependent libraries which are InfBuildData instances
85 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
86 if Module
.AutoGenVersion
>= 0x00010005:
87 return _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
89 return _ResolveLibraryReference(Module
, Platform
)
91 def _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
92 ModuleType
= Module
.ModuleType
94 # for overriding library instances with module specific setting
95 PlatformModule
= Platform
.Modules
[str(Module
)]
97 # add forced library instances (specified under LibraryClasses sections)
99 # If a module has a MODULE_TYPE of USER_DEFINED,
100 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
102 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
103 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
104 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
105 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
107 # add forced library instances (specified in module overrides)
108 for LibraryClass
in PlatformModule
.LibraryClasses
:
109 if LibraryClass
.startswith("NULL"):
110 Module
.LibraryClasses
[LibraryClass
] = PlatformModule
.LibraryClasses
[LibraryClass
]
113 LibraryConsumerList
= [Module
]
115 ConsumedByList
= OrderedListDict()
116 LibraryInstance
= sdict()
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 PlatformModule
.LibraryClasses
:
124 LibraryPath
= PlatformModule
.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
== "":
132 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
133 # for those forced library instance (NULL library), add a fake library class
134 if LibraryClassName
.startswith("NULL"):
135 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
136 elif LibraryModule
.LibraryClass
is None \
137 or len(LibraryModule
.LibraryClass
) == 0 \
138 or (ModuleType
!= 'USER_DEFINED'
139 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
140 # only USER_DEFINED can link against any library instance despite of its SupModList
143 LibraryInstance
[LibraryClassName
] = LibraryModule
144 LibraryConsumerList
.append(LibraryModule
)
146 LibraryModule
= LibraryInstance
[LibraryClassName
]
148 if LibraryModule
is None:
151 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
152 Constructor
.append(LibraryModule
)
154 # don't add current module itself to consumer list
156 if M
in ConsumedByList
[LibraryModule
]:
158 ConsumedByList
[LibraryModule
].append(M
)
160 # Initialize the sorted output list to the empty set
162 SortedLibraryList
= []
164 # Q <- Set of all nodes with no incoming edges
166 LibraryList
= [] #LibraryInstance.values()
168 for LibraryClassName
in LibraryInstance
:
169 M
= LibraryInstance
[LibraryClassName
]
170 LibraryList
.append(M
)
171 if len(ConsumedByList
[M
]) == 0:
175 # start the DAG algorithm
179 while Q
== [] and EdgeRemoved
:
181 # for each node Item with a Constructor
182 for Item
in LibraryList
:
183 if Item
not in Constructor
:
185 # for each Node without a constructor with an edge e from Item to Node
186 for Node
in ConsumedByList
[Item
]:
187 if Node
in Constructor
:
189 # remove edge e from the graph if Node has no constructor
190 ConsumedByList
[Item
].remove(Node
)
192 if len(ConsumedByList
[Item
]) == 0:
198 # DAG is done if there's no more incoming edge for all nodes
205 SortedLibraryList
.append(Node
)
207 # for each node Item with an edge e from Node to Item do
208 for Item
in LibraryList
:
209 if Node
not in ConsumedByList
[Item
]:
211 # remove edge e from the graph
212 ConsumedByList
[Item
].remove(Node
)
214 if len(ConsumedByList
[Item
]) != 0:
216 # insert Item into Q, if Item has no other incoming edges
220 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
222 for Item
in LibraryList
:
223 if len(ConsumedByList
[Item
]) != 0 and Item
in Constructor
and len(Constructor
) > 1:
225 if Item
not in SortedLibraryList
:
226 SortedLibraryList
.append(Item
)
229 # Build the list of constructor and destructir names
230 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
232 SortedLibraryList
.reverse()
233 return SortedLibraryList
235 def _ResolveLibraryReference(Module
, Platform
):
236 LibraryConsumerList
= [Module
]
238 # "CompilerStub" is a must for Edk modules
240 Module
.Libraries
.append("CompilerStub")
242 while len(LibraryConsumerList
) > 0:
243 M
= LibraryConsumerList
.pop()
244 for LibraryName
in M
.Libraries
:
245 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
247 for Key
in Platform
.LibraryClasses
.data
.keys():
248 if LibraryName
.upper() == Key
.upper():
249 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
254 if Library
not in LibraryList
:
255 LibraryList
.append(Library
)
256 LibraryConsumerList
.append(Library
)