2 # Store a Module class object to an INF file.
4 # Copyright (c) 2007, Intel Corporation
5 # All rights reserved. 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.
17 from LoadMsa
import LoadMsa
18 from CommonDataClass
.ModuleClass
import *
19 from Common
.MigrationUtilities
import *
21 ## Get the produced library class.
23 # Return the item of Library Class based on Library .
25 # @param LibraryClasses A list of library classes the module produces.
27 # @retval LibraryClassItem A text format library class item.
29 def GetModuleLibraryClass(LibraryClasses
):
30 ProducedLibraryClasses
= []
31 for LibraryClass
in LibraryClasses
:
32 ProducedLibraryClass
= LibraryClass
.LibraryClass
33 SupportedModueTypes
= " ".join(LibraryClass
.SupModuleList
)
34 if SupportedModueTypes
!= "":
35 ProducedLibraryClass
+= "|" + SupportedModueTypes
36 ProducedLibraryClasses
.append(ProducedLibraryClass
)
38 return "|".join(ProducedLibraryClasses
)
41 ## Store Defines section.
43 # Write [Defines] section to the InfFile based on Module class object.
44 # Different CPU architectures are specified in the subsection if possible.
46 # @param InfFile The output INF file to store the Defines section.
47 # @param Module An input Module class object.
49 def StoreModuleDefinesSection(InfFile
, Module
):
50 ModuleHeader
= Module
.Header
53 DefinesTupleList
.append(("INF_VERSION", ModuleHeader
.InfVersion
))
55 if ModuleHeader
.Name
!= "":
56 DefinesTupleList
.append(("BASE_NAME", ModuleHeader
.Name
))
58 if ModuleHeader
.Guid
!= "":
59 DefinesTupleList
.append(("FILE_GUID", ModuleHeader
.Guid
))
61 if ModuleHeader
.Version
!= "":
62 DefinesTupleList
.append(("VERSION_STRING", ModuleHeader
.Version
))
64 if ModuleHeader
.ModuleType
!= "":
65 DefinesTupleList
.append(("MODULE_TYPE", ModuleHeader
.ModuleType
))
67 if ModuleHeader
.UefiSpecificationVersion
!= "":
68 DefinesTupleList
.append(("UEFI_SPECIFICATION_VERSION", ModuleHeader
.UefiSpecificationVersion
))
70 if ModuleHeader
.EdkReleaseVersion
!= "":
71 DefinesTupleList
.append(("EDK_RELEASE_VERSION", ModuleHeader
.EdkReleaseVersion
))
73 ProducedLibraryClass
= GetModuleLibraryClass(ModuleHeader
.LibraryClass
)
74 if ProducedLibraryClass
!= "":
75 DefinesTupleList
.append(("LIBRARY_CLASS", ProducedLibraryClass
))
77 if ModuleHeader
.MakefileName
!= "":
78 DefinesTupleList
.append(("MAKEFILE_NAME", ModuleHeader
.MakeFileName
))
80 if ModuleHeader
.PcdIsDriver
!= "":
81 DefinesTupleList
.append(("PCD_DRIVER", "TRUE"))
83 if len(Module
.ExternImages
) > 0:
84 ModuleEntryPoint
= Module
.ExternImages
[0].ModuleEntryPoint
85 ModuleUnloadImage
= Module
.ExternImages
[0].ModuleUnloadImage
86 if ModuleEntryPoint
!= "":
87 DefinesTupleList
.append(("ENTRY_POINT", ModuleEntryPoint
))
88 if ModuleUnloadImage
!= "":
89 DefinesTupleList
.append(("UNLOAD_IMAGE", ModuleUnloadImage
))
91 if len(Module
.ExternLibraries
) > 0:
92 Constructor
= Module
.ExternLibraries
[0].Constructor
93 Destructor
= Module
.ExternLibraries
[0].Destructor
95 DefinesTupleList
.append(("CONSTRUCTOR", Constructor
))
97 DefinesTupleList
.append(("DESTRUCTOR", Destructor
))
99 StoreDefinesSection(InfFile
, DefinesTupleList
)
102 ## Return a Module Source Item.
104 # Read the input ModuleSourceFile class object and return one line of Source Item.
106 # @param ModuleSourceFile An input ModuleSourceFile class object.
108 # @retval SourceItem A Module Source Item.
110 def GetModuleSourceItem(ModuleSourceFile
):
112 Source
.append(ModuleSourceFile
.SourceFile
)
113 Source
.append(ModuleSourceFile
.ToolChainFamily
)
114 Source
.append(ModuleSourceFile
.TagName
)
115 Source
.append(ModuleSourceFile
.ToolCode
)
116 Source
.append(ModuleSourceFile
.FeatureFlag
)
117 return "|".join(Source
).rstrip("|")
120 ## Store Sources section.
122 # Write [Sources] section to the InfFile based on Module class object.
123 # Different CPU architectures are specified in the subsection if possible.
125 # @param InfFile The output INF file to store the Sources section.
126 # @param Module An input Module class object.
128 def StoreModuleSourcesSection(InfFile
, Module
):
129 Section
= GetSection("Sources", GetModuleSourceItem
, Module
.Sources
)
130 StoreTextFile(InfFile
, Section
)
133 ## Return a Module Binary Item.
135 # Read the input ModuleBinaryFile class object and return one line of Binary Item.
137 # @param ModuleBinaryFile An input ModuleBinaryFile class object.
139 # @retval BinaryItem A Module Binary Item.
141 def GetModuleBinaryItem(ModuleBinaryFile
):
143 Binary
.append(ModuleBinaryFile
.FileType
)
144 Binary
.append(ModuleBinaryFile
.BinaryFile
)
145 Binary
.append(ModuleBinaryFile
.Target
)
146 Binary
.append(ModuleBinaryFile
.FeatureFlag
)
147 return "|".join(Binary
).rstrip("|")
150 ## Store Binaries section.
152 # Write [Binaries] section to the InfFile based on Module class object.
153 # Different CPU architectures are specified in the subsection if possible.
155 # @param InfFile The output INF file to store the Binaries section.
156 # @param Module An input Module class object.
158 def StoreModuleBinariesSection(InfFile
, Module
):
159 Section
= GetSection("Binaries", GetModuleBinaryItem
, Module
.Binaries
)
160 StoreTextFile(InfFile
, Section
)
163 ## Return a Module Library Class Item.
165 # Read the input LibraryClass class object and return one line of Library Class Item.
167 # @param LibraryClass An input LibraryClass class object.
169 # @retval LibraryClassItem A Module Library Class Item.
171 def GetModuleLibraryClassItem(LibraryClass
):
172 if "ALWAYS_PRODUCED" in LibraryClass
.Usage
:
175 LibraryClassList
= []
176 LibraryClassList
.append(LibraryClass
.LibraryClass
)
177 LibraryClassList
.append(LibraryClass
.RecommendedInstance
)
178 LibraryClassList
.append(LibraryClass
.FeatureFlag
)
180 return "|".join(LibraryClassList
).rstrip("|")
183 ## Store Library Classes section.
185 # Write [LibraryClasses] section to the InfFile based on Module class object.
186 # Different CPU architectures are specified in the subsection if possible.
188 # @param InfFile The output INF file to store the Library Classes section.
189 # @param Module An input Module class object.
191 def StoreModuleLibraryClassesSection(InfFile
, Module
):
192 Section
= GetSection("LibraryClasses", GetModuleLibraryClassItem
, Module
.LibraryClasses
)
193 StoreTextFile(InfFile
, Section
)
196 ## Return a Module Package Item.
198 # Read the input PackageDependency class object and return one line of Package Item.
200 # @param PackageDependency An input PackageDependency class object.
202 # @retval PackageItem A Module Package Item.
204 def GetModulePackageItem(PackageDependency
):
205 return PackageDependency
.FilePath
208 ## Store Packages section.
210 # Write [Packages] section to the InfFile based on Module class object.
211 # Different CPU architectures are specified in the subsection if possible.
213 # @param InfFile The output INF file to store the Packages section.
214 # @param Module An input Module class object.
216 def StoreModulePackagesSection(InfFile
, Module
):
217 Section
= GetSection("Packages", GetModulePackageItem
, Module
.PackageDependencies
)
218 StoreTextFile(InfFile
, Section
)
221 ## Return a Module Guid C Name Item.
223 # Read the input Guid class object and return one line of Guid C Name Item.
225 # @param Guid An input Guid class object.
227 # @retval GuidCNameItem A Module Guid C Name Item.
229 def GetModuleGuidCNameItem(Guid
):
231 return Guid
.GuidCName
236 ## Store Protocols section.
238 # Write [Protocols] section to the InfFile based on Module class object.
239 # Different CPU architectures are specified in the subsection if possible.
241 # @param InfFile The output INF file to store the Protocols section.
242 # @param Module An input Module class object.
244 def StoreModuleProtocolsSection(InfFile
, Module
):
245 Section
= GetSection("Protocols", GetModuleGuidCNameItem
, Module
.Protocols
)
246 StoreTextFile(InfFile
, Section
)
249 ## Store Ppis section.
251 # Write [Ppis] section to the InfFile based on Module class object.
252 # Different CPU architectures are specified in the subsection if possible.
254 # @param InfFile The output INF file to store the Ppis section.
255 # @param Module An input Module class object.
257 def StoreModulePpisSection(InfFile
, Module
):
258 Section
= GetSection("Ppis", GetModuleGuidCNameItem
, Module
.Ppis
)
259 StoreTextFile(InfFile
, Section
)
262 ## Store Guids section.
264 # Write [Guids] section to the InfFile based on Module class object.
265 # Different CPU architectures are specified in the subsection if possible.
267 # @param InfFile The output INF file to store the Guids section.
268 # @param Module An input Module class object.
270 def StoreModuleGuidsSection(InfFile
, Module
):
272 Guids
+= Module
.Guids
273 Guids
+= Module
.Events
275 Guids
+= Module
.Variables
276 Guids
+= Module
.SystemTables
277 Guids
+= Module
.DataHubs
278 Guids
+= Module
.HiiPackages
279 Section
= GetSection("Guids", GetModuleGuidCNameItem
, Guids
)
280 StoreTextFile(InfFile
, Section
)
283 ## Return a Module Pcd Item.
285 # Read the input Pcd class object and return one line of Pcd Item.
287 # @param Pcd An input Pcd class object.
289 # @retval PcdItem A Module Pcd Item.
291 def GetModulePcdItem(Pcd
):
292 PcdItem
= "%s.%s" % (Pcd
.TokenSpaceGuidCName
, Pcd
.CName
)
293 if Pcd
.DefaultValue
!= "":
294 PcdItem
= "%s|%s" % (PcdItem
, Pcd
.DefaultValue
)
299 ## DEC Pcd Section Name dictionary indexed by PCD Item Type.
300 mInfPcdSectionNameDict
= {
301 "FEATURE_FLAG" : "FeaturePcd",
302 "FIXED_AT_BUILD" : "FixedPcd",
303 "PATCHABLE_IN_MODULE" : "PatchPcd",
305 "DYNAMIC_EX" : "PcdEx"
308 ## Store Pcds section.
310 # Write [(PcdType)] section to the InfFile based on Module class object.
311 # Different CPU architectures are specified in the subsection if possible.
313 # @param InfFile The output INF file to store the Pcds section.
314 # @param Module An input Module class object.
316 def StoreModulePcdsSection(InfFile
, Module
):
318 for Pcd
in Module
.PcdCodes
:
319 PcdSectionName
= mInfPcdSectionNameDict
.get(Pcd
.ItemType
)
321 PcdsDict
.setdefault(PcdSectionName
, []).append(Pcd
)
323 EdkLogger
.info("Unknown Pcd Item Type: %s" % Pcd
.ItemType
)
326 for PcdSectionName
in PcdsDict
:
327 Pcds
= PcdsDict
[PcdSectionName
]
328 Section
+= GetSection(PcdSectionName
, GetModulePcdItem
, Pcds
)
331 StoreTextFile(InfFile
, Section
)
334 ## Return a Module Depex Item.
336 # Read the input Depex class object and return one line of Depex Item.
338 # @param Depex An input Depex class object.
340 # @retval DepexItem A Module Depex Item.
342 def GetModuleDepexItem(Depex
):
346 ## Store Depex section.
348 # Write [Depex] section to the InfFile based on Module class object.
349 # Different CPU architectures are specified in the subsection if possible.
351 # @param InfFile The output INF file to store the Depex section.
352 # @param Module An input Module class object.
354 def StoreModuleDepexSection(InfFile
, Module
):
355 Section
= GetSection("Depex", GetModuleDepexItem
, Module
.Depex
)
356 StoreTextFile(InfFile
, Section
)
359 ## Return a Module Build Option Item.
361 # Read the input BuildOption class object and return one line of Build Option Item.
363 # @param BuildOption An input BuildOption class object.
365 # @retval BuildOptionItem A Module Build Option Item.
367 def GetModuleBuildOptionItem(BuildOption
):
368 BuildTarget
= BuildOption
.BuildTarget
369 if BuildTarget
== "":
372 TagName
= BuildOption
.TagName
376 ToolCode
= BuildOption
.ToolCode
380 Item
= "_".join((BuildTarget
, TagName
, "*", ToolCode
, "Flag"))
382 ToolChainFamily
= BuildOption
.ToolChainFamily
383 if ToolChainFamily
!= "":
384 Item
= "%s:%s" % (ToolChainFamily
, Item
)
386 return "%-30s = %s" % (Item
, BuildOption
.Option
)
389 ## Store Build Options section.
391 # Write [BuildOptions] section to the InfFile based on Module class object.
392 # Different CPU architectures are specified in the subsection if possible.
394 # @param InfFile The output INF file to store the Build Options section.
395 # @param Module An input Module class object.
397 def StoreModuleBuildOptionsSection(InfFile
, Module
):
398 Section
= GetSection("BuildOption", GetModuleBuildOptionItem
, Module
.BuildOptions
)
399 StoreTextFile(InfFile
, Section
)
402 ## Store User Extensions section.
404 # Write [UserExtensions] section to the InfFile based on Module class object.
406 # @param InfFile The output INF file to store the User Extensions section.
407 # @param Module An input Module class object.
409 def StoreModuleUserExtensionsSection(InfFile
, Module
):
410 Section
= "".join(map(GetUserExtensions
, Module
.UserExtensions
))
411 StoreTextFile(InfFile
, Section
)
414 ## Store a Module class object to a new INF file.
416 # Read an input Module class object and save the contents to a new INF file.
418 # @param INFFileName The output INF file.
419 # @param Module An input Package class object.
421 def StoreInf(InfFileName
, Module
):
422 InfFile
= open(InfFileName
, "w+")
423 EdkLogger
.info("Save file to %s" % InfFileName
)
425 StoreHeader(InfFile
, Module
.Header
)
426 StoreModuleDefinesSection(InfFile
, Module
)
427 StoreModuleSourcesSection(InfFile
, Module
)
428 StoreModuleBinariesSection(InfFile
, Module
)
429 StoreModulePackagesSection(InfFile
, Module
)
430 StoreModuleLibraryClassesSection(InfFile
, Module
)
431 StoreModuleProtocolsSection(InfFile
, Module
)
432 StoreModulePpisSection(InfFile
, Module
)
433 StoreModuleGuidsSection(InfFile
, Module
)
434 StoreModulePcdsSection(InfFile
, Module
)
435 StoreModuleDepexSection(InfFile
, Module
)
436 StoreModuleBuildOptionsSection(InfFile
, Module
)
437 StoreModuleUserExtensionsSection(InfFile
, Module
)
441 if __name__
== '__main__':