]>
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
,additionalPkgs
):
48 PkgList
= GetPackageList(Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
49 PkgList
= set(PkgList
)
50 PkgList |
= additionalPkgs
56 if GlobalData
.MixedPcd
:
57 for PcdItem
in GlobalData
.MixedPcd
.keys():
58 if (PcdCName
, PcdTokenName
) in GlobalData
.MixedPcd
[PcdItem
]:
61 if (PcdCName
, PcdTokenName
) not in DecPcds
.keys():
62 DecPcds
[PcdCName
, PcdTokenName
] = Pkg
.Pcds
[Pcd
]
65 ## Get all dependent libraries for a module
67 # @param Module: InfBuildData instance
68 # @param Platform: DscBuildData instance
69 # @param BuildDatabase: The database saves all data for all metafiles
70 # @param Arch: Current arch
71 # @param Target: Current target
72 # @param Toolchain: Current toolchain
73 # @retval: List of dependent libraries which are InfBuildData instances
75 def GetLiabraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
76 if Module
.AutoGenVersion
>= 0x00010005:
77 return _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
)
79 return _ResolveLibraryReference(Module
, Platform
)
81 def _GetModuleLibraryInstances(Module
, Platform
, BuildDatabase
, Arch
, Target
, Toolchain
):
82 ModuleType
= Module
.ModuleType
84 # for overriding library instances with module specific setting
85 PlatformModule
= Platform
.Modules
[str(Module
)]
87 # add forced library instances (specified under LibraryClasses sections)
89 # If a module has a MODULE_TYPE of USER_DEFINED,
90 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
92 if Module
.ModuleType
!= SUP_MODULE_USER_DEFINED
:
93 for LibraryClass
in Platform
.LibraryClasses
.GetKeys():
94 if LibraryClass
.startswith("NULL") and Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]:
95 Module
.LibraryClasses
[LibraryClass
] = Platform
.LibraryClasses
[LibraryClass
, Module
.ModuleType
]
97 # add forced library instances (specified in module overrides)
98 for LibraryClass
in PlatformModule
.LibraryClasses
:
99 if LibraryClass
.startswith("NULL"):
100 Module
.LibraryClasses
[LibraryClass
] = PlatformModule
.LibraryClasses
[LibraryClass
]
103 LibraryConsumerList
= [Module
]
105 ConsumedByList
= sdict()
106 LibraryInstance
= sdict()
108 while len(LibraryConsumerList
) > 0:
109 M
= LibraryConsumerList
.pop()
110 for LibraryClassName
in M
.LibraryClasses
:
111 if LibraryClassName
not in LibraryInstance
:
112 # override library instance for this module
113 if LibraryClassName
in PlatformModule
.LibraryClasses
:
114 LibraryPath
= PlatformModule
.LibraryClasses
[LibraryClassName
]
116 LibraryPath
= Platform
.LibraryClasses
[LibraryClassName
, ModuleType
]
117 if LibraryPath
== None or LibraryPath
== "":
118 LibraryPath
= M
.LibraryClasses
[LibraryClassName
]
119 if LibraryPath
== None or LibraryPath
== "":
122 LibraryModule
= BuildDatabase
[LibraryPath
, Arch
, Target
, Toolchain
]
123 # for those forced library instance (NULL library), add a fake library class
124 if LibraryClassName
.startswith("NULL"):
125 LibraryModule
.LibraryClass
.append(LibraryClassObject(LibraryClassName
, [ModuleType
]))
126 elif LibraryModule
.LibraryClass
== None \
127 or len(LibraryModule
.LibraryClass
) == 0 \
128 or (ModuleType
!= 'USER_DEFINED'
129 and ModuleType
not in LibraryModule
.LibraryClass
[0].SupModList
):
130 # only USER_DEFINED can link against any library instance despite of its SupModList
133 LibraryInstance
[LibraryClassName
] = LibraryModule
134 LibraryConsumerList
.append(LibraryModule
)
136 LibraryModule
= LibraryInstance
[LibraryClassName
]
138 if LibraryModule
== None:
141 if LibraryModule
.ConstructorList
!= [] and LibraryModule
not in Constructor
:
142 Constructor
.append(LibraryModule
)
144 if LibraryModule
not in ConsumedByList
:
145 ConsumedByList
[LibraryModule
] = []
146 # don't add current module itself to consumer list
148 if M
in ConsumedByList
[LibraryModule
]:
150 ConsumedByList
[LibraryModule
].append(M
)
152 # Initialize the sorted output list to the empty set
154 SortedLibraryList
= []
156 # Q <- Set of all nodes with no incoming edges
158 LibraryList
= [] #LibraryInstance.values()
160 for LibraryClassName
in LibraryInstance
:
161 M
= LibraryInstance
[LibraryClassName
]
162 LibraryList
.append(M
)
163 if ConsumedByList
[M
] == []:
167 # start the DAG algorithm
171 while Q
== [] and EdgeRemoved
:
173 # for each node Item with a Constructor
174 for Item
in LibraryList
:
175 if Item
not in Constructor
:
177 # for each Node without a constructor with an edge e from Item to Node
178 for Node
in ConsumedByList
[Item
]:
179 if Node
in Constructor
:
181 # remove edge e from the graph if Node has no constructor
182 ConsumedByList
[Item
].remove(Node
)
184 if ConsumedByList
[Item
] == []:
190 # DAG is done if there's no more incoming edge for all nodes
197 SortedLibraryList
.append(Node
)
199 # for each node Item with an edge e from Node to Item do
200 for Item
in LibraryList
:
201 if Node
not in ConsumedByList
[Item
]:
203 # remove edge e from the graph
204 ConsumedByList
[Item
].remove(Node
)
206 if ConsumedByList
[Item
] != []:
208 # insert Item into Q, if Item has no other incoming edges
212 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
214 for Item
in LibraryList
:
215 if ConsumedByList
[Item
] != [] and Item
in Constructor
and len(Constructor
) > 1:
217 if Item
not in SortedLibraryList
:
218 SortedLibraryList
.append(Item
)
221 # Build the list of constructor and destructir names
222 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
224 SortedLibraryList
.reverse()
225 return SortedLibraryList
227 def _ResolveLibraryReference(Module
, Platform
):
228 LibraryConsumerList
= [Module
]
230 # "CompilerStub" is a must for Edk modules
232 Module
.Libraries
.append("CompilerStub")
234 while len(LibraryConsumerList
) > 0:
235 M
= LibraryConsumerList
.pop()
236 for LibraryName
in M
.Libraries
:
237 Library
= Platform
.LibraryClasses
[LibraryName
, ':dummy:']
239 for Key
in Platform
.LibraryClasses
.data
.keys():
240 if LibraryName
.upper() == Key
.upper():
241 Library
= Platform
.LibraryClasses
[Key
, ':dummy:']
246 if Library
not in LibraryList
:
247 LibraryList
.append(Library
)
248 LibraryConsumerList
.append(Library
)