3 # This file contained the logical of transfer package object to INF files.
5 # Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
7 # This program and the accompanying materials are licensed and made available
8 # under the terms and conditions of the BSD License which accompanies this
9 # distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 from Core
.FileHook
import __FileHookOpen__
23 from Library
.String
import GetSplitValueList
24 from Library
.Parsing
import GenSection
25 from Library
.Parsing
import GetWorkspacePackage
26 from Library
.Parsing
import ConvertArchForInstall
27 from Library
.Misc
import SaveFileOnChange
28 from Library
.Misc
import IsAllModuleList
29 from Library
.Misc
import Sdict
30 from Library
.Misc
import ConvertPath
31 from Library
.Misc
import ConvertSpec
32 from Library
.Misc
import GetRelativePath
33 from Library
.Misc
import GetLocalValue
34 from Library
.CommentGenerating
import GenHeaderCommentSection
35 from Library
.CommentGenerating
import GenGenericCommentF
36 from Library
.CommentGenerating
import _GetHelpStr
37 from Library
import GlobalData
38 from Logger
import StringTable
as ST
39 from Logger
import ToolError
40 import Logger
.Log
as Logger
41 from Library
import DataType
as DT
42 from GenMetaFile
import GenMetaFileMisc
43 from Library
.UniClassObject
import FormatUniEntry
44 from Library
.String
import GetUniFileName
47 ## Transfer Module Object to Inf files
49 # Transfer all contents of a standard Module Object to an Inf file
50 # @param ModuleObject: A Module Object
52 def ModuleToInf(ModuleObject
, PackageObject
=None, DistHeader
=None):
53 if not GlobalData
.gWSPKG_LIST
:
54 GlobalData
.gWSPKG_LIST
= GetWorkspacePackage()
56 # Init global information for the file
58 ContainerFile
= ModuleObject
.GetFullPath()
62 # Generate file header, If any Abstract, Description, Copyright or License XML elements are missing,
63 # should 1) use the Abstract, Description, Copyright or License from the PackageSurfaceArea.Header elements
64 # that the module belongs to, or 2) if this is a stand-alone module that is not included in a PackageSurfaceArea,
65 # use the abstract, description, copyright or license from the DistributionPackage.Header elements.
67 ModuleAbstract
= GetLocalValue(ModuleObject
.GetAbstract())
68 if not ModuleAbstract
and PackageObject
:
69 ModuleAbstract
= GetLocalValue(PackageObject
.GetAbstract())
70 if not ModuleAbstract
and DistHeader
:
71 ModuleAbstract
= GetLocalValue(DistHeader
.GetAbstract())
72 ModuleDescription
= GetLocalValue(ModuleObject
.GetDescription())
73 if not ModuleDescription
and PackageObject
:
74 ModuleDescription
= GetLocalValue(PackageObject
.GetDescription())
75 if not ModuleDescription
and DistHeader
:
76 ModuleDescription
= GetLocalValue(DistHeader
.GetDescription())
78 for (Lang
, Copyright
) in ModuleObject
.GetCopyright():
81 ModuleCopyright
= Copyright
82 if not ModuleCopyright
and PackageObject
:
83 for (Lang
, Copyright
) in PackageObject
.GetCopyright():
86 ModuleCopyright
= Copyright
87 if not ModuleCopyright
and DistHeader
:
88 for (Lang
, Copyright
) in DistHeader
.GetCopyright():
91 ModuleCopyright
= Copyright
93 for (Lang
, License
) in ModuleObject
.GetLicense():
96 ModuleLicense
= License
97 if not ModuleLicense
and PackageObject
:
98 for (Lang
, License
) in PackageObject
.GetLicense():
101 ModuleLicense
= License
102 if not ModuleLicense
and DistHeader
:
103 for (Lang
, License
) in DistHeader
.GetLicense():
106 ModuleLicense
= License
109 # Generate header comment section of INF file
111 Content
+= GenHeaderCommentSection(ModuleAbstract
,
114 ModuleLicense
).replace('\r\n', '\n')
117 # Generate Binary Header
119 for UserExtension
in ModuleObject
.GetUserExtensionList():
120 if UserExtension
.GetUserID() == DT
.TAB_BINARY_HEADER_USERID \
121 and UserExtension
.GetIdentifier() == DT
.TAB_BINARY_HEADER_IDENTIFIER
:
122 ModuleBinaryAbstract
= GetLocalValue(UserExtension
.GetBinaryAbstract())
123 ModuleBinaryDescription
= GetLocalValue(UserExtension
.GetBinaryDescription())
124 ModuleBinaryCopyright
= ''
125 ModuleBinaryLicense
= ''
126 for (Lang
, Copyright
) in UserExtension
.GetBinaryCopyright():
127 ModuleBinaryCopyright
= Copyright
128 for (Lang
, License
) in UserExtension
.GetBinaryLicense():
129 ModuleBinaryLicense
= License
130 if ModuleBinaryAbstract
and ModuleBinaryDescription
and \
131 ModuleBinaryCopyright
and ModuleBinaryLicense
:
132 Content
+= GenHeaderCommentSection(ModuleBinaryAbstract
,
133 ModuleBinaryDescription
,
134 ModuleBinaryCopyright
,
139 # Generate MODULE_UNI_FILE for module
141 FileHeader
= GenHeaderCommentSection(ModuleAbstract
, ModuleDescription
, ModuleCopyright
, ModuleLicense
, False, \
142 DT
.TAB_COMMENT_EDK1_SPLIT
)
143 ModuleUniFile
= GenModuleUNIEncodeFile(ModuleObject
, FileHeader
)
145 ModuleObject
.SetModuleUniFile(os
.path
.basename(ModuleUniFile
))
148 # Judge whether the INF file is an AsBuild INF.
150 if ModuleObject
.BinaryModule
:
151 GlobalData
.gIS_BINARY_INF
= True
153 GlobalData
.gIS_BINARY_INF
= False
155 # for each section, maintain a dict, sorted arch will be its key,
156 # statement list will be its data
157 # { 'Arch1 Arch2 Arch3': [statement1, statement2],
158 # 'Arch1' : [statement1, statement3]
161 # Gen section contents
163 Content
+= GenDefines(ModuleObject
)
164 Content
+= GenBuildOptions(ModuleObject
)
165 Content
+= GenLibraryClasses(ModuleObject
)
166 Content
+= GenPackages(ModuleObject
)
167 Content
+= GenPcdSections(ModuleObject
)
168 Content
+= GenSources(ModuleObject
)
169 Content
+= GenProtocolPPiSections(ModuleObject
.GetProtocolList(), True)
170 Content
+= GenProtocolPPiSections(ModuleObject
.GetPpiList(), False)
171 Content
+= GenGuidSections(ModuleObject
.GetGuidList())
172 Content
+= GenBinaries(ModuleObject
)
173 Content
+= GenDepex(ModuleObject
)
174 __UserExtensionsContent
= GenUserExtensions(ModuleObject
)
175 Content
+= __UserExtensionsContent
176 if ModuleObject
.GetEventList() or ModuleObject
.GetBootModeList() or ModuleObject
.GetHobList():
179 # generate [Event], [BootMode], [Hob] section
181 Content
+= GenSpecialSections(ModuleObject
.GetEventList(), 'Event', __UserExtensionsContent
)
182 Content
+= GenSpecialSections(ModuleObject
.GetBootModeList(), 'BootMode', __UserExtensionsContent
)
183 Content
+= GenSpecialSections(ModuleObject
.GetHobList(), 'Hob', __UserExtensionsContent
)
184 SaveFileOnChange(ContainerFile
, Content
, False)
185 if DistHeader
.ReadOnly
:
186 os
.chmod(ContainerFile
, stat
.S_IRUSR|stat
.S_IRGRP|stat
.S_IROTH
)
188 os
.chmod(ContainerFile
, stat
.S_IRUSR|stat
.S_IRGRP|stat
.S_IROTH|stat
.S_IWUSR|stat
.S_IWGRP|stat
.S_IWOTH
)
191 ## GenModuleUNIEncodeFile
192 # GenModuleUNIEncodeFile, default is a UCS-2LE encode file
194 def GenModuleUNIEncodeFile(ModuleObject
, UniFileHeader
='', Encoding
=DT
.TAB_ENCODING_UTF16LE
):
196 OnlyLANGUAGE_EN_X
= True
198 BinaryDescription
= []
200 # If more than one language code is used for any element that would be present in the MODULE_UNI_FILE,
201 # then the MODULE_UNI_FILE must be created.
203 for (Key
, Value
) in ModuleObject
.GetAbstract() + ModuleObject
.GetDescription():
204 if Key
== DT
.TAB_LANGUAGE_EN_X
:
207 OnlyLANGUAGE_EN_X
= False
209 for UserExtension
in ModuleObject
.GetUserExtensionList():
210 if UserExtension
.GetUserID() == DT
.TAB_BINARY_HEADER_USERID \
211 and UserExtension
.GetIdentifier() == DT
.TAB_BINARY_HEADER_IDENTIFIER
:
212 for (Key
, Value
) in UserExtension
.GetBinaryAbstract():
213 if Key
== DT
.TAB_LANGUAGE_EN_X
:
216 OnlyLANGUAGE_EN_X
= False
217 BinaryAbstract
.append((Key
, Value
))
218 for (Key
, Value
) in UserExtension
.GetBinaryDescription():
219 if Key
== DT
.TAB_LANGUAGE_EN_X
:
222 OnlyLANGUAGE_EN_X
= False
223 BinaryDescription
.append((Key
, Value
))
228 elif OnlyLANGUAGE_EN_X
:
231 ModuleObject
.UNIFlag
= True
232 ContainerFile
= GetUniFileName(os
.path
.dirname(ModuleObject
.GetFullPath()), ModuleObject
.GetBaseName())
234 if not os
.path
.exists(os
.path
.dirname(ModuleObject
.GetFullPath())):
235 os
.makedirs(os
.path
.dirname(ModuleObject
.GetFullPath()))
237 Content
= UniFileHeader
+ '\r\n'
240 Content
+= FormatUniEntry('#string ' + DT
.TAB_INF_ABSTRACT
, ModuleObject
.GetAbstract(), ContainerFile
) + '\r\n'
242 Content
+= FormatUniEntry('#string ' + DT
.TAB_INF_DESCRIPTION
, ModuleObject
.GetDescription(), ContainerFile
) \
245 BinaryAbstractString
= FormatUniEntry('#string ' + DT
.TAB_INF_BINARY_ABSTRACT
, BinaryAbstract
, ContainerFile
)
246 if BinaryAbstractString
:
247 Content
+= BinaryAbstractString
+ '\r\n'
249 BinaryDescriptionString
= FormatUniEntry('#string ' + DT
.TAB_INF_BINARY_DESCRIPTION
, BinaryDescription
, \
251 if BinaryDescriptionString
:
252 Content
+= BinaryDescriptionString
+ '\r\n'
254 if not os
.path
.exists(ContainerFile
):
255 File
= codecs
.open(ContainerFile
, 'wb', Encoding
)
256 File
.write(u
'\uFEFF' + Content
)
258 Md5Sigature
= md5
.new(__FileHookOpen__(str(ContainerFile
), 'rb').read())
259 Md5Sum
= Md5Sigature
.hexdigest()
260 if (ContainerFile
, Md5Sum
) not in ModuleObject
.FileList
:
261 ModuleObject
.FileList
.append((ContainerFile
, Md5Sum
))
264 def GenDefines(ModuleObject
):
266 # generate [Defines] section
272 for UserExtension
in ModuleObject
.GetUserExtensionList():
273 DefinesDict
= UserExtension
.GetDefinesDict()
276 for Statement
in DefinesDict
:
277 if Statement
.split(DT
.TAB_EQUAL_SPLIT
) > 1:
278 Statement
= (u
'%s ' % Statement
.split(DT
.TAB_EQUAL_SPLIT
, 1)[0]).ljust(LeftOffset
) \
279 + u
'= %s' % Statement
.split(DT
.TAB_EQUAL_SPLIT
, 1)[1].lstrip()
280 SortedArch
= DT
.TAB_ARCH_COMMON
281 if Statement
.strip().startswith(DT
.TAB_INF_DEFINES_CUSTOM_MAKEFILE
):
282 pos
= Statement
.find(DT
.TAB_VALUE_SPLIT
)
284 pos
= Statement
.find(DT
.TAB_EQUAL_SPLIT
)
285 Makefile
= ConvertPath(Statement
[pos
+ 1:].strip())
286 Statement
= Statement
[:pos
+ 1] + ' ' + Makefile
287 if SortedArch
in NewSectionDict
:
288 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
290 NewSectionDict
[SortedArch
] = [Statement
]
291 SpecialStatementList
= []
293 # TAB_INF_DEFINES_INF_VERSION
294 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_INF_VERSION
).ljust(LeftOffset
) + u
'= %s' % '0x00010017'
295 SpecialStatementList
.append(Statement
)
298 BaseName
= ModuleObject
.GetBaseName()
299 if BaseName
.startswith('.') or BaseName
.startswith('-'):
300 BaseName
= '_' + BaseName
301 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_BASE_NAME
).ljust(LeftOffset
) + u
'= %s' % BaseName
302 SpecialStatementList
.append(Statement
)
304 # TAB_INF_DEFINES_FILE_GUID
305 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_FILE_GUID
).ljust(LeftOffset
) + u
'= %s' % ModuleObject
.GetGuid()
306 SpecialStatementList
.append(Statement
)
308 # TAB_INF_DEFINES_VERSION_STRING
309 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_VERSION_STRING
).ljust(LeftOffset
) + u
'= %s' % ModuleObject
.GetVersion()
310 SpecialStatementList
.append(Statement
)
312 # TAB_INF_DEFINES_VERSION_STRING
313 if ModuleObject
.UNIFlag
:
314 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_MODULE_UNI_FILE
).ljust(LeftOffset
) + \
315 u
'= %s' % ModuleObject
.GetModuleUniFile()
316 SpecialStatementList
.append(Statement
)
318 # TAB_INF_DEFINES_MODULE_TYPE
319 if ModuleObject
.GetModuleType():
320 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_MODULE_TYPE
).ljust(LeftOffset
) + u
'= %s' % ModuleObject
.GetModuleType()
321 SpecialStatementList
.append(Statement
)
323 # TAB_INF_DEFINES_PCD_IS_DRIVER
324 if ModuleObject
.GetPcdIsDriver():
325 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_PCD_IS_DRIVER
).ljust(LeftOffset
) + \
326 u
'= %s' % ModuleObject
.GetPcdIsDriver()
327 SpecialStatementList
.append(Statement
)
329 # TAB_INF_DEFINES_UEFI_SPECIFICATION_VERSION
330 if ModuleObject
.GetUefiSpecificationVersion():
331 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_UEFI_SPECIFICATION_VERSION
).ljust(LeftOffset
) + \
332 u
'= %s' % ModuleObject
.GetUefiSpecificationVersion()
333 SpecialStatementList
.append(Statement
)
335 # TAB_INF_DEFINES_PI_SPECIFICATION_VERSION
336 if ModuleObject
.GetPiSpecificationVersion():
337 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_PI_SPECIFICATION_VERSION
).ljust(LeftOffset
) + \
338 u
'= %s' % ModuleObject
.GetPiSpecificationVersion()
339 SpecialStatementList
.append(Statement
)
342 for LibraryClass
in ModuleObject
.GetLibraryClassList():
343 if LibraryClass
.GetUsage() == DT
.USAGE_ITEM_PRODUCES
or \
344 LibraryClass
.GetUsage() == DT
.USAGE_ITEM_SOMETIMES_PRODUCES
:
345 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_LIBRARY_CLASS
).ljust(LeftOffset
) + \
346 u
'= %s' % LibraryClass
.GetLibraryClass()
347 if LibraryClass
.GetSupModuleList():
348 Statement
+= '|' + DT
.TAB_SPACE_SPLIT
.join(l
for l
in LibraryClass
.GetSupModuleList())
349 SpecialStatementList
.append(Statement
)
352 for SpecItem
in ModuleObject
.GetSpecList():
353 Spec
, Version
= SpecItem
354 Spec
= ConvertSpec(Spec
)
355 Statement
= '%s %s = %s' % (DT
.TAB_INF_DEFINES_SPEC
, Spec
, Version
)
356 SpecialStatementList
.append(Statement
)
360 for Extern
in ModuleObject
.GetExternList():
361 ArchList
= Extern
.GetSupArchList()
362 EntryPoint
= Extern
.GetEntryPoint()
363 UnloadImage
= Extern
.GetUnloadImage()
364 Constructor
= Extern
.GetConstructor()
365 Destructor
= Extern
.GetDestructor()
366 HelpStringList
= Extern
.GetHelpTextList()
367 FFE
= Extern
.GetFeatureFlag()
368 ExternList
.append([ArchList
, EntryPoint
, UnloadImage
, Constructor
, Destructor
, FFE
, HelpStringList
])
370 # Add VALID_ARCHITECTURES information
372 ValidArchStatement
= None
373 if ModuleObject
.SupArchList
:
374 ValidArchStatement
= '\n' + '# ' + '\n'
375 ValidArchStatement
+= '# The following information is for reference only and not required by the build tools.\n'
376 ValidArchStatement
+= '# ' + '\n'
377 ValidArchStatement
+= '# VALID_ARCHITECTURES = %s' % (' '.join(ModuleObject
.SupArchList
)) + '\n'
378 ValidArchStatement
+= '# '
379 if DT
.TAB_ARCH_COMMON
not in NewSectionDict
:
380 NewSectionDict
[DT
.TAB_ARCH_COMMON
] = []
381 NewSectionDict
[DT
.TAB_ARCH_COMMON
] = NewSectionDict
[DT
.TAB_ARCH_COMMON
] + SpecialStatementList
382 GenMetaFileMisc
.AddExternToDefineSec(NewSectionDict
, DT
.TAB_ARCH_COMMON
, ExternList
)
383 if ValidArchStatement
is not None:
384 NewSectionDict
[DT
.TAB_ARCH_COMMON
] = NewSectionDict
[DT
.TAB_ARCH_COMMON
] + [ValidArchStatement
]
385 Content
+= GenSection('Defines', NewSectionDict
)
388 def GenLibraryClasses(ModuleObject
):
390 # generate [LibraryClasses] section
394 if not GlobalData
.gIS_BINARY_INF
:
395 for LibraryClass
in ModuleObject
.GetLibraryClassList():
396 if LibraryClass
.GetUsage() == DT
.USAGE_ITEM_PRODUCES
:
399 # Generate generic comment
401 HelpTextList
= LibraryClass
.GetHelpTextList()
402 HelpStr
= _GetHelpStr(HelpTextList
)
403 CommentStr
= GenGenericCommentF(HelpStr
)
404 Statement
= CommentStr
405 Name
= LibraryClass
.GetLibraryClass()
406 FFE
= LibraryClass
.GetFeatureFlag()
409 Statement
+= '|' + FFE
410 ModuleList
= LibraryClass
.GetSupModuleList()
411 ArchList
= LibraryClass
.GetSupArchList()
412 for Index
in xrange(0, len(ArchList
)):
413 ArchList
[Index
] = ConvertArchForInstall(ArchList
[Index
])
415 SortedArch
= ' '.join(ArchList
)
417 if not ModuleList
or IsAllModuleList(ModuleList
):
418 KeyList
= [SortedArch
]
420 ModuleString
= DT
.TAB_VALUE_SPLIT
.join(l
for l
in ModuleList
)
422 SortedArch
= DT
.TAB_ARCH_COMMON
423 KeyList
= [SortedArch
+ '.' + ModuleString
]
425 KeyList
= [Arch
+ '.' + ModuleString
for Arch
in ArchList
]
427 if Key
in NewSectionDict
:
428 NewSectionDict
[Key
] = NewSectionDict
[Key
] + [Statement
]
430 NewSectionDict
[Key
] = [Statement
]
431 Content
+= GenSection('LibraryClasses', NewSectionDict
)
433 LibraryClassDict
= {}
434 for BinaryFile
in ModuleObject
.GetBinaryFileList():
435 if not BinaryFile
.AsBuiltList
:
437 for LibraryItem
in BinaryFile
.AsBuiltList
[0].LibraryInstancesList
:
438 Statement
= '# Guid: ' + LibraryItem
.Guid
+ ' Version: ' + LibraryItem
.Version
440 if len(BinaryFile
.SupArchList
) == 0:
441 if LibraryClassDict
.has_key('COMMON') and Statement
not in LibraryClassDict
['COMMON']:
442 LibraryClassDict
['COMMON'].append(Statement
)
444 LibraryClassDict
['COMMON'] = ['## @LIB_INSTANCES']
445 LibraryClassDict
['COMMON'].append(Statement
)
447 for Arch
in BinaryFile
.SupArchList
:
448 if LibraryClassDict
.has_key(Arch
):
449 if Statement
not in LibraryClassDict
[Arch
]:
450 LibraryClassDict
[Arch
].append(Statement
)
454 LibraryClassDict
[Arch
] = ['## @LIB_INSTANCES']
455 LibraryClassDict
[Arch
].append(Statement
)
456 Content
+= GenSection('LibraryClasses', LibraryClassDict
)
460 def GenPackages(ModuleObject
):
463 # generate [Packages] section
465 NewSectionDict
= Sdict()
466 WorkspaceDir
= GlobalData
.gWORKSPACE
467 for PackageDependency
in ModuleObject
.GetPackageDependencyList():
469 # Generate generic comment
472 HelpText
= PackageDependency
.GetHelpText()
474 HelpStr
= HelpText
.GetString()
475 CommentStr
= GenGenericCommentF(HelpStr
)
476 Statement
= CommentStr
477 Guid
= PackageDependency
.GetGuid()
478 Version
= PackageDependency
.GetVersion()
479 FFE
= PackageDependency
.GetFeatureFlag()
482 # find package path/name
484 for PkgInfo
in GlobalData
.gWSPKG_LIST
:
485 if Guid
== PkgInfo
[1]:
486 if (not Version
) or (Version
== PkgInfo
[2]):
492 RelaPath
= GetRelativePath(Path
, WorkspaceDir
)
493 Statement
+= RelaPath
.replace('\\', '/')
495 Statement
+= '|' + FFE
496 ArchList
= PackageDependency
.GetSupArchList()
498 SortedArch
= ' '.join(ArchList
)
499 if SortedArch
in NewSectionDict
:
500 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
502 NewSectionDict
[SortedArch
] = [Statement
]
503 Content
+= GenSection('Packages', NewSectionDict
)
506 def GenSources(ModuleObject
):
508 # generate [Sources] section
512 for Source
in ModuleObject
.GetSourceFileList():
513 SourceFile
= Source
.GetSourceFile()
514 Family
= Source
.GetFamily()
515 FeatureFlag
= Source
.GetFeatureFlag()
516 SupArchList
= Source
.GetSupArchList()
518 SortedArch
= ' '.join(SupArchList
)
519 Statement
= GenSourceStatement(ConvertPath(SourceFile
), Family
, FeatureFlag
)
520 if SortedArch
in NewSectionDict
:
521 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
523 NewSectionDict
[SortedArch
] = [Statement
]
524 Content
+= GenSection('Sources', NewSectionDict
)
528 def GenDepex(ModuleObject
):
530 # generate [Depex] section
532 NewSectionDict
= Sdict()
534 for Depex
in ModuleObject
.GetPeiDepex() + ModuleObject
.GetDxeDepex() + ModuleObject
.GetSmmDepex():
535 HelpTextList
= Depex
.GetHelpTextList()
536 HelpStr
= _GetHelpStr(HelpTextList
)
537 CommentStr
= GenGenericCommentF(HelpStr
)
538 SupArchList
= Depex
.GetSupArchList()
539 SupModList
= Depex
.GetModuleType()
540 Expression
= Depex
.GetDepex()
541 Statement
= CommentStr
+ Expression
545 SupArchList
.append(DT
.TAB_ARCH_COMMON
.lower())
547 KeyList
= SupArchList
549 for ModuleType
in SupModList
:
550 for Arch
in SupArchList
:
551 KeyList
.append(ConvertArchForInstall(Arch
) + '.' + ModuleType
)
553 if Key
in NewSectionDict
:
554 NewSectionDict
[Key
] = NewSectionDict
[Key
] + [Statement
]
556 NewSectionDict
[Key
] = [Statement
]
557 Content
+= GenSection('Depex', NewSectionDict
, False)
564 def GenUserExtensions(ModuleObject
):
566 for UserExtension
in ModuleObject
.GetUserExtensionList():
567 if UserExtension
.GetUserID() == DT
.TAB_BINARY_HEADER_USERID
and \
568 UserExtension
.GetIdentifier() == DT
.TAB_BINARY_HEADER_IDENTIFIER
:
570 if UserExtension
.GetIdentifier() == 'Depex':
572 Statement
= UserExtension
.GetStatement()
573 # Comment the code to support user extension without any statement just the section header in []
576 ArchList
= UserExtension
.GetSupArchList()
577 for Index
in xrange(0, len(ArchList
)):
578 ArchList
[Index
] = ConvertArchForInstall(ArchList
[Index
])
582 if UserExtension
.GetUserID():
583 CommonPreFix
= UserExtension
.GetUserID()
584 if CommonPreFix
.find('.') > -1:
585 CommonPreFix
= '"' + CommonPreFix
+ '"'
586 if UserExtension
.GetIdentifier():
587 CommonPreFix
+= '.' + '"' + UserExtension
.GetIdentifier() + '"'
589 KeyList
= [CommonPreFix
+ '.' + Arch
for Arch
in ArchList
]
591 KeyList
= [CommonPreFix
]
593 if Key
in NewSectionDict
:
594 NewSectionDict
[Key
] = NewSectionDict
[Key
] + [Statement
]
596 NewSectionDict
[Key
] = [Statement
]
597 Content
= GenSection('UserExtensions', NewSectionDict
, False)
603 # @param SourceFile: string of source file path/name
604 # @param Family: string of source file family field
605 # @param FeatureFlag: string of source file FeatureFlag field
606 # @param TagName: string of source file TagName field
607 # @param ToolCode: string of source file ToolCode field
608 # @param HelpStr: string of source file HelpStr field
610 # @retval Statement: The generated statement for source
612 def GenSourceStatement(SourceFile
, Family
, FeatureFlag
, TagName
=None,
613 ToolCode
=None, HelpStr
=None):
616 Statement
+= GenGenericCommentF(HelpStr
)
618 # format of SourceFile|Family|TagName|ToolCode|FeatureFlag
620 Statement
+= SourceFile
628 Statement
+= '|' + Family
+ '|' + TagName
+ '|' + ToolCode
+ '|' + FeatureFlag
630 Statement
+= '|' + Family
+ '|' + TagName
+ '|' + ToolCode
632 Statement
+= '|' + Family
+ '|' + TagName
634 Statement
+= '|' + Family
639 # @param Key: (FileName, FileType, FFE, SortedArch)
640 # @param Value: (Target, Family, TagName, Comment)
643 def GenBinaryStatement(Key
, Value
, SubTypeGuidValue
=None):
644 (FileName
, FileType
, FFE
, SortedArch
) = Key
648 (Target
, Family
, TagName
, Comment
) = Value
655 Statement
= GenGenericCommentF(Comment
)
658 if FileType
== 'SUBTYPE_GUID' and SubTypeGuidValue
:
659 Statement
+= FileType
+ '|' + SubTypeGuidValue
+ '|' + FileName
661 Statement
+= FileType
+ '|' + FileName
662 if FileType
in DT
.BINARY_FILE_TYPE_UI_LIST
+ DT
.BINARY_FILE_TYPE_VER_LIST
:
664 Statement
+= '|' + Target
+ '|' + FFE
666 Statement
+= '|' + Target
669 Statement
+= '|' + Target
+ '|' + Family
+ '|' + TagName
+ '|' + FFE
671 Statement
+= '|' + Target
+ '|' + Family
+ '|' + TagName
673 Statement
+= '|' + Target
+ '|' + Family
675 Statement
+= '|' + Target
679 # @param GuidObjList: List of GuidObject
680 # @retVal Content: The generated section contents
682 def GenGuidSections(GuidObjList
):
684 # generate [Guids] section
688 for Guid
in GuidObjList
:
689 HelpTextList
= Guid
.GetHelpTextList()
690 HelpStr
= _GetHelpStr(HelpTextList
)
691 CName
= Guid
.GetCName()
692 FFE
= Guid
.GetFeatureFlag()
695 Statement
+= '|' + FFE
696 Usage
= Guid
.GetUsage()
697 GuidType
= Guid
.GetGuidTypeList()[0]
698 VariableName
= Guid
.GetVariableName()
700 # Differentiate the generic comment and usage comment as multiple generic comment need to be put at first
702 if Usage
== DT
.ITEM_UNDEFINED
and GuidType
== DT
.ITEM_UNDEFINED
:
703 # generate list of generic comment
704 Comment
= GenGenericCommentF(HelpStr
)
706 # generate list of other comment
707 Comment
= HelpStr
.replace('\n', ' ')
708 Comment
= Comment
.strip()
710 Comment
= ' # ' + Comment
713 if Usage
!= DT
.ITEM_UNDEFINED
and GuidType
== DT
.ITEM_UNDEFINED
:
714 Comment
= '## ' + Usage
+ Comment
715 elif GuidType
== 'Variable':
716 Comment
= '## ' + Usage
+ ' ## ' + GuidType
+ ':' + VariableName
+ Comment
718 Comment
= '## ' + Usage
+ ' ## ' + GuidType
+ Comment
723 # merge duplicate items
725 ArchList
= Guid
.GetSupArchList()
727 SortedArch
= ' '.join(ArchList
)
728 if (Statement
, SortedArch
) in GuidDict
:
729 PreviousComment
= GuidDict
[Statement
, SortedArch
]
730 Comment
= PreviousComment
+ Comment
731 GuidDict
[Statement
, SortedArch
] = Comment
732 NewSectionDict
= GenMetaFileMisc
.TransferDict(GuidDict
, 'INF_GUID')
734 # generate the section contents
737 Content
= GenSection('Guids', NewSectionDict
)
741 ## GenProtocolPPiSections
743 # @param ObjList: List of ProtocolObject or Ppi Object
744 # @retVal Content: The generated section contents
746 def GenProtocolPPiSections(ObjList
, IsProtocol
):
749 for Object
in ObjList
:
750 HelpTextList
= Object
.GetHelpTextList()
751 HelpStr
= _GetHelpStr(HelpTextList
)
752 CName
= Object
.GetCName()
753 FFE
= Object
.GetFeatureFlag()
756 Statement
+= '|' + FFE
757 Usage
= Object
.GetUsage()
758 Notify
= Object
.GetNotify()
760 # Differentiate the generic comment and usage comment as consecutive generic comment need to be put together
762 if Usage
== DT
.ITEM_UNDEFINED
and Notify
== '':
763 # generate list of generic comment
764 Comment
= GenGenericCommentF(HelpStr
)
766 # generate list of other comment
767 Comment
= HelpStr
.replace('\n', ' ')
768 Comment
= Comment
.strip()
770 Comment
= ' # ' + Comment
773 if Usage
== DT
.ITEM_UNDEFINED
and not Comment
and Notify
== '':
777 Comment
= '## ' + Usage
+ ' ## ' + 'NOTIFY' + Comment
779 Comment
= '## ' + Usage
+ Comment
783 # merge duplicate items
785 ArchList
= Object
.GetSupArchList()
787 SortedArch
= ' '.join(ArchList
)
788 if (Statement
, SortedArch
) in Dict
:
789 PreviousComment
= Dict
[Statement
, SortedArch
]
790 Comment
= PreviousComment
+ Comment
791 Dict
[Statement
, SortedArch
] = Comment
792 NewSectionDict
= GenMetaFileMisc
.TransferDict(Dict
, 'INF_PPI_PROTOCOL')
794 # generate the section contents
798 Content
= GenSection('Protocols', NewSectionDict
)
800 Content
= GenSection('Ppis', NewSectionDict
)
807 def GenPcdSections(ModuleObject
):
809 if not GlobalData
.gIS_BINARY_INF
:
811 # for each Pcd Itemtype, maintain a dict so the same type will be grouped
815 for Pcd
in ModuleObject
.GetPcdList():
816 HelpTextList
= Pcd
.GetHelpTextList()
817 HelpStr
= _GetHelpStr(HelpTextList
)
819 CName
= Pcd
.GetCName()
820 TokenSpaceGuidCName
= Pcd
.GetTokenSpaceGuidCName()
821 DefaultValue
= Pcd
.GetDefaultValue()
822 ItemType
= Pcd
.GetItemType()
823 if ItemType
in ItemTypeDict
:
824 Dict
= ItemTypeDict
[ItemType
]
827 ItemTypeDict
[ItemType
] = Dict
828 FFE
= Pcd
.GetFeatureFlag()
829 Statement
+= TokenSpaceGuidCName
+ '.' + CName
831 Statement
+= '|' + DefaultValue
833 Statement
+= '|' + FFE
835 Statement
+= '||' + FFE
839 Usage
= Pcd
.GetValidUsage()
840 # if FeatureFlag Pcd, then assume all Usage is CONSUMES
841 if ItemType
== DT
.TAB_INF_FEATURE_PCD
:
842 Usage
= DT
.USAGE_ITEM_CONSUMES
843 if Usage
== DT
.ITEM_UNDEFINED
:
844 # generate list of generic comment
845 Comment
= GenGenericCommentF(HelpStr
)
847 # generate list of other comment
848 Comment
= HelpStr
.replace('\n', ' ')
849 Comment
= Comment
.strip()
851 Comment
= ' # ' + Comment
854 Comment
= '## ' + Usage
+ Comment
858 # Merge duplicate entries
860 ArchList
= Pcd
.GetSupArchList()
862 SortedArch
= ' '.join(ArchList
)
863 if (Statement
, SortedArch
) in Dict
:
864 PreviousComment
= Dict
[Statement
, SortedArch
]
865 Comment
= PreviousComment
+ Comment
866 Dict
[Statement
, SortedArch
] = Comment
867 for ItemType
in ItemTypeDict
:
868 # First we need to transfer the Dict to use SortedArch as key
869 Dict
= ItemTypeDict
[ItemType
]
870 NewSectionDict
= GenMetaFileMisc
.TransferDict(Dict
, 'INF_PCD')
872 Content
+= GenSection(ItemType
, NewSectionDict
)
874 # For AsBuild INF files
877 Content
+= GenAsBuiltPacthPcdSections(ModuleObject
)
878 Content
+= GenAsBuiltPcdExSections(ModuleObject
)
885 def GenAsBuiltPacthPcdSections(ModuleObject
):
887 for BinaryFile
in ModuleObject
.GetBinaryFileList():
888 if not BinaryFile
.AsBuiltList
:
890 for PatchPcd
in BinaryFile
.AsBuiltList
[0].PatchPcdList
:
892 PcdCName
= PatchPcd
.CName
893 PcdValue
= PatchPcd
.DefaultValue
894 PcdOffset
= PatchPcd
.Offset
895 TokenSpaceGuidValue
= PatchPcd
.TokenSpaceGuidValue
896 Token
= PatchPcd
.Token
897 HelpTextList
= PatchPcd
.HelpTextList
899 for HelpStringItem
in HelpTextList
:
900 for HelpLine
in GetSplitValueList(HelpStringItem
.String
, '\n'):
901 HelpString
+= '## ' + HelpLine
+ '\n'
902 TokenSpaceName
, PcdCName
= GenMetaFileMisc
.ObtainPcdName(ModuleObject
.PackageDependencyList
,
905 if TokenSpaceName
== '' or PcdCName
== '':
907 ToolError
.RESOURCE_NOT_AVAILABLE
,
908 ST
.ERR_INSTALL_FILE_DEC_FILE_ERROR
% (TokenSpaceGuidValue
, Token
),
909 File
=ModuleObject
.GetFullPath())
910 Statement
= HelpString
+ TokenSpaceName
+ '.' + PcdCName
+ ' | ' + PcdValue
+ ' | ' + \
911 PcdOffset
+ DT
.TAB_SPACE_SPLIT
913 # Use binary file's Arch to be Pcd's Arch
916 FileNameObjList
= BinaryFile
.GetFileNameList()
918 ArchList
= FileNameObjList
[0].GetSupArchList()
919 if len(ArchList
) == 0:
920 if PatchPcdDict
.has_key(DT
.TAB_ARCH_COMMON
):
921 if Statement
not in PatchPcdDict
[DT
.TAB_ARCH_COMMON
]:
922 PatchPcdDict
[DT
.TAB_ARCH_COMMON
].append(Statement
)
924 PatchPcdDict
[DT
.TAB_ARCH_COMMON
] = [Statement
]
926 for Arch
in ArchList
:
927 if PatchPcdDict
.has_key(Arch
):
928 if Statement
not in PatchPcdDict
[Arch
]:
929 PatchPcdDict
[Arch
].append(Statement
)
931 PatchPcdDict
[Arch
] = [Statement
]
932 return GenSection(DT
.TAB_INF_PATCH_PCD
, PatchPcdDict
)
936 def GenAsBuiltPcdExSections(ModuleObject
):
938 for BinaryFile
in ModuleObject
.GetBinaryFileList():
939 if not BinaryFile
.AsBuiltList
:
941 for PcdExItem
in BinaryFile
.AsBuiltList
[0].PcdExValueList
:
943 PcdCName
= PcdExItem
.CName
944 TokenSpaceGuidValue
= PcdExItem
.TokenSpaceGuidValue
945 Token
= PcdExItem
.Token
946 HelpTextList
= PcdExItem
.HelpTextList
948 for HelpStringItem
in HelpTextList
:
949 for HelpLine
in GetSplitValueList(HelpStringItem
.String
, '\n'):
950 HelpString
+= '## ' + HelpLine
+ '\n'
951 TokenSpaceName
, PcdCName
= GenMetaFileMisc
.ObtainPcdName(ModuleObject
.PackageDependencyList
,
952 TokenSpaceGuidValue
, Token
)
953 if TokenSpaceName
== '' or PcdCName
== '':
955 ToolError
.RESOURCE_NOT_AVAILABLE
,
956 ST
.ERR_INSTALL_FILE_DEC_FILE_ERROR
% (TokenSpaceGuidValue
, Token
),
957 File
=ModuleObject
.GetFullPath())
959 Statement
= HelpString
+ TokenSpaceName
+ DT
.TAB_SPLIT
+ PcdCName
+ DT
.TAB_SPACE_SPLIT
962 # Use binary file's Arch to be Pcd's Arch
965 FileNameObjList
= BinaryFile
.GetFileNameList()
967 ArchList
= FileNameObjList
[0].GetSupArchList()
969 if len(ArchList
) == 0:
970 if PcdExDict
.has_key('COMMON'):
971 PcdExDict
['COMMON'].append(Statement
)
973 PcdExDict
['COMMON'] = [Statement
]
975 for Arch
in ArchList
:
976 if PcdExDict
.has_key(Arch
):
977 if Statement
not in PcdExDict
[Arch
]:
978 PcdExDict
[Arch
].append(Statement
)
980 PcdExDict
[Arch
] = [Statement
]
981 return GenSection('PcdEx', PcdExDict
)
983 ## GenSpecialSections
984 # generate special sections for Event/BootMode/Hob
986 def GenSpecialSections(ObjectList
, SectionName
, UserExtensionsContent
=''):
992 for Obj
in ObjectList
:
997 HelpTextList
= Obj
.GetHelpTextList()
998 HelpStr
= _GetHelpStr(HelpTextList
)
999 CommentStr
= GenGenericCommentF(HelpStr
)
1000 if SectionName
== 'Hob':
1001 Type
= Obj
.GetHobType()
1002 elif SectionName
== 'Event':
1003 Type
= Obj
.GetEventType()
1004 elif SectionName
== 'BootMode':
1005 Type
= Obj
.GetSupportedBootModes()
1008 Usage
= Obj
.GetUsage()
1010 # If the content already in UserExtensionsContent then ignore
1011 if '[%s]' % SectionName
in UserExtensionsContent
and Type
in UserExtensionsContent
:
1014 Statement
= ' ' + Type
+ ' ## ' + Usage
1015 if CommentStr
in ['#\n', '#\n#\n']:
1016 CommentStr
= '#\n#\n#\n'
1018 # the first head comment line should start with '##\n', if it starts with '#\n', then add one '#'
1019 # else add '##\n' to meet the format defined in INF spec
1021 if CommentStr
.startswith('#\n'):
1022 CommentStr
= '#' + CommentStr
1024 CommentStr
= '##\n' + CommentStr
1025 if CommentStr
and not CommentStr
.endswith('\n#\n'):
1026 CommentStr
= CommentStr
+ '#\n'
1027 NewStateMent
= CommentStr
+ Statement
1028 SupArch
= Obj
.GetSupArchList()
1030 SortedArch
= ' '.join(SupArch
)
1031 if SortedArch
in NewSectionDict
:
1032 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [NewStateMent
]
1034 NewSectionDict
[SortedArch
] = [NewStateMent
]
1035 SectionContent
= GenSection(SectionName
, NewSectionDict
)
1036 SectionContent
= SectionContent
.strip()
1038 Content
= '# ' + ('\n' + '# ').join(GetSplitValueList(SectionContent
, '\n'))
1039 Content
= Content
.lstrip()
1041 # add a return to differentiate it between other possible sections
1049 def GenBuildOptions(ModuleObject
):
1051 if not ModuleObject
.BinaryModule
:
1053 # generate [BuildOptions] section
1056 for UserExtension
in ModuleObject
.GetUserExtensionList():
1057 BuildOptionDict
= UserExtension
.GetBuildOptionDict()
1058 if not BuildOptionDict
:
1060 for Arch
in BuildOptionDict
:
1061 if Arch
in NewSectionDict
:
1062 NewSectionDict
[Arch
] = NewSectionDict
[Arch
] + [BuildOptionDict
[Arch
]]
1064 NewSectionDict
[Arch
] = [BuildOptionDict
[Arch
]]
1065 Content
= GenSection('BuildOptions', NewSectionDict
)
1067 BuildOptionDict
= {}
1068 for BinaryFile
in ModuleObject
.GetBinaryFileList():
1069 if not BinaryFile
.AsBuiltList
:
1071 for BuilOptionItem
in BinaryFile
.AsBuiltList
[0].BinaryBuildFlagList
:
1072 Statement
= '#' + BuilOptionItem
.AsBuiltOptionFlags
1073 if len(BinaryFile
.SupArchList
) == 0:
1074 if BuildOptionDict
.has_key('COMMON'):
1075 if Statement
not in BuildOptionDict
['COMMON']:
1076 BuildOptionDict
['COMMON'].append(Statement
)
1078 BuildOptionDict
['COMMON'] = ['## @AsBuilt']
1079 BuildOptionDict
['COMMON'].append(Statement
)
1081 for Arch
in BinaryFile
.SupArchList
:
1082 if BuildOptionDict
.has_key(Arch
):
1083 if Statement
not in BuildOptionDict
[Arch
]:
1084 BuildOptionDict
[Arch
].append(Statement
)
1086 BuildOptionDict
[Arch
] = ['## @AsBuilt']
1087 BuildOptionDict
[Arch
].append(Statement
)
1088 Content
= GenSection('BuildOptions', BuildOptionDict
)
1094 def GenBinaries(ModuleObject
):
1097 for UserExtension
in ModuleObject
.GetUserExtensionList():
1098 BinariesDict
= UserExtension
.GetBinariesDict()
1101 for BinaryFile
in ModuleObject
.GetBinaryFileList():
1102 FileNameObjList
= BinaryFile
.GetFileNameList()
1103 for FileNameObj
in FileNameObjList
:
1104 FileName
= ConvertPath(FileNameObj
.GetFilename())
1105 FileType
= FileNameObj
.GetFileType()
1106 FFE
= FileNameObj
.GetFeatureFlag()
1107 ArchList
= FileNameObj
.GetSupArchList()
1109 SortedArch
= ' '.join(ArchList
)
1110 Key
= (FileName
, FileType
, FFE
, SortedArch
)
1111 if Key
in BinariesDict
:
1112 ValueList
= BinariesDict
[Key
]
1113 for ValueItem
in ValueList
:
1114 Statement
= GenBinaryStatement(Key
, ValueItem
)
1115 if SortedArch
in NewSectionDict
:
1116 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
1118 NewSectionDict
[SortedArch
] = [Statement
]
1120 # as we already generated statement for this DictKey here set the Valuelist to be empty
1121 # to avoid generate duplicate entries as the DictKey may have multiple entries
1123 BinariesDict
[Key
] = []
1125 if FileType
== 'SUBTYPE_GUID' and FileNameObj
.GetGuidValue():
1126 Statement
= GenBinaryStatement(Key
, None, FileNameObj
.GetGuidValue())
1128 Statement
= GenBinaryStatement(Key
, None)
1129 if SortedArch
in NewSectionDict
:
1130 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
1132 NewSectionDict
[SortedArch
] = [Statement
]
1133 Content
= GenSection('Binaries', NewSectionDict
)