]>
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 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 ## Get all packages from platform for specified arch, target and toolchain
22 # @param Platform: DscBuildData instance
23 # @param BuildDatabase: The database saves all data for all metafiles
24 # @param Arch: Current arch
25 # @param Target: Current target
26 # @param Toolchain: Current toolchain
27 # @retval: List of packages which are DecBuildData instances
29 def GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
31 for ModuleFile
in Platform
.Modules
:
32 Data
= BuildDatabase
[ModuleFile
, Arch
, Target
, Toolchain
]
33 PkgSet
.update(Data
.Packages
)
34 for Lib
in GetLiabraryInstances(Data
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
35 PkgSet
.update(Lib
.Packages
)
38 ## Get all declared PCD from platform for specified arch, target and toolchain
40 # @param Platform: DscBuildData instance
41 # @param BuildDatabase: The database saves all data for all metafiles
42 # @param Arch: Current arch
43 # @param Target: Current target
44 # @param Toolchain: Current toolchain
45 # @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
46 # @retval: A dictionary contains real GUIDs of TokenSpaceGuid
48 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
,additionalPkgs
):
49 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
50 PkgList
= set(PkgList
)
51 PkgList |
= additionalPkgs
56 GuidDict
.update(Guids
)
60 if GlobalData
.MixedPcd
:
61 for PcdItem
in GlobalData
.MixedPcd
.keys():
62 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
65 if (PcdCName
, PcdTokenName
) not in DecPcds
.keys():
66 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
67 return DecPcds
, GuidDict
69 ## Get all dependent libraries for a module
71 # @param Module: InfBuildData instance
72 # @param Platform: DscBuildData instance
73 # @param BuildDatabase: The database saves all data for all metafiles
74 # @param Arch: Current arch
75 # @param Target: Current target
76 # @param Toolchain: Current toolchain
77 # @retval: List of dependent libraries which are InfBuildData instances
79 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
80 if Module
.AutoGenVersion
>= 0x00010005:
81 return _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
83 return _ResolveLibraryReference(Module
, Platform
)
85 def _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
86 ModuleType
= Module
.ModuleType
88 # for overriding library instances with module specific setting
89 PlatformModule
= Platform
.Modules
[str(Module
)]
91 # add forced library instances (specified under LibraryClasses sections)
93 # If a module has a MODULE_TYPE of USER_DEFINED,
94 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
96 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
97 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
98 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
99 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
101 # add forced library instances (specified in module overrides)
102 for LibraryClass
in PlatformModule
.LibraryClasses
:
103 if LibraryClass
.startswith("NULL"):
104 Module
.LibraryClasses
[LibraryClass
] = PlatformModule
.LibraryClasses
[LibraryClass
]
107 LibraryConsumerList
= [Module
]
109 ConsumedByList
= sdict()
110 LibraryInstance
= sdict()
112 while len(LibraryConsumerList
) > 0:
113 M
= LibraryConsumerList
.pop()
114 for LibraryClassName
in M
.LibraryClasses
:
115 if LibraryClassName
not in LibraryInstance
:
116 # override library instance for this module
117 if LibraryClassName
in PlatformModule
.LibraryClasses
:
118 LibraryPath
= PlatformModule
.LibraryClasses
[LibraryClassName
]
120 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
121 if LibraryPath
== None or LibraryPath
== "":
122 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
123 if LibraryPath
== None or LibraryPath
== "":
126 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
127 # for those forced library instance (NULL library), add a fake library class
128 if LibraryClassName
.startswith("NULL"):
129 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
130 elif LibraryModule
.LibraryClass
== None \
131 or len(LibraryModule
.LibraryClass
) == 0 \
132 or (ModuleType
!= 'USER_DEFINED'
133 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
134 # only USER_DEFINED can link against any library instance despite of its SupModList
137 LibraryInstance
[LibraryClassName
] = LibraryModule
138 LibraryConsumerList
.append(LibraryModule
)
140 LibraryModule
= LibraryInstance
[LibraryClassName
]
142 if LibraryModule
== None:
145 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
146 Constructor
.append(LibraryModule
)
148 if LibraryModule
not in ConsumedByList
:
149 ConsumedByList
[LibraryModule
] = []
150 # don't add current module itself to consumer list
152 if M
in ConsumedByList
[LibraryModule
]:
154 ConsumedByList
[LibraryModule
].append(M
)
156 # Initialize the sorted output list to the empty set
158 SortedLibraryList
= []
160 # Q <- Set of all nodes with no incoming edges
162 LibraryList
= [] #LibraryInstance.values()
164 for LibraryClassName
in LibraryInstance
:
165 M
= LibraryInstance
[LibraryClassName
]
166 LibraryList
.append(M
)
167 if ConsumedByList
[M
] == []:
171 # start the DAG algorithm
175 while Q
== [] and EdgeRemoved
:
177 # for each node Item with a Constructor
178 for Item
in LibraryList
:
179 if Item
not in Constructor
:
181 # for each Node without a constructor with an edge e from Item to Node
182 for Node
in ConsumedByList
[Item
]:
183 if Node
in Constructor
:
185 # remove edge e from the graph if Node has no constructor
186 ConsumedByList
[Item
].remove(Node
)
188 if ConsumedByList
[Item
] == []:
194 # DAG is done if there's no more incoming edge for all nodes
201 SortedLibraryList
.append(Node
)
203 # for each node Item with an edge e from Node to Item do
204 for Item
in LibraryList
:
205 if Node
not in ConsumedByList
[Item
]:
207 # remove edge e from the graph
208 ConsumedByList
[Item
].remove(Node
)
210 if ConsumedByList
[Item
] != []:
212 # insert Item into Q, if Item has no other incoming edges
216 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
218 for Item
in LibraryList
:
219 if ConsumedByList
[Item
] != [] and Item
in Constructor
and len(Constructor
) > 1:
221 if Item
not in SortedLibraryList
:
222 SortedLibraryList
.append(Item
)
225 # Build the list of constructor and destructir names
226 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
228 SortedLibraryList
.reverse()
229 return SortedLibraryList
231 def _ResolveLibraryReference(Module
, Platform
):
232 LibraryConsumerList
= [Module
]
234 # "CompilerStub" is a must for Edk modules
236 Module
.Libraries
.append("CompilerStub")
238 while len(LibraryConsumerList
) > 0:
239 M
= LibraryConsumerList
.pop()
240 for LibraryName
in M
.Libraries
:
241 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
243 for Key
in Platform
.LibraryClasses
.data
.keys():
244 if LibraryName
.upper() == Key
.upper():
245 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
250 if Library
not in LibraryList
:
251 LibraryList
.append(Library
)
252 LibraryConsumerList
.append(Library
)