3 # This file contained the logical of transfer package object to INF files.
5 # Copyright (c) 2011 - 2014, 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
46 ## Transfer Module Object to Inf files
48 # Transfer all contents of a standard Module Object to an Inf file
49 # @param ModuleObject: A Module Object
51 def ModuleToInf(ModuleObject
, PackageObject
=None, DistHeader
=None):
52 if not GlobalData
.gWSPKG_LIST
:
53 GlobalData
.gWSPKG_LIST
= GetWorkspacePackage()
55 # Init global information for the file
57 ContainerFile
= ModuleObject
.GetFullPath()
61 # Generate file header, If any Abstract, Description, Copyright or License XML elements are missing,
62 # should 1) use the Abstract, Description, Copyright or License from the PackageSurfaceArea.Header elements
63 # that the module belongs to, or 2) if this is a stand-alone module that is not included in a PackageSurfaceArea,
64 # use the abstract, description, copyright or license from the DistributionPackage.Header elements.
66 ModuleAbstract
= GetLocalValue(ModuleObject
.GetAbstract())
67 if not ModuleAbstract
and PackageObject
:
68 ModuleAbstract
= GetLocalValue(PackageObject
.GetAbstract())
69 if not ModuleAbstract
and DistHeader
:
70 ModuleAbstract
= GetLocalValue(DistHeader
.GetAbstract())
71 ModuleDescription
= GetLocalValue(ModuleObject
.GetDescription())
72 if not ModuleDescription
and PackageObject
:
73 ModuleDescription
= GetLocalValue(PackageObject
.GetDescription())
74 if not ModuleDescription
and DistHeader
:
75 ModuleDescription
= GetLocalValue(DistHeader
.GetDescription())
77 for (Lang
, Copyright
) in ModuleObject
.GetCopyright():
80 ModuleCopyright
= Copyright
81 if not ModuleCopyright
and PackageObject
:
82 for (Lang
, Copyright
) in PackageObject
.GetCopyright():
85 ModuleCopyright
= Copyright
86 if not ModuleCopyright
and DistHeader
:
87 for (Lang
, Copyright
) in DistHeader
.GetCopyright():
90 ModuleCopyright
= Copyright
92 for (Lang
, License
) in ModuleObject
.GetLicense():
95 ModuleLicense
= License
96 if not ModuleLicense
and PackageObject
:
97 for (Lang
, License
) in PackageObject
.GetLicense():
100 ModuleLicense
= License
101 if not ModuleLicense
and DistHeader
:
102 for (Lang
, License
) in DistHeader
.GetLicense():
105 ModuleLicense
= License
108 # Generate header comment section of INF file
110 Content
+= GenHeaderCommentSection(ModuleAbstract
,
113 ModuleLicense
).replace('\r\n', '\n')
116 # Generate Binary Header
118 for UserExtension
in ModuleObject
.GetUserExtensionList():
119 if UserExtension
.GetUserID() == DT
.TAB_BINARY_HEADER_USERID \
120 and UserExtension
.GetIdentifier() == DT
.TAB_BINARY_HEADER_IDENTIFIER
:
121 ModuleBinaryAbstract
= GetLocalValue(UserExtension
.GetBinaryAbstract())
122 ModuleBinaryDescription
= GetLocalValue(UserExtension
.GetBinaryDescription())
123 ModuleBinaryCopyright
= ''
124 ModuleBinaryLicense
= ''
125 for (Lang
, Copyright
) in UserExtension
.GetBinaryCopyright():
126 ModuleBinaryCopyright
= Copyright
127 for (Lang
, License
) in UserExtension
.GetBinaryLicense():
128 ModuleBinaryLicense
= License
129 if ModuleBinaryAbstract
and ModuleBinaryDescription
and \
130 ModuleBinaryCopyright
and ModuleBinaryLicense
:
131 Content
+= GenHeaderCommentSection(ModuleBinaryAbstract
,
132 ModuleBinaryDescription
,
133 ModuleBinaryCopyright
,
138 # Generate MODULE_UNI_FILE for module
140 FileHeader
= GenHeaderCommentSection(ModuleAbstract
, ModuleDescription
, ModuleCopyright
, ModuleLicense
, False, \
141 DT
.TAB_COMMENT_EDK1_SPLIT
)
142 GenModuleUNIEncodeFile(ModuleObject
, FileHeader
)
145 # Judge whether the INF file is an AsBuild INF.
147 if ModuleObject
.BinaryModule
:
148 GlobalData
.gIS_BINARY_INF
= True
150 GlobalData
.gIS_BINARY_INF
= False
152 # for each section, maintain a dict, sorted arch will be its key,
153 # statement list will be its data
154 # { 'Arch1 Arch2 Arch3': [statement1, statement2],
155 # 'Arch1' : [statement1, statement3]
158 # Gen section contents
160 Content
+= GenDefines(ModuleObject
)
161 Content
+= GenBuildOptions(ModuleObject
)
162 Content
+= GenLibraryClasses(ModuleObject
)
163 Content
+= GenPackages(ModuleObject
)
164 Content
+= GenPcdSections(ModuleObject
)
165 Content
+= GenSources(ModuleObject
)
166 Content
+= GenProtocolPPiSections(ModuleObject
.GetProtocolList(), True)
167 Content
+= GenProtocolPPiSections(ModuleObject
.GetPpiList(), False)
168 Content
+= GenGuidSections(ModuleObject
.GetGuidList())
169 Content
+= GenBinaries(ModuleObject
)
170 Content
+= GenDepex(ModuleObject
)
171 Content
+= GenUserExtensions(ModuleObject
)
172 if ModuleObject
.GetEventList() or ModuleObject
.GetBootModeList() or ModuleObject
.GetHobList():
175 # generate [Event], [BootMode], [Hob] section
177 Content
+= GenSpecialSections(ModuleObject
.GetEventList(), 'Event')
178 Content
+= GenSpecialSections(ModuleObject
.GetBootModeList(), 'BootMode')
179 Content
+= GenSpecialSections(ModuleObject
.GetHobList(), 'Hob')
180 SaveFileOnChange(ContainerFile
, Content
, False)
181 if DistHeader
.ReadOnly
:
182 os
.chmod(ContainerFile
, stat
.S_IRUSR|stat
.S_IRGRP|stat
.S_IROTH
)
184 os
.chmod(ContainerFile
, stat
.S_IRUSR|stat
.S_IRGRP|stat
.S_IROTH|stat
.S_IWUSR|stat
.S_IWGRP|stat
.S_IWOTH
)
187 ## GenModuleUNIEncodeFile
188 # GenModuleUNIEncodeFile, default is a UCS-2LE encode file
190 def GenModuleUNIEncodeFile(ModuleObject
, UniFileHeader
='', Encoding
=DT
.TAB_ENCODING_UTF16LE
):
192 OnlyLANGUAGE_EN_X
= True
194 BinaryDescription
= []
196 # If more than one language code is used for any element that would be present in the MODULE_UNI_FILE,
197 # then the MODULE_UNI_FILE must be created.
199 for (Key
, Value
) in ModuleObject
.GetAbstract() + ModuleObject
.GetDescription():
200 if Key
== DT
.TAB_LANGUAGE_EN_X
:
203 OnlyLANGUAGE_EN_X
= False
205 for UserExtension
in ModuleObject
.GetUserExtensionList():
206 if UserExtension
.GetUserID() == DT
.TAB_BINARY_HEADER_USERID \
207 and UserExtension
.GetIdentifier() == DT
.TAB_BINARY_HEADER_IDENTIFIER
:
208 for (Key
, Value
) in UserExtension
.GetBinaryAbstract():
209 if Key
== DT
.TAB_LANGUAGE_EN_X
:
212 OnlyLANGUAGE_EN_X
= False
213 BinaryAbstract
.append((Key
, Value
))
214 for (Key
, Value
) in UserExtension
.GetBinaryDescription():
215 if Key
== DT
.TAB_LANGUAGE_EN_X
:
218 OnlyLANGUAGE_EN_X
= False
219 BinaryDescription
.append((Key
, Value
))
224 elif OnlyLANGUAGE_EN_X
:
227 ModuleObject
.UNIFlag
= True
228 ContainerFile
= os
.path
.normpath(os
.path
.join(os
.path
.dirname(ModuleObject
.GetFullPath()),
229 (ModuleObject
.GetBaseName() + '.uni')))
230 if not os
.path
.exists(os
.path
.dirname(ModuleObject
.GetFullPath())):
231 os
.makedirs(os
.path
.dirname(ModuleObject
.GetFullPath()))
233 Content
= UniFileHeader
+ '\r\n'
236 Content
+= FormatUniEntry('#string ' + DT
.TAB_INF_ABSTRACT
, ModuleObject
.GetAbstract(), ContainerFile
) + '\r\n'
238 Content
+= FormatUniEntry('#string ' + DT
.TAB_INF_DESCRIPTION
, ModuleObject
.GetDescription(), ContainerFile
) \
241 BinaryAbstractString
= FormatUniEntry('#string ' + DT
.TAB_INF_BINARY_ABSTRACT
, BinaryAbstract
, ContainerFile
)
242 if BinaryAbstractString
:
243 Content
+= BinaryAbstractString
+ '\r\n'
245 BinaryDescriptionString
= FormatUniEntry('#string ' + DT
.TAB_INF_BINARY_DESCRIPTION
, BinaryDescription
, \
247 if BinaryDescriptionString
:
248 Content
+= BinaryDescriptionString
+ '\r\n'
250 if not os
.path
.exists(ContainerFile
):
251 File
= codecs
.open(ContainerFile
, 'wb', Encoding
)
252 File
.write(u
'\uFEFF' + Content
)
254 Md5Sigature
= md5
.new(__FileHookOpen__(str(ContainerFile
), 'rb').read())
255 Md5Sum
= Md5Sigature
.hexdigest()
256 if (ContainerFile
, Md5Sum
) not in ModuleObject
.FileList
:
257 ModuleObject
.FileList
.append((ContainerFile
, Md5Sum
))
260 def GenDefines(ModuleObject
):
262 # generate [Defines] section
268 for UserExtension
in ModuleObject
.GetUserExtensionList():
269 DefinesDict
= UserExtension
.GetDefinesDict()
272 for Statement
in DefinesDict
:
273 if Statement
.split(DT
.TAB_EQUAL_SPLIT
) > 1:
274 Statement
= (u
'%s ' % Statement
.split(DT
.TAB_EQUAL_SPLIT
, 1)[0]).ljust(LeftOffset
) \
275 + u
'= %s' % Statement
.split(DT
.TAB_EQUAL_SPLIT
, 1)[1].lstrip()
276 SortedArch
= DT
.TAB_ARCH_COMMON
277 if Statement
.strip().startswith(DT
.TAB_INF_DEFINES_CUSTOM_MAKEFILE
):
278 pos
= Statement
.find(DT
.TAB_VALUE_SPLIT
)
280 pos
= Statement
.find(DT
.TAB_EQUAL_SPLIT
)
281 Makefile
= ConvertPath(Statement
[pos
+ 1:].strip())
282 Statement
= Statement
[:pos
+ 1] + ' ' + Makefile
283 if SortedArch
in NewSectionDict
:
284 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
286 NewSectionDict
[SortedArch
] = [Statement
]
287 SpecialStatementList
= []
289 # TAB_INF_DEFINES_INF_VERSION
290 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_INF_VERSION
).ljust(LeftOffset
) + u
'= %s' % '0x00010017'
291 SpecialStatementList
.append(Statement
)
294 BaseName
= ModuleObject
.GetBaseName()
295 if BaseName
.startswith('.') or BaseName
.startswith('-'):
296 BaseName
= '_' + BaseName
297 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_BASE_NAME
).ljust(LeftOffset
) + u
'= %s' % BaseName
298 SpecialStatementList
.append(Statement
)
300 # TAB_INF_DEFINES_FILE_GUID
301 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_FILE_GUID
).ljust(LeftOffset
) + u
'= %s' % ModuleObject
.GetGuid()
302 SpecialStatementList
.append(Statement
)
304 # TAB_INF_DEFINES_VERSION_STRING
305 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_VERSION_STRING
).ljust(LeftOffset
) + u
'= %s' % ModuleObject
.GetVersion()
306 SpecialStatementList
.append(Statement
)
308 # TAB_INF_DEFINES_VERSION_STRING
309 if ModuleObject
.UNIFlag
:
310 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_MODULE_UNI_FILE
).ljust(LeftOffset
) + \
311 u
'= %s' % ModuleObject
.GetBaseName() + '.uni'
312 SpecialStatementList
.append(Statement
)
314 # TAB_INF_DEFINES_MODULE_TYPE
315 if ModuleObject
.GetModuleType():
316 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_MODULE_TYPE
).ljust(LeftOffset
) + u
'= %s' % ModuleObject
.GetModuleType()
317 SpecialStatementList
.append(Statement
)
319 # TAB_INF_DEFINES_PCD_IS_DRIVER
320 if ModuleObject
.GetPcdIsDriver():
321 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_PCD_IS_DRIVER
).ljust(LeftOffset
) + \
322 u
'= %s' % ModuleObject
.GetPcdIsDriver()
323 SpecialStatementList
.append(Statement
)
325 # TAB_INF_DEFINES_UEFI_SPECIFICATION_VERSION
326 if ModuleObject
.GetUefiSpecificationVersion():
327 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_UEFI_SPECIFICATION_VERSION
).ljust(LeftOffset
) + \
328 u
'= %s' % ModuleObject
.GetUefiSpecificationVersion()
329 SpecialStatementList
.append(Statement
)
331 # TAB_INF_DEFINES_PI_SPECIFICATION_VERSION
332 if ModuleObject
.GetPiSpecificationVersion():
333 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_PI_SPECIFICATION_VERSION
).ljust(LeftOffset
) + \
334 u
'= %s' % ModuleObject
.GetPiSpecificationVersion()
335 SpecialStatementList
.append(Statement
)
338 for LibraryClass
in ModuleObject
.GetLibraryClassList():
339 if LibraryClass
.GetUsage() == DT
.USAGE_ITEM_PRODUCES
or \
340 LibraryClass
.GetUsage() == DT
.USAGE_ITEM_SOMETIMES_PRODUCES
:
341 Statement
= (u
'%s ' % DT
.TAB_INF_DEFINES_LIBRARY_CLASS
).ljust(LeftOffset
) + \
342 u
'= %s' % LibraryClass
.GetLibraryClass()
343 if LibraryClass
.GetSupModuleList():
344 Statement
+= '|' + DT
.TAB_SPACE_SPLIT
.join(l
for l
in LibraryClass
.GetSupModuleList())
345 SpecialStatementList
.append(Statement
)
348 for SpecItem
in ModuleObject
.GetSpecList():
349 Spec
, Version
= SpecItem
350 Spec
= ConvertSpec(Spec
)
351 Statement
= '%s %s = %s' % (DT
.TAB_INF_DEFINES_SPEC
, Spec
, Version
)
352 SpecialStatementList
.append(Statement
)
356 for Extern
in ModuleObject
.GetExternList():
357 ArchList
= Extern
.GetSupArchList()
358 EntryPoint
= Extern
.GetEntryPoint()
359 UnloadImage
= Extern
.GetUnloadImage()
360 Constructor
= Extern
.GetConstructor()
361 Destructor
= Extern
.GetDestructor()
362 HelpStringList
= Extern
.GetHelpTextList()
363 FFE
= Extern
.GetFeatureFlag()
364 ExternList
.append([ArchList
, EntryPoint
, UnloadImage
, Constructor
, Destructor
, FFE
, HelpStringList
])
366 # Add VALID_ARCHITECTURES information
368 ValidArchStatement
= None
369 if ModuleObject
.SupArchList
:
370 ValidArchStatement
= '\n' + '# ' + '\n'
371 ValidArchStatement
+= '# The following information is for reference only and not required by the build tools.\n'
372 ValidArchStatement
+= '# ' + '\n'
373 ValidArchStatement
+= '# VALID_ARCHITECTURES = %s' % (' '.join(ModuleObject
.SupArchList
)) + '\n'
374 ValidArchStatement
+= '# '
375 if DT
.TAB_ARCH_COMMON
not in NewSectionDict
:
376 NewSectionDict
[DT
.TAB_ARCH_COMMON
] = []
377 NewSectionDict
[DT
.TAB_ARCH_COMMON
] = NewSectionDict
[DT
.TAB_ARCH_COMMON
] + SpecialStatementList
378 GenMetaFileMisc
.AddExternToDefineSec(NewSectionDict
, DT
.TAB_ARCH_COMMON
, ExternList
)
379 if ValidArchStatement
is not None:
380 NewSectionDict
[DT
.TAB_ARCH_COMMON
] = NewSectionDict
[DT
.TAB_ARCH_COMMON
] + [ValidArchStatement
]
381 Content
+= GenSection('Defines', NewSectionDict
)
384 def GenLibraryClasses(ModuleObject
):
386 # generate [LibraryClasses] section
390 if not GlobalData
.gIS_BINARY_INF
:
391 for LibraryClass
in ModuleObject
.GetLibraryClassList():
392 if LibraryClass
.GetUsage() == DT
.USAGE_ITEM_PRODUCES
:
395 # Generate generic comment
397 HelpTextList
= LibraryClass
.GetHelpTextList()
398 HelpStr
= _GetHelpStr(HelpTextList
)
399 CommentStr
= GenGenericCommentF(HelpStr
)
400 Statement
= CommentStr
401 Name
= LibraryClass
.GetLibraryClass()
402 FFE
= LibraryClass
.GetFeatureFlag()
405 Statement
+= '|' + FFE
406 ModuleList
= LibraryClass
.GetSupModuleList()
407 ArchList
= LibraryClass
.GetSupArchList()
408 for Index
in xrange(0, len(ArchList
)):
409 ArchList
[Index
] = ConvertArchForInstall(ArchList
[Index
])
411 SortedArch
= ' '.join(ArchList
)
413 if not ModuleList
or IsAllModuleList(ModuleList
):
414 KeyList
= [SortedArch
]
416 ModuleString
= DT
.TAB_VALUE_SPLIT
.join(l
for l
in ModuleList
)
418 SortedArch
= DT
.TAB_ARCH_COMMON
419 KeyList
= [SortedArch
+ '.' + ModuleString
]
421 KeyList
= [Arch
+ '.' + ModuleString
for Arch
in ArchList
]
423 if Key
in NewSectionDict
:
424 NewSectionDict
[Key
] = NewSectionDict
[Key
] + [Statement
]
426 NewSectionDict
[Key
] = [Statement
]
427 Content
+= GenSection('LibraryClasses', NewSectionDict
)
429 LibraryClassDict
= {}
430 for BinaryFile
in ModuleObject
.GetBinaryFileList():
431 if not BinaryFile
.AsBuiltList
:
433 for LibraryItem
in BinaryFile
.AsBuiltList
[0].LibraryInstancesList
:
434 Statement
= '# Guid: ' + LibraryItem
.Guid
+ ' Version: ' + LibraryItem
.Version
436 if len(BinaryFile
.SupArchList
) == 0:
437 if LibraryClassDict
.has_key('COMMON') and Statement
not in LibraryClassDict
['COMMON']:
438 LibraryClassDict
['COMMON'].append(Statement
)
440 LibraryClassDict
['COMMON'] = ['## @LIB_INSTANCES']
441 LibraryClassDict
['COMMON'].append(Statement
)
443 for Arch
in BinaryFile
.SupArchList
:
444 if LibraryClassDict
.has_key(Arch
):
445 if Statement
not in LibraryClassDict
[Arch
]:
446 LibraryClassDict
[Arch
].append(Statement
)
450 LibraryClassDict
[Arch
] = ['## @LIB_INSTANCES']
451 LibraryClassDict
[Arch
].append(Statement
)
452 Content
+= GenSection('LibraryClasses', LibraryClassDict
)
456 def GenPackages(ModuleObject
):
459 # generate [Packages] section
461 NewSectionDict
= Sdict()
462 WorkspaceDir
= GlobalData
.gWORKSPACE
463 for PackageDependency
in ModuleObject
.GetPackageDependencyList():
465 # Generate generic comment
468 HelpText
= PackageDependency
.GetHelpText()
470 HelpStr
= HelpText
.GetString()
471 CommentStr
= GenGenericCommentF(HelpStr
)
472 Statement
= CommentStr
473 Guid
= PackageDependency
.GetGuid()
474 Version
= PackageDependency
.GetVersion()
475 FFE
= PackageDependency
.GetFeatureFlag()
478 # find package path/name
480 for PkgInfo
in GlobalData
.gWSPKG_LIST
:
481 if Guid
== PkgInfo
[1]:
482 if (not Version
) or (Version
== PkgInfo
[2]):
488 RelaPath
= GetRelativePath(Path
, WorkspaceDir
)
489 Statement
+= RelaPath
.replace('\\', '/')
491 Statement
+= '|' + FFE
492 ArchList
= PackageDependency
.GetSupArchList()
494 SortedArch
= ' '.join(ArchList
)
495 if SortedArch
in NewSectionDict
:
496 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
498 NewSectionDict
[SortedArch
] = [Statement
]
499 Content
+= GenSection('Packages', NewSectionDict
)
502 def GenSources(ModuleObject
):
504 # generate [Sources] section
508 for Source
in ModuleObject
.GetSourceFileList():
509 SourceFile
= Source
.GetSourceFile()
510 Family
= Source
.GetFamily()
511 FeatureFlag
= Source
.GetFeatureFlag()
512 SupArchList
= Source
.GetSupArchList()
514 SortedArch
= ' '.join(SupArchList
)
515 Statement
= GenSourceStatement(ConvertPath(SourceFile
), Family
, FeatureFlag
)
516 if SortedArch
in NewSectionDict
:
517 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
519 NewSectionDict
[SortedArch
] = [Statement
]
520 Content
+= GenSection('Sources', NewSectionDict
)
524 def GenDepex(ModuleObject
):
526 # generate [Depex] section
528 NewSectionDict
= Sdict()
530 for Depex
in ModuleObject
.GetPeiDepex() + ModuleObject
.GetDxeDepex() + ModuleObject
.GetSmmDepex():
531 HelpTextList
= Depex
.GetHelpTextList()
532 HelpStr
= _GetHelpStr(HelpTextList
)
533 CommentStr
= GenGenericCommentF(HelpStr
)
534 SupArchList
= Depex
.GetSupArchList()
535 SupModList
= Depex
.GetModuleType()
536 Expression
= Depex
.GetDepex()
537 Statement
= CommentStr
+ Expression
541 SupArchList
.append(DT
.TAB_ARCH_COMMON
.lower())
543 KeyList
= SupArchList
545 for ModuleType
in SupModList
:
546 for Arch
in SupArchList
:
547 KeyList
.append(ConvertArchForInstall(Arch
) + '.' + ModuleType
)
549 if Key
in NewSectionDict
:
550 NewSectionDict
[Key
] = NewSectionDict
[Key
] + [Statement
]
552 NewSectionDict
[Key
] = [Statement
]
553 Content
+= GenSection('Depex', NewSectionDict
, False)
560 def GenUserExtensions(ModuleObject
):
562 for UserExtension
in ModuleObject
.GetUserExtensionList():
563 if UserExtension
.GetUserID() == DT
.TAB_BINARY_HEADER_USERID
and \
564 UserExtension
.GetIdentifier() == DT
.TAB_BINARY_HEADER_IDENTIFIER
:
566 if UserExtension
.GetIdentifier() == 'Depex':
568 Statement
= UserExtension
.GetStatement()
571 ArchList
= UserExtension
.GetSupArchList()
572 for Index
in xrange(0, len(ArchList
)):
573 ArchList
[Index
] = ConvertArchForInstall(ArchList
[Index
])
577 if UserExtension
.GetUserID():
578 CommonPreFix
= UserExtension
.GetUserID()
579 if CommonPreFix
.find('.') > -1:
580 CommonPreFix
= '"' + CommonPreFix
+ '"'
581 if UserExtension
.GetIdentifier():
582 CommonPreFix
+= '.' + '"' + UserExtension
.GetIdentifier() + '"'
584 KeyList
= [CommonPreFix
+ '.' + Arch
for Arch
in ArchList
]
586 KeyList
= [CommonPreFix
]
588 if Key
in NewSectionDict
:
589 NewSectionDict
[Key
] = NewSectionDict
[Key
] + [Statement
]
591 NewSectionDict
[Key
] = [Statement
]
592 Content
= GenSection('UserExtensions', NewSectionDict
, False)
598 # @param SourceFile: string of source file path/name
599 # @param Family: string of source file family field
600 # @param FeatureFlag: string of source file FeatureFlag field
601 # @param TagName: string of source file TagName field
602 # @param ToolCode: string of source file ToolCode field
603 # @param HelpStr: string of source file HelpStr field
605 # @retval Statement: The generated statement for source
607 def GenSourceStatement(SourceFile
, Family
, FeatureFlag
, TagName
=None,
608 ToolCode
=None, HelpStr
=None):
611 Statement
+= GenGenericCommentF(HelpStr
)
613 # format of SourceFile|Family|TagName|ToolCode|FeatureFlag
615 Statement
+= SourceFile
623 Statement
+= '|' + Family
+ '|' + TagName
+ '|' + ToolCode
+ '|' + FeatureFlag
625 Statement
+= '|' + Family
+ '|' + TagName
+ '|' + ToolCode
627 Statement
+= '|' + Family
+ '|' + TagName
629 Statement
+= '|' + Family
634 # @param Key: (FileName, FileType, FFE, SortedArch)
635 # @param Value: (Target, Family, TagName, Comment)
638 def GenBinaryStatement(Key
, Value
, SubTypeGuidValue
=None):
639 (FileName
, FileType
, FFE
, SortedArch
) = Key
643 (Target
, Family
, TagName
, Comment
) = Value
650 Statement
= GenGenericCommentF(Comment
)
653 if FileType
== 'SUBTYPE_GUID' and SubTypeGuidValue
:
654 Statement
+= FileType
+ '|' + SubTypeGuidValue
+ '|' + FileName
656 Statement
+= FileType
+ '|' + FileName
657 if FileType
in DT
.BINARY_FILE_TYPE_UI_LIST
+ DT
.BINARY_FILE_TYPE_VER_LIST
:
659 Statement
+= '|' + Target
+ '|' + FFE
661 Statement
+= '|' + Target
664 Statement
+= '|' + Target
+ '|' + Family
+ '|' + TagName
+ '|' + FFE
666 Statement
+= '|' + Target
+ '|' + Family
+ '|' + TagName
668 Statement
+= '|' + Target
+ '|' + Family
670 Statement
+= '|' + Target
674 # @param GuidObjList: List of GuidObject
675 # @retVal Content: The generated section contents
677 def GenGuidSections(GuidObjList
):
679 # generate [Guids] section
683 for Guid
in GuidObjList
:
684 HelpTextList
= Guid
.GetHelpTextList()
685 HelpStr
= _GetHelpStr(HelpTextList
)
686 CName
= Guid
.GetCName()
687 FFE
= Guid
.GetFeatureFlag()
690 Statement
+= '|' + FFE
691 Usage
= Guid
.GetUsage()
692 GuidType
= Guid
.GetGuidTypeList()[0]
693 VariableName
= Guid
.GetVariableName()
695 # Differentiate the generic comment and usage comment as multiple generic comment need to be put at first
697 if Usage
== DT
.ITEM_UNDEFINED
and GuidType
== DT
.ITEM_UNDEFINED
:
698 # generate list of generic comment
699 Comment
= GenGenericCommentF(HelpStr
)
701 # generate list of other comment
702 Comment
= HelpStr
.replace('\n', ' ')
703 Comment
= Comment
.strip()
705 Comment
= ' # ' + Comment
708 if Usage
!= DT
.ITEM_UNDEFINED
and GuidType
== DT
.ITEM_UNDEFINED
:
709 Comment
= '## ' + Usage
+ Comment
710 elif GuidType
== 'Variable':
711 Comment
= '## ' + Usage
+ ' ## ' + GuidType
+ ':' + VariableName
+ Comment
713 Comment
= '## ' + Usage
+ ' ## ' + GuidType
+ Comment
718 # merge duplicate items
720 ArchList
= Guid
.GetSupArchList()
722 SortedArch
= ' '.join(ArchList
)
723 if (Statement
, SortedArch
) in GuidDict
:
724 PreviousComment
= GuidDict
[Statement
, SortedArch
]
725 Comment
= PreviousComment
+ Comment
726 GuidDict
[Statement
, SortedArch
] = Comment
727 NewSectionDict
= GenMetaFileMisc
.TransferDict(GuidDict
, 'INF_GUID')
729 # generate the section contents
732 Content
= GenSection('Guids', NewSectionDict
)
736 ## GenProtocolPPiSections
738 # @param ObjList: List of ProtocolObject or Ppi Object
739 # @retVal Content: The generated section contents
741 def GenProtocolPPiSections(ObjList
, IsProtocol
):
744 for Object
in ObjList
:
745 HelpTextList
= Object
.GetHelpTextList()
746 HelpStr
= _GetHelpStr(HelpTextList
)
747 CName
= Object
.GetCName()
748 FFE
= Object
.GetFeatureFlag()
751 Statement
+= '|' + FFE
752 Usage
= Object
.GetUsage()
753 Notify
= Object
.GetNotify()
755 # Differentiate the generic comment and usage comment as consecutive generic comment need to be put together
757 if Usage
== DT
.ITEM_UNDEFINED
and Notify
== '':
758 # generate list of generic comment
759 Comment
= GenGenericCommentF(HelpStr
)
761 # generate list of other comment
762 Comment
= HelpStr
.replace('\n', ' ')
763 Comment
= Comment
.strip()
765 Comment
= ' # ' + Comment
768 if Usage
== DT
.ITEM_UNDEFINED
and not Comment
and Notify
== '':
772 Comment
= '## ' + Usage
+ ' ## ' + 'NOTIFY' + Comment
774 Comment
= '## ' + Usage
+ Comment
778 # merge duplicate items
780 ArchList
= Object
.GetSupArchList()
782 SortedArch
= ' '.join(ArchList
)
783 if (Statement
, SortedArch
) in Dict
:
784 PreviousComment
= Dict
[Statement
, SortedArch
]
785 Comment
= PreviousComment
+ Comment
786 Dict
[Statement
, SortedArch
] = Comment
787 NewSectionDict
= GenMetaFileMisc
.TransferDict(Dict
, 'INF_PPI_PROTOCOL')
789 # generate the section contents
793 Content
= GenSection('Protocols', NewSectionDict
)
795 Content
= GenSection('Ppis', NewSectionDict
)
802 def GenPcdSections(ModuleObject
):
804 if not GlobalData
.gIS_BINARY_INF
:
806 # for each Pcd Itemtype, maintain a dict so the same type will be grouped
810 for Pcd
in ModuleObject
.GetPcdList():
811 HelpTextList
= Pcd
.GetHelpTextList()
812 HelpStr
= _GetHelpStr(HelpTextList
)
814 CName
= Pcd
.GetCName()
815 TokenSpaceGuidCName
= Pcd
.GetTokenSpaceGuidCName()
816 DefaultValue
= Pcd
.GetDefaultValue()
817 ItemType
= Pcd
.GetItemType()
818 if ItemType
in ItemTypeDict
:
819 Dict
= ItemTypeDict
[ItemType
]
822 ItemTypeDict
[ItemType
] = Dict
823 FFE
= Pcd
.GetFeatureFlag()
824 Statement
+= TokenSpaceGuidCName
+ '.' + CName
826 Statement
+= '|' + DefaultValue
828 Statement
+= '|' + FFE
830 Statement
+= '||' + FFE
834 Usage
= Pcd
.GetValidUsage()
835 # if FeatureFlag Pcd, then assume all Usage is CONSUMES
836 if ItemType
== DT
.TAB_INF_FEATURE_PCD
:
837 Usage
= DT
.USAGE_ITEM_CONSUMES
838 if Usage
== DT
.ITEM_UNDEFINED
:
839 # generate list of generic comment
840 Comment
= GenGenericCommentF(HelpStr
)
842 # generate list of other comment
843 Comment
= HelpStr
.replace('\n', ' ')
844 Comment
= Comment
.strip()
846 Comment
= ' # ' + Comment
849 Comment
= '## ' + Usage
+ Comment
853 # Merge duplicate entries
855 ArchList
= Pcd
.GetSupArchList()
857 SortedArch
= ' '.join(ArchList
)
858 if (Statement
, SortedArch
) in Dict
:
859 PreviousComment
= Dict
[Statement
, SortedArch
]
860 Comment
= PreviousComment
+ Comment
861 Dict
[Statement
, SortedArch
] = Comment
862 for ItemType
in ItemTypeDict
:
863 # First we need to transfer the Dict to use SortedArch as key
864 Dict
= ItemTypeDict
[ItemType
]
865 NewSectionDict
= GenMetaFileMisc
.TransferDict(Dict
, 'INF_PCD')
867 Content
+= GenSection(ItemType
, NewSectionDict
)
869 # For AsBuild INF files
872 Content
+= GenAsBuiltPacthPcdSections(ModuleObject
)
873 Content
+= GenAsBuiltPcdExSections(ModuleObject
)
880 def GenAsBuiltPacthPcdSections(ModuleObject
):
882 for BinaryFile
in ModuleObject
.GetBinaryFileList():
883 if not BinaryFile
.AsBuiltList
:
885 for PatchPcd
in BinaryFile
.AsBuiltList
[0].PatchPcdList
:
887 PcdCName
= PatchPcd
.CName
888 PcdValue
= PatchPcd
.DefaultValue
889 PcdOffset
= PatchPcd
.Offset
890 TokenSpaceGuidValue
= PatchPcd
.TokenSpaceGuidValue
891 Token
= PatchPcd
.Token
892 HelpTextList
= PatchPcd
.HelpTextList
894 for HelpStringItem
in HelpTextList
:
895 for HelpLine
in GetSplitValueList(HelpStringItem
.String
, '\n'):
896 HelpString
+= '## ' + HelpLine
+ '\n'
897 TokenSpaceName
, PcdCName
= GenMetaFileMisc
.ObtainPcdName(ModuleObject
.PackageDependencyList
,
900 if TokenSpaceName
== '' or PcdCName
== '':
902 ToolError
.RESOURCE_NOT_AVAILABLE
,
903 ST
.ERR_INSTALL_FILE_DEC_FILE_ERROR
% (TokenSpaceGuidValue
, Token
),
904 File
=ModuleObject
.GetFullPath())
905 Statement
= HelpString
+ TokenSpaceName
+ '.' + PcdCName
+ ' | ' + PcdValue
+ ' | ' + \
906 PcdOffset
+ DT
.TAB_SPACE_SPLIT
908 # Use binary file's Arch to be Pcd's Arch
911 FileNameObjList
= BinaryFile
.GetFileNameList()
913 ArchList
= FileNameObjList
[0].GetSupArchList()
914 if len(ArchList
) == 0:
915 if PatchPcdDict
.has_key(DT
.TAB_ARCH_COMMON
):
916 if Statement
not in PatchPcdDict
[DT
.TAB_ARCH_COMMON
]:
917 PatchPcdDict
[DT
.TAB_ARCH_COMMON
].append(Statement
)
919 PatchPcdDict
[DT
.TAB_ARCH_COMMON
] = [Statement
]
921 for Arch
in ArchList
:
922 if PatchPcdDict
.has_key(Arch
):
923 if Statement
not in PatchPcdDict
[Arch
]:
924 PatchPcdDict
[Arch
].append(Statement
)
926 PatchPcdDict
[Arch
] = [Statement
]
927 return GenSection(DT
.TAB_INF_PATCH_PCD
, PatchPcdDict
)
931 def GenAsBuiltPcdExSections(ModuleObject
):
933 for BinaryFile
in ModuleObject
.GetBinaryFileList():
934 if not BinaryFile
.AsBuiltList
:
936 for PcdExItem
in BinaryFile
.AsBuiltList
[0].PcdExValueList
:
938 PcdCName
= PcdExItem
.CName
939 TokenSpaceGuidValue
= PcdExItem
.TokenSpaceGuidValue
940 Token
= PcdExItem
.Token
941 HelpTextList
= PcdExItem
.HelpTextList
943 for HelpStringItem
in HelpTextList
:
944 for HelpLine
in GetSplitValueList(HelpStringItem
.String
, '\n'):
945 HelpString
+= '## ' + HelpLine
+ '\n'
946 TokenSpaceName
, PcdCName
= GenMetaFileMisc
.ObtainPcdName(ModuleObject
.PackageDependencyList
,
947 TokenSpaceGuidValue
, Token
)
948 if TokenSpaceName
== '' or PcdCName
== '':
950 ToolError
.RESOURCE_NOT_AVAILABLE
,
951 ST
.ERR_INSTALL_FILE_DEC_FILE_ERROR
% (TokenSpaceGuidValue
, Token
),
952 File
=ModuleObject
.GetFullPath())
954 Statement
= HelpString
+ TokenSpaceName
+ DT
.TAB_SPLIT
+ PcdCName
+ DT
.TAB_SPACE_SPLIT
957 # Use binary file's Arch to be Pcd's Arch
960 FileNameObjList
= BinaryFile
.GetFileNameList()
962 ArchList
= FileNameObjList
[0].GetSupArchList()
964 if len(ArchList
) == 0:
965 if PcdExDict
.has_key('COMMON'):
966 PcdExDict
['COMMON'].append(Statement
)
968 PcdExDict
['COMMON'] = [Statement
]
970 for Arch
in ArchList
:
971 if PcdExDict
.has_key(Arch
):
972 if Statement
not in PcdExDict
[Arch
]:
973 PcdExDict
[Arch
].append(Statement
)
975 PcdExDict
[Arch
] = [Statement
]
976 return GenSection('PcdEx', PcdExDict
)
978 ## GenSpecialSections
979 # generate special sections for Event/BootMode/Hob
981 def GenSpecialSections(ObjectList
, SectionName
):
987 for Obj
in ObjectList
:
992 HelpTextList
= Obj
.GetHelpTextList()
993 HelpStr
= _GetHelpStr(HelpTextList
)
994 CommentStr
= GenGenericCommentF(HelpStr
)
995 if SectionName
== 'Hob':
996 Type
= Obj
.GetHobType()
997 elif SectionName
== 'Event':
998 Type
= Obj
.GetEventType()
999 elif SectionName
== 'BootMode':
1000 Type
= Obj
.GetSupportedBootModes()
1003 Usage
= Obj
.GetUsage()
1004 Statement
= ' ' + Type
+ ' ## ' + Usage
1005 if CommentStr
in ['#\n', '#\n#\n']:
1006 CommentStr
= '#\n#\n#\n'
1008 # the first head comment line should start with '##\n', if it starts with '#\n', then add one '#'
1009 # else add '##\n' to meet the format defined in INF spec
1011 if CommentStr
.startswith('#\n'):
1012 CommentStr
= '#' + CommentStr
1014 CommentStr
= '##\n' + CommentStr
1015 if CommentStr
and not CommentStr
.endswith('\n#\n'):
1016 CommentStr
= CommentStr
+ '#\n'
1017 NewStateMent
= CommentStr
+ Statement
1018 SupArch
= Obj
.GetSupArchList()
1020 SortedArch
= ' '.join(SupArch
)
1021 if SortedArch
in NewSectionDict
:
1022 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [NewStateMent
]
1024 NewSectionDict
[SortedArch
] = [NewStateMent
]
1025 SectionContent
= GenSection(SectionName
, NewSectionDict
)
1026 SectionContent
= SectionContent
.strip()
1028 Content
= '# ' + ('\n' + '# ').join(GetSplitValueList(SectionContent
, '\n'))
1029 Content
= Content
.lstrip()
1031 # add a return to differentiate it between other possible sections
1039 def GenBuildOptions(ModuleObject
):
1041 if not ModuleObject
.BinaryModule
:
1043 # generate [BuildOptions] section
1046 for UserExtension
in ModuleObject
.GetUserExtensionList():
1047 BuildOptionDict
= UserExtension
.GetBuildOptionDict()
1048 if not BuildOptionDict
:
1050 for Arch
in BuildOptionDict
:
1051 if Arch
in NewSectionDict
:
1052 NewSectionDict
[Arch
] = NewSectionDict
[Arch
] + [BuildOptionDict
[Arch
]]
1054 NewSectionDict
[Arch
] = [BuildOptionDict
[Arch
]]
1055 Content
= GenSection('BuildOptions', NewSectionDict
)
1057 BuildOptionDict
= {}
1058 for BinaryFile
in ModuleObject
.GetBinaryFileList():
1059 if not BinaryFile
.AsBuiltList
:
1061 for BuilOptionItem
in BinaryFile
.AsBuiltList
[0].BinaryBuildFlagList
:
1062 Statement
= '#' + BuilOptionItem
.AsBuiltOptionFlags
1063 if len(BinaryFile
.SupArchList
) == 0:
1064 if BuildOptionDict
.has_key('COMMON'):
1065 if Statement
not in BuildOptionDict
['COMMON']:
1066 BuildOptionDict
['COMMON'].append(Statement
)
1068 BuildOptionDict
['COMMON'] = ['## @AsBuilt']
1069 BuildOptionDict
['COMMON'].append(Statement
)
1071 for Arch
in BinaryFile
.SupArchList
:
1072 if BuildOptionDict
.has_key(Arch
):
1073 if Statement
not in BuildOptionDict
[Arch
]:
1074 BuildOptionDict
[Arch
].append(Statement
)
1076 BuildOptionDict
[Arch
] = ['## @AsBuilt']
1077 BuildOptionDict
[Arch
].append(Statement
)
1078 Content
= GenSection('BuildOptions', BuildOptionDict
)
1084 def GenBinaries(ModuleObject
):
1087 for UserExtension
in ModuleObject
.GetUserExtensionList():
1088 BinariesDict
= UserExtension
.GetBinariesDict()
1091 for BinaryFile
in ModuleObject
.GetBinaryFileList():
1092 FileNameObjList
= BinaryFile
.GetFileNameList()
1093 for FileNameObj
in FileNameObjList
:
1094 FileName
= ConvertPath(FileNameObj
.GetFilename())
1095 FileType
= FileNameObj
.GetFileType()
1096 FFE
= FileNameObj
.GetFeatureFlag()
1097 ArchList
= FileNameObj
.GetSupArchList()
1099 SortedArch
= ' '.join(ArchList
)
1100 Key
= (FileName
, FileType
, FFE
, SortedArch
)
1101 if Key
in BinariesDict
:
1102 ValueList
= BinariesDict
[Key
]
1103 for ValueItem
in ValueList
:
1104 Statement
= GenBinaryStatement(Key
, ValueItem
)
1105 if SortedArch
in NewSectionDict
:
1106 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
1108 NewSectionDict
[SortedArch
] = [Statement
]
1110 # as we already generated statement for this DictKey here set the Valuelist to be empty
1111 # to avoid generate duplicate entries as the DictKey may have multiple entries
1113 BinariesDict
[Key
] = []
1115 if FileType
== 'SUBTYPE_GUID' and FileNameObj
.GetGuidValue():
1116 Statement
= GenBinaryStatement(Key
, None, FileNameObj
.GetGuidValue())
1118 Statement
= GenBinaryStatement(Key
, None)
1119 if SortedArch
in NewSectionDict
:
1120 NewSectionDict
[SortedArch
] = NewSectionDict
[SortedArch
] + [Statement
]
1122 NewSectionDict
[SortedArch
] = [Statement
]
1123 Content
= GenSection('Binaries', NewSectionDict
)