summaryrefslogtreecommitdiffstats
path: root/BaseTools/Source/Python/msa2inf/StoreInf.py
blob: d7f6869d76fcd2ce547c019ccec5b27298bb0ae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
## @file
# Store a Module class object to an INF file.
#
# Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
from LoadMsa import LoadMsa
from CommonDataClass.ModuleClass import *
from Common.MigrationUtilities import *

## Get the produced library class.
#
# Return the item of Library Class based on Library .
#
# @param  LibraryClasses     A list of library classes the module produces.
#
# @retval LibraryClassItem   A text format library class item.
#
def GetModuleLibraryClass(LibraryClasses):
    ProducedLibraryClasses = []
    for LibraryClass in LibraryClasses:
        ProducedLibraryClass = LibraryClass.LibraryClass
        SupportedModueTypes = " ".join(LibraryClass.SupModuleList)
        if SupportedModueTypes != "":
            ProducedLibraryClass += "|" + SupportedModueTypes
        ProducedLibraryClasses.append(ProducedLibraryClass)

    return "|".join(ProducedLibraryClasses)


## Store Defines section.
#
# Write [Defines] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Defines section.
# @param  Module               An input Module class object.
#
def StoreModuleDefinesSection(InfFile, Module):
    ModuleHeader = Module.Header
    
    DefinesTupleList = []
    DefinesTupleList.append(("INF_VERSION", ModuleHeader.InfVersion))
    
    if ModuleHeader.Name != "":
        DefinesTupleList.append(("BASE_NAME", ModuleHeader.Name))

    if ModuleHeader.Guid != "":
        DefinesTupleList.append(("FILE_GUID", ModuleHeader.Guid))

    if ModuleHeader.Version != "":
        DefinesTupleList.append(("VERSION_STRING", ModuleHeader.Version))
        
    if ModuleHeader.ModuleType != "":
        DefinesTupleList.append(("MODULE_TYPE", ModuleHeader.ModuleType))

    if ModuleHeader.UefiSpecificationVersion != "":
        DefinesTupleList.append(("UEFI_SPECIFICATION_VERSION", ModuleHeader.UefiSpecificationVersion))
    
    if ModuleHeader.EdkReleaseVersion != "":
        DefinesTupleList.append(("EDK_RELEASE_VERSION", ModuleHeader.EdkReleaseVersion))
    
    ProducedLibraryClass = GetModuleLibraryClass(ModuleHeader.LibraryClass)
    if ProducedLibraryClass != "":
        DefinesTupleList.append(("LIBRARY_CLASS", ProducedLibraryClass))

    if ModuleHeader.MakefileName != "":
        DefinesTupleList.append(("MAKEFILE_NAME", ModuleHeader.MakeFileName))

    if ModuleHeader.PcdIsDriver != "":
        DefinesTupleList.append(("PCD_DRIVER", "TRUE"))

    if len(Module.ExternImages) > 0:
        ModuleEntryPoint = Module.ExternImages[0].ModuleEntryPoint
        ModuleUnloadImage = Module.ExternImages[0].ModuleUnloadImage
        if ModuleEntryPoint != "":
            DefinesTupleList.append(("ENTRY_POINT", ModuleEntryPoint))
        if ModuleUnloadImage != "":
            DefinesTupleList.append(("UNLOAD_IMAGE", ModuleUnloadImage))

    if len(Module.ExternLibraries) > 0:
        Constructor = Module.ExternLibraries[0].Constructor
        Destructor = Module.ExternLibraries[0].Destructor
        if Constructor != "":
            DefinesTupleList.append(("CONSTRUCTOR", Constructor))
        if Destructor != "":
            DefinesTupleList.append(("DESTRUCTOR", Destructor))

    StoreDefinesSection(InfFile, DefinesTupleList)
    

## Return a Module Source Item.
#
# Read the input ModuleSourceFile class object and return one line of Source Item.
#
# @param  ModuleSourceFile     An input ModuleSourceFile class object.
#
# @retval SourceItem           A Module Source Item.
#
def GetModuleSourceItem(ModuleSourceFile):
    Source = []
    Source.append(ModuleSourceFile.SourceFile)
    Source.append(ModuleSourceFile.ToolChainFamily)
    Source.append(ModuleSourceFile.TagName)
    Source.append(ModuleSourceFile.ToolCode)
    Source.append(ModuleSourceFile.FeatureFlag)
    return "|".join(Source).rstrip("|")
    

## Store Sources section.
#
# Write [Sources] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Sources section.
# @param  Module               An input Module class object.
#
def StoreModuleSourcesSection(InfFile, Module):
    Section = GetSection("Sources", GetModuleSourceItem, Module.Sources)
    StoreTextFile(InfFile, Section)


## Return a Module Binary Item.
#
# Read the input ModuleBinaryFile class object and return one line of Binary Item.
#
# @param  ModuleBinaryFile     An input ModuleBinaryFile class object.
#
# @retval BinaryItem           A Module Binary Item.
#
def GetModuleBinaryItem(ModuleBinaryFile):
    Binary = []
    Binary.append(ModuleBinaryFile.FileType)
    Binary.append(ModuleBinaryFile.BinaryFile)
    Binary.append(ModuleBinaryFile.Target)
    Binary.append(ModuleBinaryFile.FeatureFlag)
    return "|".join(Binary).rstrip("|")


## Store Binaries section.
#
# Write [Binaries] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Binaries section.
# @param  Module               An input Module class object.
#
def StoreModuleBinariesSection(InfFile, Module):
    Section = GetSection("Binaries", GetModuleBinaryItem, Module.Binaries)
    StoreTextFile(InfFile, Section)


## Return a Module Library Class Item.
#
# Read the input LibraryClass class object and return one line of Library Class Item.
#
# @param  LibraryClass         An input LibraryClass class object.
#
# @retval LibraryClassItem     A Module Library Class Item.
#
def GetModuleLibraryClassItem(LibraryClass):
    if "ALWAYS_PRODUCED" in LibraryClass.Usage:
        return ""

    LibraryClassList = []
    LibraryClassList.append(LibraryClass.LibraryClass)
    LibraryClassList.append(LibraryClass.RecommendedInstance)
    LibraryClassList.append(LibraryClass.FeatureFlag)
    
    return "|".join(LibraryClassList).rstrip("|")


## Store Library Classes section.
#
# Write [LibraryClasses] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Library Classes section.
# @param  Module               An input Module class object.
#
def StoreModuleLibraryClassesSection(InfFile, Module):
    Section = GetSection("LibraryClasses", GetModuleLibraryClassItem, Module.LibraryClasses)
    StoreTextFile(InfFile, Section)


## Return a Module Package Item.
#
# Read the input PackageDependency class object and return one line of Package Item.
#
# @param  PackageDependency    An input PackageDependency class object.
#
# @retval PackageItem          A Module Package Item.
#
def GetModulePackageItem(PackageDependency):
    return PackageDependency.FilePath


## Store Packages section.
#
# Write [Packages] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Packages section.
# @param  Module               An input Module class object.
#
def StoreModulePackagesSection(InfFile, Module):
    Section = GetSection("Packages", GetModulePackageItem, Module.PackageDependencies)
    StoreTextFile(InfFile, Section)
    

## Return a Module Guid C Name Item.
#
# Read the input Guid class object and return one line of Guid C Name Item.
#
# @param  Guid                 An input Guid class object.
#
# @retval GuidCNameItem        A Module Guid C Name Item.
#
def GetModuleGuidCNameItem(Guid):
    try:
        return Guid.GuidCName
    except:
        return Guid.CName


## Store Protocols section.
#
# Write [Protocols] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Protocols section.
# @param  Module               An input Module class object.
#
def StoreModuleProtocolsSection(InfFile, Module):
    Section = GetSection("Protocols", GetModuleGuidCNameItem, Module.Protocols)
    StoreTextFile(InfFile, Section)
    

## Store Ppis section.
#
# Write [Ppis] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Ppis section.
# @param  Module               An input Module class object.
#
def StoreModulePpisSection(InfFile, Module):
    Section = GetSection("Ppis", GetModuleGuidCNameItem, Module.Ppis)
    StoreTextFile(InfFile, Section)


## Store Guids section.
#
# Write [Guids] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Guids section.
# @param  Module               An input Module class object.
#
def StoreModuleGuidsSection(InfFile, Module):
    Guids = []
    Guids += Module.Guids
    Guids += Module.Events
    Guids += Module.Hobs
    Guids += Module.Variables
    Guids += Module.SystemTables
    Guids += Module.DataHubs
    Guids += Module.HiiPackages
    Section = GetSection("Guids", GetModuleGuidCNameItem, Guids)
    StoreTextFile(InfFile, Section)


## Return a Module Pcd Item.
#
# Read the input Pcd class object and return one line of Pcd Item.
#
# @param  Pcd                  An input Pcd class object.
#
# @retval PcdItem              A Module Pcd Item.
#
def GetModulePcdItem(Pcd):
    PcdItem = "%s.%s" % (Pcd.TokenSpaceGuidCName, Pcd.CName)
    if Pcd.DefaultValue != "":
        PcdItem = "%s|%s" % (PcdItem, Pcd.DefaultValue)

    return PcdItem


## DEC Pcd Section Name dictionary indexed by PCD Item Type.
mInfPcdSectionNameDict = {
    "FEATURE_FLAG" : "FeaturePcd",
    "FIXED_AT_BUILD" : "FixedPcd",
    "PATCHABLE_IN_MODULE" : "PatchPcd",
    "DYNAMIC" : "Pcd",
    "DYNAMIC_EX" : "PcdEx"
    }
    
## Store Pcds section.
#
# Write [(PcdType)] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Pcds section.
# @param  Module               An input Module class object.
#
def StoreModulePcdsSection(InfFile, Module):
    PcdsDict = {}
    for Pcd in Module.PcdCodes:
        PcdSectionName = mInfPcdSectionNameDict.get(Pcd.ItemType)
        if PcdSectionName:
            PcdsDict.setdefault(PcdSectionName, []).append(Pcd)
        else:
            EdkLogger.info("Unknown Pcd Item Type: %s" % Pcd.ItemType)

    Section = ""
    for PcdSectionName in PcdsDict:
        Pcds = PcdsDict[PcdSectionName]
        Section += GetSection(PcdSectionName, GetModulePcdItem, Pcds)
        Section += "\n"

    StoreTextFile(InfFile, Section)
    

## Return a Module Depex Item.
#
# Read the input Depex class object and return one line of Depex Item.
#
# @param  Depex                An input Depex class object.
#
# @retval DepexItem            A Module Depex Item.
#
def GetModuleDepexItem(Depex):
    return Depex.Depex


## Store Depex section.
#
# Write [Depex] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Depex section.
# @param  Module               An input Module class object.
#
def StoreModuleDepexSection(InfFile, Module):
    Section = GetSection("Depex", GetModuleDepexItem, Module.Depex)
    StoreTextFile(InfFile, Section)
    

## Return a Module Build Option Item.
#
# Read the input BuildOption class object and return one line of Build Option Item.
#
# @param  BuildOption          An input BuildOption class object.
#
# @retval BuildOptionItem      A Module Build Option Item.
#
def GetModuleBuildOptionItem(BuildOption):
    BuildTarget = BuildOption.BuildTarget
    if BuildTarget == "":
        BuildTarget = "*"

    TagName = BuildOption.TagName
    if TagName == "":
        TagName = "*"
        
    ToolCode = BuildOption.ToolCode
    if ToolCode == "":
        ToolCode = "*"

    Item = "_".join((BuildTarget, TagName, "*", ToolCode, "Flag"))

    ToolChainFamily = BuildOption.ToolChainFamily
    if ToolChainFamily != "":
        Item = "%s:%s" % (ToolChainFamily, Item)

    return "%-30s = %s" % (Item, BuildOption.Option)


## Store Build Options section.
#
# Write [BuildOptions] section to the InfFile based on Module class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  InfFile              The output INF file to store the Build Options section.
# @param  Module               An input Module class object.
#
def StoreModuleBuildOptionsSection(InfFile, Module):
    Section = GetSection("BuildOption", GetModuleBuildOptionItem, Module.BuildOptions)
    StoreTextFile(InfFile, Section)


## Store User Extensions section.
#
# Write [UserExtensions] section to the InfFile based on Module class object.
#
# @param  InfFile              The output INF file to store the User Extensions section.
# @param  Module               An input Module class object.
#
def StoreModuleUserExtensionsSection(InfFile, Module):
    Section = "".join(map(GetUserExtensions, Module.UserExtensions))
    StoreTextFile(InfFile, Section)


## Store a Module class object to a new INF file.
#
# Read an input Module class object and save the contents to a new INF file.
#
# @param  INFFileName          The output INF file.
# @param  Module               An input Package class object.
#
def StoreInf(InfFileName, Module):
    InfFile = open(InfFileName, "w+")
    EdkLogger.info("Save file to %s" % InfFileName)

    StoreHeader(InfFile, Module.Header)
    StoreModuleDefinesSection(InfFile, Module)
    StoreModuleSourcesSection(InfFile, Module)
    StoreModuleBinariesSection(InfFile, Module)
    StoreModulePackagesSection(InfFile, Module)
    StoreModuleLibraryClassesSection(InfFile, Module)
    StoreModuleProtocolsSection(InfFile, Module)
    StoreModulePpisSection(InfFile, Module)
    StoreModuleGuidsSection(InfFile, Module)
    StoreModulePcdsSection(InfFile, Module)
    StoreModuleDepexSection(InfFile, Module)
    StoreModuleBuildOptionsSection(InfFile, Module)
    StoreModuleUserExtensionsSection(InfFile, Module)

    InfFile.close()
    
if __name__ == '__main__':
    pass