]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/msa2inf/StoreInf.py
Sync EDKII BaseTools to BaseTools project r1903.
[mirror_edk2.git] / BaseTools / Source / Python / msa2inf / StoreInf.py
1 ## @file
2 # Store a Module class object to an INF file.
3 #
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
9 #
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.
12 #
13
14 ##
15 # Import Modules
16 #
17 from LoadMsa import LoadMsa
18 from CommonDataClass.ModuleClass import *
19 from Common.MigrationUtilities import *
20
21 ## Get the produced library class.
22 #
23 # Return the item of Library Class based on Library .
24 #
25 # @param LibraryClasses A list of library classes the module produces.
26 #
27 # @retval LibraryClassItem A text format library class item.
28 #
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)
37
38 return "|".join(ProducedLibraryClasses)
39
40
41 ## Store Defines section.
42 #
43 # Write [Defines] section to the InfFile based on Module class object.
44 # Different CPU architectures are specified in the subsection if possible.
45 #
46 # @param InfFile The output INF file to store the Defines section.
47 # @param Module An input Module class object.
48 #
49 def StoreModuleDefinesSection(InfFile, Module):
50 ModuleHeader = Module.Header
51
52 DefinesTupleList = []
53 DefinesTupleList.append(("INF_VERSION", ModuleHeader.InfVersion))
54
55 if ModuleHeader.Name != "":
56 DefinesTupleList.append(("BASE_NAME", ModuleHeader.Name))
57
58 if ModuleHeader.Guid != "":
59 DefinesTupleList.append(("FILE_GUID", ModuleHeader.Guid))
60
61 if ModuleHeader.Version != "":
62 DefinesTupleList.append(("VERSION_STRING", ModuleHeader.Version))
63
64 if ModuleHeader.ModuleType != "":
65 DefinesTupleList.append(("MODULE_TYPE", ModuleHeader.ModuleType))
66
67 if ModuleHeader.UefiSpecificationVersion != "":
68 DefinesTupleList.append(("UEFI_SPECIFICATION_VERSION", ModuleHeader.UefiSpecificationVersion))
69
70 if ModuleHeader.EdkReleaseVersion != "":
71 DefinesTupleList.append(("EDK_RELEASE_VERSION", ModuleHeader.EdkReleaseVersion))
72
73 ProducedLibraryClass = GetModuleLibraryClass(ModuleHeader.LibraryClass)
74 if ProducedLibraryClass != "":
75 DefinesTupleList.append(("LIBRARY_CLASS", ProducedLibraryClass))
76
77 if ModuleHeader.MakefileName != "":
78 DefinesTupleList.append(("MAKEFILE_NAME", ModuleHeader.MakeFileName))
79
80 if ModuleHeader.PcdIsDriver != "":
81 DefinesTupleList.append(("PCD_DRIVER", "TRUE"))
82
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))
90
91 if len(Module.ExternLibraries) > 0:
92 Constructor = Module.ExternLibraries[0].Constructor
93 Destructor = Module.ExternLibraries[0].Destructor
94 if Constructor != "":
95 DefinesTupleList.append(("CONSTRUCTOR", Constructor))
96 if Destructor != "":
97 DefinesTupleList.append(("DESTRUCTOR", Destructor))
98
99 StoreDefinesSection(InfFile, DefinesTupleList)
100
101
102 ## Return a Module Source Item.
103 #
104 # Read the input ModuleSourceFile class object and return one line of Source Item.
105 #
106 # @param ModuleSourceFile An input ModuleSourceFile class object.
107 #
108 # @retval SourceItem A Module Source Item.
109 #
110 def GetModuleSourceItem(ModuleSourceFile):
111 Source = []
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("|")
118
119
120 ## Store Sources section.
121 #
122 # Write [Sources] section to the InfFile based on Module class object.
123 # Different CPU architectures are specified in the subsection if possible.
124 #
125 # @param InfFile The output INF file to store the Sources section.
126 # @param Module An input Module class object.
127 #
128 def StoreModuleSourcesSection(InfFile, Module):
129 Section = GetSection("Sources", GetModuleSourceItem, Module.Sources)
130 StoreTextFile(InfFile, Section)
131
132
133 ## Return a Module Binary Item.
134 #
135 # Read the input ModuleBinaryFile class object and return one line of Binary Item.
136 #
137 # @param ModuleBinaryFile An input ModuleBinaryFile class object.
138 #
139 # @retval BinaryItem A Module Binary Item.
140 #
141 def GetModuleBinaryItem(ModuleBinaryFile):
142 Binary = []
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("|")
148
149
150 ## Store Binaries section.
151 #
152 # Write [Binaries] section to the InfFile based on Module class object.
153 # Different CPU architectures are specified in the subsection if possible.
154 #
155 # @param InfFile The output INF file to store the Binaries section.
156 # @param Module An input Module class object.
157 #
158 def StoreModuleBinariesSection(InfFile, Module):
159 Section = GetSection("Binaries", GetModuleBinaryItem, Module.Binaries)
160 StoreTextFile(InfFile, Section)
161
162
163 ## Return a Module Library Class Item.
164 #
165 # Read the input LibraryClass class object and return one line of Library Class Item.
166 #
167 # @param LibraryClass An input LibraryClass class object.
168 #
169 # @retval LibraryClassItem A Module Library Class Item.
170 #
171 def GetModuleLibraryClassItem(LibraryClass):
172 if "ALWAYS_PRODUCED" in LibraryClass.Usage:
173 return ""
174
175 LibraryClassList = []
176 LibraryClassList.append(LibraryClass.LibraryClass)
177 LibraryClassList.append(LibraryClass.RecommendedInstance)
178 LibraryClassList.append(LibraryClass.FeatureFlag)
179
180 return "|".join(LibraryClassList).rstrip("|")
181
182
183 ## Store Library Classes section.
184 #
185 # Write [LibraryClasses] section to the InfFile based on Module class object.
186 # Different CPU architectures are specified in the subsection if possible.
187 #
188 # @param InfFile The output INF file to store the Library Classes section.
189 # @param Module An input Module class object.
190 #
191 def StoreModuleLibraryClassesSection(InfFile, Module):
192 Section = GetSection("LibraryClasses", GetModuleLibraryClassItem, Module.LibraryClasses)
193 StoreTextFile(InfFile, Section)
194
195
196 ## Return a Module Package Item.
197 #
198 # Read the input PackageDependency class object and return one line of Package Item.
199 #
200 # @param PackageDependency An input PackageDependency class object.
201 #
202 # @retval PackageItem A Module Package Item.
203 #
204 def GetModulePackageItem(PackageDependency):
205 return PackageDependency.FilePath
206
207
208 ## Store Packages section.
209 #
210 # Write [Packages] section to the InfFile based on Module class object.
211 # Different CPU architectures are specified in the subsection if possible.
212 #
213 # @param InfFile The output INF file to store the Packages section.
214 # @param Module An input Module class object.
215 #
216 def StoreModulePackagesSection(InfFile, Module):
217 Section = GetSection("Packages", GetModulePackageItem, Module.PackageDependencies)
218 StoreTextFile(InfFile, Section)
219
220
221 ## Return a Module Guid C Name Item.
222 #
223 # Read the input Guid class object and return one line of Guid C Name Item.
224 #
225 # @param Guid An input Guid class object.
226 #
227 # @retval GuidCNameItem A Module Guid C Name Item.
228 #
229 def GetModuleGuidCNameItem(Guid):
230 try:
231 return Guid.GuidCName
232 except:
233 return Guid.CName
234
235
236 ## Store Protocols section.
237 #
238 # Write [Protocols] section to the InfFile based on Module class object.
239 # Different CPU architectures are specified in the subsection if possible.
240 #
241 # @param InfFile The output INF file to store the Protocols section.
242 # @param Module An input Module class object.
243 #
244 def StoreModuleProtocolsSection(InfFile, Module):
245 Section = GetSection("Protocols", GetModuleGuidCNameItem, Module.Protocols)
246 StoreTextFile(InfFile, Section)
247
248
249 ## Store Ppis section.
250 #
251 # Write [Ppis] section to the InfFile based on Module class object.
252 # Different CPU architectures are specified in the subsection if possible.
253 #
254 # @param InfFile The output INF file to store the Ppis section.
255 # @param Module An input Module class object.
256 #
257 def StoreModulePpisSection(InfFile, Module):
258 Section = GetSection("Ppis", GetModuleGuidCNameItem, Module.Ppis)
259 StoreTextFile(InfFile, Section)
260
261
262 ## Store Guids section.
263 #
264 # Write [Guids] section to the InfFile based on Module class object.
265 # Different CPU architectures are specified in the subsection if possible.
266 #
267 # @param InfFile The output INF file to store the Guids section.
268 # @param Module An input Module class object.
269 #
270 def StoreModuleGuidsSection(InfFile, Module):
271 Guids = []
272 Guids += Module.Guids
273 Guids += Module.Events
274 Guids += Module.Hobs
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)
281
282
283 ## Return a Module Pcd Item.
284 #
285 # Read the input Pcd class object and return one line of Pcd Item.
286 #
287 # @param Pcd An input Pcd class object.
288 #
289 # @retval PcdItem A Module Pcd Item.
290 #
291 def GetModulePcdItem(Pcd):
292 PcdItem = "%s.%s" % (Pcd.TokenSpaceGuidCName, Pcd.CName)
293 if Pcd.DefaultValue != "":
294 PcdItem = "%s|%s" % (PcdItem, Pcd.DefaultValue)
295
296 return PcdItem
297
298
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",
304 "DYNAMIC" : "Pcd",
305 "DYNAMIC_EX" : "PcdEx"
306 }
307
308 ## Store Pcds section.
309 #
310 # Write [(PcdType)] section to the InfFile based on Module class object.
311 # Different CPU architectures are specified in the subsection if possible.
312 #
313 # @param InfFile The output INF file to store the Pcds section.
314 # @param Module An input Module class object.
315 #
316 def StoreModulePcdsSection(InfFile, Module):
317 PcdsDict = {}
318 for Pcd in Module.PcdCodes:
319 PcdSectionName = mInfPcdSectionNameDict.get(Pcd.ItemType)
320 if PcdSectionName:
321 PcdsDict.setdefault(PcdSectionName, []).append(Pcd)
322 else:
323 EdkLogger.info("Unknown Pcd Item Type: %s" % Pcd.ItemType)
324
325 Section = ""
326 for PcdSectionName in PcdsDict:
327 Pcds = PcdsDict[PcdSectionName]
328 Section += GetSection(PcdSectionName, GetModulePcdItem, Pcds)
329 Section += "\n"
330
331 StoreTextFile(InfFile, Section)
332
333
334 ## Return a Module Depex Item.
335 #
336 # Read the input Depex class object and return one line of Depex Item.
337 #
338 # @param Depex An input Depex class object.
339 #
340 # @retval DepexItem A Module Depex Item.
341 #
342 def GetModuleDepexItem(Depex):
343 return Depex.Depex
344
345
346 ## Store Depex section.
347 #
348 # Write [Depex] section to the InfFile based on Module class object.
349 # Different CPU architectures are specified in the subsection if possible.
350 #
351 # @param InfFile The output INF file to store the Depex section.
352 # @param Module An input Module class object.
353 #
354 def StoreModuleDepexSection(InfFile, Module):
355 Section = GetSection("Depex", GetModuleDepexItem, Module.Depex)
356 StoreTextFile(InfFile, Section)
357
358
359 ## Return a Module Build Option Item.
360 #
361 # Read the input BuildOption class object and return one line of Build Option Item.
362 #
363 # @param BuildOption An input BuildOption class object.
364 #
365 # @retval BuildOptionItem A Module Build Option Item.
366 #
367 def GetModuleBuildOptionItem(BuildOption):
368 BuildTarget = BuildOption.BuildTarget
369 if BuildTarget == "":
370 BuildTarget = "*"
371
372 TagName = BuildOption.TagName
373 if TagName == "":
374 TagName = "*"
375
376 ToolCode = BuildOption.ToolCode
377 if ToolCode == "":
378 ToolCode = "*"
379
380 Item = "_".join((BuildTarget, TagName, "*", ToolCode, "Flag"))
381
382 ToolChainFamily = BuildOption.ToolChainFamily
383 if ToolChainFamily != "":
384 Item = "%s:%s" % (ToolChainFamily, Item)
385
386 return "%-30s = %s" % (Item, BuildOption.Option)
387
388
389 ## Store Build Options section.
390 #
391 # Write [BuildOptions] section to the InfFile based on Module class object.
392 # Different CPU architectures are specified in the subsection if possible.
393 #
394 # @param InfFile The output INF file to store the Build Options section.
395 # @param Module An input Module class object.
396 #
397 def StoreModuleBuildOptionsSection(InfFile, Module):
398 Section = GetSection("BuildOption", GetModuleBuildOptionItem, Module.BuildOptions)
399 StoreTextFile(InfFile, Section)
400
401
402 ## Store User Extensions section.
403 #
404 # Write [UserExtensions] section to the InfFile based on Module class object.
405 #
406 # @param InfFile The output INF file to store the User Extensions section.
407 # @param Module An input Module class object.
408 #
409 def StoreModuleUserExtensionsSection(InfFile, Module):
410 Section = "".join(map(GetUserExtensions, Module.UserExtensions))
411 StoreTextFile(InfFile, Section)
412
413
414 ## Store a Module class object to a new INF file.
415 #
416 # Read an input Module class object and save the contents to a new INF file.
417 #
418 # @param INFFileName The output INF file.
419 # @param Module An input Package class object.
420 #
421 def StoreInf(InfFileName, Module):
422 InfFile = open(InfFileName, "w+")
423 EdkLogger.info("Save file to %s" % InfFileName)
424
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)
438
439 InfFile.close()
440
441 if __name__ == '__main__':
442 pass