]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Workspace/WorkspaceCommon.py
2 # Common routines used by workspace
4 # Copyright (c) 2012 - 2016, 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)
47 def GetDeclaredPcd(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
48 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
54 if GlobalData
.MixedPcd
:
55 for PcdItem
in GlobalData
.MixedPcd
.keys():
56 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
59 if (PcdCName
, PcdTokenName
) not in DecPcds
.keys():
60 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
63 ## Get all dependent libraries for a module
65 # @param Module: InfBuildData instance
66 # @param Platform: DscBuildData instance
67 # @param BuildDatabase: The database saves all data for all metafiles
68 # @param Arch: Current arch
69 # @param Target: Current target
70 # @param Toolchain: Current toolchain
71 # @retval: List of dependent libraries which are InfBuildData instances
73 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
74 if Module
.AutoGenVersion
>= 0x00010005:
75 return _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
77 return _ResolveLibraryReference(Module
, Platform
)
79 def _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
80 ModuleType
= Module
.ModuleType
82 # for overriding library instances with module specific setting
83 PlatformModule
= Platform
.Modules
[str(Module
)]
85 # add forced library instances (specified under LibraryClasses sections)
87 # If a module has a MODULE_TYPE of USER_DEFINED,
88 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
90 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
91 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
92 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
93 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
95 # add forced library instances (specified in module overrides)
96 for LibraryClass
in PlatformModule
.LibraryClasses
:
97 if LibraryClass
.startswith("NULL"):
98 Module
.LibraryClasses
[LibraryClass
] = PlatformModule
.LibraryClasses
[LibraryClass
]
101 LibraryConsumerList
= [Module
]
103 ConsumedByList
= sdict()
104 LibraryInstance
= sdict()
106 while len(LibraryConsumerList
) > 0:
107 M
= LibraryConsumerList
.pop()
108 for LibraryClassName
in M
.LibraryClasses
:
109 if LibraryClassName
not in LibraryInstance
:
110 # override library instance for this module
111 if LibraryClassName
in PlatformModule
.LibraryClasses
:
112 LibraryPath
= PlatformModule
.LibraryClasses
[LibraryClassName
]
114 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
115 if LibraryPath
== None or LibraryPath
== "":
116 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
117 if LibraryPath
== None or LibraryPath
== "":
120 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
121 # for those forced library instance (NULL library), add a fake library class
122 if LibraryClassName
.startswith("NULL"):
123 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
124 elif LibraryModule
.LibraryClass
== None \
125 or len(LibraryModule
.LibraryClass
) == 0 \
126 or (ModuleType
!= 'USER_DEFINED'
127 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
128 # only USER_DEFINED can link against any library instance despite of its SupModList
131 LibraryInstance
[LibraryClassName
] = LibraryModule
132 LibraryConsumerList
.append(LibraryModule
)
134 LibraryModule
= LibraryInstance
[LibraryClassName
]
136 if LibraryModule
== None:
139 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
140 Constructor
.append(LibraryModule
)
142 if LibraryModule
not in ConsumedByList
:
143 ConsumedByList
[LibraryModule
] = []
144 # don't add current module itself to consumer list
146 if M
in ConsumedByList
[LibraryModule
]:
148 ConsumedByList
[LibraryModule
].append(M
)
150 # Initialize the sorted output list to the empty set
152 SortedLibraryList
= []
154 # Q <- Set of all nodes with no incoming edges
156 LibraryList
= [] #LibraryInstance.values()
158 for LibraryClassName
in LibraryInstance
:
159 M
= LibraryInstance
[LibraryClassName
]
160 LibraryList
.append(M
)
161 if ConsumedByList
[M
] == []:
165 # start the DAG algorithm
169 while Q
== [] and EdgeRemoved
:
171 # for each node Item with a Constructor
172 for Item
in LibraryList
:
173 if Item
not in Constructor
:
175 # for each Node without a constructor with an edge e from Item to Node
176 for Node
in ConsumedByList
[Item
]:
177 if Node
in Constructor
:
179 # remove edge e from the graph if Node has no constructor
180 ConsumedByList
[Item
].remove(Node
)
182 if ConsumedByList
[Item
] == []:
188 # DAG is done if there's no more incoming edge for all nodes
195 SortedLibraryList
.append(Node
)
197 # for each node Item with an edge e from Node to Item do
198 for Item
in LibraryList
:
199 if Node
not in ConsumedByList
[Item
]:
201 # remove edge e from the graph
202 ConsumedByList
[Item
].remove(Node
)
204 if ConsumedByList
[Item
] != []:
206 # insert Item into Q, if Item has no other incoming edges
210 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
212 for Item
in LibraryList
:
213 if ConsumedByList
[Item
] != [] and Item
in Constructor
and len(Constructor
) > 1:
215 if Item
not in SortedLibraryList
:
216 SortedLibraryList
.append(Item
)
219 # Build the list of constructor and destructir names
220 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
222 SortedLibraryList
.reverse()
223 return SortedLibraryList
225 def _ResolveLibraryReference(Module
, Platform
):
226 LibraryConsumerList
= [Module
]
228 # "CompilerStub" is a must for Edk modules
230 Module
.Libraries
.append("CompilerStub")
232 while len(LibraryConsumerList
) > 0:
233 M
= LibraryConsumerList
.pop()
234 for LibraryName
in M
.Libraries
:
235 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
237 for Key
in Platform
.LibraryClasses
.data
.keys():
238 if LibraryName
.upper() == Key
.upper():
239 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
244 if Library
not in LibraryList
:
245 LibraryList
.append(Library
)
246 LibraryConsumerList
.append(Library
)